Skip to content

Commit

Permalink
see #11, see #12
Browse files Browse the repository at this point in the history
wip
  • Loading branch information
andrieshiemstra committed Apr 14, 2021
1 parent 3f81d5d commit 1095350
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ pub mod fetch;

pub mod moduleloaders;
pub mod modules;
pub mod preprocessors;

pub fn new_greco_rt_builder() -> EsRuntimeBuilder {
let mut rt_builder = EsRuntimeBuilder::new();

rt_builder = modules::init(rt_builder);
rt_builder = features::init(rt_builder);
rt_builder = preprocessors::init(rt_builder);

rt_builder
}
Expand Down
41 changes: 41 additions & 0 deletions src/preprocessors/ifdef.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use quickjs_runtime::esruntime::ScriptPreProcessor;
use quickjs_runtime::esscript::EsScript;

pub struct IfDefPreProcessor {
defs: Vec<&'static str>,
extensions: Vec<&'static str>,
}

impl IfDefPreProcessor {
pub fn new() -> Self {
Self {
defs: vec![],
extensions: vec![],
}
}
/// add a def
pub fn def(mut self, var: &'static str) -> Self {
self.defs.push(var);
self
}
/// add a supported extension e.g. js/mjs/ts/mts/es/mes
pub fn extension(mut self, ext: &'static str) -> Self {
self.extensions.push(ext);
self
}
/// add default extensions : js/mjs/ts/mts/es/mes
pub fn default_extensions(self) -> Self {
self.extension("es")
.extension("mes")
.extension("js")
.extension("mjs")
.extension("ts")
.extension("mts")
}
}

impl ScriptPreProcessor for IfDefPreProcessor {
fn process(&self, script: EsScript) -> EsScript {
script
}
}
16 changes: 16 additions & 0 deletions src/preprocessors/macros.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use quickjs_runtime::esruntime::ScriptPreProcessor;
use quickjs_runtime::esscript::EsScript;

pub struct MacrosPreProcessor {}

impl MacrosPreProcessor {
pub fn new() -> Self {
Self {}
}
}

impl ScriptPreProcessor for MacrosPreProcessor {
fn process(&self, script: EsScript) -> EsScript {
script
}
}
21 changes: 21 additions & 0 deletions src/preprocessors/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//! IfDefPreProcessor
//!
//! this is a script preprocessor which can be used to conditionally load/unload pieces of script before compilation or evaluation
//!
//! # Example
//! ```javascript
//!
//! ```

use crate::preprocessors::ifdef::IfDefPreProcessor;
use crate::preprocessors::macros::MacrosPreProcessor;
use quickjs_runtime::esruntimebuilder::EsRuntimeBuilder;

pub mod ifdef;
pub mod macros;

pub(crate) fn init(builder: EsRuntimeBuilder) -> EsRuntimeBuilder {
builder
.script_pre_processor(MacrosPreProcessor::new())
.script_pre_processor(IfDefPreProcessor::new().default_extensions())
}

0 comments on commit 1095350

Please sign in to comment.