Skip to content

Commit

Permalink
Split JS_SetModuleLoaderFunc into three separate functions, one for e…
Browse files Browse the repository at this point in the history
…ach of its args
  • Loading branch information
suchipi committed Sep 3, 2024
1 parent 65ffc3c commit be61a31
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 26 deletions.
4 changes: 3 additions & 1 deletion src/qjsc/qjsc.c
Original file line number Diff line number Diff line change
Expand Up @@ -675,7 +675,9 @@ int main(int argc, char **argv)
#endif

/* loader for ES6 modules */
JS_SetModuleLoaderFunc(rt, NULL, jsc_module_loader, NULL);
JS_SetModuleNormalizeFunc(rt, NULL);
JS_SetModuleLoaderFunc(rt, jsc_module_loader);
JS_SetModuleLoaderOpaque(rt, NULL);

debugprint("writing file header comment and include...\n");

Expand Down
6 changes: 3 additions & 3 deletions src/quickjs-modulesys/quickjs-modulesys.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ void QJMS_InitState(JSRuntime *rt)
state = js_malloc_rt(rt, sizeof(state));
state->main_module = main_module;

JS_SetModuleLoaderFunc(rt, (JSModuleNormalizeFunc *) QJMS_NormalizeModuleName,
(JSModuleLoaderFunc *) QJMS_ModuleLoader,
(void *) state);
JS_SetModuleNormalizeFunc(rt, (JSModuleNormalizeFunc *) QJMS_NormalizeModuleName);
JS_SetModuleLoaderFunc(rt, (JSModuleLoaderFunc *) QJMS_ModuleLoader);
JS_SetModuleLoaderOpaque(rt, (void *) state);
}

void QJMS_FreeState(JSRuntime *rt)
Expand Down
30 changes: 19 additions & 11 deletions src/quickjs/quickjs.c
Original file line number Diff line number Diff line change
Expand Up @@ -27070,40 +27070,48 @@ int JS_SetModuleExport(JSContext *ctx, JSModuleDef *m, const char *export_name,
}

void JS_SetModuleLoaderFunc(JSRuntime *rt,
JSModuleNormalizeFunc *module_normalize,
JSModuleLoaderFunc *module_loader, void *opaque)
JSModuleLoaderFunc *module_loader)
{
rt->module_normalize_func = module_normalize;
rt->module_loader_func = module_loader;
rt->module_loader_opaque = opaque;
}

void JS_SetContextOpaqueValue(JSContext *ctx, JSValue value)
JSModuleLoaderFunc *JS_GetModuleLoaderFunc(JSRuntime *rt)
{
ctx->user_opaque_val = value;
return rt->module_loader_func;
}

/* NOTE: you must free it! */
JSValue JS_GetContextOpaqueValue(JSContext *ctx)
void JS_SetModuleNormalizeFunc(JSRuntime *rt,
JSModuleNormalizeFunc *module_normalize)
{
return JS_DupValue(ctx, ctx->user_opaque_val);
rt->module_normalize_func = module_normalize;
}

JSModuleNormalizeFunc *JS_GetModuleNormalizeFunc(JSRuntime *rt)
{
return rt->module_normalize_func;
}

JSModuleLoaderFunc *JS_GetModuleLoaderFunc(JSRuntime *rt)
void JS_SetModuleLoaderOpaque(JSRuntime *rt, void *opaque)
{
return rt->module_loader_func;
rt->module_loader_opaque = opaque;
}

void *JS_GetModuleLoaderOpaque(JSRuntime *rt)
{
return rt->module_loader_opaque;
}

void JS_SetContextOpaqueValue(JSContext *ctx, JSValue value)
{
ctx->user_opaque_val = value;
}

/* NOTE: you must free it! */
JSValue JS_GetContextOpaqueValue(JSContext *ctx)
{
return JS_DupValue(ctx, ctx->user_opaque_val);
}

/* default module filename normalizer */
static char *js_default_module_normalize_name(JSContext *ctx,
const char *base_name,
Expand Down
20 changes: 11 additions & 9 deletions src/quickjs/quickjs.h
Original file line number Diff line number Diff line change
Expand Up @@ -977,18 +977,20 @@ typedef char *JSModuleNormalizeFunc(JSContext *ctx,
typedef JSModuleDef *JSModuleLoaderFunc(JSContext *ctx,
const char *module_name, void *opaque);

/* module_normalize = NULL is allowed and invokes the default module
filename normalizer */
void JS_SetModuleLoaderFunc(JSRuntime *rt,
JSModuleNormalizeFunc *module_normalize,
JSModuleLoaderFunc *module_loader, void *opaque);

JSModuleLoaderFunc *module_loader);
/* return the value set by JS_SetModuleLoaderFunc. could be NULL. */
JSModuleNormalizeFunc *JS_GetModuleNormalizeFunc(JSRuntime *rt);
/* return the value set by JS_SetModuleLoaderFunc. could be NULL but only if
JS_SetModuleLoaderFunc was never called. */
JSModuleLoaderFunc *JS_GetModuleLoaderFunc(JSRuntime *rt);
/* return the value set by JS_SetModuleLoaderFunc. could be NULL. */

/* module_normalize = NULL is allowed and invokes the default module
filename normalizer */
void JS_SetModuleNormalizeFunc(JSRuntime *rt,
JSModuleNormalizeFunc *module_normalize);
/* return the value set by JS_SetModuleNormalizeFunc. could be NULL. */
JSModuleNormalizeFunc *JS_GetModuleNormalizeFunc(JSRuntime *rt);

void JS_SetModuleLoaderOpaque(JSRuntime *rt, void *opaque);
/* return the value set by JS_SetModuleLoaderOpaque. could be NULL. */
void *JS_GetModuleLoaderOpaque(JSRuntime *rt);

void JS_SetContextOpaqueValue(JSContext *ctx, JSValue value);
Expand Down
8 changes: 6 additions & 2 deletions src/run-test262/run-test262.c
Original file line number Diff line number Diff line change
Expand Up @@ -1522,7 +1522,9 @@ int run_test_buf(const char *filename, char *harness, namelist_t *ip,
JS_SetCanBlock(rt, can_block);

/* loader for ES6 modules */
JS_SetModuleLoaderFunc(rt, NULL, js_module_loader_test, NULL);
JS_SetModuleNormalizeFunc(rt, NULL);
JS_SetModuleLoaderFunc(rt, js_module_loader_test);
JS_SetModuleLoaderOpaque(rt, NULL);

add_helpers(ctx);

Expand Down Expand Up @@ -1819,7 +1821,9 @@ int run_test262_harness_test(const char *filename, BOOL is_module)
JS_SetCanBlock(rt, can_block);

/* loader for ES6 modules */
JS_SetModuleLoaderFunc(rt, NULL, js_module_loader_test, NULL);
JS_SetModuleNormalizeFunc(rt, NULL);
JS_SetModuleLoaderFunc(rt, js_module_loader_test);
JS_SetModuleLoaderOpaque(rt, NULL);

add_helpers(ctx);

Expand Down

0 comments on commit be61a31

Please sign in to comment.