feat: Added functions to get access to module exports
This commit is contained in:
parent
e995085d0c
commit
6868fb9e25
2 changed files with 43 additions and 7 deletions
33
quickjs.c
33
quickjs.c
|
@ -25275,7 +25275,7 @@ static int add_req_module_entry(JSContext *ctx, JSModuleDef *m,
|
|||
return i;
|
||||
}
|
||||
|
||||
static JSExportEntry *find_export_entry(JSContext *ctx, JSModuleDef *m,
|
||||
static JSExportEntry *find_export_entry(JSContext *ctx, const JSModuleDef *m,
|
||||
JSAtom export_name)
|
||||
{
|
||||
JSExportEntry *me;
|
||||
|
@ -34394,6 +34394,37 @@ 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,
|
||||
|
|
|
@ -966,6 +966,11 @@ 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 */
|
||||
|
||||
|
|
Loading…
Reference in a new issue