@opencode/codemode 0.0.0-dev-892 → 0.0.0-dev-900
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/README.md +3 -0
- package/dist/codemode.d.ts +2 -0
- package/dist/codemode.js +10 -4
- package/dist/vm/async.d.ts +78 -0
- package/dist/vm/async.js +335 -0
- package/dist/vm/code.d.ts +122 -0
- package/dist/vm/code.js +407 -0
- package/dist/vm/compiler/compile.d.ts +13 -0
- package/dist/vm/compiler/compile.js +1477 -0
- package/dist/vm/compiler/scope.d.ts +74 -0
- package/dist/vm/compiler/scope.js +515 -0
- package/dist/vm/data.d.ts +7 -0
- package/dist/vm/data.js +13 -0
- package/dist/vm/generators.d.ts +42 -0
- package/dist/vm/generators.js +413 -0
- package/dist/vm/host.d.ts +103 -0
- package/dist/vm/host.js +226 -0
- package/dist/vm/index.d.ts +3 -0
- package/dist/vm/index.js +3 -0
- package/dist/vm/internal-prelude.d.ts +45 -0
- package/dist/vm/internal-prelude.js +534 -0
- package/dist/vm/opcodes.d.ts +222 -0
- package/dist/vm/opcodes.js +235 -0
- package/dist/vm/prelude/bytecode.d.ts +18 -0
- package/dist/vm/prelude/bytecode.js +31 -0
- package/dist/vm/prelude/bytecode.json +1 -0
- package/dist/vm/prelude/helpers.d.ts +8 -0
- package/dist/vm/prelude/helpers.js +258 -0
- package/dist/vm/prelude/prelude.d.ts +21 -0
- package/dist/vm/prelude/prelude.js +53 -0
- package/dist/vm/prelude/source.d.ts +1 -0
- package/dist/vm/prelude/source.js +652 -0
- package/dist/vm/runtime/callback.d.ts +25 -0
- package/dist/vm/runtime/callback.js +51 -0
- package/dist/vm/runtime/driver.d.ts +41 -0
- package/dist/vm/runtime/driver.js +142 -0
- package/dist/vm/runtime/errors.d.ts +23 -0
- package/dist/vm/runtime/errors.js +169 -0
- package/dist/vm/runtime/execute.d.ts +22 -0
- package/dist/vm/runtime/execute.js +278 -0
- package/dist/vm/runtime/extensions.d.ts +16 -0
- package/dist/vm/runtime/extensions.js +270 -0
- package/dist/vm/runtime/generators.d.ts +6 -0
- package/dist/vm/runtime/generators.js +44 -0
- package/dist/vm/runtime/globals.d.ts +6 -0
- package/dist/vm/runtime/globals.js +137 -0
- package/dist/vm/runtime/interpreter.d.ts +50 -0
- package/dist/vm/runtime/interpreter.js +94 -0
- package/dist/vm/runtime/intrinsic-names.d.ts +56 -0
- package/dist/vm/runtime/intrinsic-names.js +166 -0
- package/dist/vm/runtime/intrinsics.d.ts +15 -0
- package/dist/vm/runtime/intrinsics.js +111 -0
- package/dist/vm/runtime/limits.d.ts +10 -0
- package/dist/vm/runtime/limits.js +19 -0
- package/dist/vm/runtime/model.d.ts +58 -0
- package/dist/vm/runtime/model.js +57 -0
- package/dist/vm/runtime/modules.d.ts +4 -0
- package/dist/vm/runtime/modules.js +33 -0
- package/dist/vm/runtime/native.d.ts +19 -0
- package/dist/vm/runtime/native.js +41 -0
- package/dist/vm/runtime/objects.d.ts +403 -0
- package/dist/vm/runtime/objects.js +784 -0
- package/dist/vm/runtime/operations.d.ts +49 -0
- package/dist/vm/runtime/operations.js +288 -0
- package/dist/vm/runtime/promises.d.ts +55 -0
- package/dist/vm/runtime/promises.js +232 -0
- package/dist/vm/runtime/references.d.ts +8 -0
- package/dist/vm/runtime/references.js +51 -0
- package/dist/vm/runtime/signals.d.ts +69 -0
- package/dist/vm/runtime/signals.js +68 -0
- package/dist/vm/runtime/source.d.ts +29 -0
- package/dist/vm/runtime/source.js +63 -0
- package/dist/vm/runtime/table.d.ts +43 -0
- package/dist/vm/runtime/table.js +132 -0
- package/dist/vm/scheduler.d.ts +178 -0
- package/dist/vm/scheduler.js +63 -0
- package/dist/vm/snapshot/codec.d.ts +88 -0
- package/dist/vm/snapshot/codec.js +752 -0
- package/dist/vm/snapshot/snapshot.d.ts +39 -0
- package/dist/vm/snapshot/snapshot.js +69 -0
- package/dist/vm/snapshot.d.ts +29 -0
- package/dist/vm/snapshot.js +304 -0
- package/dist/vm/stdlib/array.d.ts +16 -0
- package/dist/vm/stdlib/array.js +311 -0
- package/dist/vm/stdlib/bytes.d.ts +11 -0
- package/dist/vm/stdlib/bytes.js +222 -0
- package/dist/vm/stdlib/collections.d.ts +7 -0
- package/dist/vm/stdlib/collections.js +188 -0
- package/dist/vm/stdlib/console.d.ts +6 -0
- package/dist/vm/stdlib/console.js +93 -0
- package/dist/vm/stdlib/date.d.ts +2 -0
- package/dist/vm/stdlib/date.js +123 -0
- package/dist/vm/stdlib/headers.d.ts +2 -0
- package/dist/vm/stdlib/headers.js +92 -0
- package/dist/vm/stdlib/iterator.d.ts +2 -0
- package/dist/vm/stdlib/iterator.js +30 -0
- package/dist/vm/stdlib/json.d.ts +3 -0
- package/dist/vm/stdlib/json.js +42 -0
- package/dist/vm/stdlib/math.d.ts +3 -0
- package/dist/vm/stdlib/math.js +73 -0
- package/dist/vm/stdlib/number.d.ts +3 -0
- package/dist/vm/stdlib/number.js +72 -0
- package/dist/vm/stdlib/object.d.ts +6 -0
- package/dist/vm/stdlib/object.js +201 -0
- package/dist/vm/stdlib/regexp.d.ts +8 -0
- package/dist/vm/stdlib/regexp.js +119 -0
- package/dist/vm/stdlib/string.d.ts +2 -0
- package/dist/vm/stdlib/string.js +272 -0
- package/dist/vm/stdlib/url.d.ts +25 -0
- package/dist/vm/stdlib/url.js +255 -0
- package/dist/vm/stdlib/value.d.ts +6 -0
- package/dist/vm/stdlib/value.js +32 -0
- package/dist/vm/stdlib/web.d.ts +5 -0
- package/dist/vm/stdlib/web.js +97 -0
- package/dist/vm/vm.d.ts +224 -0
- package/dist/vm/vm.js +2921 -0
- package/package.json +5 -2
|
@@ -0,0 +1,534 @@
|
|
|
1
|
+
import { fromJson } from "./data.js";
|
|
2
|
+
import { conversions } from "./runtime/callback.js";
|
|
3
|
+
import { checkStringLength, MAX_VALUE_DEPTH } from "./runtime/limits.js";
|
|
4
|
+
import { invalidData, rangeError, typeError } from "./runtime/model.js";
|
|
5
|
+
import { Arr, Builtin, Bytes, Callable, coerceToNumber, coerceToString, DateObj, ErrorObj, get, HeadersObj, keys, Obj, PromiseObj, SetObj, URLSearchParamsObj, } from "./runtime/objects.js";
|
|
6
|
+
import { applyBinaryOperator, operatorConversion, propertyKey } from "./runtime/operations.js";
|
|
7
|
+
import { isOpaque, typeofValue } from "./runtime/references.js";
|
|
8
|
+
import { Convert } from "./runtime/signals.js";
|
|
9
|
+
import { builtinCodes } from "./prelude/bytecode.js";
|
|
10
|
+
import { UnaryOp } from "./opcodes.js";
|
|
11
|
+
import { VmInternalFn } from "./vm.js";
|
|
12
|
+
/**
|
|
13
|
+
* Lightweight internal callable for VM-only `$` intrinsics. Does not extend `Obj` or `Native`,
|
|
14
|
+
* so it never touches `builtins.Function`, `nativeRule.sealed`, or static intrinsic numbering.
|
|
15
|
+
*/
|
|
16
|
+
export class VmIntrinsic {
|
|
17
|
+
name;
|
|
18
|
+
call;
|
|
19
|
+
constructor(name, call) {
|
|
20
|
+
this.name = name;
|
|
21
|
+
this.call = call;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
export const INTERNAL_SOURCE = String.raw `
|
|
25
|
+
function $vmToPrimitive(v, hint) {
|
|
26
|
+
if (!$IsDataObj(v)) return v
|
|
27
|
+
const first = hint === "string" || (hint === "default" && $IsDate(v)) ? "toString" : "valueOf"
|
|
28
|
+
const second = first === "toString" ? "valueOf" : "toString"
|
|
29
|
+
const m1 = $GetMethod(v, first)
|
|
30
|
+
if (m1 !== undefined) {
|
|
31
|
+
const r1 = $Call(m1, v)
|
|
32
|
+
if (!$IsObj(r1)) return r1
|
|
33
|
+
}
|
|
34
|
+
const m2 = $GetMethod(v, second)
|
|
35
|
+
if (m2 !== undefined) {
|
|
36
|
+
const r2 = $Call(m2, v)
|
|
37
|
+
if (!$IsObj(r2)) return r2
|
|
38
|
+
}
|
|
39
|
+
$Throw("TypeError", "Cannot convert object to primitive value.")
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function $vmToKey(v) {
|
|
43
|
+
return $PropKey($vmToPrimitive(v, "string"))
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function $vmToString(v) {
|
|
47
|
+
return $CoerceToString($vmToPrimitive(v, "string"))
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function $vmToNumeric(incFlag, v) {
|
|
51
|
+
return $ApplyToNumeric(incFlag, $vmToPrimitive(v, "number"))
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function $vmInc(incFlag, v) {
|
|
55
|
+
const n = $ApplyToNumeric(incFlag, $vmToPrimitive(v, "number"))
|
|
56
|
+
return incFlag === 0 ? n + 1 : n - 1
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function $vmUnary(sub, v) {
|
|
60
|
+
return $ApplyUnary(sub, $vmToPrimitive(v, "number"))
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function $vmBinary(op, lhs, rhs) {
|
|
64
|
+
const kind = $OpConvKind(op, lhs, rhs)
|
|
65
|
+
if (kind === 0) return $ApplyBinary(op, lhs, rhs)
|
|
66
|
+
if (kind === 1) {
|
|
67
|
+
const key = $vmToPrimitive(lhs, "string")
|
|
68
|
+
return $ApplyBinary(op, key, rhs)
|
|
69
|
+
}
|
|
70
|
+
const hint = kind === 2 ? "default" : "number"
|
|
71
|
+
const l = $vmToPrimitive(lhs, hint)
|
|
72
|
+
const r = $vmToPrimitive(rhs, hint)
|
|
73
|
+
return $ApplyBinary(op, l, r)
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function $vmGetElem(o, k) {
|
|
77
|
+
return o[$vmToKey(k)]
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function $vmDeleteElem(o, k) {
|
|
81
|
+
return delete o[$vmToKey(k)]
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function $vmConvert(callable, target, args, constructing, replay, req) {
|
|
85
|
+
while (true) {
|
|
86
|
+
const values = $ConvValues(req)
|
|
87
|
+
const coerce = $ConvFlag(req, 0)
|
|
88
|
+
const ordinal = $ConvFlag(req, 1)
|
|
89
|
+
const len = values.length
|
|
90
|
+
const converted = []
|
|
91
|
+
let i = 0
|
|
92
|
+
while (i < len) {
|
|
93
|
+
const val = values[i]
|
|
94
|
+
const hint = $ConvHint(req, i)
|
|
95
|
+
const prim = $IsObj(val) ? $vmToPrimitive(val, hint) : val
|
|
96
|
+
converted[i] = coerce ? $ConvCoerce(hint, prim) : prim
|
|
97
|
+
i++
|
|
98
|
+
}
|
|
99
|
+
replay[ordinal] = converted
|
|
100
|
+
const res = $ConvReplay(callable, target, args, constructing, replay)
|
|
101
|
+
if (!$IsConvert(res)) return res
|
|
102
|
+
req = res
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
// A JSON walk's step for one value, holder[key]: its JSON as program data, or undefined when it is left out. A promise
|
|
107
|
+
// is yielded at the host boundary, which resumes the walk with its value.
|
|
108
|
+
function $vmJsonVisitor(boundary, replacer, properties) {
|
|
109
|
+
const ancestors = []
|
|
110
|
+
function* visit(holder, key, depth) {
|
|
111
|
+
if (depth > 100) $Throw("RangeError", "Value exceeds the maximum depth of 100.")
|
|
112
|
+
let val = holder[key]
|
|
113
|
+
if ($IsPromise(val)) {
|
|
114
|
+
if (!boundary) {
|
|
115
|
+
$Throw("InvalidData", "JSON.stringify received an un-awaited Promise; await it first - e.g. \x60const result = await tools.ns.tool(...)\x60.")
|
|
116
|
+
}
|
|
117
|
+
val = yield val
|
|
118
|
+
}
|
|
119
|
+
if ($IsObj(val)) {
|
|
120
|
+
const toJSON = val.toJSON
|
|
121
|
+
if ($IsCallableAny(toJSON)) val = $Call(toJSON, val, key)
|
|
122
|
+
}
|
|
123
|
+
if (replacer !== undefined) val = $Call(replacer, holder, key, val)
|
|
124
|
+
const kind = $JsonClassify(val, boundary, ancestors)
|
|
125
|
+
if (kind === 0) return undefined
|
|
126
|
+
if (kind === 1) return val
|
|
127
|
+
if (kind === 2) return null
|
|
128
|
+
if (kind === 3) return $JsonSpecial(val, boundary)
|
|
129
|
+
const target = kind === 4 ? $SetToArray(val) : val
|
|
130
|
+
ancestors[ancestors.length] = target
|
|
131
|
+
let out
|
|
132
|
+
if ($IsArray(target)) {
|
|
133
|
+
out = []
|
|
134
|
+
const len = target.length
|
|
135
|
+
let i = 0
|
|
136
|
+
while (i < len) {
|
|
137
|
+
const elem = yield* visit(target, String(i), depth + 1)
|
|
138
|
+
out[i] = elem === undefined ? null : elem
|
|
139
|
+
i++
|
|
140
|
+
}
|
|
141
|
+
} else {
|
|
142
|
+
out = $NewNullProtoObj()
|
|
143
|
+
const names = $JsonNames(target, boundary, properties)
|
|
144
|
+
let i = 0
|
|
145
|
+
while (i < names.length) {
|
|
146
|
+
const k = names[i]
|
|
147
|
+
const propVal = yield* visit(target, k, depth + 1)
|
|
148
|
+
if (propVal !== undefined) $Define(out, k, propVal, true)
|
|
149
|
+
i++
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
$ArrayPop(ancestors)
|
|
153
|
+
return out
|
|
154
|
+
}
|
|
155
|
+
return visit
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
function $vmJsonWalk(replacer, value, properties, indent) {
|
|
159
|
+
const json = $vmJsonVisitor(false, replacer, properties)({ "": value }, "", 0).next().value
|
|
160
|
+
return $JsonFinish(false, json, properties, indent)
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
function $vmSettleCall(callable, thisVal, args) {
|
|
164
|
+
return $Apply(callable, thisVal, args)
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
function $vmCombine(name, items) {
|
|
168
|
+
const c = $CombineStart(name, items)
|
|
169
|
+
for (const item of items) {
|
|
170
|
+
$CombineAdd(c, item)
|
|
171
|
+
}
|
|
172
|
+
$CombineEnd(c)
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
function $vmOpenSyncForAsync(v, method) {
|
|
176
|
+
return $WrapAsyncFromSync($Call(method, v))
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
function $vmAsyncFromSync(adapter, kind, hasArg, val) {
|
|
180
|
+
const iter = $AfsGetIter(adapter)
|
|
181
|
+
const method = kind === "next" ? $AfsGetNext(adapter) : $AfsGetMethod(adapter, iter, kind)
|
|
182
|
+
if (method === undefined || method === null) {
|
|
183
|
+
if (kind === "return") {
|
|
184
|
+
$AfsResolveDone(val)
|
|
185
|
+
return
|
|
186
|
+
}
|
|
187
|
+
const ret = $AfsGetMethod(adapter, iter, "return")
|
|
188
|
+
if (ret !== undefined && ret !== null) {
|
|
189
|
+
$AfsCheckObj(adapter, $Call(ret, iter), "return")
|
|
190
|
+
}
|
|
191
|
+
$AfsRejectNoThrow(adapter)
|
|
192
|
+
return
|
|
193
|
+
}
|
|
194
|
+
const res = hasArg ? $Call(method, iter, val) : $Call(method, iter)
|
|
195
|
+
$AfsCheckObj(adapter, res, kind)
|
|
196
|
+
const done = !!res.done
|
|
197
|
+
const value = res.value
|
|
198
|
+
$AfsSettle(adapter, value, done, kind !== "return")
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
function $vmAsyncFromSyncClose(iter) {
|
|
202
|
+
const ret = iter.return
|
|
203
|
+
if (ret !== undefined && ret !== null) $Call(ret, iter)
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
async function $vmCross(roots, path, resolving) {
|
|
207
|
+
const crossed = []
|
|
208
|
+
let r = 0
|
|
209
|
+
while (r < roots.length) {
|
|
210
|
+
let root = roots[r]
|
|
211
|
+
if (path === undefined && $IsThenable(root)) root = await root
|
|
212
|
+
const walk = $vmJsonVisitor(true, undefined, null)({ "": root }, "", 0)
|
|
213
|
+
let step = walk.next()
|
|
214
|
+
while (!step.done) step = walk.next(await step.value)
|
|
215
|
+
crossed[r] = $JsonFinish(true, step.value, null, undefined)
|
|
216
|
+
r++
|
|
217
|
+
}
|
|
218
|
+
if (path === undefined) return crossed[0]
|
|
219
|
+
if (path === "search") return $HostSearch(crossed)
|
|
220
|
+
$HostTool(path, crossed, resolving)
|
|
221
|
+
}
|
|
222
|
+
`;
|
|
223
|
+
let compiledInternal;
|
|
224
|
+
/** The VM's internal helpers' bytecode, and their functions' code by name. */
|
|
225
|
+
export const getInternalPrelude = () => (compiledInternal ??= builtinCodes("@vm-internal", INTERNAL_SOURCE));
|
|
226
|
+
/** The code of the internal helper `name`. */
|
|
227
|
+
export const internalCode = (name) => getInternalPrelude().byName.get(name);
|
|
228
|
+
/**
|
|
229
|
+
* Adds the intrinsics available to `CodeFlags.INTERNAL` functions (both `@prelude` and `@vm-internal`) to `map`: the
|
|
230
|
+
* internal helpers, and the `$` natives. Uses `VmIntrinsic` rather than `Native` so it never touches
|
|
231
|
+
* `builtins.Function` or `nativeRule.sealed`.
|
|
232
|
+
*/
|
|
233
|
+
export const registerVmIntrinsics = (map, vm) => {
|
|
234
|
+
const ctx = vm.ctx;
|
|
235
|
+
const builtins = ctx.builtins;
|
|
236
|
+
const currentSite = () => vm.currentSite();
|
|
237
|
+
for (const [name, code] of getInternalPrelude().byName)
|
|
238
|
+
map.set(name, new VmInternalFn(code));
|
|
239
|
+
const add = (name, fn) => {
|
|
240
|
+
map.set(name, new VmIntrinsic(name, fn));
|
|
241
|
+
};
|
|
242
|
+
add("$IsObj", ([v]) => v instanceof Obj);
|
|
243
|
+
add("$IsDataObj", ([v]) => v instanceof Obj && !isOpaque(v));
|
|
244
|
+
add("$IsDate", ([v]) => v instanceof DateObj);
|
|
245
|
+
add("$IsPromise", ([v]) => v instanceof PromiseObj);
|
|
246
|
+
add("$IsArray", ([v]) => v instanceof Arr);
|
|
247
|
+
add("$IsCallableAny", ([v]) => v instanceof Callable);
|
|
248
|
+
add("$GetMethod", ([v, name]) => {
|
|
249
|
+
const m = get(v, name);
|
|
250
|
+
return m instanceof Callable ? m : undefined;
|
|
251
|
+
});
|
|
252
|
+
add("$PropKey", ([v]) => propertyKey(v));
|
|
253
|
+
add("$CoerceToString", ([v]) => coerceToString(v));
|
|
254
|
+
add("$ApplyToNumeric", ([incFlag, v]) => {
|
|
255
|
+
if (isOpaque(v)) {
|
|
256
|
+
const op = incFlag === 0 ? "++" : "--";
|
|
257
|
+
throw invalidData(`'${op}' requires a data value.`, currentSite());
|
|
258
|
+
}
|
|
259
|
+
return coerceToNumber(v);
|
|
260
|
+
});
|
|
261
|
+
add("$ApplyUnary", ([sub, v]) => {
|
|
262
|
+
if (isOpaque(v))
|
|
263
|
+
throw invalidData("Unary operators require data values.", currentSite());
|
|
264
|
+
const n = coerceToNumber(v);
|
|
265
|
+
return sub === UnaryOp.NEG ? -n : sub === UnaryOp.PLUS ? +n : ~n;
|
|
266
|
+
});
|
|
267
|
+
add("$OpConvKind", ([op, lhs, rhs]) => {
|
|
268
|
+
const conv = operatorConversion(op, lhs, rhs);
|
|
269
|
+
if (conv === undefined)
|
|
270
|
+
return 0;
|
|
271
|
+
if (conv.kind === "key")
|
|
272
|
+
return 1;
|
|
273
|
+
return conv.hint === "default" ? 2 : 3;
|
|
274
|
+
});
|
|
275
|
+
add("$ApplyBinary", ([op, lhs, rhs]) => applyBinaryOperator(op, lhs, rhs, currentSite()));
|
|
276
|
+
// Convert helpers
|
|
277
|
+
add("$ConvValues", ([req]) => new Arr(builtins.Array, [...req.values]));
|
|
278
|
+
add("$ConvFlag", ([req, which]) => {
|
|
279
|
+
const c = req;
|
|
280
|
+
return which === 0 ? c.coerce : c.ordinal;
|
|
281
|
+
});
|
|
282
|
+
add("$ConvHint", ([req, idx]) => {
|
|
283
|
+
const hints = req.hints;
|
|
284
|
+
return typeof hints === "string" ? hints : hints[idx];
|
|
285
|
+
});
|
|
286
|
+
add("$ConvCoerce", ([hint, val]) => (hint === "string" ? coerceToString(val) : coerceToNumber(val)));
|
|
287
|
+
add("$IsConvert", ([res]) => res instanceof Convert);
|
|
288
|
+
add("$ConvReplay", ([callable, target, args, constructing, replay]) => {
|
|
289
|
+
const rep = replay.items.map((item) => (item instanceof Arr ? item.items : undefined));
|
|
290
|
+
conversions.count = 0;
|
|
291
|
+
conversions.replay = rep;
|
|
292
|
+
try {
|
|
293
|
+
const fnObj = callable;
|
|
294
|
+
const argList = args.items;
|
|
295
|
+
return constructing
|
|
296
|
+
? fnObj.construct(argList, target, currentSite())
|
|
297
|
+
: fnObj.invoke(target, argList, currentSite());
|
|
298
|
+
}
|
|
299
|
+
finally {
|
|
300
|
+
conversions.replay = undefined;
|
|
301
|
+
}
|
|
302
|
+
});
|
|
303
|
+
// JSON walk helpers
|
|
304
|
+
add("$JsonClassify", ([val, boundary, ancestors]) => {
|
|
305
|
+
if (val === undefined || typeofValue(val) === "function")
|
|
306
|
+
return 0;
|
|
307
|
+
if (typeof val === "number")
|
|
308
|
+
return Number.isFinite(val) ? 1 : 2;
|
|
309
|
+
if (val === null || typeof val === "string" || typeof val === "boolean")
|
|
310
|
+
return 1;
|
|
311
|
+
if (!(val instanceof Obj))
|
|
312
|
+
return 3;
|
|
313
|
+
if (boundary && val instanceof Bytes) {
|
|
314
|
+
throw invalidData("A Uint8Array cannot cross to the host; pass text instead, e.g. `new TextDecoder().decode(bytes)` or `bytes.toBase64()`.");
|
|
315
|
+
}
|
|
316
|
+
if ((boundary && val instanceof URLSearchParamsObj) || val instanceof HeadersObj)
|
|
317
|
+
return 3;
|
|
318
|
+
if (ancestors.items.includes(val))
|
|
319
|
+
throw typeError("Converting circular structure to JSON.");
|
|
320
|
+
if (boundary && val instanceof SetObj)
|
|
321
|
+
return 4;
|
|
322
|
+
return 5;
|
|
323
|
+
});
|
|
324
|
+
add("$JsonSpecial", ([val, boundary]) => {
|
|
325
|
+
if (!(val instanceof Obj))
|
|
326
|
+
return new Obj(builtins.Object);
|
|
327
|
+
if (boundary && val instanceof URLSearchParamsObj)
|
|
328
|
+
return val.params.toString();
|
|
329
|
+
if (val instanceof HeadersObj)
|
|
330
|
+
return fromJson(ctx, Object.fromEntries(val.headers));
|
|
331
|
+
return new Obj(builtins.Object);
|
|
332
|
+
});
|
|
333
|
+
add("$SetToArray", ([val]) => new Arr(builtins.Array, [...val.set.liveKeys()]));
|
|
334
|
+
add("$NewNullProtoObj", () => new Obj(null));
|
|
335
|
+
// The keys a JSON walk writes, in order: an error's name and message come first, then its other own keys.
|
|
336
|
+
add("$JsonNames", ([target, boundary, properties]) => {
|
|
337
|
+
const error = target instanceof ErrorObj;
|
|
338
|
+
const propFilter = properties instanceof Arr ? new Set(properties.items) : undefined;
|
|
339
|
+
const list = keys(target).filter((k) => !(error && (k === "name" || k === "message")) &&
|
|
340
|
+
!(boundary && k === "__proto__") &&
|
|
341
|
+
(propFilter === undefined || propFilter.has(k)));
|
|
342
|
+
return new Arr(builtins.Array, error ? ["name", "message", ...list] : list);
|
|
343
|
+
});
|
|
344
|
+
add("$ArrayPop", ([arr]) => arr.items.pop());
|
|
345
|
+
add("$JsonFinish", ([boundary, root, properties, indent]) => {
|
|
346
|
+
const plain = toPlainJson(root);
|
|
347
|
+
if (boundary)
|
|
348
|
+
return plain;
|
|
349
|
+
const props = properties instanceof Arr ? properties.items : null;
|
|
350
|
+
const text = JSON.stringify(plain, props, indent);
|
|
351
|
+
if (text !== undefined)
|
|
352
|
+
checkStringLength(text.length);
|
|
353
|
+
return text;
|
|
354
|
+
});
|
|
355
|
+
};
|
|
356
|
+
const toPlainJson = (val) => {
|
|
357
|
+
if (val === undefined)
|
|
358
|
+
return undefined;
|
|
359
|
+
if (val === null || typeof val === "string" || typeof val === "number" || typeof val === "boolean")
|
|
360
|
+
return val;
|
|
361
|
+
if (val instanceof Arr)
|
|
362
|
+
return val.items.map((item) => toPlainJson(item) ?? null);
|
|
363
|
+
if (val instanceof Obj) {
|
|
364
|
+
const out = {};
|
|
365
|
+
for (const [k, slot] of val.props) {
|
|
366
|
+
if (typeof k !== "string" || !slot.enumerable || !("value" in slot))
|
|
367
|
+
continue;
|
|
368
|
+
const v = toPlainJson(slot.value);
|
|
369
|
+
if (v !== undefined) {
|
|
370
|
+
if (k === "__proto__") {
|
|
371
|
+
Object.defineProperty(out, k, { value: v, enumerable: true, writable: true, configurable: true });
|
|
372
|
+
}
|
|
373
|
+
else {
|
|
374
|
+
out[k] = v;
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
return out;
|
|
379
|
+
}
|
|
380
|
+
return {};
|
|
381
|
+
};
|
|
382
|
+
/**
|
|
383
|
+
* Budgeted native fast path for copying plain data at the host boundary (`boundary`) or for `JSON.stringify`. Returns
|
|
384
|
+
* `{ ok: true, json }` if the tree has no custom `toJSON`, promises, or shared references revisited more than `budget`
|
|
385
|
+
* times; otherwise the prelude walk (`$vmJsonWalk`) does the work, ticking fuel.
|
|
386
|
+
*
|
|
387
|
+
* Both give the text `JSON.stringify` would: `toJSON` is honored, functions and `undefined` vanish, non-finite numbers
|
|
388
|
+
* become null, and everything else is copied. Two departures from JS so a mistake is not a silent `{}`: an Error
|
|
389
|
+
* serializes as `{ name, message, ...own }`, and a promise throws. At the host boundary a promise is awaited, a Set
|
|
390
|
+
* crosses as an array, a URLSearchParams as its query string, a Uint8Array asks to be encoded as text first, and a
|
|
391
|
+
* `__proto__` key is dropped so host code can never receive one.
|
|
392
|
+
*/
|
|
393
|
+
export const tryFastBoundaryOrJson = (builtins, value, boundary, revisitBudget = 2048) => {
|
|
394
|
+
if (value === undefined)
|
|
395
|
+
return { ok: true, json: undefined };
|
|
396
|
+
if (value === null || typeof value === "string" || typeof value === "boolean")
|
|
397
|
+
return { ok: true, json: value };
|
|
398
|
+
if (typeof value === "number")
|
|
399
|
+
return { ok: true, json: Number.isFinite(value) ? value : null };
|
|
400
|
+
if (value instanceof Obj &&
|
|
401
|
+
value.constructor === Obj &&
|
|
402
|
+
value.proto === builtins.Object &&
|
|
403
|
+
value.proto.intrinsic > 0 &&
|
|
404
|
+
value.props.size <= 4 &&
|
|
405
|
+
!value.props.has("toJSON")) {
|
|
406
|
+
const flatOut = {};
|
|
407
|
+
let flatOk = true;
|
|
408
|
+
for (const [k, slot] of value.props) {
|
|
409
|
+
if (typeof k !== "string" || !slot.enumerable)
|
|
410
|
+
continue;
|
|
411
|
+
if (k === "__proto__") {
|
|
412
|
+
if (!boundary)
|
|
413
|
+
flatOk = false;
|
|
414
|
+
continue;
|
|
415
|
+
}
|
|
416
|
+
if (!("value" in slot)) {
|
|
417
|
+
flatOk = false;
|
|
418
|
+
break;
|
|
419
|
+
}
|
|
420
|
+
const pv = slot.value;
|
|
421
|
+
if (pv === undefined)
|
|
422
|
+
continue;
|
|
423
|
+
if (pv === null || typeof pv === "string" || typeof pv === "boolean") {
|
|
424
|
+
flatOut[k] = pv;
|
|
425
|
+
}
|
|
426
|
+
else if (typeof pv === "number") {
|
|
427
|
+
flatOut[k] = Number.isFinite(pv) ? pv : null;
|
|
428
|
+
}
|
|
429
|
+
else {
|
|
430
|
+
flatOk = false;
|
|
431
|
+
break;
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
if (flatOk)
|
|
435
|
+
return { ok: true, json: flatOut };
|
|
436
|
+
}
|
|
437
|
+
let remainingRevisits = revisitBudget;
|
|
438
|
+
const ancestors = new Set();
|
|
439
|
+
const seen = new Set();
|
|
440
|
+
const objectProto = builtins.Object;
|
|
441
|
+
let dateProtoToJSON;
|
|
442
|
+
let urlProtoToJSON;
|
|
443
|
+
const visit = (cur, depth) => {
|
|
444
|
+
if (depth > MAX_VALUE_DEPTH)
|
|
445
|
+
throw rangeError(`Value exceeds the maximum depth of ${MAX_VALUE_DEPTH}.`);
|
|
446
|
+
if (cur === undefined || typeofValue(cur) === "function")
|
|
447
|
+
return { ok: true, json: undefined };
|
|
448
|
+
if (typeof cur === "number")
|
|
449
|
+
return { ok: true, json: Number.isFinite(cur) ? cur : null };
|
|
450
|
+
if (cur === null || typeof cur === "string" || typeof cur === "boolean")
|
|
451
|
+
return { ok: true, json: cur };
|
|
452
|
+
if (cur instanceof PromiseObj)
|
|
453
|
+
return { ok: false };
|
|
454
|
+
if (!(cur instanceof Obj))
|
|
455
|
+
return { ok: true, json: {} };
|
|
456
|
+
if (seen.has(cur)) {
|
|
457
|
+
if (--remainingRevisits <= 0)
|
|
458
|
+
return { ok: false };
|
|
459
|
+
}
|
|
460
|
+
else {
|
|
461
|
+
seen.add(cur);
|
|
462
|
+
}
|
|
463
|
+
const plain = cur.constructor === Obj;
|
|
464
|
+
const toJSON = plain && cur.proto === objectProto && objectProto.intrinsic > 0 && !cur.props.has("toJSON")
|
|
465
|
+
? undefined
|
|
466
|
+
: get(cur, "toJSON");
|
|
467
|
+
if (toJSON instanceof Callable) {
|
|
468
|
+
if (cur instanceof DateObj && toJSON === (dateProtoToJSON ??= get(builtins.Date, "toJSON"))) {
|
|
469
|
+
return { ok: true, json: Number.isFinite(cur.time) ? new Date(cur.time).toISOString() : null };
|
|
470
|
+
}
|
|
471
|
+
if (cur.tag === "URL" && toJSON === (urlProtoToJSON ??= get(builtins.URL, "toJSON"))) {
|
|
472
|
+
return { ok: true, json: cur.toString() };
|
|
473
|
+
}
|
|
474
|
+
return { ok: false };
|
|
475
|
+
}
|
|
476
|
+
if (boundary && cur instanceof Bytes) {
|
|
477
|
+
throw invalidData("A Uint8Array cannot cross to the host; pass text instead, e.g. `new TextDecoder().decode(bytes)` or `bytes.toBase64()`.");
|
|
478
|
+
}
|
|
479
|
+
if (boundary && cur instanceof URLSearchParamsObj)
|
|
480
|
+
return { ok: true, json: cur.params.toString() };
|
|
481
|
+
if (cur instanceof HeadersObj)
|
|
482
|
+
return { ok: true, json: Object.fromEntries(cur.headers) };
|
|
483
|
+
if (ancestors.has(cur))
|
|
484
|
+
throw typeError("Converting circular structure to JSON.");
|
|
485
|
+
ancestors.add(cur);
|
|
486
|
+
try {
|
|
487
|
+
if (cur instanceof Arr || (boundary && cur instanceof SetObj)) {
|
|
488
|
+
const items = cur instanceof Arr ? cur.items : [...cur.set.liveKeys()];
|
|
489
|
+
const out = new Array(items.length);
|
|
490
|
+
for (let i = 0; i < items.length; i++) {
|
|
491
|
+
const r = visit(items[i], depth + 1);
|
|
492
|
+
if (!r.ok)
|
|
493
|
+
return r;
|
|
494
|
+
out[i] = r.json ?? null;
|
|
495
|
+
}
|
|
496
|
+
return { ok: true, json: out };
|
|
497
|
+
}
|
|
498
|
+
const out = {};
|
|
499
|
+
if (cur instanceof ErrorObj) {
|
|
500
|
+
for (const k of ["name", "message"]) {
|
|
501
|
+
const r = visit(get(cur, k), depth + 1);
|
|
502
|
+
if (!r.ok)
|
|
503
|
+
return r;
|
|
504
|
+
if (r.json !== undefined)
|
|
505
|
+
out[k] = r.json;
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
// A plain object's own data is its property table; a wrapper (bytes, say) presents its own keys.
|
|
509
|
+
for (const [k, slot] of plain ? cur.props : keys(cur).map((key) => [key, undefined])) {
|
|
510
|
+
if (typeof k !== "string" || (slot !== undefined && !slot.enumerable))
|
|
511
|
+
continue;
|
|
512
|
+
if (boundary && k === "__proto__")
|
|
513
|
+
continue;
|
|
514
|
+
const propVal = slot === undefined ? get(cur, k) : "value" in slot ? slot.value : slot.get?.(cur);
|
|
515
|
+
const r = visit(propVal, depth + 1);
|
|
516
|
+
if (!r.ok)
|
|
517
|
+
return r;
|
|
518
|
+
if (r.json !== undefined) {
|
|
519
|
+
if (k === "__proto__") {
|
|
520
|
+
Object.defineProperty(out, k, { value: r.json, enumerable: true, writable: true, configurable: true });
|
|
521
|
+
}
|
|
522
|
+
else {
|
|
523
|
+
out[k] = r.json;
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
}
|
|
527
|
+
return { ok: true, json: out };
|
|
528
|
+
}
|
|
529
|
+
finally {
|
|
530
|
+
ancestors.delete(cur);
|
|
531
|
+
}
|
|
532
|
+
};
|
|
533
|
+
return visit(value, 0);
|
|
534
|
+
};
|