-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
4 changed files
with
80 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} |