Skip to content

Commit

Permalink
Prefix stdlib modules with "qjs:"
Browse files Browse the repository at this point in the history
Fixes: #616
  • Loading branch information
saghul authored Oct 24, 2024
1 parent 3339ef7 commit cc11a82
Show file tree
Hide file tree
Showing 17 changed files with 2,949 additions and 2,932 deletions.
6 changes: 3 additions & 3 deletions docs/docs/stdlib.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Returns `quickjs-ng/<version>`.

Shorthand for `std.gc()`.

## `bjson` module
## `qjs:bjson` module

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

Expand All @@ -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:

Expand Down Expand Up @@ -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.

Expand Down
2 changes: 1 addition & 1 deletion examples/test_fib.js
Original file line number Diff line number Diff line change
@@ -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'}`);
Expand Down
2 changes: 1 addition & 1 deletion examples/test_point.js
Original file line number Diff line number Diff line change
@@ -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'}`);
Expand Down
5,785 changes: 2,894 additions & 2,891 deletions gen/repl.c

Large diffs are not rendered by default.

33 changes: 17 additions & 16 deletions gen/test_fib.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

#include "quickjs-libc.h"

const uint32_t qjsc_test_fib_size = 306;
const uint32_t qjsc_test_fib_size = 314;

const uint8_t qjsc_test_fib[306] = {
0x10, 0x0d, 0x01, 0x28, 0x65, 0x78, 0x61, 0x6d,
const uint8_t qjsc_test_fib[314] = {
0x10, 0x0e, 0x01, 0x28, 0x65, 0x78, 0x61, 0x6d,
0x70, 0x6c, 0x65, 0x73, 0x2f, 0x74, 0x65, 0x73,
0x74, 0x5f, 0x66, 0x69, 0x62, 0x2e, 0x6a, 0x73,
0x01, 0x0c, 0x71, 0x6a, 0x73, 0x3a, 0x6f, 0x73,
0x01, 0x04, 0x6f, 0x73, 0x01, 0x0a, 0x69, 0x73,
0x57, 0x69, 0x6e, 0x01, 0x06, 0x66, 0x69, 0x62,
0x01, 0x10, 0x70, 0x6c, 0x61, 0x74, 0x66, 0x6f,
Expand All @@ -22,21 +23,21 @@ const uint8_t qjsc_test_fib[306] = {
0xba, 0x03, 0x01, 0xbc, 0x03, 0x00, 0x00, 0x01,
0x00, 0xfc, 0x01, 0x00, 0x01, 0x0c, 0x20, 0xfa,
0x01, 0xa2, 0x01, 0x00, 0x00, 0x00, 0x05, 0x03,
0x00, 0x73, 0x00, 0xbc, 0x03, 0x00, 0x0d, 0xbe,
0x03, 0x00, 0x0d, 0xc0, 0x03, 0x01, 0x0d, 0x08,
0xea, 0x02, 0x29, 0x65, 0x00, 0x00, 0x41, 0xe1,
0x00, 0x00, 0x00, 0x04, 0xe2, 0x00, 0x00, 0x00,
0x00, 0x73, 0x00, 0xbe, 0x03, 0x00, 0x0d, 0xc0,
0x03, 0x00, 0x0d, 0xc2, 0x03, 0x01, 0x0d, 0x08,
0xea, 0x02, 0x29, 0x65, 0x00, 0x00, 0x41, 0xe2,
0x00, 0x00, 0x00, 0x04, 0xe3, 0x00, 0x00, 0x00,
0xae, 0xe2, 0x06, 0x11, 0xf2, 0xeb, 0x0b, 0x70,
0x42, 0xe0, 0x00, 0x00, 0x00, 0xe3, 0x0e, 0xec,
0x24, 0x0e, 0x04, 0xe3, 0x00, 0x00, 0x00, 0x42,
0x42, 0xe1, 0x00, 0x00, 0x00, 0xe3, 0x0e, 0xec,
0x24, 0x0e, 0x04, 0xe4, 0x00, 0x00, 0x00, 0x42,
0x5d, 0x00, 0x00, 0x00, 0x65, 0x01, 0x00, 0xea,
0x08, 0x04, 0xe4, 0x00, 0x00, 0x00, 0xec, 0x06,
0x04, 0xe5, 0x00, 0x00, 0x00, 0x24, 0x01, 0x00,
0x35, 0x8b, 0xec, 0xd4, 0x38, 0xe6, 0x00, 0x00,
0x00, 0x42, 0xe7, 0x00, 0x00, 0x00, 0x04, 0xe8,
0x08, 0x04, 0xe5, 0x00, 0x00, 0x00, 0xec, 0x06,
0x04, 0xe6, 0x00, 0x00, 0x00, 0x24, 0x01, 0x00,
0x35, 0x8b, 0xec, 0xd4, 0x38, 0xe7, 0x00, 0x00,
0x00, 0x42, 0xe8, 0x00, 0x00, 0x00, 0x04, 0xe9,
0x00, 0x00, 0x00, 0x24, 0x01, 0x00, 0x0e, 0x38,
0xe6, 0x00, 0x00, 0x00, 0x42, 0xe7, 0x00, 0x00,
0x00, 0x04, 0xe9, 0x00, 0x00, 0x00, 0x65, 0x02,
0xe7, 0x00, 0x00, 0x00, 0x42, 0xe8, 0x00, 0x00,
0x00, 0x04, 0xea, 0x00, 0x00, 0x00, 0x65, 0x02,
0x00, 0xbc, 0x0a, 0xef, 0x24, 0x02, 0x00, 0x0e,
0x06, 0x2e, 0xba, 0x03, 0x01, 0x01, 0x22, 0x01,
0x01, 0x00, 0x04, 0x08, 0x1e, 0x2a, 0x18, 0x1b,
Expand All @@ -53,7 +54,7 @@ static JSContext *JS_NewCustomContext(JSRuntime *rt)
return NULL;
{
extern JSModuleDef *js_init_module_os(JSContext *ctx, const char *name);
js_init_module_os(ctx, "os");
js_init_module_os(ctx, "qjs:os");
}
return ctx;
}
Expand Down
6 changes: 3 additions & 3 deletions qjs.c
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
4 changes: 4 additions & 0 deletions qjsc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
10 changes: 10 additions & 0 deletions quickjs.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
6 changes: 3 additions & 3 deletions repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
6 changes: 3 additions & 3 deletions run-test262.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/detect_module/4.js
Original file line number Diff line number Diff line change
@@ -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"
4 changes: 2 additions & 2 deletions tests/microbench.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 += "";
Expand Down
4 changes: 2 additions & 2 deletions tests/test_bjson.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion tests/test_builtin.js
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
4 changes: 2 additions & 2 deletions tests/test_std.js
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
2 changes: 1 addition & 1 deletion tests/test_worker.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as os from "os";
import * as os from "qjs:os";
import { assert } from "./assert.js";

var worker;
Expand Down
3 changes: 1 addition & 2 deletions tests/test_worker_module.js
Original file line number Diff line number Diff line change
@@ -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;

Expand Down

0 comments on commit cc11a82

Please sign in to comment.