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]
rustflags = ["--cfg", "procmacro2_semver_exempt"]
rustflags = [
"--cfg", "procmacro2_semver_exempt",
"--cfg", "parallel_queries",
]
rustdocflags = [
"--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][].
```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

View File

@ -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(

View File

@ -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,

View File

@ -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<SourceMap>,
pub cm: Lrc<SourceMap>,
pub wr: Box<('a + WriteJs)>,
pub handlers: Box<('a + Handlers)>,
pub pos_of_leading_comments: HashSet<BytePos>,
@ -1478,7 +1478,7 @@ impl<'a> Emitter<'a> {
}
fn get_text_of_node<T: Spanned>(
cm: &Rc<SourceMap>,
cm: &Lrc<SourceMap>,
node: &T,
_include_travia: bool,
) -> Option<String> {

View File

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

View File

@ -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<SourceMap>,
cm: Lrc<SourceMap>,
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<SourceMap>,
cm: Lrc<SourceMap>,
new_line: &'a str,
wr: W,
srcmap: &'a mut SourceMapBuilder,

View File

@ -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<SourceMapperDyn> {
impl SourceMapperExt for Lrc<SourceMapperDyn> {
fn get_code_map(&self) -> &SourceMapper {
&**self
}
}
impl SourceMapperExt for Rc<SourceMap> {
impl SourceMapperExt for Lrc<SourceMap> {
fn get_code_map(&self) -> &SourceMapper {
&**self
}

View File

@ -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<TestDescAndFn>) -> Result<(), io::Error> {
(&*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 emitter = Emitter {

View File

@ -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<SourceMap>,
pub cm: Lrc<SourceMap>,
pub helpers: Arc<Helpers>,
}

View File

@ -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<SourceMap>,
cm: Lrc<SourceMap>,
logger: Logger,
handler: &'a Handler,
}

View File

@ -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<SourceMap>,
cm: Lrc<SourceMap>,
logger: Logger,
handler: Handler,
}
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 {
cm,
logger,

View File

@ -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<Error>> {
.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<Error>> {
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};
let helpers = Arc::new(compat::helpers::Helpers::default());

View File

@ -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<SourceMapperDyn>) -> (Handler, BufferedError) {
pub(crate) fn new_handler(cm: Lrc<SourceMapperDyn>) -> (Handler, BufferedError) {
let buf: BufferedError = Default::default();
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},
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<F, Ret>(op: F) -> Result<Ret, StdErr>
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 result =
swc_common::GLOBALS.set(&swc_common::Globals::new(), || op(logger(), cm, &handler));