Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: make ParsedModule implement Sync #11581

Merged
merged 4 commits into from
Aug 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ serde = { version = "1.0.126", features = ["derive"] }
shell-escape = "0.1.5"
sourcemap = "6.0.1"
swc_bundler = "0.46.0"
swc_common = { version = "0.11.0", features = ["sourcemap"] }
swc_common = { version = "0.11.4", features = ["sourcemap"] }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the version bump required for this PR? Normally we bump all the other uses of swc_common across the crates before a release.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup. Need it for some changes I did in swc_common.

swc_ecmascript = { version = "0.46.0", features = ["codegen", "dep_graph", "parser", "proposal", "react", "transforms", "typescript", "visit"] }
tempfile = "3.2.0"
termcolor = "1.1.2"
Expand Down
53 changes: 53 additions & 0 deletions cli/ast/bundle_hook.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file is a straight extract out of mod.ts. I did a slight bit of cleanup in this PR as well.

use deno_core::error::AnyError;

pub struct BundleHook;

impl swc_bundler::Hook for BundleHook {
fn get_import_meta_props(
&self,
span: swc_common::Span,
module_record: &swc_bundler::ModuleRecord,
) -> Result<Vec<swc_ecmascript::ast::KeyValueProp>, AnyError> {
use swc_ecmascript::ast;

// we use custom file names, and swc "wraps" these in `<` and `>` so, we
// want to strip those back out.
let mut value = module_record.file_name.to_string();
value.pop();
value.remove(0);

Ok(vec![
ast::KeyValueProp {
key: ast::PropName::Ident(ast::Ident::new("url".into(), span)),
value: Box::new(ast::Expr::Lit(ast::Lit::Str(ast::Str {
span,
value: value.into(),
kind: ast::StrKind::Synthesized,
has_escape: false,
}))),
},
ast::KeyValueProp {
key: ast::PropName::Ident(ast::Ident::new("main".into(), span)),
value: Box::new(if module_record.is_entry {
ast::Expr::Member(ast::MemberExpr {
span,
obj: ast::ExprOrSuper::Expr(Box::new(ast::Expr::MetaProp(
ast::MetaPropExpr {
meta: ast::Ident::new("import".into(), span),
prop: ast::Ident::new("meta".into(), span),
},
))),
prop: Box::new(ast::Expr::Ident(ast::Ident::new(
"main".into(),
span,
))),
computed: false,
})
} else {
ast::Expr::Lit(ast::Lit::Bool(ast::Bool { span, value: false }))
}),
},
])
}
}
106 changes: 106 additions & 0 deletions cli/ast/comments.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.

use std::cell::RefCell;
use std::rc::Rc;
use std::sync::Arc;
use swc_common::comments::Comment;
use swc_common::comments::Comments;
use swc_common::comments::SingleThreadedComments;
use swc_common::comments::SingleThreadedCommentsMapInner;
use swc_common::BytePos;

/// An implementation of swc's `Comments` that implements `Sync`
/// to support being used in multi-threaded code. This implementation
/// is immutable and should you need mutability you may create a copy
/// by converting it to an swc `SingleThreadedComments`.
#[derive(Clone, Debug)]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not that it is a big deal, but future generations might benefit from a little explanation of why we are doing this?

pub struct MultiThreadedComments {
leading: Arc<SingleThreadedCommentsMapInner>,
trailing: Arc<SingleThreadedCommentsMapInner>,
}

impl MultiThreadedComments {
pub fn from_single_threaded(comments: SingleThreadedComments) -> Self {
let (leading, trailing) = comments.take_all();
let leading = Arc::new(Rc::try_unwrap(leading).unwrap().into_inner());
let trailing = Arc::new(Rc::try_unwrap(trailing).unwrap().into_inner());
MultiThreadedComments { leading, trailing }
}

pub fn as_single_threaded(&self) -> SingleThreadedComments {
let leading = Rc::new(RefCell::new((*self.leading).to_owned()));
let trailing = Rc::new(RefCell::new((*self.trailing).to_owned()));
SingleThreadedComments::from_leading_and_trailing(leading, trailing)
}

/// Gets a vector of all the comments sorted by position.
pub fn get_vec(&self) -> Vec<Comment> {
let mut comments = self
.leading
.values()
.chain(self.trailing.values())
.flatten()
.cloned()
.collect::<Vec<_>>();
comments.sort_by_key(|comment| comment.span.lo);
comments
}
}

impl Comments for MultiThreadedComments {
fn has_leading(&self, pos: BytePos) -> bool {
self.leading.contains_key(&pos)
}

fn get_leading(&self, pos: BytePos) -> Option<Vec<Comment>> {
self.leading.get(&pos).cloned()
}

fn has_trailing(&self, pos: BytePos) -> bool {
self.trailing.contains_key(&pos)
}

fn get_trailing(&self, pos: BytePos) -> Option<Vec<Comment>> {
self.trailing.get(&pos).cloned()
}

fn add_leading(&self, _pos: BytePos, _cmt: Comment) {
panic_readonly();
}

fn add_leading_comments(&self, _pos: BytePos, _comments: Vec<Comment>) {
panic_readonly();
}

fn move_leading(&self, _from: BytePos, _to: BytePos) {
panic_readonly();
}

fn take_leading(&self, _pos: BytePos) -> Option<Vec<Comment>> {
panic_readonly();
}

fn add_trailing(&self, _pos: BytePos, _cmt: Comment) {
panic_readonly();
}

fn add_trailing_comments(&self, _pos: BytePos, _comments: Vec<Comment>) {
panic_readonly();
}

fn move_trailing(&self, _from: BytePos, _to: BytePos) {
panic_readonly();
}

fn take_trailing(&self, _pos: BytePos) -> Option<Vec<Comment>> {
panic_readonly();
}

fn add_pure_comment(&self, _pos: BytePos) {
panic_readonly();
}
}

fn panic_readonly() -> ! {
panic!("MultiThreadedComments do not support write operations")
}
Loading