pub unsafe fn compile(
    context: *mut JSContext,
    script: Script
) -> Result<JSValueRef, JsError>
Expand description

compile a script, will result in a JSValueRef with tag JS_TAG_FUNCTION_BYTECODE or JS_TAG_MODULE. It can be executed with run_compiled_function().

Example

use quickjs_runtime::builder::QuickJsRuntimeBuilder;
use hirofa_utils::js_utils::Script;
use quickjs_runtime::quickjs_utils::primitives;
use quickjs_runtime::quickjs_utils::compile::{compile, run_compiled_function};
let rt = QuickJsRuntimeBuilder::new().build();
rt.exe_rt_task_in_event_loop(|q_js_rt| {
    unsafe {
        let q_ctx = q_js_rt.get_main_context();
        let func_res = compile(q_ctx.context, Script::new("test_func.es", "let a = 7; let b = 5; a * b;"));
        let func = func_res.ok().expect("func compile failed");
        let run_res = run_compiled_function(q_ctx.context, &func);
        let res = run_res.ok().expect("run_compiled_function failed");
        let i_res = primitives::to_i32(&res);
        let i = i_res.ok().expect("could not convert to i32");
        assert_eq!(i, 7*5);
    }
});

Safety

When passing a context pointer please make sure the corresponding QuickJsContext is still valid