quickjs icon indicating copy to clipboard operation
quickjs copied to clipboard

Dynamic import of statically linked .so results in Reference error

Open boblund opened this issue 8 months ago • 1 comments

Setup: foo.c is c module named foo.

Scenario 1: foo.c is statically linked with app.mjs to create ./app:

qjsc  -e -M foo.so,foo -m -o app.c app.js
gcc -I/opt/homebrew/Cellar/quickjs/2025-04-26/include/quickjs -c app.c -o app.o
gcc -I/opt/homebrew/Cellar/quickjs/2025-04-26/include/quickjs -c foo.c -o foo.o
gcc -L/opt/homebrew/Cellar/quickjs/2025-04-26/lib/quickjs -lquickjs - -o app app.o foo.o

app.js does: import { add } from './foo.so; This works as expected

Scenario 2: app.js does: const { add } = await import( './foo.so' ) there is a reference error.

This is because the required initialization code:

{
    extern JSModuleDef *js_init_module_foo(JSContext *ctx, const char *name);
    js_init_module_foo(ctx, "foo.so");
 }

gets added to app.c in scenario 1 but not scenario 2.

If the init code is manaully added to app.c in scenario 2 it works fine. It seems like qjsc -M provides enough information to create the init code in either case.

boblund avatar Jul 02 '25 22:07 boblund

qjsc -M works only when C modules are statically imported. I agree it would make sense to allow dynamic imports as well.

bellard avatar Jul 09 '25 12:07 bellard