@earendil-works/djs 0.0.0-stage → 0.1.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.
- package/LICENSE-APACHE +202 -0
- package/LICENSE-MIT +21 -0
- package/README.md +88 -2
- package/dist/base.d.ts +33 -0
- package/dist/base.js +71 -0
- package/dist/dump-runtime.d.ts +16 -0
- package/dist/dump-runtime.js +107 -0
- package/dist/dump.d.ts +10 -0
- package/dist/dump.js +21 -0
- package/dist/env.d.ts +5 -0
- package/dist/env.js +17 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +21 -0
- package/dist/layout.generated.d.ts +921 -0
- package/dist/layout.generated.js +442 -0
- package/dist/reader.d.ts +47 -0
- package/dist/reader.js +599 -0
- package/dist/sandbox.d.ts +756 -0
- package/dist/sandbox.js +1958 -0
- package/dist/slim.d.ts +10 -0
- package/dist/slim.js +21 -0
- package/dist/trap.d.ts +16 -0
- package/dist/trap.js +23 -0
- package/dist/wire.d.ts +46 -0
- package/dist/wire.js +373 -0
- package/package.json +53 -4
- package/wasm/djs-dump.wasm +0 -0
- package/wasm/djs-slim.wasm +0 -0
- package/wasm/djs.wasm +0 -0
package/LICENSE-APACHE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/LICENSE-MIT
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Earendil Inc.
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,89 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @earendil-works/djs
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
djs is a small JavaScript and TypeScript engine compiled to WebAssembly, made for running
|
|
4
|
+
untrusted scripts inside a host: agent tool scripts, plugins, user code. Every sandbox is
|
|
5
|
+
its own wasm instance with its own heap, so sandboxes share nothing. Scripts get only the
|
|
6
|
+
host functions you give them, run under a fuel budget and a memory cap, and a run that
|
|
7
|
+
waits on the host can be suspended into a dump, a plain JavaScript file, and continued
|
|
8
|
+
later in another process.
|
|
9
|
+
|
|
10
|
+
The module has no imports and the package no dependencies: it runs in node (20 or newer),
|
|
11
|
+
Bun, Deno, browsers and web workers.
|
|
12
|
+
|
|
13
|
+
## Install
|
|
14
|
+
|
|
15
|
+
```sh
|
|
16
|
+
npm install @earendil-works/djs
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Use
|
|
20
|
+
|
|
21
|
+
```js
|
|
22
|
+
import { Sandbox } from "@earendil-works/djs";
|
|
23
|
+
|
|
24
|
+
const sandbox = await Sandbox.create({
|
|
25
|
+
asyncFunctions: { "tools.search": async (q) => search(q) },
|
|
26
|
+
maxFuel: 50_000_000,
|
|
27
|
+
memoryLimit: 64 << 20,
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
// A script is the body of an async function: its parameters are the keys of the second
|
|
31
|
+
// argument, and `await` and `return` work at the top level.
|
|
32
|
+
const hits = await sandbox.runAsync("return (await tools.search(query)).slice(0, 3)", { query: "djs" });
|
|
33
|
+
sandbox.run("return a + b", { a: 40, b: 2 }); // 42
|
|
34
|
+
sandbox.dispose();
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
Options include `typescript: true` (scripts are TypeScript), `console` (where
|
|
38
|
+
`console.log` goes), `globals`, `deterministic` (repeatable runs) and `frozenBuiltins`.
|
|
39
|
+
Errors are `SandboxError`s with a `kind`: `threw`, `syntax`, `fuel`, `memory`, ...
|
|
40
|
+
|
|
41
|
+
## Entry points
|
|
42
|
+
|
|
43
|
+
| import | module | for |
|
|
44
|
+
| --- | --- | --- |
|
|
45
|
+
| `@earendil-works/djs` | `wasm/djs.wasm` | every host |
|
|
46
|
+
| `@earendil-works/djs/slim` | `wasm/djs-slim.wasm` | hosts that count kilobytes: no typed arrays, BigInt, iterator helpers and other families scripts rarely use |
|
|
47
|
+
| `@earendil-works/djs/dump` | `wasm/djs-dump.wasm` | hosts that suspend runs into dumps: the main module plus the dumper |
|
|
48
|
+
| `@earendil-works/djs/dump-runtime` | | the helpers a dump needs to continue in a JavaScript engine other than djs |
|
|
49
|
+
|
|
50
|
+
Each entry point exports the same API, with its own module as the default. To dump runs
|
|
51
|
+
while running them in the smaller main module, give the sandbox the dump module as its
|
|
52
|
+
`dumpModule`:
|
|
53
|
+
|
|
54
|
+
```js
|
|
55
|
+
import { Sandbox } from "@earendil-works/djs";
|
|
56
|
+
import { loadModule as loadDumpModule } from "@earendil-works/djs/dump";
|
|
57
|
+
|
|
58
|
+
const sandbox = await Sandbox.create({ frozenBuiltins: true, dumpModule: await loadDumpModule(), asyncFunctions });
|
|
59
|
+
const task = sandbox.start(code, params);
|
|
60
|
+
const dump = await task.suspend(); // later, anywhere: (await Sandbox.resume(dump, options)).result
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
## Loading the module
|
|
64
|
+
|
|
65
|
+
`Sandbox.create` loads its entry point's module once per process and caches it;
|
|
66
|
+
`loadModule(location)` loads another one and `new Sandbox({ module })` uses it. In node,
|
|
67
|
+
Bun and Deno the module is read from the package's `wasm/` directory (`$DJS_WASM` names
|
|
68
|
+
another main module). In a browser it is fetched from the URL
|
|
69
|
+
`new URL("../wasm/djs.wasm", import.meta.url)`, a pattern that webpack and Vite recognize
|
|
70
|
+
and copy into the build. In Vite's dev server, exclude the package from dependency
|
|
71
|
+
pre-bundling, which would move the code away from its modules:
|
|
72
|
+
|
|
73
|
+
```js
|
|
74
|
+
// vite.config.js
|
|
75
|
+
export default { optimizeDeps: { exclude: ["@earendil-works/djs"] } };
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
In Deno, allow reading the package's directory (`--allow-read`). With a bundler that does
|
|
79
|
+
not know the pattern, serve the `.wasm` file yourself and pass its URL:
|
|
80
|
+
`Sandbox.create({ module: await loadModule("/assets/djs.wasm") })`.
|
|
81
|
+
|
|
82
|
+
## Documentation
|
|
83
|
+
|
|
84
|
+
The guide, with the whole API, the language djs runs, limits, determinism and dumps:
|
|
85
|
+
https://earendil-works.github.io/djs/js-api.html
|
|
86
|
+
|
|
87
|
+
## License
|
|
88
|
+
|
|
89
|
+
MIT or Apache-2.0, at your option.
|
package/dist/base.d.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/** The exports of an instance that `provideBase` and `rememberBase` use. */
|
|
2
|
+
export interface BaseExports {
|
|
3
|
+
memory: WebAssembly.Memory;
|
|
4
|
+
djs_base_ptr(): number;
|
|
5
|
+
djs_base_len(): number;
|
|
6
|
+
djs_base_inject_ptr?(len: number): number;
|
|
7
|
+
djs_base_inject?(): number;
|
|
8
|
+
djs_base_coded_ptr?(len: number): number;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Hands a fresh instance of `module` its coded base, the module's custom section
|
|
12
|
+
* `djs_base` (D2541): the module keeps it out of its linear memory, and an instance
|
|
13
|
+
* decodes it when it needs the base and has none injected. `provideBase` and
|
|
14
|
+
* `rememberBase` call it; a host that drives the exports itself calls it right after
|
|
15
|
+
* instantiating, before anything creates a VM or reads the base. Returns whether the
|
|
16
|
+
* instance took the bytes (false if it has its base, or the module has no such section).
|
|
17
|
+
*/
|
|
18
|
+
export declare function provideCodedBase(module: WebAssembly.Module, exports: BaseExports): boolean;
|
|
19
|
+
/**
|
|
20
|
+
* Gives a fresh instance of `module` the base snapshot an earlier instance of it decoded
|
|
21
|
+
* (D2252): the module ships the base coded, and an instance decodes it at its first use
|
|
22
|
+
* (a few ms) unless the host provides it. Call right after instantiating, before anything
|
|
23
|
+
* that creates a VM or reads the base; and `rememberBase` once a VM exists, so that later
|
|
24
|
+
* instances get it. The instance checks the bytes (length and hash). Returns whether the
|
|
25
|
+
* instance took them; if not (refused, or no instance remembered the base yet), it gets
|
|
26
|
+
* the coded base to decode its own (`provideCodedBase`).
|
|
27
|
+
*/
|
|
28
|
+
export declare function provideBase(module: WebAssembly.Module, exports: BaseExports): boolean;
|
|
29
|
+
/**
|
|
30
|
+
* Keeps the base of `module` (from this instance, once per module) for `provideBase`. Call
|
|
31
|
+
* before the instance's first `vm_new`, which takes the bytes as its arena (D2060).
|
|
32
|
+
*/
|
|
33
|
+
export declare function rememberBase(module: WebAssembly.Module, exports: BaseExports): void;
|
package/dist/base.js
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
// The module's base snapshot (D1321) on the host side: the module ships it coded in its
|
|
2
|
+
// custom section `djs_base` (D2252, D2541), every instance needs it decoded before its
|
|
3
|
+
// first VM, and decoding takes a few ms. So the host hands each fresh instance the coded
|
|
4
|
+
// section, or the bytes an earlier instance of the same module decoded (checked by the
|
|
5
|
+
// instance), and keeps those bytes once per module.
|
|
6
|
+
/** The decoded base of each module, as an earlier instance of it decoded it. */
|
|
7
|
+
const bases = new WeakMap();
|
|
8
|
+
const codedSections = new WeakMap();
|
|
9
|
+
/**
|
|
10
|
+
* Hands a fresh instance of `module` its coded base, the module's custom section
|
|
11
|
+
* `djs_base` (D2541): the module keeps it out of its linear memory, and an instance
|
|
12
|
+
* decodes it when it needs the base and has none injected. `provideBase` and
|
|
13
|
+
* `rememberBase` call it; a host that drives the exports itself calls it right after
|
|
14
|
+
* instantiating, before anything creates a VM or reads the base. Returns whether the
|
|
15
|
+
* instance took the bytes (false if it has its base, or the module has no such section).
|
|
16
|
+
*/
|
|
17
|
+
export function provideCodedBase(module, exports) {
|
|
18
|
+
if (exports.djs_base_coded_ptr === undefined)
|
|
19
|
+
return false;
|
|
20
|
+
let coded = codedSections.get(module);
|
|
21
|
+
if (coded === undefined) {
|
|
22
|
+
const [section] = WebAssembly.Module.customSections(module, "djs_base");
|
|
23
|
+
if (section === undefined)
|
|
24
|
+
return false;
|
|
25
|
+
coded = new Uint8Array(section);
|
|
26
|
+
codedSections.set(module, coded);
|
|
27
|
+
}
|
|
28
|
+
const at = exports.djs_base_coded_ptr(coded.length);
|
|
29
|
+
if (at === 0)
|
|
30
|
+
return false;
|
|
31
|
+
new Uint8Array(exports.memory.buffer, at, coded.length).set(coded);
|
|
32
|
+
return true;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Gives a fresh instance of `module` the base snapshot an earlier instance of it decoded
|
|
36
|
+
* (D2252): the module ships the base coded, and an instance decodes it at its first use
|
|
37
|
+
* (a few ms) unless the host provides it. Call right after instantiating, before anything
|
|
38
|
+
* that creates a VM or reads the base; and `rememberBase` once a VM exists, so that later
|
|
39
|
+
* instances get it. The instance checks the bytes (length and hash). Returns whether the
|
|
40
|
+
* instance took them; if not (refused, or no instance remembered the base yet), it gets
|
|
41
|
+
* the coded base to decode its own (`provideCodedBase`).
|
|
42
|
+
*/
|
|
43
|
+
export function provideBase(module, exports) {
|
|
44
|
+
const bytes = bases.get(module);
|
|
45
|
+
if (bytes !== undefined && exports.djs_base_inject_ptr !== undefined && exports.djs_base_inject !== undefined) {
|
|
46
|
+
const at = exports.djs_base_inject_ptr(bytes.length);
|
|
47
|
+
if (at !== 0) {
|
|
48
|
+
new Uint8Array(exports.memory.buffer, at, bytes.length).set(bytes);
|
|
49
|
+
if (exports.djs_base_inject() === 1)
|
|
50
|
+
return true;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
// The instance decodes its own, from the coded base.
|
|
54
|
+
provideCodedBase(module, exports);
|
|
55
|
+
return false;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Keeps the base of `module` (from this instance, once per module) for `provideBase`. Call
|
|
59
|
+
* before the instance's first `vm_new`, which takes the bytes as its arena (D2060).
|
|
60
|
+
*/
|
|
61
|
+
export function rememberBase(module, exports) {
|
|
62
|
+
if (bases.has(module))
|
|
63
|
+
return;
|
|
64
|
+
provideCodedBase(module, exports);
|
|
65
|
+
// The pointer first: an instance decodes its base at the first use (D2252), which may
|
|
66
|
+
// grow (and detach) its memory buffer.
|
|
67
|
+
const at = exports.djs_base_ptr();
|
|
68
|
+
if (at === 0)
|
|
69
|
+
throw new Error("djs-wasm has no base snapshot (no djs_base section?)");
|
|
70
|
+
bases.set(module, new Uint8Array(exports.memory.buffer, at, exports.djs_base_len()).slice());
|
|
71
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
// The declarations of dump-runtime.js (generated from Rust, D2834): what a host uses. The
|
|
2
|
+
// single helpers stay undeclared: a dump reaches them through `$djs`, a host through
|
|
3
|
+
// `helpers`.
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* The helpers a plain dump calls as `$djs.NAME`, by name. To continue a dump outside djs:
|
|
7
|
+
* `globalThis.$djs = { ...helpers, seed, resume, source, host }`, run the dump as a Script,
|
|
8
|
+
* then delete `$djs` and `$dump`.
|
|
9
|
+
*/
|
|
10
|
+
export declare const helpers: Readonly<Record<string, (...args: any[]) => any>>;
|
|
11
|
+
|
|
12
|
+
/** The same as a Script whose completion value is `helpers`. */
|
|
13
|
+
export declare const script: string;
|
|
14
|
+
|
|
15
|
+
/** `script` with only the helpers `dump` names and the ones those name. */
|
|
16
|
+
export declare function scriptFor(dump: string): string;
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
// The plain dump runtime (D2834, D2903): the helpers a plain dump calls as `$djs.NAME`.
|
|
2
|
+
// To run a dump outside djs, set `globalThis.$djs` to these and the host's hooks
|
|
3
|
+
// (`seed`, `resume`, `source`, `host`; see docs/plain-js-dumps.md, "The host interface"), then run the
|
|
4
|
+
// dump as a Script, and delete `$djs` and `$dump` after it. Generated from crates/djs-core/src/vm/dump_runtime.rs:
|
|
5
|
+
// DJS_UPDATE_DUMP_RUNTIME=1 cargo test -p djs-core dump_runtime
|
|
6
|
+
|
|
7
|
+
const matchAll = RegExp.prototype[Symbol.matchAll];
|
|
8
|
+
export const R = Reflect.apply;
|
|
9
|
+
export const G = Reflect.get;
|
|
10
|
+
export const C = Reflect.construct;
|
|
11
|
+
export const D = (o, k, v) => Object.defineProperty(o, k, { value: v, writable: true, enumerable: true, configurable: true });
|
|
12
|
+
export const A = Object.assign;
|
|
13
|
+
export const L = (it, next) => { let first = true; const r = { next() { if (first) { first = false; return { value: undefined, done: false }; } return Reflect.apply(next ?? it.next, it, []); }, get return() { const m = it.return; return m == null ? m : (...v) => Reflect.apply(m, it, v); }, set(i, n) { it = i; next = n; }, [Symbol.iterator]() { return r; } }; return r; };
|
|
14
|
+
export const I = (it, next) => { const r = { next() { return Reflect.apply(next ?? it.next, it, []); }, get return() { const m = it.return; return m == null ? m : (...v) => Reflect.apply(m, it, v); }, [Symbol.iterator]() { return r; } }; return r; };
|
|
15
|
+
export const E = () => ({ [Symbol.iterator]() { return { next() { return { value: undefined, done: true }; } }; } });
|
|
16
|
+
export const Y = (it, next, a, ...first) => { const r2 = a > 1; let ph = r2 || !first.length; const m = (k) => { if (r2 && k == 'return' && first.length) { const p = first.pop(); return () => p; } const f = it[k]; return f == null ? f : (...v) => Reflect.apply(f, it, v); }; const r = { next(...v) { if (!r2 && first.length) return first.pop(); if (ph) { ph = false; return { value: undefined, done: false }; } return Reflect.apply(next, it, v); }, get throw() { return m('throw'); }, get return() { return m('return'); }, [a ? Symbol.asyncIterator : Symbol.iterator]() { return r; } }; return r; };
|
|
17
|
+
export const DD = (o, src) => { for (const k of Reflect.ownKeys(src)) { const d = Reflect.getOwnPropertyDescriptor(src, k); if ('get' in d) { if (d.get === undefined) delete d.get; if (d.set === undefined) delete d.set; } Object.defineProperty(o, k, d); } return o; };
|
|
18
|
+
export const U = ($djs_v, $djs_m, k) => k === 4 ? { [Symbol.asyncDispose]: () => Reflect.apply($djs_m, $djs_v, []) } : { [Symbol.dispose]: () => Reflect.apply($djs_m, $djs_v, []) };
|
|
19
|
+
export const M = ($djs_f, $djs_k) => (x, i) => Reflect.apply($djs_f, undefined, [x, i + $djs_k]);
|
|
20
|
+
export const FM = (it, next, $djs_f, k, inner, innerNext) => { const $djs_k = k - 1, $djs_in = { [Symbol.iterator]: () => L(inner, innerNext) }; const h = Iterator.prototype.flatMap.call(L(it, next), (x, i) => i === 0 ? $djs_in : Reflect.apply($djs_f, undefined, [x, i + $djs_k])); h.next(); return h; };
|
|
21
|
+
export const CC = (cur, curNext, items) => { const h = Iterator.concat({ [Symbol.iterator]: () => L(cur, curNext) }, ...items); h.next(); return h; };
|
|
22
|
+
export const CI = ($djs_m, $djs_o) => ({ [Symbol.iterator]: () => Reflect.apply($djs_m, $djs_o, []) });
|
|
23
|
+
export const HD = () => { const h = Iterator.prototype.map.call({ next: () => ({ value: undefined, done: true }) }, (x) => x); h.next(); return h; };
|
|
24
|
+
export const P = (k) => Reflect.ownKeys({ [k]: 0 })[0];
|
|
25
|
+
export const S = (o, ex) => { const r = {}, f = Object(o), ks = Reflect.ownKeys(f); for (let i = 0; i < ks.length; i++) { const k = ks[i]; let skip = false; for (let j = 0; j < ex.length; j++) if (ex[j] === k) skip = true; if (skip) continue; const d = Reflect.getOwnPropertyDescriptor(f, k); if (d && d.enumerable) Object.defineProperty(r, k, { value: f[k], writable: true, enumerable: true, configurable: true }); } return r; };
|
|
26
|
+
export const N = (it, next, ...first) => { const r = { next() { return first.length ? first.pop() : Reflect.apply(next, it, []); }, get return() { const m = it.return; return m == null ? m : (...v) => Reflect.apply(m, it, v); }, [Symbol.asyncIterator]() { return r; } }; return r; };
|
|
27
|
+
export const X = (it) => { let p; try { const m = it.return; if (m == null) return; p = Promise.resolve(Reflect.apply(m, it, [])); } catch { return; } return p.then(() => {}, () => {}); };
|
|
28
|
+
export const O = (r) => { if (Object(r) !== r) throw new TypeError('iterator must return an object'); };
|
|
29
|
+
export const fromSync = (it, next) => { const q = () => { try { const m = it.return; if (m != null) Reflect.apply(m, it, []); } catch {} }; const step = (f, close) => new Promise((res, rej) => { const r = f(); if (Object(r) !== r) throw new TypeError('iterator must return an object'); const done = !!r.done; let w; try { w = Promise.resolve(r.value); } catch (e) { if (!done && close) q(); throw e; } w.then((v) => res({ value: v, done }), !done && close ? (e) => { q(); rej(e); } : rej); }); return { next: (...a) => step(() => Reflect.apply(next, it, a), true), return: (...a) => { const m = it.return; return m == null ? Promise.resolve({ value: a[0], done: true }) : step(() => Reflect.apply(m, it, a), false); }, throw: (...a) => { const m = it.throw; if (m != null) return step(() => Reflect.apply(m, it, a), true); try { const c = it.return; if (c != null) { const r = Reflect.apply(c, it, []); if (Object(r) !== r) throw new TypeError('iterator must return an object'); } throw new TypeError('throw is not a method'); } catch (e) { return Promise.reject(e); } } }; };
|
|
30
|
+
export const Q = (a, ...v) => { let n = a.length; for (const x of v) Object.defineProperty(a, n++, { value: x, writable: true, enumerable: true, configurable: true }); return a; };
|
|
31
|
+
export const regexpIterator = (re, s, f) => { const it = Reflect.apply(matchAll, re, [s]); if (f & 4) while (!it.next().done); return it; };
|
|
32
|
+
export const closeThrow = (it) => (e) => { try { const m = it.return; if (m != null) Reflect.apply(m, it, []); } catch {} throw e; };
|
|
33
|
+
export const forIn = (o, keys) => { let i = 0; const r = { next() { while (i < keys.length) { const k = keys[i++]; if (o == null || k in o) return { value: k, done: false }; } return { value: undefined, done: true }; }, [Symbol.iterator]() { return r; } }; return r; };
|
|
34
|
+
export const advance = (it, n) => { const next = it.next; for (let i = 0; i < n; i++) Reflect.apply(next, it, []); return it; };
|
|
35
|
+
export const fn = (o) => { const d = Object.getOwnPropertyDescriptor(o, Reflect.ownKeys(o)[0]); return 'value' in d ? d.value : d.get ?? d.set; };
|
|
36
|
+
export const spent = (k) => { let f; new Promise((a, b) => { f = k ? b : a; a(); }); return f; };
|
|
37
|
+
export const rejected = (e) => { const p = Promise.reject(e); p.then(undefined, () => {}); return p; };
|
|
38
|
+
export const fromAsync = (p, mapfn, thisArg, it, next, A, k, phase, items, len, error) => new Promise((resolve, reject) => { const put = (v) => { if (!Reflect.defineProperty(A, k, { value: v, writable: true, enumerable: true, configurable: true })) throw new TypeError('cannot define the element'); }; const wait = (v, ph) => { phase = ph; Promise.prototype.then.call(Promise.resolve(v), ok, bad); }; const close = (e) => { error = e; try { const m = it.return; if (m == null) { reject(e); return; } wait(Reflect.apply(m, it, []), 3); } catch { reject(error); } }; const step = () => { try { if (k >= 9007199254740991) { close(new TypeError('Array too long')); return; } wait(Reflect.apply(next, it, []), 1); } catch (e) { reject(e); } }; const store = (v) => { try { put(v); } catch (e) { close(e); return; } k++; step(); }; const result = (r) => { let v; try { if (Object(r) !== r) throw new TypeError('iterator must return an object'); if (r.done) { A.length = k; resolve(A); return; } v = r.value; } catch (e) { reject(e); return; } if (mapfn === undefined) { store(v); return; } try { wait(Reflect.apply(mapfn, thisArg, [v, k]), 2); } catch (e) { close(e); } }; const element = () => { try { if (k < len) { wait(items[k], 4); return; } A.length = len; resolve(A); } catch (e) { reject(e); } }; const value = (v) => { try { if (phase === 4 && mapfn !== undefined) { wait(Reflect.apply(mapfn, thisArg, [v, k]), 5); return; } put(v); k++; } catch (e) { reject(e); return; } element(); }; const ok = (v) => { if (phase === 1) result(v); else if (phase === 2) store(v); else if (phase === 3) reject(error); else value(v); }; const bad = (e) => { if (phase === 2) close(e); else if (phase === 3) reject(error); else reject(e); }; Promise.prototype.then.call(p, ok, bad); });
|
|
39
|
+
export const bytes = (b) => Uint8Array.fromBase64(b).buffer;
|
|
40
|
+
export const k = (K, f, at, r, h, p) => { Object.defineProperty(K, '$djs', { value: [f, at, r, h] }); if (p !== undefined) K.prototype = p; else if ('prototype' in K) K.prototype = f.prototype; return K; };
|
|
41
|
+
export const keep = (o, keys) => { for (const k of Reflect.ownKeys(o)) if (!['length', 'name', 'prototype', 'constructor'].includes(k) && !keys.includes(k) && !delete o[k]) throw new TypeError('keep'); };
|
|
42
|
+
|
|
43
|
+
/** The helpers by name: `globalThis.$djs = { ...helpers, seed, resume }`. */
|
|
44
|
+
export const helpers = { R, G, C, D, A, L, I, E, Y, DD, U, M, FM, CC, CI, HD, P, S, N, X, O, fromSync, Q, regexpIterator, closeThrow, forIn, advance, fn, spent, rejected, fromAsync, bytes, k, keep };
|
|
45
|
+
|
|
46
|
+
/** The same as a Script whose completion value is `helpers`, for a host that evaluates it in
|
|
47
|
+
* a djs VM itself (`vm_dump_runtime`). */
|
|
48
|
+
export const script = `"use strict";
|
|
49
|
+
(() => {
|
|
50
|
+
const matchAll = RegExp.prototype[Symbol.matchAll];
|
|
51
|
+
const R = Reflect.apply;
|
|
52
|
+
const G = Reflect.get;
|
|
53
|
+
const C = Reflect.construct;
|
|
54
|
+
const D = (o, k, v) => Object.defineProperty(o, k, { value: v, writable: true, enumerable: true, configurable: true });
|
|
55
|
+
const A = Object.assign;
|
|
56
|
+
const L = (it, next) => { let first = true; const r = { next() { if (first) { first = false; return { value: undefined, done: false }; } return Reflect.apply(next ?? it.next, it, []); }, get return() { const m = it.return; return m == null ? m : (...v) => Reflect.apply(m, it, v); }, set(i, n) { it = i; next = n; }, [Symbol.iterator]() { return r; } }; return r; };
|
|
57
|
+
const I = (it, next) => { const r = { next() { return Reflect.apply(next ?? it.next, it, []); }, get return() { const m = it.return; return m == null ? m : (...v) => Reflect.apply(m, it, v); }, [Symbol.iterator]() { return r; } }; return r; };
|
|
58
|
+
const E = () => ({ [Symbol.iterator]() { return { next() { return { value: undefined, done: true }; } }; } });
|
|
59
|
+
const Y = (it, next, a, ...first) => { const r2 = a > 1; let ph = r2 || !first.length; const m = (k) => { if (r2 && k == 'return' && first.length) { const p = first.pop(); return () => p; } const f = it[k]; return f == null ? f : (...v) => Reflect.apply(f, it, v); }; const r = { next(...v) { if (!r2 && first.length) return first.pop(); if (ph) { ph = false; return { value: undefined, done: false }; } return Reflect.apply(next, it, v); }, get throw() { return m('throw'); }, get return() { return m('return'); }, [a ? Symbol.asyncIterator : Symbol.iterator]() { return r; } }; return r; };
|
|
60
|
+
const DD = (o, src) => { for (const k of Reflect.ownKeys(src)) { const d = Reflect.getOwnPropertyDescriptor(src, k); if ('get' in d) { if (d.get === undefined) delete d.get; if (d.set === undefined) delete d.set; } Object.defineProperty(o, k, d); } return o; };
|
|
61
|
+
const U = ($djs_v, $djs_m, k) => k === 4 ? { [Symbol.asyncDispose]: () => Reflect.apply($djs_m, $djs_v, []) } : { [Symbol.dispose]: () => Reflect.apply($djs_m, $djs_v, []) };
|
|
62
|
+
const M = ($djs_f, $djs_k) => (x, i) => Reflect.apply($djs_f, undefined, [x, i + $djs_k]);
|
|
63
|
+
const FM = (it, next, $djs_f, k, inner, innerNext) => { const $djs_k = k - 1, $djs_in = { [Symbol.iterator]: () => L(inner, innerNext) }; const h = Iterator.prototype.flatMap.call(L(it, next), (x, i) => i === 0 ? $djs_in : Reflect.apply($djs_f, undefined, [x, i + $djs_k])); h.next(); return h; };
|
|
64
|
+
const CC = (cur, curNext, items) => { const h = Iterator.concat({ [Symbol.iterator]: () => L(cur, curNext) }, ...items); h.next(); return h; };
|
|
65
|
+
const CI = ($djs_m, $djs_o) => ({ [Symbol.iterator]: () => Reflect.apply($djs_m, $djs_o, []) });
|
|
66
|
+
const HD = () => { const h = Iterator.prototype.map.call({ next: () => ({ value: undefined, done: true }) }, (x) => x); h.next(); return h; };
|
|
67
|
+
const P = (k) => Reflect.ownKeys({ [k]: 0 })[0];
|
|
68
|
+
const S = (o, ex) => { const r = {}, f = Object(o), ks = Reflect.ownKeys(f); for (let i = 0; i < ks.length; i++) { const k = ks[i]; let skip = false; for (let j = 0; j < ex.length; j++) if (ex[j] === k) skip = true; if (skip) continue; const d = Reflect.getOwnPropertyDescriptor(f, k); if (d && d.enumerable) Object.defineProperty(r, k, { value: f[k], writable: true, enumerable: true, configurable: true }); } return r; };
|
|
69
|
+
const N = (it, next, ...first) => { const r = { next() { return first.length ? first.pop() : Reflect.apply(next, it, []); }, get return() { const m = it.return; return m == null ? m : (...v) => Reflect.apply(m, it, v); }, [Symbol.asyncIterator]() { return r; } }; return r; };
|
|
70
|
+
const X = (it) => { let p; try { const m = it.return; if (m == null) return; p = Promise.resolve(Reflect.apply(m, it, [])); } catch { return; } return p.then(() => {}, () => {}); };
|
|
71
|
+
const O = (r) => { if (Object(r) !== r) throw new TypeError('iterator must return an object'); };
|
|
72
|
+
const fromSync = (it, next) => { const q = () => { try { const m = it.return; if (m != null) Reflect.apply(m, it, []); } catch {} }; const step = (f, close) => new Promise((res, rej) => { const r = f(); if (Object(r) !== r) throw new TypeError('iterator must return an object'); const done = !!r.done; let w; try { w = Promise.resolve(r.value); } catch (e) { if (!done && close) q(); throw e; } w.then((v) => res({ value: v, done }), !done && close ? (e) => { q(); rej(e); } : rej); }); return { next: (...a) => step(() => Reflect.apply(next, it, a), true), return: (...a) => { const m = it.return; return m == null ? Promise.resolve({ value: a[0], done: true }) : step(() => Reflect.apply(m, it, a), false); }, throw: (...a) => { const m = it.throw; if (m != null) return step(() => Reflect.apply(m, it, a), true); try { const c = it.return; if (c != null) { const r = Reflect.apply(c, it, []); if (Object(r) !== r) throw new TypeError('iterator must return an object'); } throw new TypeError('throw is not a method'); } catch (e) { return Promise.reject(e); } } }; };
|
|
73
|
+
const Q = (a, ...v) => { let n = a.length; for (const x of v) Object.defineProperty(a, n++, { value: x, writable: true, enumerable: true, configurable: true }); return a; };
|
|
74
|
+
const regexpIterator = (re, s, f) => { const it = Reflect.apply(matchAll, re, [s]); if (f & 4) while (!it.next().done); return it; };
|
|
75
|
+
const closeThrow = (it) => (e) => { try { const m = it.return; if (m != null) Reflect.apply(m, it, []); } catch {} throw e; };
|
|
76
|
+
const forIn = (o, keys) => { let i = 0; const r = { next() { while (i < keys.length) { const k = keys[i++]; if (o == null || k in o) return { value: k, done: false }; } return { value: undefined, done: true }; }, [Symbol.iterator]() { return r; } }; return r; };
|
|
77
|
+
const advance = (it, n) => { const next = it.next; for (let i = 0; i < n; i++) Reflect.apply(next, it, []); return it; };
|
|
78
|
+
const fn = (o) => { const d = Object.getOwnPropertyDescriptor(o, Reflect.ownKeys(o)[0]); return 'value' in d ? d.value : d.get ?? d.set; };
|
|
79
|
+
const spent = (k) => { let f; new Promise((a, b) => { f = k ? b : a; a(); }); return f; };
|
|
80
|
+
const rejected = (e) => { const p = Promise.reject(e); p.then(undefined, () => {}); return p; };
|
|
81
|
+
const fromAsync = (p, mapfn, thisArg, it, next, A, k, phase, items, len, error) => new Promise((resolve, reject) => { const put = (v) => { if (!Reflect.defineProperty(A, k, { value: v, writable: true, enumerable: true, configurable: true })) throw new TypeError('cannot define the element'); }; const wait = (v, ph) => { phase = ph; Promise.prototype.then.call(Promise.resolve(v), ok, bad); }; const close = (e) => { error = e; try { const m = it.return; if (m == null) { reject(e); return; } wait(Reflect.apply(m, it, []), 3); } catch { reject(error); } }; const step = () => { try { if (k >= 9007199254740991) { close(new TypeError('Array too long')); return; } wait(Reflect.apply(next, it, []), 1); } catch (e) { reject(e); } }; const store = (v) => { try { put(v); } catch (e) { close(e); return; } k++; step(); }; const result = (r) => { let v; try { if (Object(r) !== r) throw new TypeError('iterator must return an object'); if (r.done) { A.length = k; resolve(A); return; } v = r.value; } catch (e) { reject(e); return; } if (mapfn === undefined) { store(v); return; } try { wait(Reflect.apply(mapfn, thisArg, [v, k]), 2); } catch (e) { close(e); } }; const element = () => { try { if (k < len) { wait(items[k], 4); return; } A.length = len; resolve(A); } catch (e) { reject(e); } }; const value = (v) => { try { if (phase === 4 && mapfn !== undefined) { wait(Reflect.apply(mapfn, thisArg, [v, k]), 5); return; } put(v); k++; } catch (e) { reject(e); return; } element(); }; const ok = (v) => { if (phase === 1) result(v); else if (phase === 2) store(v); else if (phase === 3) reject(error); else value(v); }; const bad = (e) => { if (phase === 2) close(e); else if (phase === 3) reject(error); else reject(e); }; Promise.prototype.then.call(p, ok, bad); });
|
|
82
|
+
const bytes = (b) => Uint8Array.fromBase64(b).buffer;
|
|
83
|
+
const k = (K, f, at, r, h, p) => { Object.defineProperty(K, '$djs', { value: [f, at, r, h] }); if (p !== undefined) K.prototype = p; else if ('prototype' in K) K.prototype = f.prototype; return K; };
|
|
84
|
+
const keep = (o, keys) => { for (const k of Reflect.ownKeys(o)) if (!['length', 'name', 'prototype', 'constructor'].includes(k) && !keys.includes(k) && !delete o[k]) throw new TypeError('keep'); };
|
|
85
|
+
return { R, G, C, D, A, L, I, E, Y, DD, U, M, FM, CC, CI, HD, P, S, N, X, O, fromSync, Q, regexpIterator, closeThrow, forIn, advance, fn, spent, rejected, fromAsync, bytes, k, keep };
|
|
86
|
+
})();
|
|
87
|
+
`;
|
|
88
|
+
|
|
89
|
+
/** `script` with only the helpers `dump` names (`$djs.NAME`) and the ones their texts name:
|
|
90
|
+
* a load compiles a few helpers instead of all of them. */
|
|
91
|
+
export function scriptFor(dump) {
|
|
92
|
+
const lines = script.split("\n");
|
|
93
|
+
const def = (l) => (l.startsWith("const ") && !l.startsWith("const matchAll ") ? l.slice(6, l.indexOf(" = ")) : undefined);
|
|
94
|
+
const defs = new Map(lines.filter(def).map((l) => [def(l), l]));
|
|
95
|
+
const want = new Set();
|
|
96
|
+
const add = (n) => {
|
|
97
|
+
if (!defs.has(n) || want.has(n)) return;
|
|
98
|
+
want.add(n);
|
|
99
|
+
for (const w of defs.get(n).slice(n.length + 9).split(/[^\w$]+/)) add(w);
|
|
100
|
+
};
|
|
101
|
+
for (const m of dump.split("$djs.").slice(1)) add(/^[\w$]*/.exec(m)[0]);
|
|
102
|
+
const names = [...defs.keys()].filter((n) => want.has(n));
|
|
103
|
+
return lines
|
|
104
|
+
.filter((l) => def(l) === undefined || want.has(def(l)))
|
|
105
|
+
.map((l) => (l.startsWith("return { ") ? `return { ${names.join(", ")} };` : l))
|
|
106
|
+
.join("\n");
|
|
107
|
+
}
|
package/dist/dump.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { Sandbox as Base } from "./sandbox.js";
|
|
2
|
+
export * from "./sandbox.js";
|
|
3
|
+
/** This entry point's module: the package's djs-dump.wasm. */
|
|
4
|
+
export declare function defaultWasmPath(): string | URL;
|
|
5
|
+
/** Compiles djs-wasm once per location and caches it; default: this entry point's module. */
|
|
6
|
+
export declare function loadModule(location?: string | URL): Promise<WebAssembly.Module>;
|
|
7
|
+
/** The Sandbox, with this entry point's module as the default. */
|
|
8
|
+
export declare class Sandbox extends Base {
|
|
9
|
+
static defaultModule(): string | URL;
|
|
10
|
+
}
|
package/dist/dump.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
// The package's `./dump` entry point: the Sandbox API with the dump module,
|
|
2
|
+
// wasm/djs-dump.wasm (the shipped module plus the dumper), as its default: a sandbox that dumps
|
|
3
|
+
// itself, or the `dumpModule` of a sandbox of the main entry point (D3106).
|
|
4
|
+
// Each entry point loads only its own module, so a bundler copies only the modules an
|
|
5
|
+
// application imports.
|
|
6
|
+
import { Sandbox as Base, loadModule as load } from "./sandbox.js";
|
|
7
|
+
export * from "./sandbox.js";
|
|
8
|
+
/** This entry point's module: the package's djs-dump.wasm. */
|
|
9
|
+
export function defaultWasmPath() {
|
|
10
|
+
return new URL("../wasm/djs-dump.wasm", import.meta.url);
|
|
11
|
+
}
|
|
12
|
+
/** Compiles djs-wasm once per location and caches it; default: this entry point's module. */
|
|
13
|
+
export function loadModule(location = defaultWasmPath()) {
|
|
14
|
+
return load(location);
|
|
15
|
+
}
|
|
16
|
+
/** The Sandbox, with this entry point's module as the default. */
|
|
17
|
+
export class Sandbox extends Base {
|
|
18
|
+
static defaultModule() {
|
|
19
|
+
return defaultWasmPath();
|
|
20
|
+
}
|
|
21
|
+
}
|
package/dist/env.d.ts
ADDED
package/dist/env.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
// The host environment, as far as the package reads it.
|
|
2
|
+
/**
|
|
3
|
+
* `$name` of the host's environment (node, Bun, Deno), or undefined where there is none or
|
|
4
|
+
* the host may not read it: Deno without `--allow-env` would prompt or throw.
|
|
5
|
+
*/
|
|
6
|
+
export function envVar(name) {
|
|
7
|
+
const g = globalThis;
|
|
8
|
+
try {
|
|
9
|
+
if (g.Deno !== undefined) {
|
|
10
|
+
return g.Deno.permissions.querySync?.({ name: "env", variable: name }).state === "granted" ? g.Deno.env.get(name) : undefined;
|
|
11
|
+
}
|
|
12
|
+
return g.process?.env?.[name];
|
|
13
|
+
}
|
|
14
|
+
catch {
|
|
15
|
+
return undefined;
|
|
16
|
+
}
|
|
17
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { Sandbox as Base } from "./sandbox.js";
|
|
2
|
+
export * from "./sandbox.js";
|
|
3
|
+
/** This entry point's module: `$DJS_WASM`, else the package's djs.wasm. */
|
|
4
|
+
export declare function defaultWasmPath(): string | URL;
|
|
5
|
+
/** Compiles djs-wasm once per location and caches it; default: this entry point's module. */
|
|
6
|
+
export declare function loadModule(location?: string | URL): Promise<WebAssembly.Module>;
|
|
7
|
+
/** The Sandbox, with this entry point's module as the default. */
|
|
8
|
+
export declare class Sandbox extends Base {
|
|
9
|
+
static defaultModule(): string | URL;
|
|
10
|
+
}
|