Prefix stdlib modules with "qjs:"

Fixes: https://github.com/quickjs-ng/quickjs/issues/616
This commit is contained in:
Saúl Ibarra Corretgé 2024-10-24 22:24:03 +02:00 committed by GitHub
parent 3339ef7137
commit cc11a829e8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
17 changed files with 38 additions and 25 deletions

View file

@ -33,7 +33,7 @@ Returns `quickjs-ng/<version>`.
Shorthand for `std.gc()`.
## `bjson` module
## `qjs:bjson` module
### `bjson.write(obj, [flags])`
@ -58,7 +58,7 @@ Supported flags:
- `READ_OBJ_REFERENCE`: allow de-serializing object references
- `READ_OBJ_SAB`: allow de-serializing SharedArrayBuffer instances
## `os` module
## `qjs:os` module
The `os` module provides Operating System specific functions:
@ -323,7 +323,7 @@ The worker instances have the following properties:
received message. The thread is not terminated if there is at least
one non `null` `onmessage` handler.
## `std` module
## `qjs:std` module
The `std` module provides wrappers to libc (`stdlib.h` and `stdio.h`) and a few other utilities.

View file

@ -1,5 +1,5 @@
/* example of JS module importing a C module */
import * as os from "os";
import * as os from "qjs:os";
const isWin = os.platform === 'win32';
const { fib } = await import(`./fib.${isWin ? 'dll' : 'so'}`);

View file

@ -1,5 +1,5 @@
/* example of JS module importing a C module */
import * as os from "os";
import * as os from "qjs:os";
const isWin = os.platform === 'win32';
const { Point } = await import(`./point.${isWin ? 'dll' : 'so'}`);

Binary file not shown.

Binary file not shown.

6
qjs.c
View file

@ -164,9 +164,9 @@ static JSContext *JS_NewCustomContext(JSRuntime *rt)
if (!ctx)
return NULL;
/* system modules */
js_init_module_std(ctx, "std");
js_init_module_os(ctx, "os");
js_init_module_bjson(ctx, "bjson");
js_init_module_std(ctx, "qjs:std");
js_init_module_os(ctx, "qjs:os");
js_init_module_bjson(ctx, "qjs:bjson");
JSValue global = JS_GetGlobalObject(ctx);
JS_SetPropertyFunctionList(ctx, global, global_obj, countof(global_obj));

4
qjsc.c
View file

@ -375,7 +375,11 @@ int main(int argc, char **argv)
stack_size = 0;
memset(&dynamic_module_list, 0, sizeof(dynamic_module_list));
/* add system modules */
namelist_add(&cmodule_list, "qjs:std", "std", 0);
namelist_add(&cmodule_list, "qjs:os", "os", 0);
namelist_add(&cmodule_list, "qjs:bjson", "bjson", 0);
namelist_add(&cmodule_list, "std", "std", 0);
namelist_add(&cmodule_list, "os", "os", 0);
namelist_add(&cmodule_list, "bjson", "bjson", 0);

View file

@ -26267,6 +26267,16 @@ static char *js_default_module_normalize_name(JSContext *ctx,
int len;
if (name[0] != '.') {
/* Backwards compatibility for stdlib rename. */
static const char names[] = "qjs:bjson\0qjs:std\0qjs:os";
for (const char *p = names; p < endof(names); p += 1 + strlen(p)) {
if (!strcmp(name, p+4)) {
#ifndef NDEBUG
printf("WARN: Standard library modules should be prefixed with `qjs:`. Example: %s\n", p);
#endif
return js_strdup(ctx, p);
}
}
/* if no initial dot, the module name is not modified */
return js_strdup(ctx, name);
}

View file

@ -22,9 +22,9 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
import * as std from "std";
import * as os from "os";
import * as bjson from "bjson";
import * as std from "qjs:std";
import * as os from "qjs:os";
import * as bjson from "qjs:bjson";
(function(g) {
/* add 'bjson', 'os' and 'std' bindings */

View file

@ -1695,9 +1695,9 @@ JSContext *JS_NewCustomContext(JSRuntime *rt)
ctx = JS_NewContext(rt);
if (ctx && local) {
js_init_module_std(ctx, "std");
js_init_module_os(ctx, "os");
js_init_module_bjson(ctx, "bjson");
js_init_module_std(ctx, "qjs:std");
js_init_module_os(ctx, "qjs:os");
js_init_module_bjson(ctx, "qjs:bjson");
}
return ctx;
}

View file

@ -1,3 +1,3 @@
// imports should classify it as a module, even when not at the top
os.now()
import * as os from "os"
import * as os from "qjs:os"

View file

@ -22,8 +22,8 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
import * as std from "std";
import * as os from "os";
import * as std from "qjs:std";
import * as os from "qjs:os";
function pad(str, n) {
str += "";

View file

@ -1,5 +1,5 @@
import * as std from "std";
import * as bjson from "bjson";
import * as std from "qjs:std";
import * as bjson from "qjs:bjson";
import { assert } from "./assert.js";
function base64decode(s) {

View file

@ -1,4 +1,4 @@
import * as os from "os";
import * as os from "qjs:os";
import { assert, assertThrows } from "./assert.js";
// Keep this at the top; it tests source positions.

View file

@ -1,5 +1,5 @@
import * as std from "std";
import * as os from "os";
import * as std from "qjs:std";
import * as os from "qjs:os";
import { assert } from "./assert.js";
const isWin = os.platform === 'win32';

View file

@ -1,4 +1,4 @@
import * as os from "os";
import * as os from "qjs:os";
import { assert } from "./assert.js";
var worker;

View file

@ -1,6 +1,5 @@
/* Worker code for test_worker.js */
import * as std from "std";
import * as os from "os";
import * as os from "qjs:os";
var parent = os.Worker.parent;