diff --git a/quickjs.c b/quickjs.c
index 48aeffc..6862b8c 100644
@@ -1192,6 +1192,8 @@ static void js_free_module_def(JSContext *ctx, JSModuleDef *m);
static void js_mark_module_def(JSRuntime *rt, JSModuleDef *m,
JS_MarkFunc *mark_func);
static JSValue js_import_meta(JSContext *ctx);
+JSValue JS_DynamicImportSync(JSContext *ctx, const char *specifier);
+static JSValue js_dynamic_import_run(JSContext *ctx, JSValueConst basename_val, const char *specifier);
static JSValue js_dynamic_import(JSContext *ctx, JSValueConst specifier);
static void free_var_ref(JSRuntime *rt, JSVarRef *var_ref);
static JSValue js_new_promise_capability(JSContext *ctx,
@@ -28210,29 +28212,14 @@ static JSValue js_dynamic_import_job(JSContext *ctx,
JSValueConst *resolving_funcs = argv;
JSValueConst basename_val = argv[2];
JSValueConst specifier = argv[3];
- JSModuleDef *m;
- const char *basename = NULL, *filename;
+ const char *filename;
JSValue ret, err, ns;
- if (!JS_IsString(basename_val)) {
- JS_ThrowTypeError(ctx, "no function filename for import()");
- goto exception;
- }
- basename = JS_ToCString(ctx, basename_val);
- if (!basename)
- goto exception;
-
filename = JS_ToCString(ctx, specifier);
if (!filename)
goto exception;
-
- m = JS_RunModule(ctx, basename, filename);
- JS_FreeCString(ctx, filename);
- if (!m)
- goto exception;
- /* return the module namespace */
- ns = js_get_module_ns(ctx, m);
+ ns = js_dynamic_import_run(ctx, basename_val, filename);
if (JS_IsException(ns))
goto exception;
@@ -28240,7 +28227,6 @@ static JSValue js_dynamic_import_job(JSContext *ctx,
1, (JSValueConst *)&ns);
JS_FreeValue(ctx, ret); /* XXX: what to do if exception ? */
JS_FreeValue(ctx, ns);
- JS_FreeCString(ctx, basename);
return JS_UNDEFINED;
exception:
@@ -28249,10 +28235,38 @@ static JSValue js_dynamic_import_job(JSContext *ctx,
1, (JSValueConst *)&err);
JS_FreeValue(ctx, ret); /* XXX: what to do if exception ? */
JS_FreeValue(ctx, err);
- JS_FreeCString(ctx, basename);
return JS_UNDEFINED;
}
+JSValue JS_DynamicImportSync(JSContext *ctx, const char *specifier)
+{
+ JSAtom basename_atom;
+ JSValue basename_val;
+ JSValue ns;
+
+ basename_atom = JS_GetScriptOrModuleName(ctx, 1);
+ if (basename_atom == JS_ATOM_NULL)
+ {
+ JS_FreeAtom(ctx, basename_atom);
+ basename_val = js_new_string8(ctx, NULL, 0); //module name can't be accessed if we are running (eval)
+ }
+ else
+ {
+ basename_val = JS_AtomToValue(ctx, basename_atom);
+ JS_FreeAtom(ctx, basename_atom);
+ }
+ if (JS_IsException(basename_val))
+ return basename_val;
+
+ ns = js_dynamic_import_run(ctx, basename_val, specifier);
+ if (JS_IsException(ns))
+ {
+ return JS_EXCEPTION;
+ }
+
+ return ns;
+}
+
static JSValue js_dynamic_import(JSContext *ctx, JSValueConst specifier)
{
JSAtom basename;
@@ -28343,6 +28357,44 @@ static JSValue js_evaluate_module(JSContext *ctx, JSModuleDef *m)
return ret_val;
}
+static JSValue js_dynamic_import_run(JSContext *ctx, JSValueConst basename_val, const char *specifier)
+{
+ JSModuleDef *m;
+ const char *basename = NULL;
+ JSValue ns;
+
+ if (JS_IsString(basename_val))
+ {
+ basename = JS_ToCString(ctx, basename_val);
+ if (!basename)
+ {
+ JS_ThrowTypeError(ctx, "no function filename for import()");
+ goto exception;
+ }
+ }
+ else
+ {
+ JS_ThrowTypeError(ctx, "basename received by import() was not a string");
+ goto exception;
+ }
+
+ m = JS_RunModule(ctx, basename, specifier);
+ JS_FreeCString(ctx, specifier);
+ if (!m)
+ goto exception;
+
+ /* return the module namespace */
+ ns = js_get_module_ns(ctx, m);
+ if (JS_IsException(ns))
+ goto exception;
+
+ JS_FreeCString(ctx, basename);
+ return ns;
+exception:
+ JS_FreeCString(ctx, basename);
+ return JS_EXCEPTION;
+}
+
static __exception JSAtom js_parse_from_clause(JSParseState *s)
{
JSAtom module_name;
@@ -33532,7 +33584,7 @@ static JSValue JS_EvalFunctionInternal(JSContext *ctx, JSValue fun_obj,
ret_val = js_evaluate_module(ctx, m);
if (JS_IsException(ret_val)) {
fail:
- js_free_modules(ctx, JS_FREE_MODULE_NOT_EVALUATED);
+ js_free_modules(ctx, JS_FREE_MODULE_NOT_RESOLVED);
return JS_EXCEPTION;
}
} else {
@@ -33542,6 +33594,10 @@ static JSValue JS_EvalFunctionInternal(JSContext *ctx, JSValue fun_obj,
return ret_val;
}
+void JS_FreeUnevaluatedModules(JSContext *ctx){
+ js_free_modules(ctx, JS_FREE_MODULE_NOT_EVALUATED);
+}
+
JSValue JS_EvalFunction(JSContext *ctx, JSValue fun_obj)
{
return JS_EvalFunctionInternal(ctx, fun_obj, ctx->global_obj, NULL, NULL);
diff --git a/quickjs.h b/quickjs.h
index d4a5cd3..0ad5b30 100644
@@ -866,6 +866,9 @@ void JS_SetModuleLoaderFunc(JSRuntime *rt,
JSValue JS_GetImportMeta(JSContext *ctx, JSModuleDef *m);
JSAtom JS_GetModuleName(JSContext *ctx, JSModuleDef *m);
+void JS_FreeUnevaluatedModules(JSContext *ctx);
+
+
/* JS Job support */
typedef JSValue JSJobFunc(JSContext *ctx, int argc, JSValueConst *argv);
@@ -1039,6 +1042,8 @@ int JS_SetModuleExport(JSContext *ctx, JSModuleDef *m, const char *export_name,
int JS_SetModuleExportList(JSContext *ctx, JSModuleDef *m,
const JSCFunctionListEntry *tab, int len);
+JSValue JS_DynamicImportSync(JSContext *ctx, const char *specifier);
+
#undef js_unlikely
#undef js_force_inline