Cache compiled helpers

This commit is contained in:
강동윤 2019-01-15 11:53:43 +09:00
parent e85bcce9cf
commit b9eb110549
3 changed files with 38 additions and 29 deletions

View File

@ -15,8 +15,9 @@ swc_ecma_ast = { path ="../ast" }
swc_ecma_parser = { path ="../parser" }
either = "1.5"
fnv = "1"
serde = { version = "1", features = ["derive"] }
lazy_static = "1"
objekt = "0.1"
serde = { version = "1", features = ["derive"] }
[dev-dependencies]
testing = { path ="../../testing" }

View File

@ -6,7 +6,7 @@ use std::sync::{
use swc_common::{
errors::{ColorConfig, Handler},
sync::Lrc,
FileName, Fold, SourceMap,
FileName, FilePathMapping, Fold, SourceMap,
};
use swc_ecma_parser::{Parser, Session, SourceFileInput, Syntax};
@ -53,36 +53,42 @@ pub struct InjectHelpers {
impl InjectHelpers {
fn mk_helpers(&self) -> Vec<Stmt> {
lazy_static! {
static ref CM: Lrc<SourceMap> = { Lrc::new(SourceMap::new(FilePathMapping::empty())) };
static ref HANDLER: Handler =
{ Handler::with_tty_emitter(ColorConfig::Always, false, true, Some(CM.clone())) };
static ref SESSION: Session<'static> = { Session { handler: &*HANDLER } };
}
let mut buf = vec![];
let handler =
Handler::with_tty_emitter(ColorConfig::Always, false, true, Some(self.cm.clone()));
let session = Session { handler: &handler };
let mut add = |name: &str, flag: &AtomicBool, code: &'static str| {
let enable = flag.load(Ordering::Relaxed);
if !enable {
return;
}
let fm = self
.cm
.new_source_file(FileName::Custom(name.into()), code.into());
let mut stmts = Parser::new(session, Syntax::default(), SourceFileInput::from(&*fm))
.parse_script()
.map_err(|e| {
e.emit();
()
})
.unwrap();
buf.append(&mut stmts);
};
macro_rules! add {
($name:tt,$b:expr) => {
add($name, $b, include_str!($name));
};
($name:tt, $b:expr) => {{
lazy_static! {
static ref STMTS: Vec<Stmt> = {
let code = include_str!($name);
let fm = CM.new_source_file(
FileName::Custom(stringify!($name).into()),
code.into(),
);
let stmts =
Parser::new(*SESSION, Syntax::default(), SourceFileInput::from(&*fm))
.parse_script()
.map_err(|e| {
e.emit();
()
})
.unwrap();
stmts
};
}
let enable = $b.load(Ordering::Relaxed);
if enable {
buf.extend_from_slice(&STMTS)
}
}};
}
add!("_extends.js", &self.helpers.extends);

View File

@ -18,6 +18,8 @@ extern crate swc_ecma_ast as ast;
#[cfg(test)]
extern crate swc_ecma_codegen;
extern crate swc_ecma_parser;
#[macro_use]
extern crate lazy_static;
#[cfg(test)]
#[macro_use]
extern crate pretty_assertions;