Dynamic import of statically linked .so results in Reference error

Open
#423 1 comment 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 5 days

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
38/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
c
Domain
compilers

Research direction

Reproduce the two scenarios using foo.c, app.js, qjsc, and the generated app.c, then compare the initialization code produced for static and dynamic imports. Start by tracing qjsc's -M handling and verify that the dynamic import path can initialize foo.so; done means the dynamic import no longer raises ReferenceError without manually editing app.c.

Written by the indexing model from the issue text.

Description

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.

Dominant language
C
Stars
11.1k
Forks
1.2k
PR merge metrics
No merged PRs in 30d

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from bellard/quickjs

All issues in bellard/quickjs

Similar issues

More C issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.