Skip to content

Commit 6868fb9

Browse files
lbguilhermesaghul
authored andcommitted
feat: Added functions to get access to module exports
1 parent e995085 commit 6868fb9

File tree

2 files changed

+43
-7
lines changed

2 files changed

+43
-7
lines changed

quickjs.c

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5571,7 +5571,7 @@ static void mark_children(JSRuntime *rt, JSGCObjectHeader *gp,
55715571
for (i = 0; i < b->ic->count; i++) {
55725572
shapes = &b->ic->cache[i].shape;
55735573
for (shape = *shapes; shape != endof(*shapes); shape++)
5574-
if (*shape)
5574+
if (*shape)
55755575
mark_func(rt, &(*shape)->header);
55765576
}
55775577
}
@@ -7217,8 +7217,8 @@ JSValue JS_GetPropertyInternal(JSContext *ctx, JSValue obj,
72177217

72187218
static JSValue JS_GetPropertyInternalWithIC(JSContext *ctx, JSValue obj,
72197219
JSAtom prop, JSValue this_obj,
7220-
JSInlineCache *ic, int32_t offset,
7221-
BOOL throw_ref_error)
7220+
JSInlineCache *ic, int32_t offset,
7221+
BOOL throw_ref_error)
72227222
{
72237223
uint32_t tag;
72247224
JSObject *p;
@@ -7230,7 +7230,7 @@ static JSValue JS_GetPropertyInternalWithIC(JSContext *ctx, JSValue obj,
72307230
if (likely(offset >= 0))
72317231
return js_dup(p->prop[offset].u.value);
72327232
slow_path:
7233-
return JS_GetPropertyInternal2(ctx, obj, prop, this_obj, ic, throw_ref_error);
7233+
return JS_GetPropertyInternal2(ctx, obj, prop, this_obj, ic, throw_ref_error);
72347234
}
72357235

72367236
static JSValue JS_ThrowTypeErrorPrivateNotFound(JSContext *ctx, JSAtom atom)
@@ -15718,7 +15718,7 @@ static JSValue JS_CallInternal(JSContext *caller_ctx, JSValue func_obj,
1571815718
}
1571915719
BREAK;
1572015720

15721-
CASE(OP_get_field_ic):
15721+
CASE(OP_get_field_ic):
1572215722
{
1572315723
JSValue val;
1572415724
JSAtom atom;
@@ -15801,7 +15801,7 @@ static JSValue JS_CallInternal(JSContext *caller_ctx, JSValue func_obj,
1580115801
atom = get_ic_atom(ic, ic_offset);
1580215802
pc += 4;
1580315803
ret = JS_SetPropertyInternalWithIC(ctx, sp[-2], atom, sp[-1], JS_PROP_THROW_STRICT, ic, ic_offset);
15804-
ic->updated = FALSE;
15804+
ic->updated = FALSE;
1580515805
JS_FreeValue(ctx, sp[-2]);
1580615806
sp -= 2;
1580715807
if (unlikely(ret < 0))
@@ -25275,7 +25275,7 @@ static int add_req_module_entry(JSContext *ctx, JSModuleDef *m,
2527525275
return i;
2527625276
}
2527725277

25278-
static JSExportEntry *find_export_entry(JSContext *ctx, JSModuleDef *m,
25278+
static JSExportEntry *find_export_entry(JSContext *ctx, const JSModuleDef *m,
2527925279
JSAtom export_name)
2528025280
{
2528125281
JSExportEntry *me;
@@ -34394,6 +34394,37 @@ int JS_SetModuleExportList(JSContext *ctx, JSModuleDef *m,
3439434394
return 0;
3439534395
}
3439634396

34397+
JSValue JS_GetModuleExport(JSContext *ctx, const JSModuleDef *m, const char *export_name) {
34398+
JSExportEntry *me;
34399+
JSAtom name;
34400+
name = JS_NewAtom(ctx, export_name);
34401+
if (name == JS_ATOM_NULL)
34402+
goto fail;
34403+
me = find_export_entry(ctx, m, name);
34404+
JS_FreeAtom(ctx, name);
34405+
if (!me)
34406+
goto fail;
34407+
return JS_DupValue(ctx, me->u.local.var_ref->value);
34408+
fail:
34409+
return JS_UNDEFINED;
34410+
}
34411+
34412+
int JS_CountModuleExport(JSContext *ctx, const JSModuleDef *m) {
34413+
return m->export_entries_count;
34414+
}
34415+
34416+
JSAtom JS_GetModuleExportName(JSContext *ctx, const JSModuleDef *m, int idx) {
34417+
if (idx >= m->export_entries_count || idx < 0)
34418+
return JS_ATOM_NULL;
34419+
return JS_DupAtom(ctx, m->export_entries[idx].export_name);
34420+
}
34421+
34422+
JSValue JS_GetModuleExportValue(JSContext *ctx, const JSModuleDef *m, int idx) {
34423+
if (idx >= m->export_entries_count || idx < 0)
34424+
return JS_UNDEFINED;
34425+
return JS_DupValue(ctx, m->export_entries[idx].u.local.var_ref->value);
34426+
}
34427+
3439734428
/* Note: 'func_obj' is not necessarily a constructor */
3439834429
static void JS_SetConstructor2(JSContext *ctx,
3439934430
JSValue func_obj,

quickjs.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -966,6 +966,11 @@ JS_EXTERN int JS_SetModuleExport(JSContext *ctx, JSModuleDef *m, const char *exp
966966
JSValue val);
967967
JS_EXTERN int JS_SetModuleExportList(JSContext *ctx, JSModuleDef *m,
968968
const JSCFunctionListEntry *tab, int len);
969+
/* can only be called after the module is initialized */
970+
JS_EXTERN JSValue JS_GetModuleExport(JSContext *ctx, const JSModuleDef *m, const char *export_name);
971+
JS_EXTERN int JS_CountModuleExport(JSContext *ctx, const JSModuleDef *m);
972+
JS_EXTERN JSAtom JS_GetModuleExportName(JSContext *ctx, const JSModuleDef *m, int idx);
973+
JS_EXTERN JSValue JS_GetModuleExportValue(JSContext *ctx, const JSModuleDef *m, int idx);
969974

970975
/* Promise */
971976

0 commit comments

Comments
 (0)