Skip to content

Commit

Permalink
Align module export API with upstream
Browse files Browse the repository at this point in the history
Partially reverts
6868fb9
but the same behavior can be implemented in userland by getting the
module ns and querying its properties.

Ref: bellard/quickjs@c6cc6a9
Fixes: #259
  • Loading branch information
saghul committed Feb 12, 2024
1 parent 359a118 commit 7ded62c
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 43 deletions.
43 changes: 5 additions & 38 deletions quickjs.c
Original file line number Diff line number Diff line change
Expand Up @@ -25839,13 +25839,11 @@ static int exported_names_cmp(const void *p1, const void *p2, void *opaque)
return ret;
}

static JSValue js_get_module_ns(JSContext *ctx, JSModuleDef *m);

static JSValue js_module_ns_autoinit(JSContext *ctx, JSObject *p, JSAtom atom,
void *opaque)
{
JSModuleDef *m = opaque;
return js_get_module_ns(ctx, m);
return JS_GetModuleNamespace(ctx, m);
}

static JSValue js_build_module_ns(JSContext *ctx, JSModuleDef *m)
Expand Down Expand Up @@ -25951,7 +25949,7 @@ static JSValue js_build_module_ns(JSContext *ctx, JSModuleDef *m)
return JS_EXCEPTION;
}

static JSValue js_get_module_ns(JSContext *ctx, JSModuleDef *m)
JSValue JS_GetModuleNamespace(JSContext *ctx, JSModuleDef *m)
{
if (JS_IsUndefined(m->module_ns)) {
JSValue val;
Expand Down Expand Up @@ -26185,7 +26183,7 @@ static int js_link_module(JSContext *ctx, JSModuleDef *m)
if (mi->import_name == JS_ATOM__star_) {
JSValue val;
/* name space import */
val = js_get_module_ns(ctx, m1);
val = JS_GetModuleNamespace(ctx, m1);
if (JS_IsException(val))
goto fail;
set_value(ctx, &var_refs[mi->var_idx]->value, val);
Expand All @@ -26209,7 +26207,7 @@ static int js_link_module(JSContext *ctx, JSModuleDef *m)
JSModuleDef *m2;
/* name space import from */
m2 = res_m->req_module_entries[res_me->u.req_module_idx].module;
val = js_get_module_ns(ctx, m2);
val = JS_GetModuleNamespace(ctx, m2);
if (JS_IsException(val))
goto fail;
var_ref = js_create_module_var(ctx, TRUE);
Expand Down Expand Up @@ -26396,7 +26394,7 @@ static JSValue js_dynamic_import_job(JSContext *ctx,
goto exception;

/* return the module namespace */
ns = js_get_module_ns(ctx, m);
ns = JS_GetModuleNamespace(ctx, m);
if (JS_IsException(ns))
goto exception;

Expand Down Expand Up @@ -34396,37 +34394,6 @@ int JS_SetModuleExportList(JSContext *ctx, JSModuleDef *m,
return 0;
}

JSValue JS_GetModuleExport(JSContext *ctx, const JSModuleDef *m, const char *export_name) {
JSExportEntry *me;
JSAtom name;
name = JS_NewAtom(ctx, export_name);
if (name == JS_ATOM_NULL)
goto fail;
me = find_export_entry(ctx, m, name);
JS_FreeAtom(ctx, name);
if (!me)
goto fail;
return JS_DupValue(ctx, me->u.local.var_ref->value);
fail:
return JS_UNDEFINED;
}

int JS_CountModuleExport(JSContext *ctx, const JSModuleDef *m) {
return m->export_entries_count;
}

JSAtom JS_GetModuleExportName(JSContext *ctx, const JSModuleDef *m, int idx) {
if (idx >= m->export_entries_count || idx < 0)
return JS_ATOM_NULL;
return JS_DupAtom(ctx, m->export_entries[idx].export_name);
}

JSValue JS_GetModuleExportValue(JSContext *ctx, const JSModuleDef *m, int idx) {
if (idx >= m->export_entries_count || idx < 0)
return JS_UNDEFINED;
return JS_DupValue(ctx, m->export_entries[idx].u.local.var_ref->value);
}

/* Note: 'func_obj' is not necessarily a constructor */
static void JS_SetConstructor2(JSContext *ctx,
JSValue func_obj,
Expand Down
6 changes: 1 addition & 5 deletions quickjs.h
Original file line number Diff line number Diff line change
Expand Up @@ -792,6 +792,7 @@ JS_EXTERN void JS_SetModuleLoaderFunc(JSRuntime *rt,
/* return the import.meta object of a module */
JS_EXTERN JSValue JS_GetImportMeta(JSContext *ctx, JSModuleDef *m);
JS_EXTERN JSAtom JS_GetModuleName(JSContext *ctx, JSModuleDef *m);
JSValue JS_GetModuleNamespace(JSContext *ctx, JSModuleDef *m);

/* JS Job support */

Expand Down Expand Up @@ -966,11 +967,6 @@ JS_EXTERN int JS_SetModuleExport(JSContext *ctx, JSModuleDef *m, const char *exp
JSValue val);
JS_EXTERN int JS_SetModuleExportList(JSContext *ctx, JSModuleDef *m,
const JSCFunctionListEntry *tab, int len);
/* can only be called after the module is initialized */
JS_EXTERN JSValue JS_GetModuleExport(JSContext *ctx, const JSModuleDef *m, const char *export_name);
JS_EXTERN int JS_CountModuleExport(JSContext *ctx, const JSModuleDef *m);
JS_EXTERN JSAtom JS_GetModuleExportName(JSContext *ctx, const JSModuleDef *m, int idx);
JS_EXTERN JSValue JS_GetModuleExportValue(JSContext *ctx, const JSModuleDef *m, int idx);

/* Promise */

Expand Down

0 comments on commit 7ded62c

Please sign in to comment.