Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-js

Source of release 0.6.1

0.6.1 / src / host / eval_binary.lucb

#==============================================================================================
#
#   eval_binary - Running bytecode compiled ahead of time (qjsc and ljsc output)
#
#   DESCRIPTION:
#       Port of quickjs-libc.c lines 4353-4402: js_std_eval_binary, which reads a script or
#       a module written with JS_WriteObject and runs it (or, for a module, only loads it),
#       and js_std_eval_binary_json_module, which makes a JSON module of a value written
#       the same way. Programs generated by ljsc (tests/ljsc) and the REPL of ljs use them.
#       As in the C, a failure prints the exception and exits with status 1.
#
#==============================================================================================

## Read the bytecode `buf` (JS_WriteObject with write_obj_bytecode) and run it; with
## `load_only`, a module is only loaded (declared to the module loader) and not evaluated
## (js_std_eval_binary). Exits with status 1 after printing the exception on a failure.
pub func js_std_eval_binary(ctx: Context*, buf: const u8*, buf_len: usize, load_only: bool):
    eval_binary(ctx, buf, buf_len, load_only) catch:
        js_std_dump_error(ctx)
        c_exit(1)

func eval_binary(ctx: Context*, buf: const u8*, buf_len: usize, load_only: bool) -> !:
    let obj = try js.js_read_object(ctx, buf, buf_len, js.read_obj_bytecode)
    if load_only:
        if js.js_value_get_tag(obj) == js.Tag.module:
            js_module_set_import_meta(ctx, obj, false, false) catch failure:
                js.js_free_value(ctx, obj)
                error(failure.code, failure.message)
        js.js_free_value(ctx, obj)
    else:
        var val = js.js_undefined()
        if js.js_value_get_tag(obj) == js.Tag.module:
            js.js_resolve_module(ctx, obj) catch failure:
                js.js_free_value(ctx, obj)
                error(failure.code, failure.message)
            js_module_set_import_meta(ctx, obj, false, true) catch failure:
                js.js_free_value(ctx, obj)
                error(failure.code, failure.message)
            val = try js_std_await(ctx, try js.js_eval_function(ctx, obj))
        else:
            val = try js.js_eval_function(ctx, obj)
        js.js_free_value(ctx, val)

## Read the value `buf` (JS_WriteObject without flags) and declare it as the JSON module
## `module_name` (js_std_eval_binary_json_module). Exits with status 1 after printing the
## exception on a failure.
pub func js_std_eval_binary_json_module(ctx: Context*, buf: const u8*, buf_len: usize, module_name: const u8*):
    eval_binary_json_module(ctx, buf, buf_len, module_name) catch:
        js_std_dump_error(ctx)
        c_exit(1)

func eval_binary_json_module(ctx: Context*, buf: const u8*, buf_len: usize, module_name: const u8*) -> !:
    let obj = try js.js_read_object(ctx, buf, buf_len, 0)
    _ = try create_json_module(ctx, module_name, obj)