@threadedstack/wmd-web 0.1.30 → 0.1.32
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 +23 -2
- package/RELEASE.json +123 -16
- package/guest/arm64/Image +1 -1
- package/guest/arm64/Image-4k +1 -1
- package/guest/arm64/initramfs.cpio.gz +0 -0
- package/package.json +3 -3
- package/src/boot.js +47 -2
- package/src/edge/bridge.js +297 -0
- package/src/edge/codes.js +10 -0
- package/src/edge/ctl.js +199 -0
- package/src/edge/error-kind.js +4 -0
- package/src/edge/frame.html +4 -0
- package/src/edge/frame.js +48 -0
- package/src/edge/fs-route.js +301 -0
- package/src/edge/hist.js +46 -0
- package/src/edge/msg.js +106 -0
- package/src/edge/node-args.js +194 -0
- package/src/edge/node-guest-opts.js +108 -0
- package/src/edge/node-opts.js +82 -0
- package/src/edge/own/adapter.js +271 -0
- package/src/edge/own/capi.js +200 -0
- package/src/edge/own/child-host.js +196 -0
- package/src/edge/own/fs-bridge.js +71 -0
- package/src/edge/own/fs-own-route.js +356 -0
- package/src/edge/own/guest-spawner.js +235 -0
- package/src/edge/own/hub.js +430 -0
- package/src/edge/own/loader.js +81 -0
- package/src/edge/own/memcap.js +95 -0
- package/src/edge/own/payload-size.js +15 -0
- package/src/edge/own/pool.js +160 -0
- package/src/edge/own/procfs.js +84 -0
- package/src/edge/own/serdes-codec.js +317 -0
- package/src/edge/own/service-receiver.js +98 -0
- package/src/edge/own/shim-post.js +185 -0
- package/src/edge/own/stack-fix.js +36 -0
- package/src/edge/own/stdio-pipe.js +80 -0
- package/src/edge/own/v8-serdes.js +970 -0
- package/src/edge/own/wasm-counters.js +144 -0
- package/src/edge/own/worker.js +97 -0
- package/src/edge/page.js +162 -0
- package/src/edge/plan.js +65 -0
- package/src/edge/resolve.js +44 -0
- package/src/edge/runtime.js +94 -0
- package/src/edge/sdk/adapter.js +151 -0
- package/src/edge/sdk/forward.js +80 -0
- package/src/edge/sdk/napi-stub.js +50 -0
- package/src/edge/sdk/relay.js +17 -0
- package/src/edge/service-host.js +382 -0
- package/src/edge/service-port.js +245 -0
- package/src/edge/shim.js +161 -0
- package/src/edge/site.js +106 -0
- package/src/edge/spawn-guard.js +80 -0
- package/src/edge/tz.js +103 -0
- package/src/edge/worker.js +121 -0
- package/src/edge/workers.js +27 -0
- package/src/host/chan-service.js +75 -8
- package/src/host/chan-services.js +77 -3
- package/src/host/ctl.js +26 -5
- package/src/host/edge-go.js +7 -3
- package/src/host/edge-host.js +327 -0
- package/src/host/yamux.js +21 -8
- package/wmdpack.wasm +0 -0
|
@@ -0,0 +1,970 @@
|
|
|
1
|
+
// V8's value serializer wire format (version 15) for the napi engine's `Serializer` and `Deserializer` (what node's `v8.serialize`,
|
|
2
|
+
// `v8.deserialize` and `child_process`' advanced serialization stand on). The engine used to write a private tag ("WSH1WSV2") and, for
|
|
3
|
+
// the values its codec did not carry, an id into a table: files written by `v8.serialize` in the frame could not be read by the guest's
|
|
4
|
+
// node (fix7 F3), and the bytes were a Uint8Array, not a Buffer. This writes and reads the bytes V8 writes.
|
|
5
|
+
//
|
|
6
|
+
// Sources: node's lib/v8.js and src/node_serdes.cc (the shape of the class the binding gives node: writeHeader, writeValue,
|
|
7
|
+
// releaseBuffer, transferArrayBuffer, writeUint32, writeUint64, writeDouble, writeRawBytes, _setTreatArrayBufferViewsAsHostObjects;
|
|
8
|
+
// readHeader, readValue, getWireFormatVersion, transferArrayBuffer, readUint32, readUint64, readDouble, _readRawBytes), both MIT, and
|
|
9
|
+
// V8's src/objects/value-serializer.cc (BSD-3-Clause) for the tags, the order of the parts of each value and the alignment rule of
|
|
10
|
+
// two-byte strings. The bytes were also compared with a host node's `v8.serialize` on generated values (test/unit/v8-serdes.test.js).
|
|
11
|
+
//
|
|
12
|
+
// makeSerdesBinding({ bufferOf?, structuredClone? }) -> { Serializer, Deserializer }
|
|
13
|
+
// bufferOf(u8): the Buffer over those bytes (the program's `Buffer`; without it releaseBuffer gives the Uint8Array)
|
|
14
|
+
//
|
|
15
|
+
// What cannot be told apart from JavaScript, and so may differ in bytes from V8's (the value read back is equal either way):
|
|
16
|
+
// a number that V8 holds as a heap number but that is a whole number in the 32-bit range (V8 writes it as a double, this writes it as
|
|
17
|
+
// an integer); a dense array V8 keeps in a holey elements kind (`new Array(3).fill(1)`; V8 writes it as a sparse array, this as a
|
|
18
|
+
// dense one); an array of numbers that V8 holds as doubles even when each is whole.
|
|
19
|
+
|
|
20
|
+
const T = {
|
|
21
|
+
PAD: 0x00, VERIFY: 0x3f, UNDEF: 0x5f, NULL: 0x30, TRUE: 0x54, FALSE: 0x46, INT32: 0x49, UINT32: 0x55, DOUBLE: 0x4e, BIGINT: 0x5a,
|
|
22
|
+
UTF8: 0x53, ONE_BYTE: 0x22, TWO_BYTE: 0x63, REF: 0x5e, OBJECT: 0x6f, END_OBJECT: 0x7b, DENSE: 0x41, END_DENSE: 0x24, SPARSE: 0x61,
|
|
23
|
+
END_SPARSE: 0x40, DATE: 0x44, TRUE_OBJ: 0x79, FALSE_OBJ: 0x78, NUM_OBJ: 0x6e, BIGINT_OBJ: 0x7a, STR_OBJ: 0x73, REGEXP: 0x52, MAP: 0x3b,
|
|
24
|
+
END_MAP: 0x3a, SET: 0x27, END_SET: 0x2c, ABUF: 0x42, RESIZABLE_ABUF: 0x7e, TRANSFERRED_ABUF: 0x74, SHARED_ABUF: 0x75, VIEW: 0x56,
|
|
25
|
+
HOST: 0x5c, ERROR: 0x72, HOLE: 0x2d,
|
|
26
|
+
}
|
|
27
|
+
const ERR = { EVAL: 0x45, RANGE: 0x52, REFERENCE: 0x46, SYNTAX: 0x53, TYPE: 0x54, URI: 0x55, MESSAGE: 0x6d, CAUSE: 0x63, STACK: 0x73, END: 0x2e }
|
|
28
|
+
const ERROR_NAMES = { EvalError: ERR.EVAL, RangeError: ERR.RANGE, ReferenceError: ERR.REFERENCE, SyntaxError: ERR.SYNTAX, TypeError: ERR.TYPE, URIError: ERR.URI }
|
|
29
|
+
// the sub-tag of an ArrayBufferView and the constructor of its kind
|
|
30
|
+
const VIEW_TAGS = [
|
|
31
|
+
['Int8Array', 0x62], ['Uint8Array', 0x42], ['Uint8ClampedArray', 0x43], ['Int16Array', 0x77], ['Uint16Array', 0x57], ['Int32Array', 0x64], ['Uint32Array', 0x44],
|
|
32
|
+
['Float16Array', 0x68], ['Float32Array', 0x66], ['Float64Array', 0x46], ['BigInt64Array', 0x71], ['BigUint64Array', 0x51], ['DataView', 0x3f],
|
|
33
|
+
]
|
|
34
|
+
const VERSION = 15
|
|
35
|
+
const FLAG_BITS = [['g', 1], ['i', 2], ['m', 4], ['y', 8], ['u', 16], ['s', 32], ['l', 64], ['d', 128], ['v', 256]]
|
|
36
|
+
const MAX_INDEX = 4294967294
|
|
37
|
+
// the flags V8 writes after a view (version 14 and later): bit 0, the view's length follows its buffer; bit 1, its buffer is resizable
|
|
38
|
+
const VIEW_LENGTH_TRACKING = 1
|
|
39
|
+
const VIEW_BACKED_BY_RAB = 2
|
|
40
|
+
const VERSION_ERROR = 'Unable to deserialize cloned data due to invalid or unsupported version.'
|
|
41
|
+
const MIN_VERSION = 13 // node's v8.Deserializer refuses an older wire format (V8's API: no legacy format without SetSupportsLegacyWireFormat)
|
|
42
|
+
|
|
43
|
+
// -1 for a view that is out of bounds (its buffer shrank under it): a DataView's byteLength throws then, and so does any method of a typed array
|
|
44
|
+
const viewByteLength = (v) => {
|
|
45
|
+
try {
|
|
46
|
+
if (typeof v.at === 'function') v.at(0)
|
|
47
|
+
return v.byteLength
|
|
48
|
+
} catch {
|
|
49
|
+
return -1
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
// Whether a view on a resizable buffer follows the buffer's length (V8 writes the flag and a byte length of 0 for it). A view cannot be
|
|
53
|
+
// asked; the buffer is grown by one element and put back, and a view that grew follows it. A buffer already at its largest size is
|
|
54
|
+
// shrunk by one element instead (its last bytes are put back). Nothing outside this call can see it: it runs to its end before any other code.
|
|
55
|
+
const tracksLength = (v, ab) => {
|
|
56
|
+
const before = viewByteLength(v)
|
|
57
|
+
if (before < 0) return false
|
|
58
|
+
const size = ab.byteLength
|
|
59
|
+
const per = v.BYTES_PER_ELEMENT || 1
|
|
60
|
+
try {
|
|
61
|
+
if (size + per <= ab.maxByteLength) {
|
|
62
|
+
ab.resize(size + per)
|
|
63
|
+
const grew = viewByteLength(v) !== before
|
|
64
|
+
ab.resize(size)
|
|
65
|
+
return grew
|
|
66
|
+
}
|
|
67
|
+
if (size - per >= v.byteOffset) {
|
|
68
|
+
const kept = new Uint8Array(ab).slice(size - per)
|
|
69
|
+
ab.resize(size - per)
|
|
70
|
+
const after = viewByteLength(v)
|
|
71
|
+
ab.resize(size)
|
|
72
|
+
new Uint8Array(ab).set(kept, size - per)
|
|
73
|
+
return before >= per && after === before - per // a view of no elements cannot be told from a fixed one of none: it stays fixed
|
|
74
|
+
}
|
|
75
|
+
} catch {}
|
|
76
|
+
return false
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const own = (o, k) => Object.getOwnPropertyDescriptor(o, k)
|
|
80
|
+
const tagOf = (o) => Object.prototype.toString.call(o)
|
|
81
|
+
// a brand check across realms: the getter or method of the intrinsic throws for an object without the internal slot
|
|
82
|
+
const brand = (f) => (o) => {
|
|
83
|
+
try {
|
|
84
|
+
f(o)
|
|
85
|
+
return true
|
|
86
|
+
} catch {
|
|
87
|
+
return false
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
const isDate = brand((o) => Date.prototype.getTime.call(o))
|
|
91
|
+
const isMap = brand((o) => own(Map.prototype, 'size').get.call(o))
|
|
92
|
+
const isSet = brand((o) => own(Set.prototype, 'size').get.call(o))
|
|
93
|
+
const isArrayBuffer = brand((o) => own(ArrayBuffer.prototype, 'byteLength').get.call(o))
|
|
94
|
+
const isSharedArrayBuffer = typeof SharedArrayBuffer === 'function' ? brand((o) => own(SharedArrayBuffer.prototype, 'byteLength').get.call(o)) : () => false
|
|
95
|
+
const isRegExp = brand((o) => {
|
|
96
|
+
if (o === RegExp.prototype) throw 0
|
|
97
|
+
own(RegExp.prototype, 'source').get.call(o)
|
|
98
|
+
})
|
|
99
|
+
const isBoolObj = brand((o) => Boolean.prototype.valueOf.call(o))
|
|
100
|
+
const isNumObj = brand((o) => Number.prototype.valueOf.call(o))
|
|
101
|
+
const isStrObj = brand((o) => String.prototype.valueOf.call(o))
|
|
102
|
+
const isBigObj = brand((o) => BigInt.prototype.valueOf.call(o))
|
|
103
|
+
const NOT_CLONABLE = new Set(['[object Promise]', '[object WeakMap]', '[object WeakSet]', '[object WeakRef]', '[object FinalizationRegistry]', '[object Generator]', '[object AsyncGenerator]', '[object Iterator]'])
|
|
104
|
+
|
|
105
|
+
const isIndexKey = (k) => {
|
|
106
|
+
if (k.length === 0 || k.length > 10) return false
|
|
107
|
+
const c = k.charCodeAt(0)
|
|
108
|
+
if (c < 48 || c > 57 || (c === 48 && k.length > 1)) return false
|
|
109
|
+
for (let i = 1; i < k.length; i++) {
|
|
110
|
+
const d = k.charCodeAt(i)
|
|
111
|
+
if (d < 48 || d > 57) return false
|
|
112
|
+
}
|
|
113
|
+
return Number(k) <= MAX_INDEX
|
|
114
|
+
}
|
|
115
|
+
const isInt32 = (n) => n === (n | 0) && !(n === 0 && 1 / n < 0)
|
|
116
|
+
|
|
117
|
+
class Writer {
|
|
118
|
+
constructor() {
|
|
119
|
+
this.buf = new Uint8Array(256)
|
|
120
|
+
this.len = 0
|
|
121
|
+
this.dv = new DataView(this.buf.buffer)
|
|
122
|
+
}
|
|
123
|
+
need(n) {
|
|
124
|
+
if (this.len + n <= this.buf.length) return
|
|
125
|
+
let cap = this.buf.length * 2
|
|
126
|
+
while (cap < this.len + n) cap *= 2
|
|
127
|
+
const b = new Uint8Array(cap)
|
|
128
|
+
b.set(this.buf.subarray(0, this.len))
|
|
129
|
+
this.buf = b
|
|
130
|
+
this.dv = new DataView(b.buffer)
|
|
131
|
+
}
|
|
132
|
+
byte(x) {
|
|
133
|
+
this.need(1)
|
|
134
|
+
this.buf[this.len++] = x
|
|
135
|
+
}
|
|
136
|
+
varint(n) {
|
|
137
|
+
this.need(10)
|
|
138
|
+
if (n <= 0xffffffff) {
|
|
139
|
+
n >>>= 0
|
|
140
|
+
do {
|
|
141
|
+
let b = n & 0x7f
|
|
142
|
+
n >>>= 7
|
|
143
|
+
if (n) b |= 0x80
|
|
144
|
+
this.buf[this.len++] = b
|
|
145
|
+
} while (n)
|
|
146
|
+
return
|
|
147
|
+
}
|
|
148
|
+
let big = BigInt(n)
|
|
149
|
+
do {
|
|
150
|
+
let b = Number(big & 0x7fn)
|
|
151
|
+
big >>= 7n
|
|
152
|
+
if (big) b |= 0x80
|
|
153
|
+
this.buf[this.len++] = b
|
|
154
|
+
} while (big)
|
|
155
|
+
}
|
|
156
|
+
varintBig(big) {
|
|
157
|
+
this.need(10)
|
|
158
|
+
do {
|
|
159
|
+
let b = Number(big & 0x7fn)
|
|
160
|
+
big >>= 7n
|
|
161
|
+
if (big) b |= 0x80
|
|
162
|
+
this.buf[this.len++] = b
|
|
163
|
+
} while (big)
|
|
164
|
+
}
|
|
165
|
+
f64(n) {
|
|
166
|
+
this.need(8)
|
|
167
|
+
this.dv.setFloat64(this.len, n, true)
|
|
168
|
+
this.len += 8
|
|
169
|
+
}
|
|
170
|
+
bytes(u8) {
|
|
171
|
+
this.need(u8.length)
|
|
172
|
+
this.buf.set(u8, this.len)
|
|
173
|
+
this.len += u8.length
|
|
174
|
+
}
|
|
175
|
+
out() {
|
|
176
|
+
return this.buf.slice(0, this.len)
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
const varintSize = (n) => {
|
|
181
|
+
let c = 1
|
|
182
|
+
while (n >= 128) {
|
|
183
|
+
n = Math.floor(n / 128)
|
|
184
|
+
c++
|
|
185
|
+
}
|
|
186
|
+
return c
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
export function makeSerdesBinding({ bufferOf = null } = {}) {
|
|
190
|
+
class Serializer {
|
|
191
|
+
constructor() {
|
|
192
|
+
this._w = new Writer()
|
|
193
|
+
this._ids = new Map()
|
|
194
|
+
this._next = 0
|
|
195
|
+
this._hostViews = false
|
|
196
|
+
this._transfer = new Map()
|
|
197
|
+
}
|
|
198
|
+
writeHeader() {
|
|
199
|
+
this._w.byte(0xff)
|
|
200
|
+
this._w.varint(VERSION)
|
|
201
|
+
}
|
|
202
|
+
writeValue(value) {
|
|
203
|
+
this._write(value)
|
|
204
|
+
return true
|
|
205
|
+
}
|
|
206
|
+
releaseBuffer() {
|
|
207
|
+
const out = this._w.out()
|
|
208
|
+
this._w = new Writer()
|
|
209
|
+
return bufferOf ? bufferOf(out) : out
|
|
210
|
+
}
|
|
211
|
+
transferArrayBuffer(id, ab) {
|
|
212
|
+
this._transfer.set(ab, Number(id) >>> 0)
|
|
213
|
+
}
|
|
214
|
+
writeUint32(n) {
|
|
215
|
+
this._w.varint(Number(n) >>> 0)
|
|
216
|
+
}
|
|
217
|
+
writeUint64(hi, lo) {
|
|
218
|
+
this._w.varintBig((BigInt(Number(hi) >>> 0) << 32n) | BigInt(Number(lo) >>> 0))
|
|
219
|
+
}
|
|
220
|
+
writeDouble(n) {
|
|
221
|
+
this._w.f64(Number(n))
|
|
222
|
+
}
|
|
223
|
+
writeRawBytes(src) {
|
|
224
|
+
this._w.bytes(rawBytes(src))
|
|
225
|
+
}
|
|
226
|
+
_setTreatArrayBufferViewsAsHostObjects(on) {
|
|
227
|
+
this._hostViews = Boolean(on)
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
_fail(text) {
|
|
231
|
+
const E = this._getDataCloneError || Error
|
|
232
|
+
throw new E(text)
|
|
233
|
+
}
|
|
234
|
+
_describe(v) {
|
|
235
|
+
try {
|
|
236
|
+
if (typeof v === 'function' || typeof v === 'symbol') return String(v)
|
|
237
|
+
const c = v && v.constructor && v.constructor.name
|
|
238
|
+
return `#<${c || 'Object'}>`
|
|
239
|
+
} catch {
|
|
240
|
+
return '#<Object>'
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
_string(s) {
|
|
245
|
+
const w = this._w
|
|
246
|
+
const n = s.length
|
|
247
|
+
let one = true
|
|
248
|
+
for (let i = 0; i < n; i++) {
|
|
249
|
+
if (s.charCodeAt(i) > 255) {
|
|
250
|
+
one = false
|
|
251
|
+
break
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
if (one) {
|
|
255
|
+
w.byte(T.ONE_BYTE)
|
|
256
|
+
w.varint(n)
|
|
257
|
+
w.need(n)
|
|
258
|
+
for (let i = 0; i < n; i++) w.buf[w.len++] = s.charCodeAt(i)
|
|
259
|
+
return
|
|
260
|
+
}
|
|
261
|
+
const bytes = n * 2
|
|
262
|
+
// the data of a two-byte string starts at an even offset: a padding tag goes first when it would not
|
|
263
|
+
if ((w.len + 1 + varintSize(bytes)) & 1) w.byte(T.PAD)
|
|
264
|
+
w.byte(T.TWO_BYTE)
|
|
265
|
+
w.varint(bytes)
|
|
266
|
+
w.need(bytes)
|
|
267
|
+
for (let i = 0; i < n; i++) {
|
|
268
|
+
const c = s.charCodeAt(i)
|
|
269
|
+
w.buf[w.len++] = c & 255
|
|
270
|
+
w.buf[w.len++] = c >> 8
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
_bigintBody(v) {
|
|
274
|
+
const w = this._w
|
|
275
|
+
const neg = v < 0n
|
|
276
|
+
let mag = neg ? -v : v
|
|
277
|
+
const digits = []
|
|
278
|
+
while (mag > 0n) {
|
|
279
|
+
digits.push(mag & 0xffffffffffffffffn)
|
|
280
|
+
mag >>= 64n
|
|
281
|
+
}
|
|
282
|
+
w.varint(digits.length * 8 * 2 + (neg ? 1 : 0))
|
|
283
|
+
for (const d of digits) {
|
|
284
|
+
w.need(8)
|
|
285
|
+
w.dv.setBigUint64(w.len, d, true)
|
|
286
|
+
w.len += 8
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
_number(n) {
|
|
290
|
+
const w = this._w
|
|
291
|
+
if (isInt32(n)) {
|
|
292
|
+
w.byte(T.INT32)
|
|
293
|
+
w.varint(((n << 1) ^ (n >> 31)) >>> 0)
|
|
294
|
+
} else {
|
|
295
|
+
w.byte(T.DOUBLE)
|
|
296
|
+
w.f64(n)
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
_key(k) {
|
|
300
|
+
if (isIndexKey(k)) this._number(Number(k))
|
|
301
|
+
else this._string(k)
|
|
302
|
+
}
|
|
303
|
+
_props(o, keys) {
|
|
304
|
+
let n = 0
|
|
305
|
+
for (const k of keys) {
|
|
306
|
+
// a key a getter removed on the way is not written
|
|
307
|
+
if (!Object.prototype.propertyIsEnumerable.call(o, k)) continue
|
|
308
|
+
this._key(k)
|
|
309
|
+
this._write(o[k])
|
|
310
|
+
n++
|
|
311
|
+
}
|
|
312
|
+
return n
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
_write(v) {
|
|
316
|
+
const w = this._w
|
|
317
|
+
switch (typeof v) {
|
|
318
|
+
case 'undefined':
|
|
319
|
+
return w.byte(T.UNDEF)
|
|
320
|
+
case 'boolean':
|
|
321
|
+
return w.byte(v ? T.TRUE : T.FALSE)
|
|
322
|
+
case 'number':
|
|
323
|
+
return this._number(v)
|
|
324
|
+
case 'bigint':
|
|
325
|
+
w.byte(T.BIGINT)
|
|
326
|
+
return this._bigintBody(v)
|
|
327
|
+
case 'string':
|
|
328
|
+
return this._string(v)
|
|
329
|
+
case 'object':
|
|
330
|
+
if (v === null) return w.byte(T.NULL)
|
|
331
|
+
return this._receiver(v)
|
|
332
|
+
default:
|
|
333
|
+
return this._fail(`${this._describe(v)} could not be cloned.`)
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
_receiver(o) {
|
|
338
|
+
const w = this._w
|
|
339
|
+
const seen = this._ids.get(o)
|
|
340
|
+
if (seen !== undefined) {
|
|
341
|
+
w.byte(T.REF)
|
|
342
|
+
return w.varint(seen)
|
|
343
|
+
}
|
|
344
|
+
// an array buffer view written as a view takes its id after its buffer (V8 writes the buffer first)
|
|
345
|
+
if (!this._hostViews && !Array.isArray(o) && ArrayBuffer.isView(o)) return this._view(o)
|
|
346
|
+
this._ids.set(o, this._next++)
|
|
347
|
+
if (Array.isArray(o)) return this._array(o)
|
|
348
|
+
if (isDate(o)) {
|
|
349
|
+
w.byte(T.DATE)
|
|
350
|
+
return w.f64(Date.prototype.getTime.call(o))
|
|
351
|
+
}
|
|
352
|
+
if (isBoolObj(o)) return w.byte(Boolean.prototype.valueOf.call(o) ? T.TRUE_OBJ : T.FALSE_OBJ)
|
|
353
|
+
if (isNumObj(o)) {
|
|
354
|
+
w.byte(T.NUM_OBJ)
|
|
355
|
+
return w.f64(Number.prototype.valueOf.call(o))
|
|
356
|
+
}
|
|
357
|
+
if (isStrObj(o)) {
|
|
358
|
+
w.byte(T.STR_OBJ)
|
|
359
|
+
return this._string(String.prototype.valueOf.call(o))
|
|
360
|
+
}
|
|
361
|
+
if (isBigObj(o)) {
|
|
362
|
+
w.byte(T.BIGINT_OBJ)
|
|
363
|
+
return this._bigintBody(BigInt.prototype.valueOf.call(o))
|
|
364
|
+
}
|
|
365
|
+
if (isRegExp(o)) {
|
|
366
|
+
w.byte(T.REGEXP)
|
|
367
|
+
this._string(own(RegExp.prototype, 'source').get.call(o))
|
|
368
|
+
const flags = own(RegExp.prototype, 'flags').get.call(o)
|
|
369
|
+
let bits = 0
|
|
370
|
+
for (const [c, b] of FLAG_BITS) if (flags.includes(c)) bits |= b
|
|
371
|
+
return w.varint(bits)
|
|
372
|
+
}
|
|
373
|
+
if (isMap(o)) {
|
|
374
|
+
w.byte(T.MAP)
|
|
375
|
+
const entries = Array.from(Map.prototype.entries.call(o))
|
|
376
|
+
for (const [k, x] of entries) {
|
|
377
|
+
this._write(k)
|
|
378
|
+
this._write(x)
|
|
379
|
+
}
|
|
380
|
+
w.byte(T.END_MAP)
|
|
381
|
+
return w.varint(entries.length * 2)
|
|
382
|
+
}
|
|
383
|
+
if (isSet(o)) {
|
|
384
|
+
w.byte(T.SET)
|
|
385
|
+
const items = Array.from(Set.prototype.values.call(o))
|
|
386
|
+
for (const x of items) this._write(x)
|
|
387
|
+
w.byte(T.END_SET)
|
|
388
|
+
return w.varint(items.length)
|
|
389
|
+
}
|
|
390
|
+
if (isArrayBuffer(o)) return this._arrayBuffer(o)
|
|
391
|
+
if (isSharedArrayBuffer(o)) {
|
|
392
|
+
if (typeof this._getSharedArrayBufferId !== 'function') return this._fail(`${this._describe(o)} could not be cloned.`)
|
|
393
|
+
w.byte(T.SHARED_ABUF)
|
|
394
|
+
return w.varint(Number(this._getSharedArrayBufferId(o)) >>> 0)
|
|
395
|
+
}
|
|
396
|
+
if (ArrayBuffer.isView(o)) return this._view(o)
|
|
397
|
+
if (typeof o === 'function') return this._fail(`${String(o)} could not be cloned.`)
|
|
398
|
+
const tag = tagOf(o)
|
|
399
|
+
if (tag === '[object Error]') return this._error(o)
|
|
400
|
+
if (NOT_CLONABLE.has(tag)) return this._fail(`${this._describe(o)} could not be cloned.`)
|
|
401
|
+
w.byte(T.OBJECT)
|
|
402
|
+
const n = this._props(o, Object.keys(o))
|
|
403
|
+
w.byte(T.END_OBJECT)
|
|
404
|
+
return w.varint(n)
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
_array(a) {
|
|
408
|
+
const w = this._w
|
|
409
|
+
const len = a.length
|
|
410
|
+
let packed = true
|
|
411
|
+
for (let i = 0; i < len; i++) {
|
|
412
|
+
if (!(i in a)) {
|
|
413
|
+
packed = false
|
|
414
|
+
break
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
if (!packed) {
|
|
418
|
+
w.byte(T.SPARSE)
|
|
419
|
+
w.varint(len)
|
|
420
|
+
const n = this._props(a, Object.keys(a))
|
|
421
|
+
w.byte(T.END_SPARSE)
|
|
422
|
+
w.varint(n)
|
|
423
|
+
return w.varint(len)
|
|
424
|
+
}
|
|
425
|
+
w.byte(T.DENSE)
|
|
426
|
+
w.varint(len)
|
|
427
|
+
// an array of numbers with a fractional one is a double array in V8: every element is written as a double
|
|
428
|
+
let doubles = len > 0
|
|
429
|
+
let fractional = false
|
|
430
|
+
for (let i = 0; i < len && doubles; i++) {
|
|
431
|
+
const x = a[i]
|
|
432
|
+
if (typeof x !== 'number') doubles = false
|
|
433
|
+
else if (!isInt32(x)) fractional = true
|
|
434
|
+
}
|
|
435
|
+
doubles = doubles && fractional
|
|
436
|
+
for (let i = 0; i < len; i++) {
|
|
437
|
+
if (doubles) {
|
|
438
|
+
w.byte(T.DOUBLE)
|
|
439
|
+
w.f64(a[i])
|
|
440
|
+
} else this._write(a[i])
|
|
441
|
+
}
|
|
442
|
+
const keys = Object.keys(a)
|
|
443
|
+
const named = keys.length > len ? keys.slice(len) : []
|
|
444
|
+
const n = this._props(a, named)
|
|
445
|
+
w.byte(T.END_DENSE)
|
|
446
|
+
w.varint(n)
|
|
447
|
+
return w.varint(len)
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
_arrayBuffer(ab) {
|
|
451
|
+
const w = this._w
|
|
452
|
+
const moved = this._transfer.get(ab)
|
|
453
|
+
if (moved !== undefined) {
|
|
454
|
+
w.byte(T.TRANSFERRED_ABUF)
|
|
455
|
+
return w.varint(moved)
|
|
456
|
+
}
|
|
457
|
+
const bytes = new Uint8Array(ab)
|
|
458
|
+
if (ab.resizable) {
|
|
459
|
+
w.byte(T.RESIZABLE_ABUF)
|
|
460
|
+
w.varint(ab.byteLength)
|
|
461
|
+
w.varint(ab.maxByteLength)
|
|
462
|
+
} else {
|
|
463
|
+
w.byte(T.ABUF)
|
|
464
|
+
w.varint(ab.byteLength)
|
|
465
|
+
}
|
|
466
|
+
return w.bytes(bytes)
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
_view(v) {
|
|
470
|
+
const w = this._w
|
|
471
|
+
if (this._hostViews) {
|
|
472
|
+
if (typeof this._writeHostObject !== 'function') return this._fail(`${this._describe(v)} could not be cloned.`)
|
|
473
|
+
w.byte(T.HOST)
|
|
474
|
+
return this._writeHostObject(v)
|
|
475
|
+
}
|
|
476
|
+
const kind = VIEW_TAGS.findIndex(([name]) => typeof globalThis[name] === 'function' && v instanceof globalThis[name])
|
|
477
|
+
const k = kind >= 0 ? kind : VIEW_TAGS.findIndex(([name]) => name !== 'DataView' && tagOf(v) === `[object ${name}]`)
|
|
478
|
+
const idx = k >= 0 ? k : tagOf(v) === '[object DataView]' ? VIEW_TAGS.length - 1 : -1
|
|
479
|
+
if (idx < 0) return this._fail(`${this._describe(v)} could not be cloned.`)
|
|
480
|
+
// a view on a resizable buffer says so, and says when it follows the buffer's length (then its byte length is 0, as V8 writes it)
|
|
481
|
+
let flags = 0
|
|
482
|
+
let length = v.byteLength
|
|
483
|
+
if (v.buffer.resizable === true) {
|
|
484
|
+
flags = VIEW_BACKED_BY_RAB
|
|
485
|
+
if (tracksLength(v, v.buffer)) {
|
|
486
|
+
flags |= VIEW_LENGTH_TRACKING
|
|
487
|
+
length = 0
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
// the buffer goes first (it is written when it has no id yet), then the view takes its id
|
|
491
|
+
if (!this._ids.has(v.buffer)) {
|
|
492
|
+
this._ids.set(v.buffer, this._next++)
|
|
493
|
+
this._arrayBuffer(v.buffer)
|
|
494
|
+
} else {
|
|
495
|
+
w.byte(T.REF)
|
|
496
|
+
w.varint(this._ids.get(v.buffer))
|
|
497
|
+
}
|
|
498
|
+
this._ids.set(v, this._next++)
|
|
499
|
+
w.byte(T.VIEW)
|
|
500
|
+
w.byte(VIEW_TAGS[idx][1])
|
|
501
|
+
w.varint(v.byteOffset)
|
|
502
|
+
w.varint(length)
|
|
503
|
+
return w.varint(flags)
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
_error(e) {
|
|
507
|
+
const w = this._w
|
|
508
|
+
w.byte(T.ERROR)
|
|
509
|
+
let name = ''
|
|
510
|
+
try {
|
|
511
|
+
name = String(e.name)
|
|
512
|
+
} catch {
|
|
513
|
+
name = ''
|
|
514
|
+
}
|
|
515
|
+
if (Object.hasOwn(ERROR_NAMES, name)) w.byte(ERROR_NAMES[name])
|
|
516
|
+
const m = own(e, 'message')
|
|
517
|
+
if (m && 'value' in m) {
|
|
518
|
+
w.byte(ERR.MESSAGE)
|
|
519
|
+
this._string(String(m.value))
|
|
520
|
+
}
|
|
521
|
+
let stack
|
|
522
|
+
try {
|
|
523
|
+
stack = e.stack
|
|
524
|
+
} catch {
|
|
525
|
+
stack = undefined
|
|
526
|
+
}
|
|
527
|
+
if (typeof stack === 'string') {
|
|
528
|
+
w.byte(ERR.STACK)
|
|
529
|
+
this._string(stack)
|
|
530
|
+
}
|
|
531
|
+
const c = own(e, 'cause')
|
|
532
|
+
if (c && 'value' in c) {
|
|
533
|
+
w.byte(ERR.CAUSE)
|
|
534
|
+
this._write(c.value)
|
|
535
|
+
}
|
|
536
|
+
return w.byte(ERR.END)
|
|
537
|
+
}
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
class Deserializer {
|
|
541
|
+
constructor(value) {
|
|
542
|
+
this.buffer = value
|
|
543
|
+
this.bytes = rawBytes(value)
|
|
544
|
+
this.offset = 0
|
|
545
|
+
this._version = 0
|
|
546
|
+
this._objects = []
|
|
547
|
+
this._transfer = new Map()
|
|
548
|
+
this._dv = new DataView(this.bytes.buffer, this.bytes.byteOffset, this.bytes.byteLength)
|
|
549
|
+
}
|
|
550
|
+
// V8's ReadHeader and the API's check: no version tag, a cut version, a version above 15 or below 13 give the version error
|
|
551
|
+
readHeader() {
|
|
552
|
+
let version = 0
|
|
553
|
+
if (this.bytes[this.offset] === 0xff) {
|
|
554
|
+
this.offset++
|
|
555
|
+
try {
|
|
556
|
+
version = this._varint32()
|
|
557
|
+
} catch (e) {
|
|
558
|
+
throw e && e.__serdes ? new Error(VERSION_ERROR) : e
|
|
559
|
+
}
|
|
560
|
+
this._version = version
|
|
561
|
+
if (version > VERSION) throw new Error(VERSION_ERROR)
|
|
562
|
+
}
|
|
563
|
+
if (version < MIN_VERSION) throw new Error(VERSION_ERROR)
|
|
564
|
+
return true
|
|
565
|
+
}
|
|
566
|
+
readValue() {
|
|
567
|
+
try {
|
|
568
|
+
return this._read()
|
|
569
|
+
} catch (e) {
|
|
570
|
+
if (e && e.__serdes) throw new Error('Unable to deserialize cloned data.')
|
|
571
|
+
throw e
|
|
572
|
+
}
|
|
573
|
+
}
|
|
574
|
+
getWireFormatVersion() {
|
|
575
|
+
return this._version
|
|
576
|
+
}
|
|
577
|
+
transferArrayBuffer(id, ab) {
|
|
578
|
+
this._transfer.set(Number(id) >>> 0, ab)
|
|
579
|
+
}
|
|
580
|
+
// the reads a program makes itself fail with node's texts (src/node_serdes.cc), not with the reader's own
|
|
581
|
+
_called(text, f) {
|
|
582
|
+
try {
|
|
583
|
+
return f()
|
|
584
|
+
} catch (e) {
|
|
585
|
+
throw e && e.__serdes ? new Error(text) : e
|
|
586
|
+
}
|
|
587
|
+
}
|
|
588
|
+
readUint32() {
|
|
589
|
+
return this._called('ReadUint32() failed', () => this._varint32())
|
|
590
|
+
}
|
|
591
|
+
readUint64() {
|
|
592
|
+
return this._called('ReadUint64() failed', () => {
|
|
593
|
+
const big = this._varintBig()
|
|
594
|
+
return [Number(big >> 32n) >>> 0, Number(big & 0xffffffffn)]
|
|
595
|
+
})
|
|
596
|
+
}
|
|
597
|
+
readDouble() {
|
|
598
|
+
return this._called('ReadDouble() failed', () => {
|
|
599
|
+
this._need(8)
|
|
600
|
+
const n = this._dv.getFloat64(this.offset, true)
|
|
601
|
+
this.offset += 8
|
|
602
|
+
return n
|
|
603
|
+
})
|
|
604
|
+
}
|
|
605
|
+
_readRawBytes(length) {
|
|
606
|
+
const n = Number(length) >>> 0
|
|
607
|
+
return this._called('ReadRawBytes() failed', () => {
|
|
608
|
+
this._need(n)
|
|
609
|
+
const at = this.offset
|
|
610
|
+
this.offset += n
|
|
611
|
+
return at
|
|
612
|
+
})
|
|
613
|
+
}
|
|
614
|
+
|
|
615
|
+
_bad() {
|
|
616
|
+
const e = new Error('bad')
|
|
617
|
+
e.__serdes = true
|
|
618
|
+
return e
|
|
619
|
+
}
|
|
620
|
+
_need(n) {
|
|
621
|
+
if (n < 0 || this.offset + n > this.bytes.length) throw this._bad()
|
|
622
|
+
}
|
|
623
|
+
_byte() {
|
|
624
|
+
this._need(1)
|
|
625
|
+
return this.bytes[this.offset++]
|
|
626
|
+
}
|
|
627
|
+
// V8's ReadVarint<uint32_t>: five bytes at most, the high bits dropped; a sixth byte must exist but is not consumed
|
|
628
|
+
_varint32() {
|
|
629
|
+
let n = 0
|
|
630
|
+
for (let i = 0; ; i++) {
|
|
631
|
+
this._need(1)
|
|
632
|
+
const b = this.bytes[this.offset]
|
|
633
|
+
if (i === 5) return n >>> 0
|
|
634
|
+
this.offset++
|
|
635
|
+
n |= (b & 0x7f) << (7 * i)
|
|
636
|
+
if (!(b & 0x80)) return n >>> 0
|
|
637
|
+
}
|
|
638
|
+
}
|
|
639
|
+
_varint() {
|
|
640
|
+
let n = 0
|
|
641
|
+
let scale = 1
|
|
642
|
+
for (let i = 0; ; i++) {
|
|
643
|
+
const b = this._byte()
|
|
644
|
+
n += (b & 0x7f) * scale
|
|
645
|
+
if (!(b & 0x80)) return n
|
|
646
|
+
scale *= 128
|
|
647
|
+
if (i > 8) throw this._bad()
|
|
648
|
+
}
|
|
649
|
+
}
|
|
650
|
+
_varintBig() {
|
|
651
|
+
let n = 0n
|
|
652
|
+
let shift = 0n
|
|
653
|
+
for (;;) {
|
|
654
|
+
const b = this._byte()
|
|
655
|
+
n |= BigInt(b & 0x7f) << shift
|
|
656
|
+
if (!(b & 0x80)) return n
|
|
657
|
+
shift += 7n
|
|
658
|
+
if (shift > 70n) throw this._bad()
|
|
659
|
+
}
|
|
660
|
+
}
|
|
661
|
+
_peek() {
|
|
662
|
+
return this.offset < this.bytes.length ? this.bytes[this.offset] : -1
|
|
663
|
+
}
|
|
664
|
+
_skipPadding() {
|
|
665
|
+
while (this._peek() === T.PAD) this.offset++
|
|
666
|
+
}
|
|
667
|
+
_stringBody(tag) {
|
|
668
|
+
if (tag === T.ONE_BYTE) {
|
|
669
|
+
const n = this._varint()
|
|
670
|
+
this._need(n)
|
|
671
|
+
let s = ''
|
|
672
|
+
for (let i = 0; i < n; i += 8192) s += String.fromCharCode.apply(null, this.bytes.subarray(this.offset + i, this.offset + Math.min(n, i + 8192)))
|
|
673
|
+
this.offset += n
|
|
674
|
+
return s
|
|
675
|
+
}
|
|
676
|
+
if (tag === T.TWO_BYTE) {
|
|
677
|
+
const n = this._varint()
|
|
678
|
+
if (n & 1) throw this._bad()
|
|
679
|
+
this._need(n)
|
|
680
|
+
let s = ''
|
|
681
|
+
const units = n / 2
|
|
682
|
+
for (let i = 0; i < units; i += 4096) {
|
|
683
|
+
const part = []
|
|
684
|
+
for (let j = i; j < Math.min(units, i + 4096); j++) part.push(this.bytes[this.offset + j * 2] | (this.bytes[this.offset + j * 2 + 1] << 8))
|
|
685
|
+
s += String.fromCharCode.apply(null, part)
|
|
686
|
+
}
|
|
687
|
+
this.offset += n
|
|
688
|
+
return s
|
|
689
|
+
}
|
|
690
|
+
if (tag === T.UTF8) {
|
|
691
|
+
const n = this._varint()
|
|
692
|
+
this._need(n)
|
|
693
|
+
const s = new TextDecoder().decode(this.bytes.subarray(this.offset, this.offset + n))
|
|
694
|
+
this.offset += n
|
|
695
|
+
return s
|
|
696
|
+
}
|
|
697
|
+
throw this._bad()
|
|
698
|
+
}
|
|
699
|
+
_readString() {
|
|
700
|
+
this._skipPadding()
|
|
701
|
+
return this._stringBody(this._byte())
|
|
702
|
+
}
|
|
703
|
+
_bigintBody() {
|
|
704
|
+
const bits = this._varint()
|
|
705
|
+
const neg = bits & 1
|
|
706
|
+
const n = Math.floor(bits / 2)
|
|
707
|
+
if (neg && n === 0) throw this._bad() // V8 has no negative zero
|
|
708
|
+
this._need(n)
|
|
709
|
+
// V8 takes any length (the last digit is padded): the bytes of a part digit are the highest
|
|
710
|
+
const tail = n % 8
|
|
711
|
+
let v = 0n
|
|
712
|
+
for (let i = n - 1; i >= n - tail; i--) v = (v << 8n) | BigInt(this.bytes[this.offset + i])
|
|
713
|
+
for (let i = (n - tail) / 8 - 1; i >= 0; i--) v = (v << 64n) | this._dv.getBigUint64(this.offset + i * 8, true)
|
|
714
|
+
this.offset += n
|
|
715
|
+
return neg ? -v : v
|
|
716
|
+
}
|
|
717
|
+
_add(o) {
|
|
718
|
+
this._objects.push(o)
|
|
719
|
+
return o
|
|
720
|
+
}
|
|
721
|
+
_setKey(o, k, v) {
|
|
722
|
+
if (Object.hasOwn(o, k)) throw this._bad() // V8 refuses a key the object already has
|
|
723
|
+
if (k === '__proto__') Object.defineProperty(o, k, { value: v, writable: true, enumerable: true, configurable: true })
|
|
724
|
+
else o[k] = v
|
|
725
|
+
}
|
|
726
|
+
_props(o, endTag) {
|
|
727
|
+
let n = 0
|
|
728
|
+
for (;;) {
|
|
729
|
+
this._skipPadding()
|
|
730
|
+
if (this._peek() === endTag) {
|
|
731
|
+
this.offset++
|
|
732
|
+
return n
|
|
733
|
+
}
|
|
734
|
+
const k = this._read()
|
|
735
|
+
if (typeof k !== 'string' && typeof k !== 'number') throw this._bad()
|
|
736
|
+
this._setKey(o, k, this._read())
|
|
737
|
+
n++
|
|
738
|
+
}
|
|
739
|
+
}
|
|
740
|
+
|
|
741
|
+
_read() {
|
|
742
|
+
this._skipPadding()
|
|
743
|
+
const t = this._byte()
|
|
744
|
+
switch (t) {
|
|
745
|
+
case T.VERIFY:
|
|
746
|
+
this._varint32() // V8 reads the object count and ignores it
|
|
747
|
+
return this._read()
|
|
748
|
+
case T.UNDEF:
|
|
749
|
+
return undefined
|
|
750
|
+
case T.NULL:
|
|
751
|
+
return null
|
|
752
|
+
case T.TRUE:
|
|
753
|
+
return true
|
|
754
|
+
case T.FALSE:
|
|
755
|
+
return false
|
|
756
|
+
case T.INT32: {
|
|
757
|
+
const z = this._varint()
|
|
758
|
+
return (z >>> 1) ^ -(z & 1)
|
|
759
|
+
}
|
|
760
|
+
case T.UINT32:
|
|
761
|
+
return this._varint()
|
|
762
|
+
case T.DOUBLE: {
|
|
763
|
+
this._need(8)
|
|
764
|
+
const n = this._dv.getFloat64(this.offset, true)
|
|
765
|
+
this.offset += 8
|
|
766
|
+
return n
|
|
767
|
+
}
|
|
768
|
+
case T.BIGINT:
|
|
769
|
+
return this._bigintBody()
|
|
770
|
+
case T.ONE_BYTE:
|
|
771
|
+
case T.TWO_BYTE:
|
|
772
|
+
case T.UTF8:
|
|
773
|
+
return this._stringBody(t)
|
|
774
|
+
case T.REF: {
|
|
775
|
+
const id = this._varint()
|
|
776
|
+
if (id >= this._objects.length) throw this._bad()
|
|
777
|
+
const o = this._objects[id]
|
|
778
|
+
return o instanceof ArrayBuffer ? this._maybeView(o) : o
|
|
779
|
+
}
|
|
780
|
+
case T.OBJECT: {
|
|
781
|
+
const o = this._add({})
|
|
782
|
+
const n = this._props(o, T.END_OBJECT)
|
|
783
|
+
if (this._varint() !== n) throw this._bad()
|
|
784
|
+
return o
|
|
785
|
+
}
|
|
786
|
+
case T.DENSE: {
|
|
787
|
+
const len = this._varint()
|
|
788
|
+
if (len > this.bytes.length - this.offset) throw this._bad() // V8: each element takes a byte at least
|
|
789
|
+
const a = this._add(new Array(len))
|
|
790
|
+
for (let i = 0; i < len; i++) {
|
|
791
|
+
if (this._peek() === T.HOLE) {
|
|
792
|
+
this.offset++
|
|
793
|
+
continue
|
|
794
|
+
}
|
|
795
|
+
a[i] = this._read()
|
|
796
|
+
}
|
|
797
|
+
const n = this._props(a, T.END_DENSE)
|
|
798
|
+
if (this._varint() !== n) throw this._bad()
|
|
799
|
+
if (this._varint() !== len) throw this._bad()
|
|
800
|
+
return a
|
|
801
|
+
}
|
|
802
|
+
case T.SPARSE: {
|
|
803
|
+
const len = this._varint()
|
|
804
|
+
const a = this._add([])
|
|
805
|
+
a.length = len
|
|
806
|
+
const n = this._props(a, T.END_SPARSE)
|
|
807
|
+
if (this._varint() !== n) throw this._bad()
|
|
808
|
+
if (this._varint() !== len) throw this._bad()
|
|
809
|
+
return a
|
|
810
|
+
}
|
|
811
|
+
case T.DATE: {
|
|
812
|
+
this._need(8)
|
|
813
|
+
const d = this._add(new Date(this._dv.getFloat64(this.offset, true)))
|
|
814
|
+
this.offset += 8
|
|
815
|
+
return d
|
|
816
|
+
}
|
|
817
|
+
case T.TRUE_OBJ:
|
|
818
|
+
return this._add(Object(true))
|
|
819
|
+
case T.FALSE_OBJ:
|
|
820
|
+
return this._add(Object(false))
|
|
821
|
+
case T.NUM_OBJ: {
|
|
822
|
+
this._need(8)
|
|
823
|
+
const n = this._add(Object(this._dv.getFloat64(this.offset, true)))
|
|
824
|
+
this.offset += 8
|
|
825
|
+
return n
|
|
826
|
+
}
|
|
827
|
+
case T.BIGINT_OBJ:
|
|
828
|
+
return this._add(Object(this._bigintBody()))
|
|
829
|
+
case T.STR_OBJ:
|
|
830
|
+
return this._add(Object(this._readString()))
|
|
831
|
+
case T.REGEXP: {
|
|
832
|
+
const source = this._readString()
|
|
833
|
+
const bits = this._varint32()
|
|
834
|
+
// V8: only the nine flag bits, not the linear one (64), and not both u and v
|
|
835
|
+
if (bits & ~0x1bf || (bits & 16 && bits & 256)) throw this._bad()
|
|
836
|
+
let flags = ''
|
|
837
|
+
for (const [c, b] of FLAG_BITS) if (bits & b) flags += c
|
|
838
|
+
return this._add(new RegExp(source, flags)) // a pattern that does not compile throws the engine's SyntaxError, as V8's reader does
|
|
839
|
+
}
|
|
840
|
+
case T.MAP: {
|
|
841
|
+
const m = this._add(new Map())
|
|
842
|
+
let n = 0
|
|
843
|
+
for (;;) {
|
|
844
|
+
this._skipPadding()
|
|
845
|
+
if (this._peek() === T.END_MAP) {
|
|
846
|
+
this.offset++
|
|
847
|
+
break
|
|
848
|
+
}
|
|
849
|
+
const k = this._read()
|
|
850
|
+
m.set(k, this._read())
|
|
851
|
+
n += 2
|
|
852
|
+
}
|
|
853
|
+
if (this._varint() !== n) throw this._bad()
|
|
854
|
+
return m
|
|
855
|
+
}
|
|
856
|
+
case T.SET: {
|
|
857
|
+
const s = this._add(new Set())
|
|
858
|
+
let n = 0
|
|
859
|
+
for (;;) {
|
|
860
|
+
this._skipPadding()
|
|
861
|
+
if (this._peek() === T.END_SET) {
|
|
862
|
+
this.offset++
|
|
863
|
+
break
|
|
864
|
+
}
|
|
865
|
+
s.add(this._read())
|
|
866
|
+
n++
|
|
867
|
+
}
|
|
868
|
+
if (this._varint() !== n) throw this._bad()
|
|
869
|
+
return s
|
|
870
|
+
}
|
|
871
|
+
case T.ABUF:
|
|
872
|
+
case T.RESIZABLE_ABUF: {
|
|
873
|
+
const len = this._varint()
|
|
874
|
+
const max = t === T.RESIZABLE_ABUF ? this._varint() : len
|
|
875
|
+
this._need(len)
|
|
876
|
+
const ab = t === T.RESIZABLE_ABUF ? new ArrayBuffer(len, { maxByteLength: max }) : new ArrayBuffer(len)
|
|
877
|
+
new Uint8Array(ab).set(this.bytes.subarray(this.offset, this.offset + len))
|
|
878
|
+
this.offset += len
|
|
879
|
+
this._add(ab)
|
|
880
|
+
return this._maybeView(ab)
|
|
881
|
+
}
|
|
882
|
+
case T.TRANSFERRED_ABUF: {
|
|
883
|
+
const ab = this._transfer.get(this._varint())
|
|
884
|
+
if (!ab) throw this._bad()
|
|
885
|
+
this._add(ab)
|
|
886
|
+
return this._maybeView(ab)
|
|
887
|
+
}
|
|
888
|
+
case T.HOST: {
|
|
889
|
+
if (typeof this._readHostObject !== 'function') throw this._bad()
|
|
890
|
+
const at = this._objects.length
|
|
891
|
+
this._objects.push(undefined)
|
|
892
|
+
const o = this._readHostObject()
|
|
893
|
+
this._objects[at] = o
|
|
894
|
+
return o
|
|
895
|
+
}
|
|
896
|
+
case T.ERROR:
|
|
897
|
+
return this._readError()
|
|
898
|
+
default:
|
|
899
|
+
throw this._bad()
|
|
900
|
+
}
|
|
901
|
+
}
|
|
902
|
+
|
|
903
|
+
_maybeView(ab) {
|
|
904
|
+
if (this._peek() !== T.VIEW) return ab
|
|
905
|
+
this.offset++
|
|
906
|
+
const sub = this._byte()
|
|
907
|
+
const k = VIEW_TAGS.findIndex((x) => x[1] === sub)
|
|
908
|
+
if (k < 0) throw this._bad()
|
|
909
|
+
const offset = this._varint()
|
|
910
|
+
const length = this._varint()
|
|
911
|
+
const flags = this._version >= 14 ? this._varint() >>> 0 : 0
|
|
912
|
+
const tracking = (flags & VIEW_LENGTH_TRACKING) !== 0
|
|
913
|
+
const rab = (flags & VIEW_BACKED_BY_RAB) !== 0
|
|
914
|
+
// V8's ValidateJSArrayBufferViewFlags: the flags and the buffer must agree on whether it is resizable
|
|
915
|
+
if ((tracking || rab) && ab.resizable !== true) throw this._bad()
|
|
916
|
+
if (ab.resizable === true && !rab) throw this._bad()
|
|
917
|
+
const C = globalThis[VIEW_TAGS[k][0]]
|
|
918
|
+
if (typeof C !== 'function') throw this._bad()
|
|
919
|
+
const per = C === DataView ? 1 : C.BYTES_PER_ELEMENT || 1
|
|
920
|
+
if (offset % per || length % per) throw this._bad() // V8: the offset and the length are whole elements
|
|
921
|
+
let v
|
|
922
|
+
try {
|
|
923
|
+
if (C === DataView) v = tracking ? new DataView(ab, offset) : new DataView(ab, offset, length)
|
|
924
|
+
else v = tracking ? new C(ab, offset) : new C(ab, offset, length / (C.BYTES_PER_ELEMENT || 1))
|
|
925
|
+
} catch {
|
|
926
|
+
throw this._bad()
|
|
927
|
+
}
|
|
928
|
+
return this._add(v)
|
|
929
|
+
}
|
|
930
|
+
|
|
931
|
+
_readError() {
|
|
932
|
+
const at = this._objects.length
|
|
933
|
+
this._objects.push(undefined)
|
|
934
|
+
let Ctor = Error
|
|
935
|
+
let message
|
|
936
|
+
let stack
|
|
937
|
+
let hasCause = false
|
|
938
|
+
let cause
|
|
939
|
+
for (;;) {
|
|
940
|
+
const t = this._byte()
|
|
941
|
+
if (t === ERR.END) break
|
|
942
|
+
if (t === ERR.EVAL) Ctor = EvalError
|
|
943
|
+
else if (t === ERR.RANGE) Ctor = RangeError
|
|
944
|
+
else if (t === ERR.REFERENCE) Ctor = ReferenceError
|
|
945
|
+
else if (t === ERR.SYNTAX) Ctor = SyntaxError
|
|
946
|
+
else if (t === ERR.TYPE) Ctor = TypeError
|
|
947
|
+
else if (t === ERR.URI) Ctor = URIError
|
|
948
|
+
else if (t === ERR.MESSAGE) message = this._readString()
|
|
949
|
+
else if (t === ERR.STACK) stack = this._readString()
|
|
950
|
+
else if (t === ERR.CAUSE) {
|
|
951
|
+
hasCause = true
|
|
952
|
+
cause = this._read()
|
|
953
|
+
} else throw this._bad()
|
|
954
|
+
}
|
|
955
|
+
const e = message === undefined ? new Ctor() : new Ctor(message)
|
|
956
|
+
this._objects[at] = e
|
|
957
|
+
if (hasCause) Object.defineProperty(e, 'cause', { value: cause, writable: true, enumerable: false, configurable: true })
|
|
958
|
+
if (stack !== undefined) Object.defineProperty(e, 'stack', { value: stack, writable: true, enumerable: false, configurable: true })
|
|
959
|
+
return e
|
|
960
|
+
}
|
|
961
|
+
}
|
|
962
|
+
|
|
963
|
+
return { Serializer, Deserializer }
|
|
964
|
+
}
|
|
965
|
+
|
|
966
|
+
function rawBytes(value) {
|
|
967
|
+
if (value instanceof ArrayBuffer || (typeof SharedArrayBuffer === 'function' && value instanceof SharedArrayBuffer)) return new Uint8Array(value)
|
|
968
|
+
if (ArrayBuffer.isView(value)) return new Uint8Array(value.buffer, value.byteOffset, value.byteLength)
|
|
969
|
+
throw new TypeError('buffer must be an ArrayBuffer or an ArrayBuffer view')
|
|
970
|
+
}
|