@suchipi/quickjs 0.4.2 → 0.6.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 (107) hide show
  1. package/README.md +57 -32
  2. package/build/aarch64-apple-darwin/bin/qjs +0 -0
  3. package/build/aarch64-apple-darwin/bin/qjsbootstrap +0 -0
  4. package/build/aarch64-apple-darwin/bin/qjsbootstrap-bytecode +0 -0
  5. package/build/aarch64-apple-darwin/bin/qjsc +0 -0
  6. package/build/aarch64-apple-darwin/bin/quickjs-run +0 -0
  7. package/build/aarch64-apple-darwin/extras/is-stdin-a-tty +0 -0
  8. package/build/aarch64-apple-darwin/extras/run-test262 +0 -0
  9. package/build/aarch64-apple-darwin/extras/sample-program/sum +0 -0
  10. package/build/aarch64-apple-darwin/extras/stack-limit-test +0 -0
  11. package/build/aarch64-apple-darwin/lib/quickjs-core.a +0 -0
  12. package/build/aarch64-apple-darwin/lib/quickjs-full.a +0 -0
  13. package/build/aarch64-unknown-linux-gnu/bin/qjs +0 -0
  14. package/build/aarch64-unknown-linux-gnu/bin/qjsbootstrap +0 -0
  15. package/build/aarch64-unknown-linux-gnu/bin/qjsbootstrap-bytecode +0 -0
  16. package/build/aarch64-unknown-linux-gnu/bin/qjsc +0 -0
  17. package/build/aarch64-unknown-linux-gnu/bin/quickjs-run +0 -0
  18. package/build/aarch64-unknown-linux-gnu/extras/is-stdin-a-tty +0 -0
  19. package/build/aarch64-unknown-linux-gnu/extras/run-test262 +0 -0
  20. package/build/aarch64-unknown-linux-gnu/extras/sample-program/sum +0 -0
  21. package/build/aarch64-unknown-linux-gnu/extras/stack-limit-test +0 -0
  22. package/build/aarch64-unknown-linux-gnu/lib/quickjs-core.a +0 -0
  23. package/build/aarch64-unknown-linux-gnu/lib/quickjs-full.a +0 -0
  24. package/build/aarch64-unknown-linux-musl/bin/qjs +0 -0
  25. package/build/aarch64-unknown-linux-musl/bin/qjsbootstrap +0 -0
  26. package/build/aarch64-unknown-linux-musl/bin/qjsbootstrap-bytecode +0 -0
  27. package/build/aarch64-unknown-linux-musl/bin/qjsc +0 -0
  28. package/build/aarch64-unknown-linux-musl/bin/quickjs-run +0 -0
  29. package/build/aarch64-unknown-linux-musl/extras/is-stdin-a-tty +0 -0
  30. package/build/aarch64-unknown-linux-musl/extras/run-test262 +0 -0
  31. package/build/aarch64-unknown-linux-musl/extras/sample-program/sum +0 -0
  32. package/build/aarch64-unknown-linux-musl/extras/stack-limit-test +0 -0
  33. package/build/aarch64-unknown-linux-musl/lib/quickjs-core.a +0 -0
  34. package/build/aarch64-unknown-linux-musl/lib/quickjs-full.a +0 -0
  35. package/build/aarch64-unknown-linux-static/bin/qjs +0 -0
  36. package/build/aarch64-unknown-linux-static/bin/qjsbootstrap +0 -0
  37. package/build/aarch64-unknown-linux-static/bin/qjsbootstrap-bytecode +0 -0
  38. package/build/aarch64-unknown-linux-static/bin/qjsc +0 -0
  39. package/build/aarch64-unknown-linux-static/bin/quickjs-run +0 -0
  40. package/build/aarch64-unknown-linux-static/extras/is-stdin-a-tty +0 -0
  41. package/build/aarch64-unknown-linux-static/extras/run-test262 +0 -0
  42. package/build/aarch64-unknown-linux-static/extras/sample-program/sum +0 -0
  43. package/build/aarch64-unknown-linux-static/extras/stack-limit-test +0 -0
  44. package/build/aarch64-unknown-linux-static/lib/quickjs-core.a +0 -0
  45. package/build/aarch64-unknown-linux-static/lib/quickjs-full.a +0 -0
  46. package/build/dts/quickjs-libc.d.ts +38 -45
  47. package/build/dts/quickjs-libcontext.d.ts +21 -22
  48. package/build/dts/quickjs-libmodule.d.ts +81 -0
  49. package/build/dts/quickjs-modulesys.d.ts +10 -12
  50. package/build/x86_64-apple-darwin/bin/qjs +0 -0
  51. package/build/x86_64-apple-darwin/bin/qjsbootstrap +0 -0
  52. package/build/x86_64-apple-darwin/bin/qjsbootstrap-bytecode +0 -0
  53. package/build/x86_64-apple-darwin/bin/qjsc +0 -0
  54. package/build/x86_64-apple-darwin/bin/quickjs-run +0 -0
  55. package/build/x86_64-apple-darwin/extras/is-stdin-a-tty +0 -0
  56. package/build/x86_64-apple-darwin/extras/run-test262 +0 -0
  57. package/build/x86_64-apple-darwin/extras/sample-program/sum +0 -0
  58. package/build/x86_64-apple-darwin/extras/stack-limit-test +0 -0
  59. package/build/x86_64-apple-darwin/lib/quickjs-core.a +0 -0
  60. package/build/x86_64-apple-darwin/lib/quickjs-full.a +0 -0
  61. package/build/x86_64-pc-windows-static/bin/qjs.exe +0 -0
  62. package/build/x86_64-pc-windows-static/bin/qjsbootstrap-bytecode.exe +0 -0
  63. package/build/x86_64-pc-windows-static/bin/qjsbootstrap.exe +0 -0
  64. package/build/x86_64-pc-windows-static/bin/qjsc.exe +0 -0
  65. package/build/x86_64-pc-windows-static/bin/quickjs-run.exe +0 -0
  66. package/build/x86_64-pc-windows-static/extras/is-stdin-a-tty.exe +0 -0
  67. package/build/x86_64-pc-windows-static/extras/log-argv.exe +0 -0
  68. package/build/x86_64-pc-windows-static/extras/run-test262.exe +0 -0
  69. package/build/x86_64-pc-windows-static/extras/sample-program/sum.exe +0 -0
  70. package/build/x86_64-pc-windows-static/extras/stack-limit-test.exe +0 -0
  71. package/build/x86_64-pc-windows-static/lib/quickjs-core.a +0 -0
  72. package/build/x86_64-pc-windows-static/lib/quickjs-full.a +0 -0
  73. package/build/x86_64-unknown-linux-gnu/bin/qjs +0 -0
  74. package/build/x86_64-unknown-linux-gnu/bin/qjsbootstrap +0 -0
  75. package/build/x86_64-unknown-linux-gnu/bin/qjsbootstrap-bytecode +0 -0
  76. package/build/x86_64-unknown-linux-gnu/bin/qjsc +0 -0
  77. package/build/x86_64-unknown-linux-gnu/bin/quickjs-run +0 -0
  78. package/build/x86_64-unknown-linux-gnu/extras/is-stdin-a-tty +0 -0
  79. package/build/x86_64-unknown-linux-gnu/extras/run-test262 +0 -0
  80. package/build/x86_64-unknown-linux-gnu/extras/sample-program/sum +0 -0
  81. package/build/x86_64-unknown-linux-gnu/extras/stack-limit-test +0 -0
  82. package/build/x86_64-unknown-linux-gnu/lib/quickjs-core.a +0 -0
  83. package/build/x86_64-unknown-linux-gnu/lib/quickjs-full.a +0 -0
  84. package/build/x86_64-unknown-linux-musl/bin/qjs +0 -0
  85. package/build/x86_64-unknown-linux-musl/bin/qjsbootstrap +0 -0
  86. package/build/x86_64-unknown-linux-musl/bin/qjsbootstrap-bytecode +0 -0
  87. package/build/x86_64-unknown-linux-musl/bin/qjsc +0 -0
  88. package/build/x86_64-unknown-linux-musl/bin/quickjs-run +0 -0
  89. package/build/x86_64-unknown-linux-musl/extras/is-stdin-a-tty +0 -0
  90. package/build/x86_64-unknown-linux-musl/extras/run-test262 +0 -0
  91. package/build/x86_64-unknown-linux-musl/extras/sample-program/sum +0 -0
  92. package/build/x86_64-unknown-linux-musl/extras/stack-limit-test +0 -0
  93. package/build/x86_64-unknown-linux-musl/lib/quickjs-core.a +0 -0
  94. package/build/x86_64-unknown-linux-musl/lib/quickjs-full.a +0 -0
  95. package/build/x86_64-unknown-linux-static/bin/qjs +0 -0
  96. package/build/x86_64-unknown-linux-static/bin/qjsbootstrap +0 -0
  97. package/build/x86_64-unknown-linux-static/bin/qjsbootstrap-bytecode +0 -0
  98. package/build/x86_64-unknown-linux-static/bin/qjsc +0 -0
  99. package/build/x86_64-unknown-linux-static/bin/quickjs-run +0 -0
  100. package/build/x86_64-unknown-linux-static/extras/is-stdin-a-tty +0 -0
  101. package/build/x86_64-unknown-linux-static/extras/run-test262 +0 -0
  102. package/build/x86_64-unknown-linux-static/extras/sample-program/sum +0 -0
  103. package/build/x86_64-unknown-linux-static/extras/stack-limit-test +0 -0
  104. package/build/x86_64-unknown-linux-static/lib/quickjs-core.a +0 -0
  105. package/build/x86_64-unknown-linux-static/lib/quickjs-full.a +0 -0
  106. package/package.json +1 -1
  107. 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,23 +57,24 @@ 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.
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).
52
66
 
53
- ## Changes to the `qjs` repl:
67
+ ### Changes to the `qjs` binary:
54
68
 
55
- - The new `inspect` global is used to print results
56
- - The last error is accessible via the `_error` global
57
- - If a thrown error has additional properties added onto it, those are printed along with the thrown error
58
- - Ctrl+W deletes words backwards from the cursor position
59
69
  - Blocking the main thread is allowed
70
+ - `-m` (module mode) now affects eval strings passed to `-e`
71
+ - Changes to the repl:
72
+ - Ctrl+W deletes words backwards from the cursor position
73
+ - The last error is accessible via the `_error` global
74
+ - If a thrown error has additional properties added onto it, those are printed along with the thrown error
75
+ - The new `inspect` global is used to print results
60
76
 
61
- ## New binary: `qjsbootstrap`:
77
+ ### New binary: `qjsbootstrap`:
62
78
 
63
79
  Appending some JS code to the end of this binary changes it into a binary that executes that JS code:
64
80
 
@@ -73,23 +89,23 @@ You can use this to create distributable binaries that run JS code without needi
73
89
 
74
90
  > 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.
75
91
 
76
- ## New binary: `quickjs-run`:
92
+ ### New binary: `quickjs-run`:
77
93
 
78
94
  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.
79
95
 
80
- ## New module: "quickjs:bytecode"
96
+ ### New module: "quickjs:bytecode"
81
97
 
82
98
  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`.
83
99
 
84
- ## New module: "quickjs:context"
100
+ ### New module: "quickjs:context"
85
101
 
86
102
  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.
87
103
 
88
- ## New module: "quickjs:pointer"
104
+ ### New module: "quickjs:pointer"
89
105
 
90
106
  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.
91
107
 
92
- ## New library: `quickjs-utils`
108
+ ### New library: `quickjs-utils`
93
109
 
94
110
  Helper structs, functions, and macros that make it easier to work with QuickJS in C code.
95
111
 
@@ -97,19 +113,19 @@ Helper structs, functions, and macros that make it easier to work with QuickJS i
97
113
  - Helper function for loading a file from disk into char a buffer
98
114
  - Helper functions for printing JS errors to stderr
99
115
 
100
- ## New library: `quickjs-modulesys`
116
+ ### Changes to the module loader
101
117
 
102
- - Module-loading code in `quickjs-libc` was moved into `quickjs-modulesys`, with the following changes:
103
- - `.js` extensions can now be omitted from import specifiers; they're optional.
104
- - If your import specifier points to a folder, it will attempt to load `index.js` from that folder.
118
+ - `.js` extensions can now be omitted from import specifiers; they're optional.
119
+ - If your import specifier points to a folder, it will attempt to load `index.js` from that folder.
105
120
  - Adds the global `require`, a CommonJS-like synchronous module loading function.
106
121
  - The `require` function is not fully CommonJS-compliant; for instance, `require.main` is not present. `require.resolve` is, though.
107
122
  - Adds `import.meta.require`
108
123
  - 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`.
109
124
  - Adds `import.meta.resolve`
110
125
  - 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`.
111
- - Adds the global object `Module`.
112
- - `instanceof Module` can be used to identify module namespace objects.
126
+ - Module and eval helpers have been moved from "quickjs:std" to the new module "quickjs:module".
127
+ - Makes the module loader's resolution and loading behavior configurable, and allows users to define additional builtin modules
128
+ - The module "quickjs:module" exports an object called `Module`.
113
129
  - You can specify additional implicit import specifier extensions by adding to the `Module.searchExtensions` array.
114
130
  - 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.
115
131
  - You can define custom builtin modules using the `Module.define` function.
@@ -117,32 +133,41 @@ Helper structs, functions, and macros that make it easier to work with QuickJS i
117
133
  - Note that you must handle `Module.searchExtensions` yourself in your replacement implementation.
118
134
  - You can override the method used to load modules by replacing the `Module.read` function.
119
135
  - Note that you must handle `Module.compilers` yourself in your replacement implementation.
120
- - The `eval_*` functions that were duplicated in each of the programs (`eval_buf`, `eval_file`, and `eval_binary`) were moved here
121
- - 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
122
- - 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.
123
-
124
- ## Changes to project organization
136
+ - Makes `import.meta.main` configurable
137
+ - The module "quickjs:module" exports two functions named `setMainModule` and `isMainModule`.
138
+ - 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:module", or `require`.
139
+ - You can use `isMainModule` to check if a given module would be the main module without loading it.
140
+ - Adds a mechanism for identifying module namespace objects
141
+ - The module "quickjs:module" exports an object called `Module`.
142
+ - `something instanceof Module` will be true when `something` is a module namespace object.
143
+ - 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:module"`'s `importModule` always receive the usual module namespace object.
144
+ - Synchronous import function added (`importModule`), which provides the same module record object you would get via dynamic (async) import.
145
+ - JS api for using the engine's configured module name normalization function was added (`resolveModule`).
146
+
147
+ ### Changes to project organization
125
148
 
126
149
  - Stuff is reorganized into separate folders under `src`.
127
150
  - 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).
128
151
  - Line endings have been made consistent and trailing whitespace has been removed
129
152
  - The tests are authored in a new format which leverages jest snapshot testing.
153
+ - Module-loading code in `quickjs-libc` was moved into `quickjs-modulesys` and `quickjs-libmodule`.
154
+ - 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`.
130
155
 
131
- ## More target OSes/runtimes
156
+ ### More target OSes/runtimes
132
157
 
133
158
  We now include support for more platforms, and cross-compilation scripts to build most of those platforms from any platform where Docker is available.
134
159
 
135
160
  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.
136
161
 
137
- ## Library Archives
162
+ ### Library Archives
138
163
 
139
164
  We create `.a` files containing all of quickjs as part of the build.
140
165
 
141
- ## Other changes
166
+ ### Other changes
142
167
 
143
168
  There are also probably some other miscellaneous changes I forgot to write down in the README.
144
169
 
145
- # Compiling
170
+ ## Compiling
146
171
 
147
172
  The repo has stuff set up to compile quickjs binaries for:
148
173
 
@@ -162,7 +187,7 @@ Linux, macOS, iOS, and Windows binaries can be compiled using Docker. Or, you ca
162
187
 
163
188
  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.
164
189
 
165
- ## Compilation Instructions
190
+ ### Compilation Instructions
166
191
 
167
192
  To compile binaries for Linux, macOS, iOS, and Windows (using Docker):
168
193
 
Binary file
Binary file
@@ -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
  *
@@ -129,54 +143,42 @@ declare interface FILE {
129
143
 
130
144
  declare module "quickjs:std" {
131
145
  /**
132
- * Exit the process with the provided status code.
146
+ * Set the exit code that the process should exit with in the future, if it
147
+ * exits normally.
133
148
  *
134
- * @param statusCode The exit code; 0 for success, nonzero for failure.
135
- */
136
- export function exit(statusCode: number): void;
137
-
138
- /**
139
- * Evaluate the string `code` as a script (global eval).
149
+ * Can only be called from the main thread.
140
150
  *
141
- * @param code - The code to evaluate.
142
- * @param options - An optional object containing the following optional properties:
143
- * @property backtraceBarrier - Boolean (default = false). If true, error backtraces do not list the stack frames below the evalScript.
144
- * @property filename - String (default = "<evalScript>"). The filename to associate with the code being executed.
145
- * @returns The result of the evaluation.
146
- */
147
- export function evalScript(
148
- code: string,
149
- options?: { backtraceBarrier?: boolean; filename?: string }
150
- ): any;
151
-
152
- /**
153
- * Evaluate the file `filename` as a script (global eval).
151
+ * This exit code will only be used if the process exits "normally", ie, when
152
+ * there are no more pending JS tasks/listeners. If an unhandled exception is
153
+ * thrown, the process will always exit with status `1`, regardless of the
154
+ * status code passed to `setExitCode`. If someone calls {@link exit} and
155
+ * passes in a status code, that status code will take precedence over the
156
+ * status code passed to `setExitCode`.
154
157
  *
155
- * @param filename - The relative or absolute path to the file to load. Relative paths are resolved relative to the process's current working directory.
156
- * @returns The result of the evaluation.
158
+ * @param statusCode The future exit code; 0 for success, nonzero for failure.
157
159
  */
158
- export function loadScript(filename: string): any;
160
+ export function setExitCode(statusCode: number): void;
159
161
 
160
162
  /**
161
- * Evaluate the file `filename` as a module. Effectively a synchronous dynamic `import()`.
163
+ * Return the exit code that was previously set by {@link setExitCode}, or 0 if
164
+ * it hasn't yet been set.
162
165
  *
163
- * @param filename - The relative or absolute path to the file to import. Relative paths are resolved relative to the file calling `importModule`, or `basename` if present.
164
- * @param basename - If present and `filename` is a relative path, `filename` will be resolved relative to this basename.
165
- * @returns The result of the evaluation (module namespace object).
166
+ * Can only be called from the main thread.
166
167
  */
167
- export function importModule(
168
- filename: string,
169
- basename?: string
170
- ): { [key: string]: any };
168
+ export function getExitCode(): number;
171
169
 
172
170
  /**
173
- * Return the resolved path to a module.
171
+ * Exit the process with the provided status code.
172
+ *
173
+ * Can only be called from the main thread.
174
+ *
175
+ * If `statusCode` is not provided, a value previously passed into
176
+ * {@link setExitCode} will be used. If no value was previously passed into
177
+ * setExitCode, `0` will be used.
174
178
  *
175
- * @param filename - The relative or absolute path to the file to import. Relative paths are resolved relative to the file calling `importModule`, or `basename` if present.
176
- * @param basename - If present and `filename` is a relative path, `filename` will be resolved relative to this basename.
177
- * @returns The resolved module path.
179
+ * @param statusCode The exit code; 0 for success, nonzero for failure.
178
180
  */
179
- export function resolveModule(filename: string, basename?: string): string;
181
+ export function exit(statusCode?: number): never;
180
182
 
181
183
  /**
182
184
  * Load the file `filename` and return it as a string assuming UTF-8 encoding.
@@ -185,15 +187,6 @@ declare module "quickjs:std" {
185
187
  */
186
188
  export function loadFile(filename: string): string;
187
189
 
188
- /**
189
- * Read the script of module filename from an active stack frame, then return it as a string.
190
- *
191
- * If there isn't a valid filename for the specified stack frame, an error will be thrown.
192
- *
193
- * @param stackLevels - How many levels up the stack to search for a filename. Defaults to 0, which uses the current stack frame.
194
- */
195
- export function getFileNameFromStack(stackLevels?: number): string;
196
-
197
190
  /**
198
191
  * Return a boolean indicating whether the provided value is a FILE object.
199
192
  *
@@ -44,6 +44,10 @@ declare module "quickjs:context" {
44
44
  * - Symbol
45
45
  * - eval (but it doesn't work unless the `eval` option is enabled)
46
46
  * - globalThis
47
+ *
48
+ * Note that new contexts don't have a `scriptArgs` global. If you need one
49
+ * to be present in the new context, you can add one onto the Context's
50
+ * `globalThis` property.
47
51
  */
48
52
  constructor(options?: {
49
53
  /** Enables `Date`. Defaults to `true` */
@@ -55,6 +59,9 @@ declare module "quickjs:context" {
55
59
  /** Enables `String.prototype.normalize`. Defaults to `true`. */
56
60
  stringNormalize?: boolean;
57
61
 
62
+ /** Enables `String.dedent`. Defaults to `true`. */
63
+ stringDedent?: boolean;
64
+
58
65
  /** Enables `RegExp`. Defaults to `true`. */
59
66
  regExp?: boolean;
60
67
 
@@ -121,32 +128,22 @@ declare module "quickjs:context" {
121
128
  */
122
129
  operators?: boolean;
123
130
 
124
- /** Enables "use math". Defaults to `true`. */
131
+ /** Enables `"use math"`. Defaults to `true`. */
125
132
  useMath?: boolean;
126
133
 
134
+ /** Enables `inspect`. Defaults to `true`. */
135
+ inspect?: boolean;
136
+ /** Enables `console`. Defaults to `true`. */
137
+ console?: boolean;
138
+ /** Enables `print`. Defaults to `true`. */
139
+ print?: boolean;
140
+ /** Enables `require` and `Module`. Defaults to `true`. */
141
+ moduleGlobals?: boolean;
127
142
  /**
128
- * Enables:
129
- *
130
- * - inspect
131
- * - console
132
- * - print
133
- * - require (and require.resolve)
134
- * - setTimeout
135
- * - clearTimeout
136
- * - setInterval
137
- * - clearInterval
138
- * - String.cooked
139
- * - String.dedent
140
- *
141
- * Defaults to `true`.
142
- *
143
- * NOTE: The following globals, normally part of `js_std_add_helpers`, are NEVER added:
144
- *
145
- * - scriptArgs
146
- *
147
- * If you need them in the new context, copy them over from your context's globalThis onto the child context's globalThis.
143
+ * Enables `setTimeout`, `clearTimeout`, `setInterval`, and
144
+ * `clearInterval`. Defaults to `true`.
148
145
  */
149
- stdHelpers?: boolean;
146
+ timers?: boolean;
150
147
 
151
148
  /** Enable builtin modules. */
152
149
  modules?: {
@@ -158,6 +155,8 @@ declare module "quickjs:context" {
158
155
  "quickjs:bytecode"?: boolean;
159
156
  /** Enables the "quickjs:context" module. Defaults to `true`. */
160
157
  "quickjs:context"?: boolean;
158
+ /** Enables the "quickjs:module" module. Defaults to `true`. */
159
+ "quickjs:module"?: boolean;
161
160
  };
162
161
  });
163
162
 
@@ -0,0 +1,81 @@
1
+ declare module "quickjs:module" {
2
+ /**
3
+ * Return whether the provided resolved module path is set as the main module.
4
+ *
5
+ * In other words, return what the value of `import.meta.main` would be within
6
+ * the module.
7
+ *
8
+ * The main module can be set via {@link setMainModule}.
9
+ */
10
+ export function isMainModule(resolvedFilepath: string): boolean;
11
+
12
+ /**
13
+ * Set the main module to the module with the provided resolved path.
14
+ *
15
+ * This will affect the value of `import.meta.main` for modules loaded in the
16
+ * future, but it will NOT retroactively change the value of
17
+ * `import.meta.main` in existing already-loaded modules.
18
+ */
19
+ export function setMainModule(resolvedFilepath: string): void;
20
+
21
+ /**
22
+ * Evaluate the string `code` as a script (global eval).
23
+ *
24
+ * @param code - The code to evaluate.
25
+ * @param options - An optional object containing the following optional properties:
26
+ * @property backtraceBarrier - Boolean (default = false). If true, error backtraces do not list the stack frames below the evalScript.
27
+ * @property filename - String (default = "<evalScript>"). The filename to associate with the code being executed.
28
+ * @returns The result of the evaluation.
29
+ */
30
+ export function evalScript(
31
+ code: string,
32
+ options?: { backtraceBarrier?: boolean; filename?: string }
33
+ ): any;
34
+
35
+ /**
36
+ * Evaluate the file `filename` as a script (global eval).
37
+ *
38
+ * @param filename - The relative or absolute path to the file to load. Relative paths are resolved relative to the process's current working directory.
39
+ * @returns The result of the evaluation.
40
+ */
41
+ export function runScript(filename: string): any;
42
+
43
+ /**
44
+ * Evaluate the file `filename` as a module. Effectively a synchronous dynamic `import()`.
45
+ *
46
+ * @param filename - The relative or absolute path to the file to import. Relative paths are resolved relative to the file calling `importModule`, or `basename` if present.
47
+ * @param basename - If present and `filename` is a relative path, `filename` will be resolved relative to this basename.
48
+ * @returns The result of the evaluation (module namespace object).
49
+ */
50
+ export function importModule(
51
+ filename: string,
52
+ basename?: string
53
+ ): { [key: string]: any };
54
+
55
+ /**
56
+ * Return the resolved path to a module.
57
+ *
58
+ * @param filename - The relative or absolute path to the file to import. Relative paths are resolved relative to the file calling `importModule`, or `basename` if present.
59
+ * @param basename - If present and `filename` is a relative path, `filename` will be resolved relative to this basename.
60
+ * @returns The resolved module path.
61
+ */
62
+ export function resolveModule(filename: string, basename?: string): string;
63
+
64
+ /**
65
+ * Read the script of module filename from an active stack frame, then return it as a string.
66
+ *
67
+ * If there isn't a valid filename for the specified stack frame, an error will be thrown.
68
+ *
69
+ * @param stackLevels - How many levels up the stack to search for a filename. Defaults to 0, which uses the current stack frame.
70
+ */
71
+ export function getFileNameFromStack(stackLevels?: number): string;
72
+
73
+ /**
74
+ * An object which lets you configure the module loader (import/export/require).
75
+ * You can use these properties to add support for importing new filetypes.
76
+ *
77
+ * This object can also be used to identify whether an object is a module
78
+ * namespace record.
79
+ */
80
+ export const Module: Module;
81
+ }
@@ -1,11 +1,11 @@
1
1
  /**
2
- * A global which lets you configure the module loader (import/export/require).
2
+ * An object which lets you configure the module loader (import/export/require).
3
3
  * You can use these properties to add support for importing new filetypes.
4
4
  *
5
- * This global can also be used to identify whether an object is a module
5
+ * This object can also be used to identify whether an object is a module
6
6
  * namespace record.
7
7
  */
8
- interface ModuleGlobal {
8
+ interface Module {
9
9
  /**
10
10
  * Returns true if `target` is a module namespace object.
11
11
  */
@@ -24,7 +24,7 @@ interface ModuleGlobal {
24
24
  * See the doc comment on {@link require} for more information.
25
25
  *
26
26
  * NOTE: If you add a new extension to this array, you will likely also want
27
- * to add to {@link Module.compilers}.
27
+ * to add to {@link compilers}.
28
28
  */
29
29
  searchExtensions: Array<string>;
30
30
 
@@ -65,7 +65,7 @@ interface ModuleGlobal {
65
65
  * And `names` will be a string containing the contents of names.txt.
66
66
  *
67
67
  * NOTE: When adding to this object, you may also wish to add to
68
- * {@link Module.searchExtensions}.
68
+ * {@link searchExtensions}.
69
69
  */
70
70
  compilers: {
71
71
  [extensionWithDot: string]: (filename: string, content: string) => string;
@@ -78,7 +78,8 @@ interface ModuleGlobal {
78
78
  define(name: string, obj: { [key: string]: any }): void;
79
79
 
80
80
  /**
81
- * Resolves a require/import request from `fromFile` into a canonicalized path.
81
+ * Resolves a require/import request from `fromFile` into a canonicalized
82
+ * path.
82
83
  *
83
84
  * To change native module resolution behavior, replace this function with
84
85
  * your own implementation. Note that you must handle
@@ -96,9 +97,6 @@ interface ModuleGlobal {
96
97
  read(modulePath: string): string;
97
98
  }
98
99
 
99
- // global added by QJMS_AddModuleGlobal
100
- declare var Module: ModuleGlobal;
101
-
102
100
  interface RequireFunction {
103
101
  /**
104
102
  * Synchronously import a module.
@@ -125,8 +123,8 @@ interface RequireFunction {
125
123
  * ```
126
124
  *
127
125
  * then the engine will look for `somewhere.js`. If that doesn't exist, the
128
- * engine will look for `somewhere/index.js`. If *that* doesn't exist, an error
129
- * will be thrown.
126
+ * engine will look for `somewhere/index.js`. If *that* doesn't exist, an
127
+ * error will be thrown.
130
128
  *
131
129
  * If you add more extensions to `Module.searchExtensions`, then the engine
132
130
  * will use those, too. It will search in the same order as the strings appear
@@ -140,7 +138,7 @@ interface RequireFunction {
140
138
  resolve: (source: string) => string;
141
139
  }
142
140
 
143
- // global added by QJMS_AddRequireGlobal
141
+ // global added by QJMS_InitContext
144
142
  declare var require: RequireFunction;
145
143
 
146
144
  // gets set per-module by QJMS_SetModuleImportMeta
Binary file
Binary file
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@suchipi/quickjs",
3
3
  "description": "Suchipi's QuickJS Fork",
4
- "version": "0.4.2",
4
+ "version": "0.6.0",
5
5
  "main": "./npm/index.js",
6
6
  "bin": {
7
7
  "qjs": "npm/cli/qjs.js",
package/.gitmodules DELETED
@@ -1,6 +0,0 @@
1
- [submodule "src/run-test262/test262"]
2
- path = src/run-test262/test262
3
- url = https://github.com/tc39/test262.git
4
- [submodule "src/run-test262/test262o"]
5
- path = src/run-test262/test262o
6
- url = https://github.com/tc39/test262.git