Skip to content

Commit cc11a82

Browse files
authored
Prefix stdlib modules with "qjs:"
Fixes: #616
1 parent 3339ef7 commit cc11a82

17 files changed

+2949
-2932
lines changed

docs/docs/stdlib.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ Returns `quickjs-ng/<version>`.
3333

3434
Shorthand for `std.gc()`.
3535

36-
## `bjson` module
36+
## `qjs:bjson` module
3737

3838
### `bjson.write(obj, [flags])`
3939

@@ -58,7 +58,7 @@ Supported flags:
5858
- `READ_OBJ_REFERENCE`: allow de-serializing object references
5959
- `READ_OBJ_SAB`: allow de-serializing SharedArrayBuffer instances
6060

61-
## `os` module
61+
## `qjs:os` module
6262

6363
The `os` module provides Operating System specific functions:
6464

@@ -323,7 +323,7 @@ The worker instances have the following properties:
323323
received message. The thread is not terminated if there is at least
324324
one non `null` `onmessage` handler.
325325

326-
## `std` module
326+
## `qjs:std` module
327327

328328
The `std` module provides wrappers to libc (`stdlib.h` and `stdio.h`) and a few other utilities.
329329

examples/test_fib.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* example of JS module importing a C module */
2-
import * as os from "os";
2+
import * as os from "qjs:os";
33

44
const isWin = os.platform === 'win32';
55
const { fib } = await import(`./fib.${isWin ? 'dll' : 'so'}`);

examples/test_point.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* example of JS module importing a C module */
2-
import * as os from "os";
2+
import * as os from "qjs:os";
33

44
const isWin = os.platform === 'win32';
55
const { Point } = await import(`./point.${isWin ? 'dll' : 'so'}`);

gen/repl.c

+2,894-2,891
Large diffs are not rendered by default.

gen/test_fib.c

+17-16
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22

33
#include "quickjs-libc.h"
44

5-
const uint32_t qjsc_test_fib_size = 306;
5+
const uint32_t qjsc_test_fib_size = 314;
66

7-
const uint8_t qjsc_test_fib[306] = {
8-
0x10, 0x0d, 0x01, 0x28, 0x65, 0x78, 0x61, 0x6d,
7+
const uint8_t qjsc_test_fib[314] = {
8+
0x10, 0x0e, 0x01, 0x28, 0x65, 0x78, 0x61, 0x6d,
99
0x70, 0x6c, 0x65, 0x73, 0x2f, 0x74, 0x65, 0x73,
1010
0x74, 0x5f, 0x66, 0x69, 0x62, 0x2e, 0x6a, 0x73,
11+
0x01, 0x0c, 0x71, 0x6a, 0x73, 0x3a, 0x6f, 0x73,
1112
0x01, 0x04, 0x6f, 0x73, 0x01, 0x0a, 0x69, 0x73,
1213
0x57, 0x69, 0x6e, 0x01, 0x06, 0x66, 0x69, 0x62,
1314
0x01, 0x10, 0x70, 0x6c, 0x61, 0x74, 0x66, 0x6f,
@@ -22,21 +23,21 @@ const uint8_t qjsc_test_fib[306] = {
2223
0xba, 0x03, 0x01, 0xbc, 0x03, 0x00, 0x00, 0x01,
2324
0x00, 0xfc, 0x01, 0x00, 0x01, 0x0c, 0x20, 0xfa,
2425
0x01, 0xa2, 0x01, 0x00, 0x00, 0x00, 0x05, 0x03,
25-
0x00, 0x73, 0x00, 0xbc, 0x03, 0x00, 0x0d, 0xbe,
26-
0x03, 0x00, 0x0d, 0xc0, 0x03, 0x01, 0x0d, 0x08,
27-
0xea, 0x02, 0x29, 0x65, 0x00, 0x00, 0x41, 0xe1,
28-
0x00, 0x00, 0x00, 0x04, 0xe2, 0x00, 0x00, 0x00,
26+
0x00, 0x73, 0x00, 0xbe, 0x03, 0x00, 0x0d, 0xc0,
27+
0x03, 0x00, 0x0d, 0xc2, 0x03, 0x01, 0x0d, 0x08,
28+
0xea, 0x02, 0x29, 0x65, 0x00, 0x00, 0x41, 0xe2,
29+
0x00, 0x00, 0x00, 0x04, 0xe3, 0x00, 0x00, 0x00,
2930
0xae, 0xe2, 0x06, 0x11, 0xf2, 0xeb, 0x0b, 0x70,
30-
0x42, 0xe0, 0x00, 0x00, 0x00, 0xe3, 0x0e, 0xec,
31-
0x24, 0x0e, 0x04, 0xe3, 0x00, 0x00, 0x00, 0x42,
31+
0x42, 0xe1, 0x00, 0x00, 0x00, 0xe3, 0x0e, 0xec,
32+
0x24, 0x0e, 0x04, 0xe4, 0x00, 0x00, 0x00, 0x42,
3233
0x5d, 0x00, 0x00, 0x00, 0x65, 0x01, 0x00, 0xea,
33-
0x08, 0x04, 0xe4, 0x00, 0x00, 0x00, 0xec, 0x06,
34-
0x04, 0xe5, 0x00, 0x00, 0x00, 0x24, 0x01, 0x00,
35-
0x35, 0x8b, 0xec, 0xd4, 0x38, 0xe6, 0x00, 0x00,
36-
0x00, 0x42, 0xe7, 0x00, 0x00, 0x00, 0x04, 0xe8,
34+
0x08, 0x04, 0xe5, 0x00, 0x00, 0x00, 0xec, 0x06,
35+
0x04, 0xe6, 0x00, 0x00, 0x00, 0x24, 0x01, 0x00,
36+
0x35, 0x8b, 0xec, 0xd4, 0x38, 0xe7, 0x00, 0x00,
37+
0x00, 0x42, 0xe8, 0x00, 0x00, 0x00, 0x04, 0xe9,
3738
0x00, 0x00, 0x00, 0x24, 0x01, 0x00, 0x0e, 0x38,
38-
0xe6, 0x00, 0x00, 0x00, 0x42, 0xe7, 0x00, 0x00,
39-
0x00, 0x04, 0xe9, 0x00, 0x00, 0x00, 0x65, 0x02,
39+
0xe7, 0x00, 0x00, 0x00, 0x42, 0xe8, 0x00, 0x00,
40+
0x00, 0x04, 0xea, 0x00, 0x00, 0x00, 0x65, 0x02,
4041
0x00, 0xbc, 0x0a, 0xef, 0x24, 0x02, 0x00, 0x0e,
4142
0x06, 0x2e, 0xba, 0x03, 0x01, 0x01, 0x22, 0x01,
4243
0x01, 0x00, 0x04, 0x08, 0x1e, 0x2a, 0x18, 0x1b,
@@ -53,7 +54,7 @@ static JSContext *JS_NewCustomContext(JSRuntime *rt)
5354
return NULL;
5455
{
5556
extern JSModuleDef *js_init_module_os(JSContext *ctx, const char *name);
56-
js_init_module_os(ctx, "os");
57+
js_init_module_os(ctx, "qjs:os");
5758
}
5859
return ctx;
5960
}

qjs.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -164,9 +164,9 @@ static JSContext *JS_NewCustomContext(JSRuntime *rt)
164164
if (!ctx)
165165
return NULL;
166166
/* system modules */
167-
js_init_module_std(ctx, "std");
168-
js_init_module_os(ctx, "os");
169-
js_init_module_bjson(ctx, "bjson");
167+
js_init_module_std(ctx, "qjs:std");
168+
js_init_module_os(ctx, "qjs:os");
169+
js_init_module_bjson(ctx, "qjs:bjson");
170170

171171
JSValue global = JS_GetGlobalObject(ctx);
172172
JS_SetPropertyFunctionList(ctx, global, global_obj, countof(global_obj));

qjsc.c

+4
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,11 @@ int main(int argc, char **argv)
375375
stack_size = 0;
376376
memset(&dynamic_module_list, 0, sizeof(dynamic_module_list));
377377

378+
378379
/* add system modules */
380+
namelist_add(&cmodule_list, "qjs:std", "std", 0);
381+
namelist_add(&cmodule_list, "qjs:os", "os", 0);
382+
namelist_add(&cmodule_list, "qjs:bjson", "bjson", 0);
379383
namelist_add(&cmodule_list, "std", "std", 0);
380384
namelist_add(&cmodule_list, "os", "os", 0);
381385
namelist_add(&cmodule_list, "bjson", "bjson", 0);

quickjs.c

+10
Original file line numberDiff line numberDiff line change
@@ -26267,6 +26267,16 @@ static char *js_default_module_normalize_name(JSContext *ctx,
2626726267
int len;
2626826268

2626926269
if (name[0] != '.') {
26270+
/* Backwards compatibility for stdlib rename. */
26271+
static const char names[] = "qjs:bjson\0qjs:std\0qjs:os";
26272+
for (const char *p = names; p < endof(names); p += 1 + strlen(p)) {
26273+
if (!strcmp(name, p+4)) {
26274+
#ifndef NDEBUG
26275+
printf("WARN: Standard library modules should be prefixed with `qjs:`. Example: %s\n", p);
26276+
#endif
26277+
return js_strdup(ctx, p);
26278+
}
26279+
}
2627026280
/* if no initial dot, the module name is not modified */
2627126281
return js_strdup(ctx, name);
2627226282
}

repl.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@
2222
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2323
* THE SOFTWARE.
2424
*/
25-
import * as std from "std";
26-
import * as os from "os";
27-
import * as bjson from "bjson";
25+
import * as std from "qjs:std";
26+
import * as os from "qjs:os";
27+
import * as bjson from "qjs:bjson";
2828

2929
(function(g) {
3030
/* add 'bjson', 'os' and 'std' bindings */

run-test262.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -1695,9 +1695,9 @@ JSContext *JS_NewCustomContext(JSRuntime *rt)
16951695

16961696
ctx = JS_NewContext(rt);
16971697
if (ctx && local) {
1698-
js_init_module_std(ctx, "std");
1699-
js_init_module_os(ctx, "os");
1700-
js_init_module_bjson(ctx, "bjson");
1698+
js_init_module_std(ctx, "qjs:std");
1699+
js_init_module_os(ctx, "qjs:os");
1700+
js_init_module_bjson(ctx, "qjs:bjson");
17011701
}
17021702
return ctx;
17031703
}

tests/detect_module/4.js

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

tests/microbench.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2323
* THE SOFTWARE.
2424
*/
25-
import * as std from "std";
26-
import * as os from "os";
25+
import * as std from "qjs:std";
26+
import * as os from "qjs:os";
2727

2828
function pad(str, n) {
2929
str += "";

tests/test_bjson.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import * as std from "std";
2-
import * as bjson from "bjson";
1+
import * as std from "qjs:std";
2+
import * as bjson from "qjs:bjson";
33
import { assert } from "./assert.js";
44

55
function base64decode(s) {

tests/test_builtin.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import * as os from "os";
1+
import * as os from "qjs:os";
22
import { assert, assertThrows } from "./assert.js";
33

44
// Keep this at the top; it tests source positions.

tests/test_std.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import * as std from "std";
2-
import * as os from "os";
1+
import * as std from "qjs:std";
2+
import * as os from "qjs:os";
33
import { assert } from "./assert.js";
44

55
const isWin = os.platform === 'win32';

tests/test_worker.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import * as os from "os";
1+
import * as os from "qjs:os";
22
import { assert } from "./assert.js";
33

44
var worker;

tests/test_worker_module.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
/* Worker code for test_worker.js */
2-
import * as std from "std";
3-
import * as os from "os";
2+
import * as os from "qjs:os";
43

54
var parent = os.Worker.parent;
65

0 commit comments

Comments
 (0)