Expand description
C modules: JS_NewCModule and friends, and the dlopen that finds one.
§What a C module is
A .so built from point.c leaves every JS_* symbol undefined and exports exactly one
of its own:
JSModuleDef *js_init_module(JSContext *ctx, const char *module_name)
{
JSModuleDef *m = JS_NewCModule(ctx, module_name, js_point_init);
if (!m) return NULL;
JS_AddModuleExport(ctx, m, "Point");
return m;
}(JS_INIT_MODULE expands to that name when JS_SHARED_LIBRARY is defined.) The
declarations happen here; the init function — which calls JS_SetModuleExport with the
actual values — runs later, when the module is instantiated. Keeping that split matters:
point.c’s init sets an export the same call has just declared, and running init earlier
would fail on “no such export”.
§Why the loader lives here and not in quickjs-host
The entry point takes a JSContext * and calls back into the C API, so the thing that
calls it has to be able to produce one. That is this crate. quickjs-cli supplies the
path and the Interp, and gets the exports back as ordinary engine values.
§The dlopen handle is never closed
A module’s code, its static strings and its function pointers all live in the loaded
object. Unloading it while any of them is reachable — a Point.prototype method, a class
definition’s finalizer — would be a jump into unmapped memory. The reference has the same
choice to make and makes the same one: js_module_loader_so closes the handle only on a
failure path.
Functions§
- JS_
AddModule ⚠Export JS_AddModuleExport: declare a name this module will export.- JS_
AddModule ⚠Export List JS_AddModuleExportList: declare every name in a function-list table.- JS_
NewC ⚠Module JS_NewCModule: create a module and remember how to fill it in.- JS_
SetModule ⚠Export JS_SetModuleExport: give a declared export its value, consumingval.- JS_
SetModule ⚠Export List JS_SetModuleExportList: create each value from a function-list table and export it.- exports_
of ⚠ - The
JSModuleDefa native module’s entry point answered, for a caller that has it. - instantiate⚠
- Run a module’s init function and collect what it exported.
- load_
native_ module - Load a native module and hand back its exports.