Skip to content

Commit e77cfb6

Browse files
committed
Add "compile_module" and "eval_module" flags to std.evalScript
This is preparation for standalone binaries support, we need the ability to compile source as a module and then evaluate it.
1 parent 517e9e2 commit e77cfb6

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

quickjs-libc.c

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -865,7 +865,9 @@ static JSValue js_evalScript(JSContext *ctx, JSValue this_val,
865865
JSValue options_obj;
866866
BOOL backtrace_barrier = FALSE;
867867
BOOL eval_function = FALSE;
868+
BOOL eval_module = FALSE;
868869
BOOL compile_only = FALSE;
870+
BOOL compile_module = FALSE;
869871
BOOL is_async = FALSE;
870872
int flags;
871873

@@ -877,14 +879,33 @@ static JSValue js_evalScript(JSContext *ctx, JSValue this_val,
877879
if (get_bool_option(ctx, &eval_function, options_obj,
878880
"eval_function"))
879881
return JS_EXCEPTION;
882+
if (get_bool_option(ctx, &eval_module, options_obj,
883+
"eval_module"))
884+
return JS_EXCEPTION;
880885
if (get_bool_option(ctx, &compile_only, options_obj,
881886
"compile_only"))
882887
return JS_EXCEPTION;
888+
if (get_bool_option(ctx, &compile_module, options_obj,
889+
"compile_module"))
890+
return JS_EXCEPTION;
883891
if (get_bool_option(ctx, &is_async, options_obj,
884892
"async"))
885893
return JS_EXCEPTION;
886894
}
887895

896+
if (eval_module) {
897+
obj = argv[0];
898+
if (JS_VALUE_GET_TAG(obj) != JS_TAG_MODULE)
899+
return JS_ThrowTypeError(ctx, "not a module");
900+
901+
if (JS_ResolveModule(ctx, obj) < 0)
902+
return JS_EXCEPTION;
903+
904+
js_module_set_import_meta(ctx, obj, FALSE, FALSE);
905+
906+
return JS_EvalFunction(ctx, obj);
907+
}
908+
888909
if (!eval_function) {
889910
str = JS_ToCStringLen(ctx, &len, argv[0]);
890911
if (!str)
@@ -894,7 +915,7 @@ static JSValue js_evalScript(JSContext *ctx, JSValue this_val,
894915
/* install the interrupt handler */
895916
JS_SetInterruptHandler(JS_GetRuntime(ctx), interrupt_handler, NULL);
896917
}
897-
flags = JS_EVAL_TYPE_GLOBAL;
918+
flags = compile_module ? JS_EVAL_TYPE_MODULE : JS_EVAL_TYPE_GLOBAL;
898919
if (backtrace_barrier)
899920
flags |= JS_EVAL_FLAG_BACKTRACE_BARRIER;
900921
if (compile_only)

0 commit comments

Comments
 (0)