Skip to content

Commit

Permalink
Add "compile_module" and "eval_module" flags to std.evalScript
Browse files Browse the repository at this point in the history
This is preparation for standalone binaries support, we need the ability
to compile source as a module and then evaluate it.
  • Loading branch information
saghul committed Dec 2, 2024
1 parent 517e9e2 commit e77cfb6
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion quickjs-libc.c
Original file line number Diff line number Diff line change
Expand Up @@ -865,7 +865,9 @@ static JSValue js_evalScript(JSContext *ctx, JSValue this_val,
JSValue options_obj;
BOOL backtrace_barrier = FALSE;
BOOL eval_function = FALSE;
BOOL eval_module = FALSE;
BOOL compile_only = FALSE;
BOOL compile_module = FALSE;
BOOL is_async = FALSE;
int flags;

Expand All @@ -877,14 +879,33 @@ static JSValue js_evalScript(JSContext *ctx, JSValue this_val,
if (get_bool_option(ctx, &eval_function, options_obj,
"eval_function"))
return JS_EXCEPTION;
if (get_bool_option(ctx, &eval_module, options_obj,
"eval_module"))
return JS_EXCEPTION;
if (get_bool_option(ctx, &compile_only, options_obj,
"compile_only"))
return JS_EXCEPTION;
if (get_bool_option(ctx, &compile_module, options_obj,
"compile_module"))
return JS_EXCEPTION;
if (get_bool_option(ctx, &is_async, options_obj,
"async"))
return JS_EXCEPTION;
}

if (eval_module) {
obj = argv[0];
if (JS_VALUE_GET_TAG(obj) != JS_TAG_MODULE)
return JS_ThrowTypeError(ctx, "not a module");

if (JS_ResolveModule(ctx, obj) < 0)
return JS_EXCEPTION;

js_module_set_import_meta(ctx, obj, FALSE, FALSE);

return JS_EvalFunction(ctx, obj);
}

if (!eval_function) {
str = JS_ToCStringLen(ctx, &len, argv[0]);
if (!str)
Expand All @@ -894,7 +915,7 @@ static JSValue js_evalScript(JSContext *ctx, JSValue this_val,
/* install the interrupt handler */
JS_SetInterruptHandler(JS_GetRuntime(ctx), interrupt_handler, NULL);
}
flags = JS_EVAL_TYPE_GLOBAL;
flags = compile_module ? JS_EVAL_TYPE_MODULE : JS_EVAL_TYPE_GLOBAL;
if (backtrace_barrier)
flags |= JS_EVAL_FLAG_BACKTRACE_BARRIER;
if (compile_only)
Expand Down

0 comments on commit e77cfb6

Please sign in to comment.