From 8e0e9ca4c717838816cb0143fe36fd5d7cc86a3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Sat, 17 Nov 2018 17:02:34 +0900 Subject: [PATCH] Make it thread safe --- .cargo/config | 7 ++++++- README.md | 2 +- common/src/errors/tests.rs | 4 ++-- common/src/lib.rs | 1 + ecmascript/codegen/src/lib.rs | 8 ++++---- ecmascript/codegen/src/tests.rs | 4 ++-- ecmascript/codegen/src/text_writer/basic_impl.rs | 11 ++++------- ecmascript/codegen/src/util.rs | 9 +++++---- ecmascript/codegen/tests/test262.rs | 5 ++--- ecmascript/transforms/src/compat/helpers/mod.rs | 4 ++-- ecmascript/transforms/src/tests.rs | 7 +++---- libswc/src/lib.rs | 7 +++---- src/bin/swc.rs | 7 +++---- testing/src/errors.rs | 8 +++++--- testing/src/lib.rs | 7 +++---- 15 files changed, 46 insertions(+), 45 deletions(-) diff --git a/.cargo/config b/.cargo/config index f3eb8d6e5a1..c1ea8d02bf4 100644 --- a/.cargo/config +++ b/.cargo/config @@ -1,6 +1,11 @@ [build] -rustflags = ["--cfg", "procmacro2_semver_exempt"] +rustflags = [ + "--cfg", "procmacro2_semver_exempt", + "--cfg", "parallel_queries", +] + rustdocflags = [ "--cfg", "procmacro2_semver_exempt", + "--cfg", "parallel_queries", ] diff --git a/README.md b/README.md index 8e339d93cbb..e38ae3c04ef 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ swc is rust port of [babel][] and [closure compiler][]. Requires nightly version of [rust][]. ```sh -RUSTFLAGS='--cfg procmacro2_semver_exempt' cargo install --git https://github.com/swc-project/swc.git +RUSTFLAGS='--cfg procmacro2_semver_exempt --cfg parallel_queries' cargo install --git https://github.com/swc-project/swc.git ``` # Features diff --git a/common/src/errors/tests.rs b/common/src/errors/tests.rs index b8e605e1319..c9351e99ed2 100644 --- a/common/src/errors/tests.rs +++ b/common/src/errors/tests.rs @@ -3,8 +3,8 @@ use crate::{FileLoader, FilePathMapping, SourceMap}; use std::{ io, path::{Path, PathBuf}, - rc::Rc, }; +use sync::Lrc; use BytePos; use Span; @@ -47,7 +47,7 @@ fn test() { let end_pos = file_map.end_pos - BytePos(1); let full = Span::new(start_pos, end_pos, Default::default()); - let handler = Handler::with_tty_emitter(ColorConfig::Always, false, false, Some(Rc::new(cm))); + let handler = Handler::with_tty_emitter(ColorConfig::Always, false, false, Some(Lrc::new(cm))); ::syntax_pos::GLOBALS.set(&::syntax_pos::Globals::new(), || { DiagnosticBuilder::new_with_code( diff --git a/common/src/lib.rs b/common/src/lib.rs index c0d58ab39a5..26ab3dce757 100644 --- a/common/src/lib.rs +++ b/common/src/lib.rs @@ -17,6 +17,7 @@ pub use self::{ pos::*, }; pub use ast_node::{ast_node, Fold, FromVariant, Spanned}; +pub use rustc_data_structures::sync; use std::fmt::Debug; pub use syntax::source_map::{ FileLines, FileLoader, FileName, FilePathMapping, SourceMap, SpanSnippetError, diff --git a/ecmascript/codegen/src/lib.rs b/ecmascript/codegen/src/lib.rs index 6528e28c81b..f60355b2572 100644 --- a/ecmascript/codegen/src/lib.rs +++ b/ecmascript/codegen/src/lib.rs @@ -21,9 +21,9 @@ use self::{ text_writer::WriteJs, util::{SourceMapperExt, SpanExt, StartsWithAlphaNum}, }; -use std::{collections::HashSet, io, rc::Rc}; +use std::{collections::HashSet, io}; use swc_atoms::JsWord; -use swc_common::{BytePos, SourceMap, Span, Spanned, SyntaxContext, DUMMY_SP}; +use swc_common::{sync::Lrc, BytePos, SourceMap, Span, Spanned, SyntaxContext, DUMMY_SP}; use swc_ecma_ast::*; use swc_ecma_codegen_macros::emitter; @@ -61,7 +61,7 @@ impl<'a, N: Node> Node for &'a N { pub struct Emitter<'a> { pub cfg: config::Config, - pub cm: Rc, + pub cm: Lrc, pub wr: Box<('a + WriteJs)>, pub handlers: Box<('a + Handlers)>, pub pos_of_leading_comments: HashSet, @@ -1478,7 +1478,7 @@ impl<'a> Emitter<'a> { } fn get_text_of_node( - cm: &Rc, + cm: &Lrc, node: &T, _include_travia: bool, ) -> Option { diff --git a/ecmascript/codegen/src/tests.rs b/ecmascript/codegen/src/tests.rs index 6c414bd9f22..35c66ae7a4e 100644 --- a/ecmascript/codegen/src/tests.rs +++ b/ecmascript/codegen/src/tests.rs @@ -19,7 +19,7 @@ impl Handlers for Noop {} struct Builder { cfg: Config, - cm: Rc, + cm: Lrc, } fn test() -> Builder { @@ -27,7 +27,7 @@ fn test() -> Builder { Builder { cfg: Default::default(), - cm: Rc::new(src), + cm: Lrc::new(src), } } diff --git a/ecmascript/codegen/src/text_writer/basic_impl.rs b/ecmascript/codegen/src/text_writer/basic_impl.rs index b457d96d981..026020d97a1 100644 --- a/ecmascript/codegen/src/text_writer/basic_impl.rs +++ b/ecmascript/codegen/src/text_writer/basic_impl.rs @@ -1,10 +1,7 @@ use super::{Result, WriteJs}; use sourcemap::SourceMapBuilder; -use std::{ - io::{self, Write}, - rc::Rc, -}; -use swc_common::{SourceMap, Span}; +use std::io::{self, Write}; +use swc_common::{sync::Lrc, SourceMap, Span}; /// /// ----- @@ -13,7 +10,7 @@ use swc_common::{SourceMap, Span}; /// /// https://github.com/Microsoft/TypeScript/blob/45eaf42006/src/compiler/utilities.ts#L2548 pub struct JsWriter<'a, W: Write> { - cm: Rc, + cm: Lrc, indent: usize, line_start: bool, line_count: usize, @@ -26,7 +23,7 @@ pub struct JsWriter<'a, W: Write> { impl<'a, W: Write> JsWriter<'a, W> { pub fn new( - cm: Rc, + cm: Lrc, new_line: &'a str, wr: W, srcmap: &'a mut SourceMapBuilder, diff --git a/ecmascript/codegen/src/util.rs b/ecmascript/codegen/src/util.rs index 3cc8328f800..232caf3ef36 100644 --- a/ecmascript/codegen/src/util.rs +++ b/ecmascript/codegen/src/util.rs @@ -1,7 +1,7 @@ use super::list::ListFormat; -use std::rc::Rc; use swc_common::{ - errors::SourceMapper, BytePos, SourceMap, SourceMapperDyn, Span, Spanned, SyntaxContext, + errors::SourceMapper, sync::Lrc, BytePos, SourceMap, SourceMapperDyn, Span, Spanned, + SyntaxContext, }; use swc_ecma_ast::*; @@ -127,12 +127,13 @@ impl SourceMapperExt for SourceMapper { self } } -impl SourceMapperExt for Rc { +impl SourceMapperExt for Lrc { fn get_code_map(&self) -> &SourceMapper { &**self } } -impl SourceMapperExt for Rc { + +impl SourceMapperExt for Lrc { fn get_code_map(&self) -> &SourceMapper { &**self } diff --git a/ecmascript/codegen/tests/test262.rs b/ecmascript/codegen/tests/test262.rs index d90d47a8312..79b9b353c49 100644 --- a/ecmascript/codegen/tests/test262.rs +++ b/ecmascript/codegen/tests/test262.rs @@ -15,10 +15,9 @@ use std::{ fs::{read_dir, File}, io::{self, Read, Write}, path::Path, - rc::Rc, sync::{Arc, RwLock}, }; -use swc_common::{Fold, FoldWith, Span}; +use swc_common::{sync::Lrc, Fold, FoldWith, Span}; use swc_ecma_ast::*; use swc_ecma_codegen::Emitter; use swc_ecma_parser::{Parser, Session, SourceFileInput}; @@ -158,7 +157,7 @@ fn error_tests(tests: &mut Vec) -> Result<(), io::Error> { (&*src).into(), ); - let s: Rc = src.src.as_ref().map(|s| s.clone()).unwrap(); + let s: Lrc = src.src.as_ref().map(|s| s.clone()).unwrap(); let mut src_map_builder = SourceMapBuilder::new(Some(&s)); { let mut emitter = Emitter { diff --git a/ecmascript/transforms/src/compat/helpers/mod.rs b/ecmascript/transforms/src/compat/helpers/mod.rs index f5da6a1bb2a..aefe82d7de5 100644 --- a/ecmascript/transforms/src/compat/helpers/mod.rs +++ b/ecmascript/transforms/src/compat/helpers/mod.rs @@ -1,7 +1,6 @@ use ast::*; use std::{ ops::BitOr, - rc::Rc, sync::{ atomic::{AtomicBool, Ordering}, Arc, @@ -9,6 +8,7 @@ use std::{ }; use swc_common::{ errors::{ColorConfig, Handler}, + sync::Lrc, FileName, Fold, SourceMap, }; use swc_ecma_parser::{Parser, Session, SourceFileInput}; @@ -49,7 +49,7 @@ impl<'a> BitOr<&'a Helpers> for Helpers { } pub struct InjectHelpers { - pub cm: Rc, + pub cm: Lrc, pub helpers: Arc, } diff --git a/ecmascript/transforms/src/tests.rs b/ecmascript/transforms/src/tests.rs index eb81027b766..cf13a0f8b5b 100644 --- a/ecmascript/transforms/src/tests.rs +++ b/ecmascript/transforms/src/tests.rs @@ -1,12 +1,11 @@ +use ast::*; use slog::Logger; use sourcemap::SourceMapBuilder; use std::{ io::{self, Write}, - rc::Rc, sync::{Arc, RwLock}, }; -use swc_common::{errors::Handler, FileName, Fold, FoldWith, SourceMap}; -use ast::*; +use swc_common::{errors::Handler, sync::Lrc, FileName, Fold, FoldWith, SourceMap}; use swc_ecma_codegen::Emitter; use swc_ecma_parser::{Parser, Session, SourceFileInput}; @@ -15,7 +14,7 @@ struct MyHandlers; impl swc_ecma_codegen::Handlers for MyHandlers {} pub(crate) struct Tester<'a> { - cm: Rc, + cm: Lrc, logger: Logger, handler: &'a Handler, } diff --git a/libswc/src/lib.rs b/libswc/src/lib.rs index 4e84c8f0775..69b6e9b1195 100644 --- a/libswc/src/lib.rs +++ b/libswc/src/lib.rs @@ -9,7 +9,7 @@ pub extern crate swc_common as common; pub extern crate swc_ecmascript as ecmascript; use self::{ - common::{errors::Handler, SourceMap}, + common::{errors::Handler, sync::Lrc, SourceMap}, ecmascript::{ ast::Module, codegen::{self, Emitter}, @@ -21,17 +21,16 @@ use sourcemap::SourceMapBuilder; use std::{ io::{self, Write}, path::Path, - rc::Rc, }; pub struct Compiler { - cm: Rc, + cm: Lrc, logger: Logger, handler: Handler, } impl Compiler { - pub fn new(logger: Logger, cm: Rc, handler: Handler) -> Self { + pub fn new(logger: Logger, cm: Lrc, handler: Handler) -> Self { Compiler { cm, logger, diff --git a/src/bin/swc.rs b/src/bin/swc.rs index a21e1073255..14f7e56fa6a 100644 --- a/src/bin/swc.rs +++ b/src/bin/swc.rs @@ -14,11 +14,10 @@ use std::{ error::Error, io::{self, Write}, path::Path, - rc::Rc, sync::Arc, }; use swc::{ - common::{errors::Handler, FilePathMapping, Fold, SourceMap}, + common::{errors::Handler, sync::Lrc, FilePathMapping, Fold, SourceMap}, ecmascript::{ast::Module, codegen}, Compiler, }; @@ -70,7 +69,7 @@ fn run() -> Result<(), Box> { .build_global() .expect("failed to configure rayon::ThreadPool"); - let cm = Rc::new(SourceMap::new(FilePathMapping::empty())); + let cm = Lrc::new(SourceMap::new(FilePathMapping::empty())); let handler = Handler::with_tty_emitter( swc::common::errors::ColorConfig::Always, @@ -110,7 +109,7 @@ fn run() -> Result<(), Box> { Ok(()) } -fn js_pass(cm: Rc, matches: &ArgMatches) -> Box> { +fn js_pass(cm: Lrc, matches: &ArgMatches) -> Box> { use swc::ecmascript::transforms::{compat, simplifier}; let helpers = Arc::new(compat::helpers::Helpers::default()); diff --git a/testing/src/errors.rs b/testing/src/errors.rs index 2cf78158b7c..423e71e7f6e 100644 --- a/testing/src/errors.rs +++ b/testing/src/errors.rs @@ -1,13 +1,15 @@ use super::StdErr; use std::{ io::{self, Write}, - rc::Rc, sync::{Arc, RwLock}, }; -use swc_common::errors::{EmitterWriter, Handler, HandlerFlags, SourceMapperDyn}; +use swc_common::{ + errors::{EmitterWriter, Handler, HandlerFlags, SourceMapperDyn}, + sync::Lrc, +}; /// Creates a new handler for testing. -pub(crate) fn new_handler(cm: Rc) -> (Handler, BufferedError) { +pub(crate) fn new_handler(cm: Lrc) -> (Handler, BufferedError) { let buf: BufferedError = Default::default(); let e = EmitterWriter::new(box buf.clone(), Some(cm.clone()), false, true); diff --git a/testing/src/lib.rs b/testing/src/lib.rs index 83d4ba7c6d7..da3894fec5b 100644 --- a/testing/src/lib.rs +++ b/testing/src/lib.rs @@ -22,10 +22,9 @@ use std::{ fs::{create_dir_all, File}, io::{self, Write}, path::Path, - rc::Rc, thread, }; -use swc_common::{errors::Handler, FilePathMapping, Fold, FoldWith, SourceMap, Span}; +use swc_common::{errors::Handler, sync::Lrc, FilePathMapping, Fold, FoldWith, SourceMap, Span}; #[macro_use] mod macros; @@ -35,9 +34,9 @@ mod paths; pub fn run_test(op: F) -> Result where - F: FnOnce(Logger, Rc, &Handler) -> Result, + F: FnOnce(Logger, Lrc, &Handler) -> Result, { - let cm = Rc::new(SourceMap::new(FilePathMapping::empty())); + let cm = Lrc::new(SourceMap::new(FilePathMapping::empty())); let (handler, errors) = self::errors::new_handler(cm.clone()); let result = swc_common::GLOBALS.set(&swc_common::Globals::new(), || op(logger(), cm, &handler));