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 15, 2021
1 parent 55f91a6 commit 5bf7488
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 22 deletions.
5 changes: 4 additions & 1 deletion src/features/require.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,13 @@ unsafe extern "C" fn require(
cur_path = "file:///node_modules/foo.js".to_string();
}

log::debug!("require: {} -> {}", cur_path, name);

if let Some(module_script) =
q_js_rt.load_module_script_opt(cur_path.as_str(), name.as_str())
{
let wrapped_function_code = format!(
"function(){{const module = {{exports:{{}}}};let exports = module.exports;{{\n{}\n}} return module.exports;}};",
"const module = {{exports:{{}}}};let exports = module.exports;{{\n{}\n}} return module.exports;",
module_script.as_str()
);

Expand Down Expand Up @@ -99,6 +101,7 @@ unsafe extern "C" fn require(
Err(e) => q_ctx.report_ex(format!("Module parsing failed: {}", e).as_str()),
}
} else {
log::error!("module not found: {} -> {}", cur_path, name);
q_ctx.report_ex("module not found")
}
// wip
Expand Down
46 changes: 27 additions & 19 deletions src/preprocessors/cpp.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::moduleloaders::{FileSystemModuleLoader, HttpModuleLoader};
use crate::new_greco_rt_builder2;
use quickjs_runtime::esruntime::{EsRuntime, ScriptPreProcessor};
use quickjs_runtime::esscript::EsScript;
Expand All @@ -7,7 +8,12 @@ use quickjs_runtime::quickjs_utils::objects::get_property_q;
use std::sync::Arc;

lazy_static! {
static ref UTIL_RT: Arc<EsRuntime> = new_greco_rt_builder2(false, true, true).build();
static ref UTIL_RT: Arc<EsRuntime> = new_greco_rt_builder2(false, true, true)
.script_module_loader(Box::new(
HttpModuleLoader::new().validate_content_type(false)
))
.script_module_loader(Box::new(FileSystemModuleLoader::new("./")))
.build();
}

pub struct CppPreProcessor {
Expand Down Expand Up @@ -65,20 +71,19 @@ impl ScriptPreProcessor for CppPreProcessor {
q_ctx
.eval(EsScript::new(
"CppPreProcessor.not_es",
"this.CppPreProcessor = {process: function(src, ...vars){\
let compiler = require('https://raw.githubusercontent.com/ParksProjets/C-Preprocessor/master/lib/compiler.js');\
let options = {constants: {DEBUG: true}}; /* todo replace with vars */\
return new Promise((resolve, reject) => {\
compiler.compile(src, options, (err, result) => {\
if result {\
resolve(result);\
} else {\
reject(err);\
}\
});\
});\
}};",
"this.CppPreProcessor = {process: function(src, vars){\n\
let compiler = require('https://raw.githubusercontent.com/ParksProjets/C-Preprocessor/master/lib/compiler.js');\n\
let options = {constants: {DEBUG: true}};\n\
return new Promise((resolve, reject) => {\n\
compiler.compile(src, options, (err, result) => {\n\
if (result) {\n\
resolve(result);\n\
} else {\n\
reject(err);\n\
}\n\
});\n\
});\n\
}};",
))
.ok()
.expect("CppPreProcessor init script failed");
Expand All @@ -88,10 +93,13 @@ impl ScriptPreProcessor for CppPreProcessor {
});

let src = script.get_code().to_string().to_es_value_facade();
let proc_res_prom = rt
.call_function_sync(vec!["CppPreProcessor"], "process", vec![src])
.ok()
.expect("proc func failed");
let proc_res_prom =
match rt.call_function_sync(vec!["CppPreProcessor"], "process", vec![src]) {
Ok(res) => res,
Err(err) => {
panic!("process call failed: {}", err);
}
};

let proc_res = proc_res_prom.get_promise_result_sync();
let new_code = proc_res.ok().expect("prom did not resolve");
Expand Down
4 changes: 2 additions & 2 deletions src/preprocessors/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ pub mod macros;

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

0 comments on commit 5bf7488

Please sign in to comment.