Make it thread safe

This commit is contained in:
강동윤 2018-11-17 17:02:34 +09:00
parent b3a9d1a264
commit 8e0e9ca4c7
15 changed files with 46 additions and 45 deletions

View File

@ -1,6 +1,11 @@
[build] [build]
rustflags = ["--cfg", "procmacro2_semver_exempt"] rustflags = [
"--cfg", "procmacro2_semver_exempt",
"--cfg", "parallel_queries",
]
rustdocflags = [ rustdocflags = [
"--cfg", "procmacro2_semver_exempt", "--cfg", "procmacro2_semver_exempt",
"--cfg", "parallel_queries",
] ]

View File

@ -15,7 +15,7 @@ swc is rust port of [babel][] and [closure compiler][].
Requires nightly version of [rust][]. Requires nightly version of [rust][].
```sh ```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 # Features

View File

@ -3,8 +3,8 @@ use crate::{FileLoader, FilePathMapping, SourceMap};
use std::{ use std::{
io, io,
path::{Path, PathBuf}, path::{Path, PathBuf},
rc::Rc,
}; };
use sync::Lrc;
use BytePos; use BytePos;
use Span; use Span;
@ -47,7 +47,7 @@ fn test() {
let end_pos = file_map.end_pos - BytePos(1); let end_pos = file_map.end_pos - BytePos(1);
let full = Span::new(start_pos, end_pos, Default::default()); 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(), || { ::syntax_pos::GLOBALS.set(&::syntax_pos::Globals::new(), || {
DiagnosticBuilder::new_with_code( DiagnosticBuilder::new_with_code(

View File

@ -17,6 +17,7 @@ pub use self::{
pos::*, pos::*,
}; };
pub use ast_node::{ast_node, Fold, FromVariant, Spanned}; pub use ast_node::{ast_node, Fold, FromVariant, Spanned};
pub use rustc_data_structures::sync;
use std::fmt::Debug; use std::fmt::Debug;
pub use syntax::source_map::{ pub use syntax::source_map::{
FileLines, FileLoader, FileName, FilePathMapping, SourceMap, SpanSnippetError, FileLines, FileLoader, FileName, FilePathMapping, SourceMap, SpanSnippetError,

View File

@ -21,9 +21,9 @@ use self::{
text_writer::WriteJs, text_writer::WriteJs,
util::{SourceMapperExt, SpanExt, StartsWithAlphaNum}, util::{SourceMapperExt, SpanExt, StartsWithAlphaNum},
}; };
use std::{collections::HashSet, io, rc::Rc}; use std::{collections::HashSet, io};
use swc_atoms::JsWord; 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_ast::*;
use swc_ecma_codegen_macros::emitter; use swc_ecma_codegen_macros::emitter;
@ -61,7 +61,7 @@ impl<'a, N: Node> Node for &'a N {
pub struct Emitter<'a> { pub struct Emitter<'a> {
pub cfg: config::Config, pub cfg: config::Config,
pub cm: Rc<SourceMap>, pub cm: Lrc<SourceMap>,
pub wr: Box<('a + WriteJs)>, pub wr: Box<('a + WriteJs)>,
pub handlers: Box<('a + Handlers)>, pub handlers: Box<('a + Handlers)>,
pub pos_of_leading_comments: HashSet<BytePos>, pub pos_of_leading_comments: HashSet<BytePos>,
@ -1478,7 +1478,7 @@ impl<'a> Emitter<'a> {
} }
fn get_text_of_node<T: Spanned>( fn get_text_of_node<T: Spanned>(
cm: &Rc<SourceMap>, cm: &Lrc<SourceMap>,
node: &T, node: &T,
_include_travia: bool, _include_travia: bool,
) -> Option<String> { ) -> Option<String> {

View File

@ -19,7 +19,7 @@ impl Handlers for Noop {}
struct Builder { struct Builder {
cfg: Config, cfg: Config,
cm: Rc<SourceMap>, cm: Lrc<SourceMap>,
} }
fn test() -> Builder { fn test() -> Builder {
@ -27,7 +27,7 @@ fn test() -> Builder {
Builder { Builder {
cfg: Default::default(), cfg: Default::default(),
cm: Rc::new(src), cm: Lrc::new(src),
} }
} }

View File

@ -1,10 +1,7 @@
use super::{Result, WriteJs}; use super::{Result, WriteJs};
use sourcemap::SourceMapBuilder; use sourcemap::SourceMapBuilder;
use std::{ use std::io::{self, Write};
io::{self, Write}, use swc_common::{sync::Lrc, SourceMap, Span};
rc::Rc,
};
use swc_common::{SourceMap, Span};
/// ///
/// ----- /// -----
@ -13,7 +10,7 @@ use swc_common::{SourceMap, Span};
/// ///
/// https://github.com/Microsoft/TypeScript/blob/45eaf42006/src/compiler/utilities.ts#L2548 /// https://github.com/Microsoft/TypeScript/blob/45eaf42006/src/compiler/utilities.ts#L2548
pub struct JsWriter<'a, W: Write> { pub struct JsWriter<'a, W: Write> {
cm: Rc<SourceMap>, cm: Lrc<SourceMap>,
indent: usize, indent: usize,
line_start: bool, line_start: bool,
line_count: usize, line_count: usize,
@ -26,7 +23,7 @@ pub struct JsWriter<'a, W: Write> {
impl<'a, W: Write> JsWriter<'a, W> { impl<'a, W: Write> JsWriter<'a, W> {
pub fn new( pub fn new(
cm: Rc<SourceMap>, cm: Lrc<SourceMap>,
new_line: &'a str, new_line: &'a str,
wr: W, wr: W,
srcmap: &'a mut SourceMapBuilder, srcmap: &'a mut SourceMapBuilder,

View File

@ -1,7 +1,7 @@
use super::list::ListFormat; use super::list::ListFormat;
use std::rc::Rc;
use swc_common::{ 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::*; use swc_ecma_ast::*;
@ -127,12 +127,13 @@ impl SourceMapperExt for SourceMapper {
self self
} }
} }
impl SourceMapperExt for Rc<SourceMapperDyn> { impl SourceMapperExt for Lrc<SourceMapperDyn> {
fn get_code_map(&self) -> &SourceMapper { fn get_code_map(&self) -> &SourceMapper {
&**self &**self
} }
} }
impl SourceMapperExt for Rc<SourceMap> {
impl SourceMapperExt for Lrc<SourceMap> {
fn get_code_map(&self) -> &SourceMapper { fn get_code_map(&self) -> &SourceMapper {
&**self &**self
} }

View File

@ -15,10 +15,9 @@ use std::{
fs::{read_dir, File}, fs::{read_dir, File},
io::{self, Read, Write}, io::{self, Read, Write},
path::Path, path::Path,
rc::Rc,
sync::{Arc, RwLock}, sync::{Arc, RwLock},
}; };
use swc_common::{Fold, FoldWith, Span}; use swc_common::{sync::Lrc, Fold, FoldWith, Span};
use swc_ecma_ast::*; use swc_ecma_ast::*;
use swc_ecma_codegen::Emitter; use swc_ecma_codegen::Emitter;
use swc_ecma_parser::{Parser, Session, SourceFileInput}; use swc_ecma_parser::{Parser, Session, SourceFileInput};
@ -158,7 +157,7 @@ fn error_tests(tests: &mut Vec<TestDescAndFn>) -> Result<(), io::Error> {
(&*src).into(), (&*src).into(),
); );
let s: Rc<String> = src.src.as_ref().map(|s| s.clone()).unwrap(); let s: Lrc<String> = src.src.as_ref().map(|s| s.clone()).unwrap();
let mut src_map_builder = SourceMapBuilder::new(Some(&s)); let mut src_map_builder = SourceMapBuilder::new(Some(&s));
{ {
let mut emitter = Emitter { let mut emitter = Emitter {

View File

@ -1,7 +1,6 @@
use ast::*; use ast::*;
use std::{ use std::{
ops::BitOr, ops::BitOr,
rc::Rc,
sync::{ sync::{
atomic::{AtomicBool, Ordering}, atomic::{AtomicBool, Ordering},
Arc, Arc,
@ -9,6 +8,7 @@ use std::{
}; };
use swc_common::{ use swc_common::{
errors::{ColorConfig, Handler}, errors::{ColorConfig, Handler},
sync::Lrc,
FileName, Fold, SourceMap, FileName, Fold, SourceMap,
}; };
use swc_ecma_parser::{Parser, Session, SourceFileInput}; use swc_ecma_parser::{Parser, Session, SourceFileInput};
@ -49,7 +49,7 @@ impl<'a> BitOr<&'a Helpers> for Helpers {
} }
pub struct InjectHelpers { pub struct InjectHelpers {
pub cm: Rc<SourceMap>, pub cm: Lrc<SourceMap>,
pub helpers: Arc<Helpers>, pub helpers: Arc<Helpers>,
} }

View File

@ -1,12 +1,11 @@
use ast::*;
use slog::Logger; use slog::Logger;
use sourcemap::SourceMapBuilder; use sourcemap::SourceMapBuilder;
use std::{ use std::{
io::{self, Write}, io::{self, Write},
rc::Rc,
sync::{Arc, RwLock}, sync::{Arc, RwLock},
}; };
use swc_common::{errors::Handler, FileName, Fold, FoldWith, SourceMap}; use swc_common::{errors::Handler, sync::Lrc, FileName, Fold, FoldWith, SourceMap};
use ast::*;
use swc_ecma_codegen::Emitter; use swc_ecma_codegen::Emitter;
use swc_ecma_parser::{Parser, Session, SourceFileInput}; use swc_ecma_parser::{Parser, Session, SourceFileInput};
@ -15,7 +14,7 @@ struct MyHandlers;
impl swc_ecma_codegen::Handlers for MyHandlers {} impl swc_ecma_codegen::Handlers for MyHandlers {}
pub(crate) struct Tester<'a> { pub(crate) struct Tester<'a> {
cm: Rc<SourceMap>, cm: Lrc<SourceMap>,
logger: Logger, logger: Logger,
handler: &'a Handler, handler: &'a Handler,
} }

View File

@ -9,7 +9,7 @@ pub extern crate swc_common as common;
pub extern crate swc_ecmascript as ecmascript; pub extern crate swc_ecmascript as ecmascript;
use self::{ use self::{
common::{errors::Handler, SourceMap}, common::{errors::Handler, sync::Lrc, SourceMap},
ecmascript::{ ecmascript::{
ast::Module, ast::Module,
codegen::{self, Emitter}, codegen::{self, Emitter},
@ -21,17 +21,16 @@ use sourcemap::SourceMapBuilder;
use std::{ use std::{
io::{self, Write}, io::{self, Write},
path::Path, path::Path,
rc::Rc,
}; };
pub struct Compiler { pub struct Compiler {
cm: Rc<SourceMap>, cm: Lrc<SourceMap>,
logger: Logger, logger: Logger,
handler: Handler, handler: Handler,
} }
impl Compiler { impl Compiler {
pub fn new(logger: Logger, cm: Rc<SourceMap>, handler: Handler) -> Self { pub fn new(logger: Logger, cm: Lrc<SourceMap>, handler: Handler) -> Self {
Compiler { Compiler {
cm, cm,
logger, logger,

View File

@ -14,11 +14,10 @@ use std::{
error::Error, error::Error,
io::{self, Write}, io::{self, Write},
path::Path, path::Path,
rc::Rc,
sync::Arc, sync::Arc,
}; };
use swc::{ use swc::{
common::{errors::Handler, FilePathMapping, Fold, SourceMap}, common::{errors::Handler, sync::Lrc, FilePathMapping, Fold, SourceMap},
ecmascript::{ast::Module, codegen}, ecmascript::{ast::Module, codegen},
Compiler, Compiler,
}; };
@ -70,7 +69,7 @@ fn run() -> Result<(), Box<Error>> {
.build_global() .build_global()
.expect("failed to configure rayon::ThreadPool"); .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( let handler = Handler::with_tty_emitter(
swc::common::errors::ColorConfig::Always, swc::common::errors::ColorConfig::Always,
@ -110,7 +109,7 @@ fn run() -> Result<(), Box<Error>> {
Ok(()) Ok(())
} }
fn js_pass(cm: Rc<SourceMap>, matches: &ArgMatches) -> Box<Fold<Module>> { fn js_pass(cm: Lrc<SourceMap>, matches: &ArgMatches) -> Box<Fold<Module>> {
use swc::ecmascript::transforms::{compat, simplifier}; use swc::ecmascript::transforms::{compat, simplifier};
let helpers = Arc::new(compat::helpers::Helpers::default()); let helpers = Arc::new(compat::helpers::Helpers::default());

View File

@ -1,13 +1,15 @@
use super::StdErr; use super::StdErr;
use std::{ use std::{
io::{self, Write}, io::{self, Write},
rc::Rc,
sync::{Arc, RwLock}, 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. /// Creates a new handler for testing.
pub(crate) fn new_handler(cm: Rc<SourceMapperDyn>) -> (Handler, BufferedError) { pub(crate) fn new_handler(cm: Lrc<SourceMapperDyn>) -> (Handler, BufferedError) {
let buf: BufferedError = Default::default(); let buf: BufferedError = Default::default();
let e = EmitterWriter::new(box buf.clone(), Some(cm.clone()), false, true); let e = EmitterWriter::new(box buf.clone(), Some(cm.clone()), false, true);

View File

@ -22,10 +22,9 @@ use std::{
fs::{create_dir_all, File}, fs::{create_dir_all, File},
io::{self, Write}, io::{self, Write},
path::Path, path::Path,
rc::Rc,
thread, 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] #[macro_use]
mod macros; mod macros;
@ -35,9 +34,9 @@ mod paths;
pub fn run_test<F, Ret>(op: F) -> Result<Ret, StdErr> pub fn run_test<F, Ret>(op: F) -> Result<Ret, StdErr>
where where
F: FnOnce(Logger, Rc<SourceMap>, &Handler) -> Result<Ret, ()>, F: FnOnce(Logger, Lrc<SourceMap>, &Handler) -> Result<Ret, ()>,
{ {
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 (handler, errors) = self::errors::new_handler(cm.clone());
let result = let result =
swc_common::GLOBALS.set(&swc_common::Globals::new(), || op(logger(), cm, &handler)); swc_common::GLOBALS.set(&swc_common::Globals::new(), || op(logger(), cm, &handler));