@suchipi/quickjs 0.5.0 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (137) hide show
  1. package/README.md +67 -43
  2. package/build/aarch64-apple-darwin/bin/file-to-bytecode.js +7 -2
  3. package/build/aarch64-apple-darwin/bin/qjs +0 -0
  4. package/build/aarch64-apple-darwin/bin/qjsbootstrap +0 -0
  5. package/build/aarch64-apple-darwin/bin/qjsbootstrap-bytecode +0 -0
  6. package/build/aarch64-apple-darwin/bin/qjsc +0 -0
  7. package/build/aarch64-apple-darwin/bin/quickjs-run +0 -0
  8. package/build/aarch64-apple-darwin/extras/fib.so +0 -0
  9. package/build/aarch64-apple-darwin/extras/is-stdin-a-tty +0 -0
  10. package/build/aarch64-apple-darwin/extras/log-argv +0 -0
  11. package/build/aarch64-apple-darwin/extras/point.so +0 -0
  12. package/build/aarch64-apple-darwin/extras/run-test262 +0 -0
  13. package/build/aarch64-apple-darwin/extras/sample-program/sum +0 -0
  14. package/build/aarch64-apple-darwin/extras/stack-limit-test +0 -0
  15. package/build/aarch64-apple-darwin/lib/quickjs-core.a +0 -0
  16. package/build/aarch64-apple-darwin/lib/quickjs-full.a +0 -0
  17. package/build/aarch64-unknown-linux-gnu/bin/file-to-bytecode.js +7 -2
  18. package/build/aarch64-unknown-linux-gnu/bin/qjs +0 -0
  19. package/build/aarch64-unknown-linux-gnu/bin/qjsbootstrap +0 -0
  20. package/build/aarch64-unknown-linux-gnu/bin/qjsbootstrap-bytecode +0 -0
  21. package/build/aarch64-unknown-linux-gnu/bin/qjsc +0 -0
  22. package/build/aarch64-unknown-linux-gnu/bin/quickjs-run +0 -0
  23. package/build/aarch64-unknown-linux-gnu/extras/fib.so +0 -0
  24. package/build/aarch64-unknown-linux-gnu/extras/is-stdin-a-tty +0 -0
  25. package/build/aarch64-unknown-linux-gnu/extras/log-argv +0 -0
  26. package/build/aarch64-unknown-linux-gnu/extras/point.so +0 -0
  27. package/build/aarch64-unknown-linux-gnu/extras/run-test262 +0 -0
  28. package/build/aarch64-unknown-linux-gnu/extras/sample-program/sum +0 -0
  29. package/build/aarch64-unknown-linux-gnu/extras/stack-limit-test +0 -0
  30. package/build/aarch64-unknown-linux-gnu/lib/quickjs-core.a +0 -0
  31. package/build/aarch64-unknown-linux-gnu/lib/quickjs-full.a +0 -0
  32. package/build/aarch64-unknown-linux-musl/bin/file-to-bytecode.js +7 -2
  33. package/build/aarch64-unknown-linux-musl/bin/qjs +0 -0
  34. package/build/aarch64-unknown-linux-musl/bin/qjsbootstrap +0 -0
  35. package/build/aarch64-unknown-linux-musl/bin/qjsbootstrap-bytecode +0 -0
  36. package/build/aarch64-unknown-linux-musl/bin/qjsc +0 -0
  37. package/build/aarch64-unknown-linux-musl/bin/quickjs-run +0 -0
  38. package/build/aarch64-unknown-linux-musl/extras/fib.so +0 -0
  39. package/build/aarch64-unknown-linux-musl/extras/is-stdin-a-tty +0 -0
  40. package/build/aarch64-unknown-linux-musl/extras/point.so +0 -0
  41. package/build/aarch64-unknown-linux-musl/extras/run-test262 +0 -0
  42. package/build/aarch64-unknown-linux-musl/extras/sample-program/sum +0 -0
  43. package/build/aarch64-unknown-linux-musl/extras/stack-limit-test +0 -0
  44. package/build/aarch64-unknown-linux-musl/lib/quickjs-core.a +0 -0
  45. package/build/aarch64-unknown-linux-musl/lib/quickjs-full.a +0 -0
  46. package/build/aarch64-unknown-linux-static/bin/file-to-bytecode.js +7 -2
  47. package/build/aarch64-unknown-linux-static/bin/qjs +0 -0
  48. package/build/aarch64-unknown-linux-static/bin/qjsbootstrap +0 -0
  49. package/build/aarch64-unknown-linux-static/bin/qjsbootstrap-bytecode +0 -0
  50. package/build/aarch64-unknown-linux-static/bin/qjsc +0 -0
  51. package/build/aarch64-unknown-linux-static/bin/quickjs-run +0 -0
  52. package/build/aarch64-unknown-linux-static/extras/fib.so +0 -0
  53. package/build/aarch64-unknown-linux-static/extras/is-stdin-a-tty +0 -0
  54. package/build/aarch64-unknown-linux-static/extras/point.so +0 -0
  55. package/build/aarch64-unknown-linux-static/extras/run-test262 +0 -0
  56. package/build/aarch64-unknown-linux-static/extras/sample-program/sum +0 -0
  57. package/build/aarch64-unknown-linux-static/extras/stack-limit-test +0 -0
  58. package/build/aarch64-unknown-linux-static/lib/quickjs-core.a +0 -0
  59. package/build/aarch64-unknown-linux-static/lib/quickjs-full.a +0 -0
  60. package/build/dts/quickjs-libbytecode.d.ts +5 -1
  61. package/build/dts/quickjs-libc.d.ts +15 -4
  62. package/build/dts/quickjs-libcontext.d.ts +3 -3
  63. package/build/dts/{quickjs-libmodule.d.ts → quickjs-libengine.d.ts} +29 -1
  64. package/build/dts/quickjs-modulesys.d.ts +27 -43
  65. package/build/x86_64-apple-darwin/bin/file-to-bytecode.js +7 -2
  66. package/build/x86_64-apple-darwin/bin/qjs +0 -0
  67. package/build/x86_64-apple-darwin/bin/qjsbootstrap +0 -0
  68. package/build/x86_64-apple-darwin/bin/qjsbootstrap-bytecode +0 -0
  69. package/build/x86_64-apple-darwin/bin/qjsc +0 -0
  70. package/build/x86_64-apple-darwin/bin/quickjs-run +0 -0
  71. package/build/x86_64-apple-darwin/extras/fib.so +0 -0
  72. package/build/x86_64-apple-darwin/extras/is-stdin-a-tty +0 -0
  73. package/build/x86_64-apple-darwin/extras/log-argv +0 -0
  74. package/build/x86_64-apple-darwin/extras/point.so +0 -0
  75. package/build/x86_64-apple-darwin/extras/run-test262 +0 -0
  76. package/build/x86_64-apple-darwin/extras/sample-program/sum +0 -0
  77. package/build/x86_64-apple-darwin/extras/stack-limit-test +0 -0
  78. package/build/x86_64-apple-darwin/lib/quickjs-core.a +0 -0
  79. package/build/x86_64-apple-darwin/lib/quickjs-full.a +0 -0
  80. package/build/x86_64-pc-windows-static/bin/file-to-bytecode.js +7 -2
  81. package/build/x86_64-pc-windows-static/bin/qjs.exe +0 -0
  82. package/build/x86_64-pc-windows-static/bin/qjsbootstrap-bytecode.exe +0 -0
  83. package/build/x86_64-pc-windows-static/bin/qjsbootstrap.exe +0 -0
  84. package/build/x86_64-pc-windows-static/bin/qjsc.exe +0 -0
  85. package/build/x86_64-pc-windows-static/bin/quickjs-run.exe +0 -0
  86. package/build/x86_64-pc-windows-static/extras/is-stdin-a-tty.exe +0 -0
  87. package/build/x86_64-pc-windows-static/extras/log-argv.exe +0 -0
  88. package/build/x86_64-pc-windows-static/extras/run-test262.exe +0 -0
  89. package/build/x86_64-pc-windows-static/extras/sample-program/sum.exe +0 -0
  90. package/build/x86_64-pc-windows-static/extras/stack-limit-test.exe +0 -0
  91. package/build/x86_64-pc-windows-static/lib/quickjs-core.a +0 -0
  92. package/build/x86_64-pc-windows-static/lib/quickjs-full.a +0 -0
  93. package/build/x86_64-unknown-linux-gnu/bin/file-to-bytecode.js +7 -2
  94. package/build/x86_64-unknown-linux-gnu/bin/qjs +0 -0
  95. package/build/x86_64-unknown-linux-gnu/bin/qjsbootstrap +0 -0
  96. package/build/x86_64-unknown-linux-gnu/bin/qjsbootstrap-bytecode +0 -0
  97. package/build/x86_64-unknown-linux-gnu/bin/qjsc +0 -0
  98. package/build/x86_64-unknown-linux-gnu/bin/quickjs-run +0 -0
  99. package/build/x86_64-unknown-linux-gnu/extras/fib.so +0 -0
  100. package/build/x86_64-unknown-linux-gnu/extras/is-stdin-a-tty +0 -0
  101. package/build/x86_64-unknown-linux-gnu/extras/log-argv +0 -0
  102. package/build/x86_64-unknown-linux-gnu/extras/point.so +0 -0
  103. package/build/x86_64-unknown-linux-gnu/extras/run-test262 +0 -0
  104. package/build/x86_64-unknown-linux-gnu/extras/sample-program/sum +0 -0
  105. package/build/x86_64-unknown-linux-gnu/extras/stack-limit-test +0 -0
  106. package/build/x86_64-unknown-linux-gnu/lib/quickjs-core.a +0 -0
  107. package/build/x86_64-unknown-linux-gnu/lib/quickjs-full.a +0 -0
  108. package/build/x86_64-unknown-linux-musl/bin/file-to-bytecode.js +7 -2
  109. package/build/x86_64-unknown-linux-musl/bin/qjs +0 -0
  110. package/build/x86_64-unknown-linux-musl/bin/qjsbootstrap +0 -0
  111. package/build/x86_64-unknown-linux-musl/bin/qjsbootstrap-bytecode +0 -0
  112. package/build/x86_64-unknown-linux-musl/bin/qjsc +0 -0
  113. package/build/x86_64-unknown-linux-musl/bin/quickjs-run +0 -0
  114. package/build/x86_64-unknown-linux-musl/extras/fib.so +0 -0
  115. package/build/x86_64-unknown-linux-musl/extras/is-stdin-a-tty +0 -0
  116. package/build/x86_64-unknown-linux-musl/extras/point.so +0 -0
  117. package/build/x86_64-unknown-linux-musl/extras/run-test262 +0 -0
  118. package/build/x86_64-unknown-linux-musl/extras/sample-program/sum +0 -0
  119. package/build/x86_64-unknown-linux-musl/extras/stack-limit-test +0 -0
  120. package/build/x86_64-unknown-linux-musl/lib/quickjs-core.a +0 -0
  121. package/build/x86_64-unknown-linux-musl/lib/quickjs-full.a +0 -0
  122. package/build/x86_64-unknown-linux-static/bin/file-to-bytecode.js +7 -2
  123. package/build/x86_64-unknown-linux-static/bin/qjs +0 -0
  124. package/build/x86_64-unknown-linux-static/bin/qjsbootstrap +0 -0
  125. package/build/x86_64-unknown-linux-static/bin/qjsbootstrap-bytecode +0 -0
  126. package/build/x86_64-unknown-linux-static/bin/qjsc +0 -0
  127. package/build/x86_64-unknown-linux-static/bin/quickjs-run +0 -0
  128. package/build/x86_64-unknown-linux-static/extras/fib.so +0 -0
  129. package/build/x86_64-unknown-linux-static/extras/is-stdin-a-tty +0 -0
  130. package/build/x86_64-unknown-linux-static/extras/point.so +0 -0
  131. package/build/x86_64-unknown-linux-static/extras/run-test262 +0 -0
  132. package/build/x86_64-unknown-linux-static/extras/sample-program/sum +0 -0
  133. package/build/x86_64-unknown-linux-static/extras/stack-limit-test +0 -0
  134. package/build/x86_64-unknown-linux-static/lib/quickjs-core.a +0 -0
  135. package/build/x86_64-unknown-linux-static/lib/quickjs-full.a +0 -0
  136. package/package.json +2 -2
  137. package/.gitmodules +0 -6
package/README.md CHANGED
@@ -1,8 +1,21 @@
1
1
  # suchipi/quickjs
2
2
 
3
- Fork of the fantastic QuickJS engine by Fabrice Bellard, with the following changes:
3
+ Fork of the fantastic QuickJS engine by Fabrice Bellard, with many changes.
4
4
 
5
- ## Changes to `quickjs`:
5
+ ## High-level List of Most Notable Changes
6
+
7
+ - APIs from 'std' and 'os' modules were changed to be more idiomatic-to-JS; notably, they throw Errors on failure instead of returning null or errno numbers.
8
+ - TypeScript-format type interface files (`.d.ts` files) were added for everything.
9
+ - Hooks have been added to the module loader that make its functionality more customizable.
10
+ - Some ES2022 features were added, and some non-standard ECMAScript proposals and extensions were added.
11
+ - The way the project is organized and built was changed dramatically.
12
+ - Several new JS bindings for C APIs have been added, such as `strftime`, `access`, `fsync`, `setvbuf`, `getuid`...
13
+ - Scripts were added that cross-compile binaries for several operating systems and architectures
14
+ - FreeBSD support added
15
+
16
+ ## Detailed List of Changes
17
+
18
+ ### Changes to `quickjs`:
6
19
 
7
20
  - A TypeScript `.d.ts` file is provided for all QuickJS-specific APIs (operator overloading APIs, BigInt extensions, BigFloat, BigDecimal, etc).
8
21
  - Non-standard `Object.toPrimitive` added (static method that invokes ToPrimitive on the given value, using the optionally-provided hint).
@@ -23,8 +36,9 @@ Fork of the fantastic QuickJS engine by Fabrice Bellard, with the following chan
23
36
  - `JS_FreezeObjectValue` (performs Object.freeze)
24
37
  - `JS_IsPrimitive`
25
38
  - ModuleDefs now have an optional "user_data" property (pointer to void) which can be accessed during module initialization (via `JS_GetModuleUserData` and `JS_SetModuleUserData`)
39
+ - Added `JS_SetContextOpaqueValue` and `JS_GetContextOpaqueValue`, which let you associate a JSValue with a JSContext, which will be garbage-collected when that JSContext is garbage-collected.
26
40
 
27
- ## Changes to `quickjs-libc`:
41
+ ### Changes to `quickjs-libc`:
28
42
 
29
43
  - `std` and `os` builtin modules are now namespaced under "quickjs:". In other words, you have to import them as "quickjs:std" and "quickjs:os".
30
44
  - APIs in `std` and `os` no longer return errno anywhere; instead, Error objects are thrown. `errno` is available as a property on the thrown Error objects.
@@ -35,6 +49,7 @@ Fork of the fantastic QuickJS engine by Fabrice Bellard, with the following chan
35
49
  - `os.access` function added (wrapper for libc `access`).
36
50
  - `FILE.prototype.sync` method added (wrapper for `fsync`).
37
51
  - `FILE.prototype.setvbuf` method added (wrapper for `setvbuf`).
52
+ - `FILE.prototype.writeTo` method added (pipe data from one FILE to another, given a buffer size and limit).
38
53
  - `std.isFILE` function added (returns whether the provided object is a `FILE` (via `js_std_file_class_id`)).
39
54
  - `os.{WUNTRACED,WEXITSTATUS,WTERMSIG,WSTOPSIG,WIFEXITED,WIFSIGNALED,WIFSTOPPED,WIFCONTINUED}` added, for working with `os.waitpid`.
40
55
  - `os.{S_IRWXU,S_IRUSR,S_IWUSR,S_IXUSR,S_IRWXG,S_IRGRP,S_IWGRP,S_IXGRP,S_IRWXO,S_IROTH,S_IWOTH,S_IXOTH}` added, for working with file modes.
@@ -42,24 +57,25 @@ Fork of the fantastic QuickJS engine by Fabrice Bellard, with the following chan
42
57
  - `std.strftime` added (wrapper for libc `strftime`).
43
58
  - Added `std.getuid`, `std.geteuid`, `std.getgid`, and `std.getegid` (wrappers for the libc functions of the same names).
44
59
  - Most module-loading-related code was moved into `quickjs-modulesys`.
45
- - Synchronous import function added (`std.importModule`), which provides the same module record object you would get via dynamic (async) import.
46
- - JS api for using the engine's configured module name normalization function was added (`std.resolveModule`).
47
60
  - `setTimeout` and `clearTimeout` are now available as globals (previously they were only available as exports).
48
61
  - `setInterval` and `clearInterval` are added, available as globals.
49
62
  - `String.dedent` added (template tag function, like the proposed [String.dedent](https://github.com/tc39/proposal-string-dedent)).
50
63
  - Several C-side helper functions were moved out of quickjs-libc and into quickjs-utils.
51
64
  - Most module-related code (setting import.meta, etc) was moved into quickjs-modulesys.
52
65
  - Added `std.setExitCode`, `std.getExitCode`, and made `std.exit`'s parameter optional. The value passed to `std.setExitCode` will be used when the process exits normally, or when `std.exit` is called without any arguments. `std.setExitCode`, `std.getExitCode`, and `std.exit` throw if called from a thread other than the main thread (ie. a Worker).
66
+ - The manual garbage collection function `std.gc()` was moved to `"quickjs:engine"`.
53
67
 
54
- ## Changes to the `qjs` repl:
68
+ ### Changes to the `qjs` binary:
55
69
 
56
- - The new `inspect` global is used to print results
57
- - The last error is accessible via the `_error` global
58
- - If a thrown error has additional properties added onto it, those are printed along with the thrown error
59
- - Ctrl+W deletes words backwards from the cursor position
60
70
  - Blocking the main thread is allowed
71
+ - `-m` (module mode) now affects eval strings passed to `-e`
72
+ - Changes to the repl:
73
+ - Ctrl+W deletes words backwards from the cursor position
74
+ - The last error is accessible via the `_error` global
75
+ - If a thrown error has additional properties added onto it, those are printed along with the thrown error
76
+ - The new `inspect` global is used to print results
61
77
 
62
- ## New binary: `qjsbootstrap`:
78
+ ### New binary: `qjsbootstrap`:
63
79
 
64
80
  Appending some JS code to the end of this binary changes it into a binary that executes that JS code:
65
81
 
@@ -74,23 +90,23 @@ You can use this to create distributable binaries that run JS code without needi
74
90
 
75
91
  > Note: On FreeBSD, `qjsbootstrap` requires procfs. You can mount it with `mount -t procfs proc /proc`. I started some work to use libprocstat instead, but haven't completed it yet.
76
92
 
77
- ## New binary: `quickjs-run`:
93
+ ### New binary: `quickjs-run`:
78
94
 
79
95
  Barebones binary for running files, without any of the arg parsing logic from qjs. Good for testing some unusual cases, or writing programs with custom argv parsing logic.
80
96
 
81
- ## New module: "quickjs:bytecode"
97
+ ### New module: "quickjs:bytecode"
82
98
 
83
99
  A Module that exposes QuickJS's value <-> bytecode (de)serialization APIs to JavaScript code. Generated bytecode can be combined with `qjsbootstrap-bytecode` in the same way that source code strings can be combined with `qjsbootstrap`.
84
100
 
85
- ## New module: "quickjs:context"
101
+ ### New module: "quickjs:context"
86
102
 
87
103
  A Module that allows JS code to create new JS Contexts (Realms). You can create new Contexts and run code inside them. Contexts can have certain features disabled (like eval) for security purposes. You can share values between Contexts. Contexts are destroyed when they get garbage-collected.
88
104
 
89
- ## New module: "quickjs:pointer"
105
+ ### New module: "quickjs:pointer"
90
106
 
91
107
  A barebones Module that exports a JS class which can be used to represent an opaque pointer. C modules can use the `js_new_pointer` function provided by this module to pass opaque pointer handles to users without needing to make their own wrapper class for stuff. This is mostly just useful in order to have a codified convention for how FFI libraries and such should represent foreign pointers.
92
108
 
93
- ## New library: `quickjs-utils`
109
+ ### New library: `quickjs-utils`
94
110
 
95
111
  Helper structs, functions, and macros that make it easier to work with QuickJS in C code.
96
112
 
@@ -98,55 +114,63 @@ Helper structs, functions, and macros that make it easier to work with QuickJS i
98
114
  - Helper function for loading a file from disk into char a buffer
99
115
  - Helper functions for printing JS errors to stderr
100
116
 
101
- ## New library: `quickjs-modulesys`
117
+ ### New module: "quickjs:engine"
102
118
 
103
- - Module-loading code in `quickjs-libc` was moved into `quickjs-modulesys`, with the following changes:
104
- - `.js` extensions can now be omitted from import specifiers; they're optional.
105
- - If your import specifier points to a folder, it will attempt to load `index.js` from that folder.
119
+ This module contains APIs related to engine internals like script execution, module loading, code eval, filename reflection, and garbage collection. Several parts of quickjs-libc were moved here so that quickjs-libc could be focused on "C standard library" bindings.
120
+
121
+ ### Changes to the module loader
122
+
123
+ - `.js` extensions can now be omitted from import specifiers; they're optional.
124
+ - If your import specifier points to a folder, it will attempt to load `index.js` from that folder.
106
125
  - Adds the global `require`, a CommonJS-like synchronous module loading function.
107
126
  - The `require` function is not fully CommonJS-compliant; for instance, `require.main` is not present. `require.resolve` is, though.
108
127
  - Adds `import.meta.require`
109
128
  - It's the same as the global `require`; it's just added to import.meta for compatibility with bundlers that output `import.meta.require`, like `bun`.
110
129
  - Adds `import.meta.resolve`
111
130
  - Similar to [the one in the browser](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/import.meta/resolve#specifications), but it's actually just `require.resolve` exposed via `import.meta`.
131
+ - Module and eval helpers have been moved from "quickjs:std" to the new module "quickjs:engine".
132
+ - Makes the module loader's resolution and loading behavior configurable
133
+ - The module "quickjs:engine" exports an object called `ModuleDelegate`.
134
+ - You can specify additional implicit import specifier extensions by adding to the `ModuleDelegate.searchExtensions` array.
135
+ - You can transform any file prior to evaluating it as a module by adding a function to the `ModuleDelegate.compilers` object. Useful for compile-to-ts languages like TypeScript, Coffeescript, etc.
136
+ - You can override module name normalization (aka module resolution) by replacing the `ModuleDelegate.resolve` function.
137
+ - Note that you must handle `ModuleDelegate.searchExtensions` yourself in your replacement implementation.
138
+ - You can override the method used to load modules by replacing the `ModuleDelegate.read` function.
139
+ - Note that you must handle `ModuleDelegate.compilers` yourself in your replacement implementation.
112
140
  - Makes `import.meta.main` configurable
113
- - You can use `std.setMainModule` to make `import.meta.main` true within that module's code. Note, however, that it does not work retroactively; only modules loaded after the `setMainModule` call will be affected. To defer module load, use `import()`, `std.importModule`, or `require`.
114
- - You can use `std.isMainModule` to check if a given module would be the main module without loading it.
115
- - Adds the global object `Module`.
116
- - `instanceof Module` can be used to identify module namespace objects.
117
- - You can specify additional implicit import specifier extensions by adding to the `Module.searchExtensions` array.
118
- - You can transform any file prior to evaluating it as a module by adding a function to the `Module.compilers` object. Useful for compile-to-ts languages like TypeScript, Coffeescript, etc.
119
- - You can define custom builtin modules using the `Module.define` function.
120
- - You can override module name normalization (aka module resolution) by replacing the `Module.resolve` function.
121
- - Note that you must handle `Module.searchExtensions` yourself in your replacement implementation.
122
- - You can override the method used to load modules by replacing the `Module.read` function.
123
- - Note that you must handle `Module.compilers` yourself in your replacement implementation.
124
- - The `eval_*` functions that were duplicated in each of the programs (`eval_buf`, `eval_file`, and `eval_binary`) were moved here
125
- - The default module loader function from quickjs-libc was moved here and augmented to support the features related to the `Module` global as mentioned above
126
- - When using `require` to load a module which contains an export named `__cjsExports`, the value of the `__cjsExports` property will be returned from `require` instead of the usual module namespace object. This can be leveraged by user-defined properties on `Module` to add some CommonJS <-> ESM interop. Note, however, that dynamic import and `std.importModule` always receive the usual module namespace object.
127
-
128
- ## Changes to project organization
141
+ - The module "quickjs:engine" exports two functions named `setMainModule` and `isMainModule`.
142
+ - You can use `setMainModule` to make `import.meta.main` true within that module's code. Note, however, that it does not work retroactively; only modules loaded after the `setMainModule` call will be affected. To defer module load, use `import()`, `importModule` from "quickjs:engine", or `require`.
143
+ - You can use `isMainModule` to check if a given module would be the main module without loading it.
144
+ - New `isModuleNamespace` function lets users identify module namespace objects
145
+ - New `defineBuiltinModule` function lets users add their own builtin modules
146
+ - When using `require` to load a module which contains an export named `__cjsExports`, the value of the `__cjsExports` property will be returned from `require` instead of the usual module namespace object. This can be leveraged by users configuring the module loader to add some CommonJS <-> ESM interop. Note, however, that dynamic import and `"quickjs:engine"`'s `importModule` always receive the usual module namespace object.
147
+ - Synchronous import function added (`importModule`), which provides the same module record object you would get via dynamic (async) import.
148
+ - JS api for using the engine's configured module name normalization function was added (`resolveModule`).
149
+
150
+ ### Changes to project organization
129
151
 
130
152
  - Stuff is reorganized into separate folders under `src`.
131
153
  - Ninja is used instead of make. Ninja build config is generated via `.ninja.js` files which get loaded into [@suchipi/shinobi](https://github.com/suchipi/shinobi).
132
154
  - Line endings have been made consistent and trailing whitespace has been removed
133
155
  - The tests are authored in a new format which leverages jest snapshot testing.
156
+ - Some parts of `quickjs-libc` were moved into `quickjs-modulesys` and `quickjs-libengine`.
157
+ - The `eval_*` functions that were duplicated in each of the programs (`eval_buf`, `eval_file`, and `eval_binary`) were deduplicated and moved into `quickjs-modulesys`.
134
158
 
135
- ## More target OSes/runtimes
159
+ ### More target OSes/runtimes
136
160
 
137
161
  We now include support for more platforms, and cross-compilation scripts to build most of those platforms from any platform where Docker is available.
138
162
 
139
163
  See the `meta/ninja/envs` folder to see all the supported platforms. The `host` folder represents the machine where compilation is taking place, and the `target` folder represents the target platform for the output binaries.
140
164
 
141
- ## Library Archives
165
+ ### Library Archives
142
166
 
143
167
  We create `.a` files containing all of quickjs as part of the build.
144
168
 
145
- ## Other changes
169
+ ### Other changes
146
170
 
147
171
  There are also probably some other miscellaneous changes I forgot to write down in the README.
148
172
 
149
- # Compiling
173
+ ## Compiling
150
174
 
151
175
  The repo has stuff set up to compile quickjs binaries for:
152
176
 
@@ -164,9 +188,9 @@ QuickJS itself has no external dependencies outside this repo except pthreads, a
164
188
 
165
189
  Linux, macOS, iOS, and Windows binaries can be compiled using Docker. Or, you can compile binaries for just your own unix system, without using Docker.
166
190
 
167
- If you're not gonna use Docker, you'll need to install [Ninja](https://ninja-build.org/) and [Node.js](https://nodejs.org/) in order to compile. I use Ninja 1.10.1 and Node.js 18.12.1, but it should work with most versions of both of those.
191
+ If you're not gonna use Docker, you'll need to install [Ninja](https://ninja-build.org/) and [Node.js](https://nodejs.org/) in order to compile. I use Ninja 1.10.1 and Node.js 18.18.0, but it should work with most versions of both of those.
168
192
 
169
- ## Compilation Instructions
193
+ ### Compilation Instructions
170
194
 
171
195
  To compile binaries for Linux, macOS, iOS, and Windows (using Docker):
172
196
 
@@ -177,7 +201,7 @@ To compile binaries for Linux, macOS, iOS, and Windows (using Docker):
177
201
 
178
202
  Or, to compile binaries for just your own unix system:
179
203
 
180
- - Make sure you have both [Ninja](https://ninja-build.org/) and [Node.js](https://nodejs.org/) installed. I use Ninja 1.10.1 and Node.js 18.12.1, but it should work with most versions of both of those.
204
+ - Make sure you have both [Ninja](https://ninja-build.org/) and [Node.js](https://nodejs.org/) installed. I use Ninja 1.10.1 and Node.js 18.18.0, but it should work with most versions of both of those.
181
205
  - Clone the repo and cd to its folder
182
206
  - Run `meta/build.sh`
183
207
  - Build artifacts will be placed in the `build` folder. You're probably most interested in stuff in the `build/bin` and `build/lib` folders.
@@ -11,7 +11,8 @@ import * as std from "quickjs:std";
11
11
  import * as Bytecode from "quickjs:bytecode";
12
12
 
13
13
  function main() {
14
- const [_quickjsRun, _thisScript, inputFile, outputFile] = scriptArgs;
14
+ let [_quickjsRun, _thisScript, inputFile, outputFile, encodedFileName] =
15
+ scriptArgs;
15
16
 
16
17
  if (!(inputFile && outputFile)) {
17
18
  throw new Error(
@@ -19,7 +20,11 @@ function main() {
19
20
  );
20
21
  }
21
22
 
22
- const bytecode = Bytecode.fromFile(inputFile);
23
+ if (!encodedFileName) {
24
+ encodedFileName = inputFile;
25
+ }
26
+
27
+ const bytecode = Bytecode.fromFile(inputFile, { encodedFileName });
23
28
  const out = std.open(outputFile, "wb");
24
29
  out.write(bytecode, 0, bytecode.byteLength);
25
30
  }
Binary file
Binary file
@@ -11,7 +11,8 @@ import * as std from "quickjs:std";
11
11
  import * as Bytecode from "quickjs:bytecode";
12
12
 
13
13
  function main() {
14
- const [_quickjsRun, _thisScript, inputFile, outputFile] = scriptArgs;
14
+ let [_quickjsRun, _thisScript, inputFile, outputFile, encodedFileName] =
15
+ scriptArgs;
15
16
 
16
17
  if (!(inputFile && outputFile)) {
17
18
  throw new Error(
@@ -19,7 +20,11 @@ function main() {
19
20
  );
20
21
  }
21
22
 
22
- const bytecode = Bytecode.fromFile(inputFile);
23
+ if (!encodedFileName) {
24
+ encodedFileName = inputFile;
25
+ }
26
+
27
+ const bytecode = Bytecode.fromFile(inputFile, { encodedFileName });
23
28
  const out = std.open(outputFile, "wb");
24
29
  out.write(bytecode, 0, bytecode.byteLength);
25
30
  }
@@ -11,7 +11,8 @@ import * as std from "quickjs:std";
11
11
  import * as Bytecode from "quickjs:bytecode";
12
12
 
13
13
  function main() {
14
- const [_quickjsRun, _thisScript, inputFile, outputFile] = scriptArgs;
14
+ let [_quickjsRun, _thisScript, inputFile, outputFile, encodedFileName] =
15
+ scriptArgs;
15
16
 
16
17
  if (!(inputFile && outputFile)) {
17
18
  throw new Error(
@@ -19,7 +20,11 @@ function main() {
19
20
  );
20
21
  }
21
22
 
22
- const bytecode = Bytecode.fromFile(inputFile);
23
+ if (!encodedFileName) {
24
+ encodedFileName = inputFile;
25
+ }
26
+
27
+ const bytecode = Bytecode.fromFile(inputFile, { encodedFileName });
23
28
  const out = std.open(outputFile, "wb");
24
29
  out.write(bytecode, 0, bytecode.byteLength);
25
30
  }
@@ -11,7 +11,8 @@ import * as std from "quickjs:std";
11
11
  import * as Bytecode from "quickjs:bytecode";
12
12
 
13
13
  function main() {
14
- const [_quickjsRun, _thisScript, inputFile, outputFile] = scriptArgs;
14
+ let [_quickjsRun, _thisScript, inputFile, outputFile, encodedFileName] =
15
+ scriptArgs;
15
16
 
16
17
  if (!(inputFile && outputFile)) {
17
18
  throw new Error(
@@ -19,7 +20,11 @@ function main() {
19
20
  );
20
21
  }
21
22
 
22
- const bytecode = Bytecode.fromFile(inputFile);
23
+ if (!encodedFileName) {
24
+ encodedFileName = inputFile;
25
+ }
26
+
27
+ const bytecode = Bytecode.fromFile(inputFile, { encodedFileName });
23
28
  const out = std.open(outputFile, "wb");
24
29
  out.write(bytecode, 0, bytecode.byteLength);
25
30
  }
@@ -6,7 +6,11 @@ declare module "quickjs:bytecode" {
6
6
  */
7
7
  export function fromFile(
8
8
  path: string,
9
- options?: { byteSwap?: boolean; sourceType?: "module" | "script" }
9
+ options?: {
10
+ byteSwap?: boolean;
11
+ sourceType?: "module" | "script";
12
+ encodedFileName?: string;
13
+ }
10
14
  ): ArrayBuffer;
11
15
 
12
16
  /**
@@ -98,6 +98,20 @@ declare interface FILE {
98
98
  /** Write `length` bytes from the ArrayBuffer `buffer` at byte position `position` into the file (wrapper to the libc `fwrite`). Returns the number of bytes written. */
99
99
  write(buffer: ArrayBuffer, position: number, length: number): number;
100
100
 
101
+ /**
102
+ * Write this file into `target`, using a memory buffer of size `bufferSize`.
103
+ *
104
+ * If `limit` is specified, only that amount of bytes will be read and
105
+ * written. Otherwise, data is read and written until this file reaches EOF.
106
+ *
107
+ * A `limit` of 0 is treated the same as not specifying a limit.
108
+ *
109
+ * Internally, this function uses libc `fread` and `fwrite` in a loop.
110
+ *
111
+ * Returns the number of bytes read and written.
112
+ */
113
+ writeTo(target: FILE, bufferSize: number, limit?: number): number;
114
+
101
115
  /**
102
116
  * Return the next line from the file, assuming UTF-8 encoding, excluding the trailing line feed or EOF.
103
117
  *
@@ -257,9 +271,6 @@ declare module "quickjs:std" {
257
271
  /** Constant for {@link FILE.setvbuf}. Declares that the buffer mode should be 'no buffering'. */
258
272
  export var _IONBF: number;
259
273
 
260
- /** Manually invoke the cycle removal algorithm (garbage collector). The cycle removal algorithm is automatically started when needed, so this function is useful in case of specific memory constraints or for testing. */
261
- export function gc(): void;
262
-
263
274
  /** Return the value of the environment variable `name` or `undefined` if it is not defined. */
264
275
  export function getenv(name: string): string | undefined;
265
276
 
@@ -910,7 +921,7 @@ declare module "quickjs:os" {
910
921
  export function dup2(oldfd: number, newfd: number): number;
911
922
 
912
923
  /** `pipe` Unix system call. Return two handles as `[read_fd, write_fd]`. */
913
- export function pipe(): null | [number, number];
924
+ export function pipe(): [number, number];
914
925
 
915
926
  /** Sleep for `delay_ms` milliseconds. */
916
927
  export function sleep(delay_ms: number): void;
@@ -137,7 +137,7 @@ declare module "quickjs:context" {
137
137
  console?: boolean;
138
138
  /** Enables `print`. Defaults to `true`. */
139
139
  print?: boolean;
140
- /** Enables `require` and `Module`. Defaults to `true`. */
140
+ /** Enables `require`. Defaults to `true`. */
141
141
  moduleGlobals?: boolean;
142
142
  /**
143
143
  * Enables `setTimeout`, `clearTimeout`, `setInterval`, and
@@ -155,8 +155,8 @@ declare module "quickjs:context" {
155
155
  "quickjs:bytecode"?: boolean;
156
156
  /** Enables the "quickjs:context" module. Defaults to `true`. */
157
157
  "quickjs:context"?: boolean;
158
- /** Enables the "quickjs:module" module. Defaults to `true`. */
159
- "quickjs:module"?: boolean;
158
+ /** Enables the "quickjs:engine" module. Defaults to `true`. */
159
+ "quickjs:engine"?: boolean;
160
160
  };
161
161
  });
162
162
 
@@ -1,4 +1,4 @@
1
- declare module "quickjs:module" {
1
+ declare module "quickjs:engine" {
2
2
  /**
3
3
  * Return whether the provided resolved module path is set as the main module.
4
4
  *
@@ -69,4 +69,32 @@ declare module "quickjs:module" {
69
69
  * @param stackLevels - How many levels up the stack to search for a filename. Defaults to 0, which uses the current stack frame.
70
70
  */
71
71
  export function getFileNameFromStack(stackLevels?: number): string;
72
+
73
+ /**
74
+ * Returns true if `target` is a module namespace object.
75
+ */
76
+ export function isModuleNamespace(target: any): boolean;
77
+
78
+ /**
79
+ * Create a virtual built-in module whose exports consist of the own
80
+ * enumerable properties of `obj`.
81
+ */
82
+ export function defineBuiltinModule(
83
+ name: string,
84
+ obj: { [key: string]: any }
85
+ ): void;
86
+
87
+ /**
88
+ * An object which lets you configure the module loader (import/export/require).
89
+ * You can change these properties to add support for importing new filetypes.
90
+ */
91
+ export const ModuleDelegate: ModuleDelegate;
92
+
93
+ /**
94
+ * Manually invoke the cycle removal algorithm (garbage collector).
95
+ *
96
+ * The cycle removal algorithm is automatically started when needed, so this
97
+ * function is useful in case of specific memory constraints or for testing.
98
+ */
99
+ export function gc(): void;
72
100
  }