json-as 1.3.0 → 1.3.1
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/CHANGELOG.md +417 -0
- package/README.md +96 -14
- package/assembly/deserialize/index/typedarray.ts +14 -0
- package/assembly/deserialize/simd/string.ts +3 -3
- package/assembly/deserialize/simple/string.ts +88 -2
- package/assembly/deserialize/swar/string.ts +8 -11
- package/assembly/index.ts +137 -181
- package/assembly/serialize/index/arbitrary.ts +5 -0
- package/assembly/serialize/index/typedarray.ts +65 -0
- package/assembly/serialize/simd/string.ts +3 -11
- package/assembly/serialize/simple/arbitrary.ts +6 -0
- package/assembly/serialize/simple/string.ts +3 -11
- package/assembly/serialize/swar/string.ts +3 -11
- package/assembly/util/swar.ts +49 -0
- package/lib/as-bs.ts +88 -69
- package/package.json +19 -1
- package/transform/lib/index.d.ts +1 -0
- package/transform/lib/index.d.ts.map +1 -1
- package/transform/lib/index.js +153 -32
- package/transform/lib/index.js.map +1 -1
- package/transform/lib/linkers/custom.d.ts.map +1 -1
- package/transform/lib/linkers/custom.js +7 -3
- package/transform/lib/linkers/custom.js.map +1 -1
- package/transform/lib/types.d.ts +2 -0
- package/transform/lib/types.d.ts.map +1 -1
- package/transform/lib/types.js +13 -0
- package/transform/lib/types.js.map +1 -1
- package/.as-test/coverage/naive/coverage.arbitrary.spec.ts.log.json +0 -5628
- package/.as-test/coverage/naive/coverage.array.spec.ts.log.json +0 -5187
- package/.as-test/coverage/naive/coverage.bool.spec.ts.log.json +0 -5187
- package/.as-test/coverage/naive/coverage.box.spec.ts.log.json +0 -5187
- package/.as-test/coverage/naive/coverage.custom.spec.ts.log.json +0 -5187
- package/.as-test/coverage/naive/coverage.date.spec.ts.log.json +0 -5187
- package/.as-test/coverage/naive/coverage.enum.spec.ts.log.json +0 -5187
- package/.as-test/coverage/naive/coverage.float.spec.ts.log.json +0 -5187
- package/.as-test/coverage/naive/coverage.generics.spec.ts.log.json +0 -5187
- package/.as-test/coverage/naive/coverage.hierarchy.spec.ts.log.json +0 -5187
- package/.as-test/coverage/naive/coverage.integer.spec.ts.log.json +0 -5187
- package/.as-test/coverage/naive/coverage.map.spec.ts.log.json +0 -5187
- package/.as-test/coverage/naive/coverage.namespace.spec.ts.log.json +0 -5187
- package/.as-test/coverage/naive/coverage.null.spec.ts.log.json +0 -5187
- package/.as-test/coverage/naive/coverage.raw.spec.ts.log.json +0 -5187
- package/.as-test/coverage/naive/coverage.resolving.spec.ts.log.json +0 -5628
- package/.as-test/coverage/naive/coverage.set.spec.ts.log.json +0 -5187
- package/.as-test/coverage/naive/coverage.staticarray.spec.ts.log.json +0 -5187
- package/.as-test/coverage/naive/coverage.string.spec.ts.log.json +0 -5187
- package/.as-test/coverage/naive/coverage.struct.spec.ts.log.json +0 -5187
- package/.as-test/coverage/naive/coverage.types.spec.ts.log.json +0 -5187
- package/.as-test/coverage/simd/coverage.arbitrary.spec.ts.log.json +0 -5628
- package/.as-test/coverage/simd/coverage.array.spec.ts.log.json +0 -5187
- package/.as-test/coverage/simd/coverage.bool.spec.ts.log.json +0 -5187
- package/.as-test/coverage/simd/coverage.box.spec.ts.log.json +0 -5187
- package/.as-test/coverage/simd/coverage.custom.spec.ts.log.json +0 -5187
- package/.as-test/coverage/simd/coverage.date.spec.ts.log.json +0 -5187
- package/.as-test/coverage/simd/coverage.enum.spec.ts.log.json +0 -5187
- package/.as-test/coverage/simd/coverage.float.spec.ts.log.json +0 -5187
- package/.as-test/coverage/simd/coverage.generics.spec.ts.log.json +0 -5187
- package/.as-test/coverage/simd/coverage.hierarchy.spec.ts.log.json +0 -5187
- package/.as-test/coverage/simd/coverage.integer.spec.ts.log.json +0 -5187
- package/.as-test/coverage/simd/coverage.map.spec.ts.log.json +0 -5187
- package/.as-test/coverage/simd/coverage.namespace.spec.ts.log.json +0 -5187
- package/.as-test/coverage/simd/coverage.null.spec.ts.log.json +0 -5187
- package/.as-test/coverage/simd/coverage.raw.spec.ts.log.json +0 -5187
- package/.as-test/coverage/simd/coverage.resolving.spec.ts.log.json +0 -5628
- package/.as-test/coverage/simd/coverage.set.spec.ts.log.json +0 -5187
- package/.as-test/coverage/simd/coverage.staticarray.spec.ts.log.json +0 -5187
- package/.as-test/coverage/simd/coverage.string.spec.ts.log.json +0 -5187
- package/.as-test/coverage/simd/coverage.struct.spec.ts.log.json +0 -5187
- package/.as-test/coverage/simd/coverage.types.spec.ts.log.json +0 -5187
- package/.as-test/coverage/swar/coverage.arbitrary.spec.ts.log.json +0 -5628
- package/.as-test/coverage/swar/coverage.array.spec.ts.log.json +0 -5187
- package/.as-test/coverage/swar/coverage.bool.spec.ts.log.json +0 -5187
- package/.as-test/coverage/swar/coverage.box.spec.ts.log.json +0 -5187
- package/.as-test/coverage/swar/coverage.custom.spec.ts.log.json +0 -5187
- package/.as-test/coverage/swar/coverage.date.spec.ts.log.json +0 -5187
- package/.as-test/coverage/swar/coverage.enum.spec.ts.log.json +0 -5187
- package/.as-test/coverage/swar/coverage.float.spec.ts.log.json +0 -5187
- package/.as-test/coverage/swar/coverage.generics.spec.ts.log.json +0 -5187
- package/.as-test/coverage/swar/coverage.hierarchy.spec.ts.log.json +0 -5187
- package/.as-test/coverage/swar/coverage.integer.spec.ts.log.json +0 -5187
- package/.as-test/coverage/swar/coverage.map.spec.ts.log.json +0 -5187
- package/.as-test/coverage/swar/coverage.namespace.spec.ts.log.json +0 -5187
- package/.as-test/coverage/swar/coverage.null.spec.ts.log.json +0 -5187
- package/.as-test/coverage/swar/coverage.raw.spec.ts.log.json +0 -5187
- package/.as-test/coverage/swar/coverage.resolving.spec.ts.log.json +0 -5628
- package/.as-test/coverage/swar/coverage.set.spec.ts.log.json +0 -5187
- package/.as-test/coverage/swar/coverage.staticarray.spec.ts.log.json +0 -5187
- package/.as-test/coverage/swar/coverage.string.spec.ts.log.json +0 -5187
- package/.as-test/coverage/swar/coverage.struct.spec.ts.log.json +0 -5187
- package/.as-test/coverage/swar/coverage.types.spec.ts.log.json +0 -5187
- package/.as-test/logs/naive/run.arbitrary.spec.ts.log.json +0 -943
- package/.as-test/logs/naive/run.array.spec.ts.log.json +0 -1053
- package/.as-test/logs/naive/run.bool.spec.ts.log.json +0 -257
- package/.as-test/logs/naive/run.box.spec.ts.log.json +0 -353
- package/.as-test/logs/naive/run.custom.spec.ts.log.json +0 -309
- package/.as-test/logs/naive/run.date.spec.ts.log.json +0 -397
- package/.as-test/logs/naive/run.enum.spec.ts.log.json +0 -343
- package/.as-test/logs/naive/run.float.spec.ts.log.json +0 -453
- package/.as-test/logs/naive/run.generics.spec.ts.log.json +0 -393
- package/.as-test/logs/naive/run.hierarchy.spec.ts.log.json +0 -325
- package/.as-test/logs/naive/run.integer.spec.ts.log.json +0 -373
- package/.as-test/logs/naive/run.map.spec.ts.log.json +0 -247
- package/.as-test/logs/naive/run.namespace.spec.ts.log.json +0 -361
- package/.as-test/logs/naive/run.null.spec.ts.log.json +0 -273
- package/.as-test/logs/naive/run.raw.spec.ts.log.json +0 -309
- package/.as-test/logs/naive/run.resolving.spec.ts.log.json +0 -273
- package/.as-test/logs/naive/run.set.spec.ts.log.json +0 -733
- package/.as-test/logs/naive/run.staticarray.spec.ts.log.json +0 -931
- package/.as-test/logs/naive/run.string.spec.ts.log.json +0 -2289
- package/.as-test/logs/naive/run.struct.spec.ts.log.json +0 -523
- package/.as-test/logs/naive/run.types.spec.ts.log.json +0 -273
- package/.as-test/logs/naive/test.arbitrary.spec.ts.log.json +0 -943
- package/.as-test/logs/naive/test.array.spec.ts.log.json +0 -1053
- package/.as-test/logs/naive/test.bool.spec.ts.log.json +0 -257
- package/.as-test/logs/naive/test.box.spec.ts.log.json +0 -353
- package/.as-test/logs/naive/test.custom.spec.ts.log.json +0 -309
- package/.as-test/logs/naive/test.date.spec.ts.log.json +0 -397
- package/.as-test/logs/naive/test.enum.spec.ts.log.json +0 -343
- package/.as-test/logs/naive/test.float.spec.ts.log.json +0 -453
- package/.as-test/logs/naive/test.generics.spec.ts.log.json +0 -393
- package/.as-test/logs/naive/test.hierarchy.spec.ts.log.json +0 -325
- package/.as-test/logs/naive/test.integer.spec.ts.log.json +0 -373
- package/.as-test/logs/naive/test.log.json +0 -2289
- package/.as-test/logs/naive/test.map.spec.ts.log.json +0 -247
- package/.as-test/logs/naive/test.namespace.spec.ts.log.json +0 -361
- package/.as-test/logs/naive/test.null.spec.ts.log.json +0 -273
- package/.as-test/logs/naive/test.raw.spec.ts.log.json +0 -309
- package/.as-test/logs/naive/test.resolving.spec.ts.log.json +0 -273
- package/.as-test/logs/naive/test.set.spec.ts.log.json +0 -733
- package/.as-test/logs/naive/test.staticarray.spec.ts.log.json +0 -931
- package/.as-test/logs/naive/test.string.spec.ts.log.json +0 -2345
- package/.as-test/logs/naive/test.struct.spec.ts.log.json +0 -523
- package/.as-test/logs/naive/test.types.spec.ts.log.json +0 -273
- package/.as-test/logs/simd/run.arbitrary.spec.ts.log.json +0 -943
- package/.as-test/logs/simd/run.array.spec.ts.log.json +0 -1053
- package/.as-test/logs/simd/run.bool.spec.ts.log.json +0 -257
- package/.as-test/logs/simd/run.box.spec.ts.log.json +0 -353
- package/.as-test/logs/simd/run.custom.spec.ts.log.json +0 -309
- package/.as-test/logs/simd/run.date.spec.ts.log.json +0 -397
- package/.as-test/logs/simd/run.enum.spec.ts.log.json +0 -343
- package/.as-test/logs/simd/run.float.spec.ts.log.json +0 -453
- package/.as-test/logs/simd/run.generics.spec.ts.log.json +0 -393
- package/.as-test/logs/simd/run.hierarchy.spec.ts.log.json +0 -325
- package/.as-test/logs/simd/run.integer.spec.ts.log.json +0 -373
- package/.as-test/logs/simd/run.map.spec.ts.log.json +0 -247
- package/.as-test/logs/simd/run.namespace.spec.ts.log.json +0 -361
- package/.as-test/logs/simd/run.null.spec.ts.log.json +0 -273
- package/.as-test/logs/simd/run.raw.spec.ts.log.json +0 -309
- package/.as-test/logs/simd/run.resolving.spec.ts.log.json +0 -273
- package/.as-test/logs/simd/run.set.spec.ts.log.json +0 -733
- package/.as-test/logs/simd/run.staticarray.spec.ts.log.json +0 -931
- package/.as-test/logs/simd/run.string.spec.ts.log.json +0 -2289
- package/.as-test/logs/simd/run.struct.spec.ts.log.json +0 -523
- package/.as-test/logs/simd/run.types.spec.ts.log.json +0 -273
- package/.as-test/logs/simd/test.arbitrary.spec.ts.log.json +0 -943
- package/.as-test/logs/simd/test.array.spec.ts.log.json +0 -1053
- package/.as-test/logs/simd/test.bool.spec.ts.log.json +0 -257
- package/.as-test/logs/simd/test.box.spec.ts.log.json +0 -353
- package/.as-test/logs/simd/test.custom.spec.ts.log.json +0 -309
- package/.as-test/logs/simd/test.date.spec.ts.log.json +0 -397
- package/.as-test/logs/simd/test.enum.spec.ts.log.json +0 -343
- package/.as-test/logs/simd/test.float.spec.ts.log.json +0 -453
- package/.as-test/logs/simd/test.generics.spec.ts.log.json +0 -393
- package/.as-test/logs/simd/test.hierarchy.spec.ts.log.json +0 -325
- package/.as-test/logs/simd/test.integer.spec.ts.log.json +0 -373
- package/.as-test/logs/simd/test.log.json +0 -11371
- package/.as-test/logs/simd/test.map.spec.ts.log.json +0 -247
- package/.as-test/logs/simd/test.namespace.spec.ts.log.json +0 -361
- package/.as-test/logs/simd/test.null.spec.ts.log.json +0 -273
- package/.as-test/logs/simd/test.raw.spec.ts.log.json +0 -309
- package/.as-test/logs/simd/test.resolving.spec.ts.log.json +0 -273
- package/.as-test/logs/simd/test.set.spec.ts.log.json +0 -733
- package/.as-test/logs/simd/test.staticarray.spec.ts.log.json +0 -931
- package/.as-test/logs/simd/test.string.spec.ts.log.json +0 -2345
- package/.as-test/logs/simd/test.struct.spec.ts.log.json +0 -523
- package/.as-test/logs/simd/test.types.spec.ts.log.json +0 -273
- package/.as-test/logs/swar/run.arbitrary.spec.ts.log.json +0 -943
- package/.as-test/logs/swar/run.array.spec.ts.log.json +0 -1053
- package/.as-test/logs/swar/run.bool.spec.ts.log.json +0 -257
- package/.as-test/logs/swar/run.box.spec.ts.log.json +0 -353
- package/.as-test/logs/swar/run.custom.spec.ts.log.json +0 -309
- package/.as-test/logs/swar/run.date.spec.ts.log.json +0 -397
- package/.as-test/logs/swar/run.enum.spec.ts.log.json +0 -343
- package/.as-test/logs/swar/run.float.spec.ts.log.json +0 -453
- package/.as-test/logs/swar/run.generics.spec.ts.log.json +0 -393
- package/.as-test/logs/swar/run.hierarchy.spec.ts.log.json +0 -325
- package/.as-test/logs/swar/run.integer.spec.ts.log.json +0 -373
- package/.as-test/logs/swar/run.map.spec.ts.log.json +0 -247
- package/.as-test/logs/swar/run.namespace.spec.ts.log.json +0 -361
- package/.as-test/logs/swar/run.null.spec.ts.log.json +0 -273
- package/.as-test/logs/swar/run.raw.spec.ts.log.json +0 -309
- package/.as-test/logs/swar/run.resolving.spec.ts.log.json +0 -273
- package/.as-test/logs/swar/run.set.spec.ts.log.json +0 -733
- package/.as-test/logs/swar/run.staticarray.spec.ts.log.json +0 -931
- package/.as-test/logs/swar/run.string.spec.ts.log.json +0 -2289
- package/.as-test/logs/swar/run.struct.spec.ts.log.json +0 -523
- package/.as-test/logs/swar/run.types.spec.ts.log.json +0 -273
- package/.as-test/logs/swar/test.arbitrary.spec.ts.log.json +0 -943
- package/.as-test/logs/swar/test.array.spec.ts.log.json +0 -1053
- package/.as-test/logs/swar/test.bool.spec.ts.log.json +0 -257
- package/.as-test/logs/swar/test.box.spec.ts.log.json +0 -353
- package/.as-test/logs/swar/test.custom.spec.ts.log.json +0 -309
- package/.as-test/logs/swar/test.date.spec.ts.log.json +0 -397
- package/.as-test/logs/swar/test.enum.spec.ts.log.json +0 -343
- package/.as-test/logs/swar/test.float.spec.ts.log.json +0 -453
- package/.as-test/logs/swar/test.generics.spec.ts.log.json +0 -393
- package/.as-test/logs/swar/test.hierarchy.spec.ts.log.json +0 -325
- package/.as-test/logs/swar/test.integer.spec.ts.log.json +0 -373
- package/.as-test/logs/swar/test.log.json +0 -11371
- package/.as-test/logs/swar/test.map.spec.ts.log.json +0 -247
- package/.as-test/logs/swar/test.namespace.spec.ts.log.json +0 -361
- package/.as-test/logs/swar/test.null.spec.ts.log.json +0 -273
- package/.as-test/logs/swar/test.raw.spec.ts.log.json +0 -309
- package/.as-test/logs/swar/test.resolving.spec.ts.log.json +0 -273
- package/.as-test/logs/swar/test.set.spec.ts.log.json +0 -733
- package/.as-test/logs/swar/test.staticarray.spec.ts.log.json +0 -931
- package/.as-test/logs/swar/test.string.spec.ts.log.json +0 -2345
- package/.as-test/logs/swar/test.struct.spec.ts.log.json +0 -523
- package/.as-test/logs/swar/test.types.spec.ts.log.json +0 -273
- package/.as-test/logs/test.arbitrary.spec.ts.log.json +0 -943
- package/.as-test/logs/test.array.spec.ts.log.json +0 -1053
- package/.as-test/logs/test.bool.spec.ts.log.json +0 -257
- package/.as-test/logs/test.box.spec.ts.log.json +0 -353
- package/.as-test/logs/test.custom.spec.ts.log.json +0 -309
- package/.as-test/logs/test.date.spec.ts.log.json +0 -397
- package/.as-test/logs/test.enum.spec.ts.log.json +0 -343
- package/.as-test/logs/test.float.spec.ts.log.json +0 -453
- package/.as-test/logs/test.generics.spec.ts.log.json +0 -393
- package/.as-test/logs/test.hierarchy.spec.ts.log.json +0 -325
- package/.as-test/logs/test.integer.spec.ts.log.json +0 -373
- package/.as-test/logs/test.log.json +0 -11371
- package/.as-test/logs/test.map.spec.ts.log.json +0 -247
- package/.as-test/logs/test.namespace.spec.ts.log.json +0 -361
- package/.as-test/logs/test.null.spec.ts.log.json +0 -273
- package/.as-test/logs/test.raw.spec.ts.log.json +0 -309
- package/.as-test/logs/test.resolving.spec.ts.log.json +0 -273
- package/.as-test/logs/test.set.spec.ts.log.json +0 -733
- package/.as-test/logs/test.staticarray.spec.ts.log.json +0 -931
- package/.as-test/logs/test.string.spec.ts.log.json +0 -2289
- package/.as-test/logs/test.struct.spec.ts.log.json +0 -523
- package/.as-test/logs/test.types.spec.ts.log.json +0 -273
- package/.as-test/runners/default.bindings.js +0 -68
- package/.as-test/runners/default.wasi.js +0 -38
- package/ARCHITECTURE.md +0 -323
- package/CONTRIBUTING.md +0 -244
- package/TODO +0 -1
- package/as-test.config.json +0 -40
- package/assembly/test.ts +0 -24
- package/eslint.config.js +0 -68
- package/lib/tsconfig.json +0 -8
- package/test.ts +0 -99
- package/tools/assemblyscript-eslint-local.js +0 -6
- package/tools/assemblyscript-eslint.js +0 -53
- package/tools/assemblyscript-prettier-plugin.js +0 -33
- package/tools/replacer.js +0 -63
- package/transform/tsconfig.json +0 -35
|
@@ -1,13 +1,37 @@
|
|
|
1
|
+
import { OBJECT, TOTAL_OVERHEAD } from "rt/common";
|
|
1
2
|
import { bs } from "../../../lib/as-bs";
|
|
3
|
+
import { BACK_SLASH, QUOTE } from "../../custom/chars";
|
|
2
4
|
import { DESERIALIZE_ESCAPE_TABLE, ESCAPE_HEX_TABLE } from "../../globals/tables";
|
|
3
5
|
import { hex4_to_u16_swar } from "../../util/swar";
|
|
4
6
|
|
|
7
|
+
// @ts-ignore: inline
|
|
8
|
+
@inline function writeStringToField(dstFieldPtr: usize, srcStart: usize, byteLength: u32): void {
|
|
9
|
+
if (byteLength == 0) {
|
|
10
|
+
store<usize>(dstFieldPtr, changetype<usize>(""));
|
|
11
|
+
return;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const current = load<usize>(dstFieldPtr);
|
|
15
|
+
let stringPtr: usize;
|
|
16
|
+
if (current != 0 && changetype<OBJECT>(current - TOTAL_OVERHEAD).rtSize == byteLength) {
|
|
17
|
+
stringPtr = current;
|
|
18
|
+
} else if (current != 0 && current != changetype<usize>("")) {
|
|
19
|
+
stringPtr = __renew(current, byteLength);
|
|
20
|
+
store<usize>(dstFieldPtr, stringPtr);
|
|
21
|
+
} else {
|
|
22
|
+
stringPtr = __new(byteLength, idof<string>());
|
|
23
|
+
store<usize>(dstFieldPtr, stringPtr);
|
|
24
|
+
}
|
|
25
|
+
memory.copy(stringPtr, srcStart, byteLength);
|
|
26
|
+
}
|
|
27
|
+
|
|
5
28
|
// @ts-ignore: inline
|
|
6
29
|
@inline export function deserializeString(srcStart: usize, srcEnd: usize): string {
|
|
7
30
|
// Strip quotes
|
|
8
31
|
srcStart += 2;
|
|
9
32
|
srcEnd -= 2;
|
|
10
|
-
bs.
|
|
33
|
+
const outStart = bs.offset - bs.buffer;
|
|
34
|
+
bs.ensureSize(u32(srcEnd - srcStart));
|
|
11
35
|
|
|
12
36
|
while (srcStart < srcEnd) {
|
|
13
37
|
const block = load<u16>(srcStart);
|
|
@@ -37,5 +61,67 @@ import { hex4_to_u16_swar } from "../../util/swar";
|
|
|
37
61
|
|
|
38
62
|
bs.offset += 2;
|
|
39
63
|
}
|
|
40
|
-
return bs.
|
|
64
|
+
return bs.sliceOut<string>(outStart);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// @ts-ignore: inline
|
|
68
|
+
@inline export function deserializeStringField<T extends string | null>(srcStart: usize, srcEnd: usize, dstFieldPtr: usize): usize {
|
|
69
|
+
if (srcStart + 2 > srcEnd || load<u16>(srcStart) != QUOTE) abort("Expected leading quote");
|
|
70
|
+
|
|
71
|
+
const payloadStart = srcStart + 2;
|
|
72
|
+
srcStart = payloadStart;
|
|
73
|
+
|
|
74
|
+
while (srcStart < srcEnd) {
|
|
75
|
+
const char = load<u16>(srcStart);
|
|
76
|
+
if (char == QUOTE) {
|
|
77
|
+
writeStringToField(dstFieldPtr, payloadStart, <u32>(srcStart - payloadStart));
|
|
78
|
+
return srcStart + 2;
|
|
79
|
+
}
|
|
80
|
+
if (char != BACK_SLASH) {
|
|
81
|
+
srcStart += 2;
|
|
82
|
+
continue;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const outStart = bs.offset - bs.buffer;
|
|
86
|
+
bs.ensureSize(u32(srcEnd - payloadStart));
|
|
87
|
+
const prefixLen = <u32>(srcStart - payloadStart);
|
|
88
|
+
if (prefixLen != 0) {
|
|
89
|
+
memory.copy(bs.offset, payloadStart, prefixLen);
|
|
90
|
+
bs.offset += prefixLen;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
while (srcStart < srcEnd) {
|
|
94
|
+
const block = load<u16>(srcStart);
|
|
95
|
+
if (block == QUOTE) {
|
|
96
|
+
bs.toField(outStart, dstFieldPtr);
|
|
97
|
+
return srcStart + 2;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
if (block != BACK_SLASH) {
|
|
101
|
+
store<u16>(bs.offset, block);
|
|
102
|
+
srcStart += 2;
|
|
103
|
+
bs.offset += 2;
|
|
104
|
+
continue;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
srcStart += 2;
|
|
108
|
+
const code = load<u16>(srcStart);
|
|
109
|
+
if (code !== 0x75) {
|
|
110
|
+
const escape = load<u16>(DESERIALIZE_ESCAPE_TABLE + code);
|
|
111
|
+
store<u16>(bs.offset, escape);
|
|
112
|
+
srcStart += 2;
|
|
113
|
+
} else {
|
|
114
|
+
const escaped = hex4_to_u16_swar(load<u64>(srcStart, 2));
|
|
115
|
+
store<u16>(bs.offset, escaped);
|
|
116
|
+
srcStart += 10;
|
|
117
|
+
}
|
|
118
|
+
bs.offset += 2;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
bs.offset = bs.buffer + outStart;
|
|
122
|
+
break;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
abort("Unterminated string literal");
|
|
126
|
+
return srcStart;
|
|
41
127
|
}
|
|
@@ -43,7 +43,6 @@ import { hex4_to_u16_swar } from "../../util/swar";
|
|
|
43
43
|
// @ts-expect-error: @inline is a valid decorator
|
|
44
44
|
@inline function copyStringFromSource(srcStart: usize, byteLength: usize): string {
|
|
45
45
|
if (byteLength == 0) return changetype<string>("");
|
|
46
|
-
// @ts-expect-error: __new is a runtime builtin
|
|
47
46
|
const out = __new(byteLength, idof<string>());
|
|
48
47
|
memory.copy(out, srcStart, byteLength);
|
|
49
48
|
return changetype<string>(out);
|
|
@@ -53,10 +52,10 @@ import { hex4_to_u16_swar } from "../../util/swar";
|
|
|
53
52
|
@inline function deserializeEscapedString_SWAR(payloadStart: usize, escapeStart: usize, srcEnd: usize): string {
|
|
54
53
|
const srcEnd8 = srcEnd - 8;
|
|
55
54
|
const prefixLen = <u32>(escapeStart - payloadStart);
|
|
56
|
-
bs.offset
|
|
55
|
+
const outStart = bs.offset - bs.buffer;
|
|
57
56
|
bs.ensureSize(<u32>(srcEnd - payloadStart));
|
|
58
57
|
if (prefixLen != 0) {
|
|
59
|
-
memory.copy(bs.
|
|
58
|
+
memory.copy(bs.offset, payloadStart, prefixLen);
|
|
60
59
|
bs.offset += prefixLen;
|
|
61
60
|
}
|
|
62
61
|
|
|
@@ -141,7 +140,7 @@ import { hex4_to_u16_swar } from "../../util/swar";
|
|
|
141
140
|
|
|
142
141
|
bs.offset += 2;
|
|
143
142
|
}
|
|
144
|
-
return bs.
|
|
143
|
+
return bs.sliceOut<string>(outStart);
|
|
145
144
|
}
|
|
146
145
|
|
|
147
146
|
export function deserializeString_SWAR(srcStart: usize, srcEnd: usize): string {
|
|
@@ -608,10 +607,10 @@ export function deserializeStringField_SWAR<T extends string | null>(srcStart: u
|
|
|
608
607
|
@inline function deserializeEscapedStringScan_SWAR(payloadStart: usize, escapeStart: usize, srcEnd: usize, dstFieldPtr: usize): usize {
|
|
609
608
|
const prefixLen = <u32>(escapeStart - payloadStart);
|
|
610
609
|
const srcEnd8 = srcEnd >= 8 ? srcEnd - 8 : 0;
|
|
611
|
-
bs.offset
|
|
610
|
+
const outStart = bs.offset - bs.buffer;
|
|
612
611
|
bs.ensureSize(<u32>(srcEnd - payloadStart));
|
|
613
612
|
if (prefixLen != 0) {
|
|
614
|
-
memory.copy(bs.
|
|
613
|
+
memory.copy(bs.offset, payloadStart, prefixLen);
|
|
615
614
|
bs.offset += prefixLen;
|
|
616
615
|
}
|
|
617
616
|
|
|
@@ -637,8 +636,7 @@ export function deserializeStringField_SWAR<T extends string | null>(srcStart: u
|
|
|
637
636
|
memory.copy(bs.offset, lastPtr, runLen);
|
|
638
637
|
bs.offset += runLen;
|
|
639
638
|
}
|
|
640
|
-
|
|
641
|
-
bs.offset = bs.buffer;
|
|
639
|
+
bs.toField(outStart, dstFieldPtr);
|
|
642
640
|
return srcIdx + 2;
|
|
643
641
|
}
|
|
644
642
|
if (char != BACK_SLASH) continue;
|
|
@@ -674,8 +672,7 @@ export function deserializeStringField_SWAR<T extends string | null>(srcStart: u
|
|
|
674
672
|
memory.copy(bs.offset, lastPtr, runLen);
|
|
675
673
|
bs.offset += runLen;
|
|
676
674
|
}
|
|
677
|
-
|
|
678
|
-
bs.offset = bs.buffer;
|
|
675
|
+
bs.toField(outStart, dstFieldPtr);
|
|
679
676
|
return srcStart + 2;
|
|
680
677
|
}
|
|
681
678
|
if (char != BACK_SLASH) {
|
|
@@ -703,7 +700,7 @@ export function deserializeStringField_SWAR<T extends string | null>(srcStart: u
|
|
|
703
700
|
lastPtr = srcStart;
|
|
704
701
|
}
|
|
705
702
|
|
|
706
|
-
bs.offset = bs.buffer;
|
|
703
|
+
bs.offset = bs.buffer + outStart;
|
|
707
704
|
abort("Unterminated string literal");
|
|
708
705
|
return srcStart;
|
|
709
706
|
}
|
package/assembly/index.ts
CHANGED
|
@@ -34,7 +34,7 @@ import { deserializeRaw } from "./deserialize/index/raw";
|
|
|
34
34
|
import { deserializeString } from "./deserialize/index/string";
|
|
35
35
|
import { serializeString } from "./serialize/index/string";
|
|
36
36
|
import { deserializeArrayBuffer, deserializeTypedArray } from "./deserialize/index/typedarray";
|
|
37
|
-
import { serializeArrayBufferUnsafe, serializeTypedArray } from "./serialize/index/typedarray";
|
|
37
|
+
import { serializeArrayBufferUnsafe, serializeDynamic, serializeTypedArray } from "./serialize/index/typedarray";
|
|
38
38
|
|
|
39
39
|
/**
|
|
40
40
|
* Offset of the 'storage' property in the JSON.Value class.
|
|
@@ -59,7 +59,7 @@ export namespace JSON {
|
|
|
59
59
|
* ```
|
|
60
60
|
*/
|
|
61
61
|
export function shrink(): void {
|
|
62
|
-
bs.
|
|
62
|
+
bs.shrink();
|
|
63
63
|
}
|
|
64
64
|
}
|
|
65
65
|
/**
|
|
@@ -118,9 +118,14 @@ export namespace JSON {
|
|
|
118
118
|
} else if (isString<nonnull<T>>()) {
|
|
119
119
|
serializeString(data as string);
|
|
120
120
|
return bs.out<string>();
|
|
121
|
-
// @ts-expect-error:
|
|
121
|
+
// @ts-expect-error: Defined by transform
|
|
122
|
+
} else if (isDefined(data.__SERIALIZE_CUSTOM)) {
|
|
123
|
+
// @ts-expect-error: Defined by transform
|
|
124
|
+
data.__SERIALIZE_CUSTOM();
|
|
125
|
+
return bs.out<string>();
|
|
126
|
+
// @ts-expect-error: Defined by transform
|
|
122
127
|
} else if (isDefined(data.__SERIALIZE)) {
|
|
123
|
-
// @ts-expect-error
|
|
128
|
+
// @ts-expect-error: Defined by transform
|
|
124
129
|
inline.always(data.__SERIALIZE(changetype<usize>(data)));
|
|
125
130
|
return bs.out<string>();
|
|
126
131
|
} else if (data instanceof Date) {
|
|
@@ -171,6 +176,10 @@ export namespace JSON {
|
|
|
171
176
|
} else if (data instanceof Float64Array) {
|
|
172
177
|
inline.always(serializeTypedArray<Float64Array>(data));
|
|
173
178
|
return bs.out<string>();
|
|
179
|
+
} else if (data instanceof ArrayBuffer) {
|
|
180
|
+
const dataStart = changetype<usize>(data);
|
|
181
|
+
serializeArrayBufferUnsafe(dataStart, changetype<OBJECT>(dataStart - TOTAL_OVERHEAD).rtSize);
|
|
182
|
+
return bs.out<string>();
|
|
174
183
|
} else if (data instanceof Set) {
|
|
175
184
|
// @ts-expect-error
|
|
176
185
|
inline.always(serializeSet(changetype<nonnull<T>>(data)));
|
|
@@ -191,16 +200,10 @@ export namespace JSON {
|
|
|
191
200
|
} else if (data instanceof JSON.Box) {
|
|
192
201
|
return JSON.stringify(data.value);
|
|
193
202
|
} else {
|
|
194
|
-
throw new Error(`Could not serialize data of type '${nameof<T>()}'. ` + `If this is a custom class, add the @json decorator: @json class ${nameof<T>()} { ... }. ` + `Supported types: primitives, string, Array, StaticArray, TypedArray, Map, Date, and @json decorated classes
|
|
203
|
+
throw new Error(`Could not serialize data of type '${nameof<T>()}'. ` + `If this is a custom class, add the @json decorator: @json class ${nameof<T>()} { ... }. ` + `Supported types: primitives, string, Array, StaticArray, TypedArray, ArrayBuffer, Map, Date, and @json decorated classes.`);
|
|
195
204
|
}
|
|
196
205
|
}
|
|
197
206
|
|
|
198
|
-
|
|
199
|
-
@inline export function stringifyArrayBuffer(dataStart: usize): string {
|
|
200
|
-
serializeArrayBufferUnsafe(dataStart, changetype<OBJECT>(dataStart - TOTAL_OVERHEAD).rtSize);
|
|
201
|
-
return bs.out<string>();
|
|
202
|
-
}
|
|
203
|
-
|
|
204
207
|
/**
|
|
205
208
|
* Parses valid JSON strings into their original format
|
|
206
209
|
* ```js
|
|
@@ -225,6 +228,20 @@ export namespace JSON {
|
|
|
225
228
|
return deserializeString(dataPtr, dataPtr + dataSize) as T;
|
|
226
229
|
} else {
|
|
227
230
|
let type: nonnull<T> = changetype<nonnull<T>>(0);
|
|
231
|
+
// @ts-expect-error: Defined by transform
|
|
232
|
+
if (isDefined(type.__DESERIALIZE_CUSTOM)) {
|
|
233
|
+
const out = changetype<nonnull<T>>(0);
|
|
234
|
+
// @ts-expect-error
|
|
235
|
+
return out.__DESERIALIZE_CUSTOM(data);
|
|
236
|
+
// @ts-expect-error: Defined by transform
|
|
237
|
+
} else if (isDefined(type.__DESERIALIZE)) {
|
|
238
|
+
const out = changetype<nonnull<T>>(__new(offsetof<nonnull<T>>(), idof<nonnull<T>>()));
|
|
239
|
+
// @ts-expect-error: Defined by transform
|
|
240
|
+
if (isDefined(type.__INITIALIZE)) out.__INITIALIZE();
|
|
241
|
+
// @ts-expect-error: Defined by transform
|
|
242
|
+
out.__DESERIALIZE(dataPtr, dataPtr + dataSize, out);
|
|
243
|
+
return out;
|
|
244
|
+
}
|
|
228
245
|
if (type instanceof StaticArray) {
|
|
229
246
|
// @ts-expect-error
|
|
230
247
|
return inline.always(deserializeStaticArray<nonnull<T>>(dataPtr, dataPtr + dataSize, 0));
|
|
@@ -232,40 +249,32 @@ export namespace JSON {
|
|
|
232
249
|
// @ts-expect-error
|
|
233
250
|
return inline.always(deserializeArray<nonnull<T>>(dataPtr, dataPtr + dataSize, changetype<usize>(instantiate<T>())));
|
|
234
251
|
} else if (type instanceof Int8Array) {
|
|
235
|
-
return deserializeTypedArray<
|
|
252
|
+
return deserializeTypedArray<nonnull<T>>(dataPtr, dataPtr + dataSize, 0) as T;
|
|
236
253
|
} else if (type instanceof Uint8Array) {
|
|
237
|
-
return deserializeTypedArray<
|
|
254
|
+
return deserializeTypedArray<nonnull<T>>(dataPtr, dataPtr + dataSize, 0) as T;
|
|
238
255
|
} else if (type instanceof Uint8ClampedArray) {
|
|
239
|
-
return deserializeTypedArray<
|
|
256
|
+
return deserializeTypedArray<nonnull<T>>(dataPtr, dataPtr + dataSize, 0) as T;
|
|
240
257
|
} else if (type instanceof Int16Array) {
|
|
241
|
-
return deserializeTypedArray<
|
|
258
|
+
return deserializeTypedArray<nonnull<T>>(dataPtr, dataPtr + dataSize, 0) as T;
|
|
242
259
|
} else if (type instanceof Uint16Array) {
|
|
243
|
-
return deserializeTypedArray<
|
|
260
|
+
return deserializeTypedArray<nonnull<T>>(dataPtr, dataPtr + dataSize, 0) as T;
|
|
244
261
|
} else if (type instanceof Int32Array) {
|
|
245
|
-
return deserializeTypedArray<
|
|
262
|
+
return deserializeTypedArray<nonnull<T>>(dataPtr, dataPtr + dataSize, 0) as T;
|
|
246
263
|
} else if (type instanceof Uint32Array) {
|
|
247
|
-
return deserializeTypedArray<
|
|
264
|
+
return deserializeTypedArray<nonnull<T>>(dataPtr, dataPtr + dataSize, 0) as T;
|
|
248
265
|
} else if (type instanceof Int64Array) {
|
|
249
|
-
return deserializeTypedArray<
|
|
266
|
+
return deserializeTypedArray<nonnull<T>>(dataPtr, dataPtr + dataSize, 0) as T;
|
|
250
267
|
} else if (type instanceof Uint64Array) {
|
|
251
|
-
return deserializeTypedArray<
|
|
268
|
+
return deserializeTypedArray<nonnull<T>>(dataPtr, dataPtr + dataSize, 0) as T;
|
|
252
269
|
} else if (type instanceof Float32Array) {
|
|
253
|
-
return deserializeTypedArray<
|
|
270
|
+
return deserializeTypedArray<nonnull<T>>(dataPtr, dataPtr + dataSize, 0) as T;
|
|
254
271
|
} else if (type instanceof Float64Array) {
|
|
255
|
-
return deserializeTypedArray<
|
|
272
|
+
return deserializeTypedArray<nonnull<T>>(dataPtr, dataPtr + dataSize, 0) as T;
|
|
256
273
|
} else if (type instanceof ArrayBuffer) {
|
|
257
274
|
return deserializeArrayBuffer(dataPtr, dataPtr + dataSize, 0) as T;
|
|
258
275
|
} else if (type instanceof Set) {
|
|
259
276
|
// @ts-expect-error
|
|
260
277
|
return inline.always(deserializeSet<nonnull<T>>(dataPtr, dataPtr + dataSize, 0));
|
|
261
|
-
// @ts-expect-error: Defined by transform
|
|
262
|
-
} else if (isDefined(type.__DESERIALIZE)) {
|
|
263
|
-
const out = changetype<nonnull<T>>(__new(offsetof<nonnull<T>>(), idof<nonnull<T>>()));
|
|
264
|
-
// @ts-expect-error: Defined by transform
|
|
265
|
-
if (isDefined(type.__INITIALIZE)) out.__INITIALIZE();
|
|
266
|
-
// @ts-expect-error
|
|
267
|
-
out.__DESERIALIZE(dataPtr, dataPtr + dataSize, out);
|
|
268
|
-
return out;
|
|
269
278
|
} else if (type instanceof Map) {
|
|
270
279
|
// @ts-expect-error
|
|
271
280
|
return inline.always(deserializeMap<nonnull<T>>(dataPtr, dataPtr + dataSize, 0));
|
|
@@ -299,7 +308,7 @@ export namespace JSON {
|
|
|
299
308
|
* Enum-like namespace representing the different types supported by JSON.Value.
|
|
300
309
|
*
|
|
301
310
|
* Used internally to track the runtime type of values stored in JSON.Value instances.
|
|
302
|
-
* Types 0-
|
|
311
|
+
* Types 0-19 are reserved for built-in types; custom @json classes use idof<T>() + Struct.
|
|
303
312
|
*/
|
|
304
313
|
export namespace Types {
|
|
305
314
|
/** Represents a null value */
|
|
@@ -340,6 +349,10 @@ export namespace JSON {
|
|
|
340
349
|
@inline export const Map: u16 = 16;
|
|
341
350
|
// @ts-expect-error
|
|
342
351
|
@inline export const Struct: u16 = 17;
|
|
352
|
+
// @ts-expect-error
|
|
353
|
+
@inline export const TypedArray: u16 = 18;
|
|
354
|
+
// @ts-expect-error
|
|
355
|
+
@inline export const ArrayBuffer: u16 = 19;
|
|
343
356
|
}
|
|
344
357
|
|
|
345
358
|
/**
|
|
@@ -470,12 +483,23 @@ export namespace JSON {
|
|
|
470
483
|
if (value instanceof u64) return JSON.Types.U64;
|
|
471
484
|
if (value instanceof f32) return JSON.Types.F32;
|
|
472
485
|
if (value instanceof f64) return JSON.Types.F64;
|
|
486
|
+
// @ts-expect-error: supplied by transform
|
|
487
|
+
if (isDefined(value.__SERIALIZE) && isManaged<T>(value)) return u16(idof<T>()) + JSON.Types.Struct;
|
|
488
|
+
if (value instanceof Int8Array) return JSON.Types.TypedArray;
|
|
489
|
+
if (value instanceof Uint8Array) return JSON.Types.TypedArray;
|
|
490
|
+
if (value instanceof Uint8ClampedArray) return JSON.Types.TypedArray;
|
|
491
|
+
if (value instanceof Int16Array) return JSON.Types.TypedArray;
|
|
492
|
+
if (value instanceof Uint16Array) return JSON.Types.TypedArray;
|
|
493
|
+
if (value instanceof Int32Array) return JSON.Types.TypedArray;
|
|
494
|
+
if (value instanceof Uint32Array) return JSON.Types.TypedArray;
|
|
495
|
+
if (value instanceof Int64Array) return JSON.Types.TypedArray;
|
|
496
|
+
if (value instanceof Uint64Array) return JSON.Types.TypedArray;
|
|
497
|
+
if (value instanceof Float32Array) return JSON.Types.TypedArray;
|
|
498
|
+
if (value instanceof Float64Array) return JSON.Types.TypedArray;
|
|
499
|
+
if (value instanceof ArrayBuffer) return JSON.Types.ArrayBuffer;
|
|
473
500
|
if (value instanceof Map) return JSON.Types.Map;
|
|
474
501
|
if (value instanceof JSON.Raw) return JSON.Types.Raw;
|
|
475
502
|
if (value instanceof JSON.Obj) return JSON.Types.Object;
|
|
476
|
-
|
|
477
|
-
// @ts-expect-error: supplied by transform
|
|
478
|
-
if (isDefined(value.__SERIALIZE) && isManaged<T>(value)) return u16(idof<T>()) + JSON.Types.Struct;
|
|
479
503
|
return JSON.Types.Null;
|
|
480
504
|
}
|
|
481
505
|
/**
|
|
@@ -492,16 +516,28 @@ export namespace JSON {
|
|
|
492
516
|
else if (isNullable<T>() && changetype<usize>(value) === 0) store<usize>(changetype<usize>(this), 0, STORAGE);
|
|
493
517
|
else if (isString<T>()) store<T>(changetype<usize>(this), value, STORAGE);
|
|
494
518
|
else if (value instanceof JSON.Raw) store<T>(changetype<usize>(this), value, STORAGE);
|
|
519
|
+
// @ts-expect-error: supplied by transform
|
|
520
|
+
else if (isDefined(value.__SERIALIZE) && isManaged<T>(value)) {
|
|
521
|
+
// @ts-expect-error
|
|
522
|
+
if (!JSON.Value.METHODS.has(idof<T>())) JSON.Value.METHODS.set(idof<T>(), value.__SERIALIZE.index);
|
|
523
|
+
store<usize>(changetype<usize>(this), changetype<usize>(value), STORAGE);
|
|
524
|
+
} else if (value instanceof Int8Array) store<T>(changetype<usize>(this), value, STORAGE);
|
|
525
|
+
else if (value instanceof Uint8Array) store<T>(changetype<usize>(this), value, STORAGE);
|
|
526
|
+
else if (value instanceof Uint8ClampedArray) store<T>(changetype<usize>(this), value, STORAGE);
|
|
527
|
+
else if (value instanceof Int16Array) store<T>(changetype<usize>(this), value, STORAGE);
|
|
528
|
+
else if (value instanceof Uint16Array) store<T>(changetype<usize>(this), value, STORAGE);
|
|
529
|
+
else if (value instanceof Int32Array) store<T>(changetype<usize>(this), value, STORAGE);
|
|
530
|
+
else if (value instanceof Uint32Array) store<T>(changetype<usize>(this), value, STORAGE);
|
|
531
|
+
else if (value instanceof Int64Array) store<T>(changetype<usize>(this), value, STORAGE);
|
|
532
|
+
else if (value instanceof Uint64Array) store<T>(changetype<usize>(this), value, STORAGE);
|
|
533
|
+
else if (value instanceof Float32Array) store<T>(changetype<usize>(this), value, STORAGE);
|
|
534
|
+
else if (value instanceof Float64Array) store<T>(changetype<usize>(this), value, STORAGE);
|
|
535
|
+
else if (value instanceof ArrayBuffer) store<T>(changetype<usize>(this), value, STORAGE);
|
|
495
536
|
else if (value instanceof Map) {
|
|
496
537
|
if (idof<T>() !== idof<Map<string, JSON.Value>>()) {
|
|
497
538
|
abort("Maps must be of type Map<string, JSON.Value>!");
|
|
498
539
|
}
|
|
499
540
|
store<T>(changetype<usize>(this), value, STORAGE);
|
|
500
|
-
// @ts-expect-error: supplied by transform
|
|
501
|
-
} else if (isDefined(value.__SERIALIZE) && isManaged<T>(value)) {
|
|
502
|
-
// @ts-expect-error
|
|
503
|
-
if (!JSON.Value.METHODS.has(idof<T>())) JSON.Value.METHODS.set(idof<T>(), value.__SERIALIZE.index);
|
|
504
|
-
store<usize>(changetype<usize>(this), changetype<usize>(value), STORAGE);
|
|
505
541
|
} else if (value instanceof JSON.Obj) {
|
|
506
542
|
store<T>(changetype<usize>(this), value, STORAGE);
|
|
507
543
|
// @ts-expect-error
|
|
@@ -587,6 +623,11 @@ export namespace JSON {
|
|
|
587
623
|
|
|
588
624
|
return out.toString();
|
|
589
625
|
}
|
|
626
|
+
case JSON.Types.TypedArray:
|
|
627
|
+
case JSON.Types.ArrayBuffer: {
|
|
628
|
+
serializeDynamic(this.type, this.get<usize>());
|
|
629
|
+
return bs.out<string>();
|
|
630
|
+
}
|
|
590
631
|
case JSON.Types.Object: {
|
|
591
632
|
return JSON.stringify(this.get<JSON.Obj>());
|
|
592
633
|
}
|
|
@@ -808,13 +849,13 @@ export namespace JSON {
|
|
|
808
849
|
bs.offset += 8;
|
|
809
850
|
} else if (isString<nonnull<T>>()) {
|
|
810
851
|
serializeString(data as string);
|
|
811
|
-
// @ts-expect-error:
|
|
852
|
+
// @ts-expect-error: Defined by transform
|
|
812
853
|
} else if (isDefined(data.__SERIALIZE_CUSTOM)) {
|
|
813
854
|
// @ts-expect-error
|
|
814
855
|
return data.__SERIALIZE_CUSTOM();
|
|
815
|
-
// @ts-expect-error:
|
|
856
|
+
// @ts-expect-error: Defined by transform
|
|
816
857
|
} else if (isDefined(data.__SERIALIZE)) {
|
|
817
|
-
// @ts-expect-error
|
|
858
|
+
// @ts-expect-error: type
|
|
818
859
|
serializeStruct(changetype<nonnull<T>>(data));
|
|
819
860
|
} else if (data instanceof Date) {
|
|
820
861
|
// @ts-expect-error
|
|
@@ -847,6 +888,9 @@ export namespace JSON {
|
|
|
847
888
|
serializeTypedArray<Float32Array>(data);
|
|
848
889
|
} else if (data instanceof Float64Array) {
|
|
849
890
|
serializeTypedArray<Float64Array>(data);
|
|
891
|
+
} else if (data instanceof ArrayBuffer) {
|
|
892
|
+
const dataStart = changetype<usize>(data);
|
|
893
|
+
serializeArrayBufferUnsafe(dataStart, changetype<OBJECT>(dataStart - TOTAL_OVERHEAD).rtSize);
|
|
850
894
|
} else if (data instanceof Set) {
|
|
851
895
|
// @ts-expect-error
|
|
852
896
|
serializeSet(changetype<nonnull<T>>(data));
|
|
@@ -862,33 +906,10 @@ export namespace JSON {
|
|
|
862
906
|
} else if (data instanceof JSON.Box) {
|
|
863
907
|
__serialize(data.value);
|
|
864
908
|
} else {
|
|
865
|
-
throw new Error(`Could not serialize data of type '${nameof<T>()}'. ` + `If this is a custom class, add the @json decorator: @json class ${nameof<T>()} { ... }. ` + `Supported types: primitives, string, Array, StaticArray, TypedArray, Map, Date, and @json decorated classes
|
|
909
|
+
throw new Error(`Could not serialize data of type '${nameof<T>()}'. ` + `If this is a custom class, add the @json decorator: @json class ${nameof<T>()} { ... }. ` + `Supported types: primitives, string, Array, StaticArray, TypedArray, ArrayBuffer, Map, Date, and @json decorated classes.`);
|
|
866
910
|
}
|
|
867
911
|
}
|
|
868
912
|
|
|
869
|
-
|
|
870
|
-
@inline export function __serializeArrayBufferUnsafe(dataStart: usize): void {
|
|
871
|
-
serializeArrayBufferUnsafe(dataStart, changetype<OBJECT>(dataStart - TOTAL_OVERHEAD).rtSize);
|
|
872
|
-
}
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
@inline export function __serializeArrayBuffer(data: ArrayBuffer): void {
|
|
876
|
-
const dataStart = changetype<usize>(data);
|
|
877
|
-
serializeArrayBufferUnsafe(dataStart, changetype<OBJECT>(dataStart - TOTAL_OVERHEAD).rtSize);
|
|
878
|
-
}
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
@inline export function parseArrayBuffer(data: string): ArrayBuffer {
|
|
882
|
-
const dataSize = bytes(data);
|
|
883
|
-
const dataPtr = changetype<usize>(data);
|
|
884
|
-
return deserializeArrayBuffer(dataPtr, dataPtr + dataSize, 0);
|
|
885
|
-
}
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
@inline export function __deserializeArrayBuffer(srcStart: usize, srcEnd: usize, dst: usize = 0): ArrayBuffer {
|
|
889
|
-
return deserializeArrayBuffer(srcStart, srcEnd, dst);
|
|
890
|
-
}
|
|
891
|
-
|
|
892
913
|
/**
|
|
893
914
|
* Deserializes JSON data directly from the buffer.
|
|
894
915
|
* Should only be used if you know what you are doing.
|
|
@@ -913,6 +934,20 @@ export namespace JSON {
|
|
|
913
934
|
return null;
|
|
914
935
|
} else {
|
|
915
936
|
let type: nonnull<T> = changetype<nonnull<T>>(0);
|
|
937
|
+
// @ts-expect-error: Defined by transform
|
|
938
|
+
if (isDefined(type.__DESERIALIZE_CUSTOM)) {
|
|
939
|
+
const out = changetype<nonnull<T>>(0);
|
|
940
|
+
// @ts-expect-error: Defined by transform
|
|
941
|
+
return out.__DESERIALIZE_CUSTOM(ptrToStr(srcStart, srcEnd));
|
|
942
|
+
// @ts-expect-error: Defined by transform
|
|
943
|
+
} else if (isDefined(type.__DESERIALIZE)) {
|
|
944
|
+
const out = changetype<nonnull<T>>(dst || __new(offsetof<nonnull<T>>(), idof<nonnull<T>>()));
|
|
945
|
+
// @ts-expect-error: Defined by transform
|
|
946
|
+
if (isDefined(type.__INITIALIZE)) out.__INITIALIZE();
|
|
947
|
+
// @ts-expect-error: Defined by transform
|
|
948
|
+
out.__DESERIALIZE(srcStart, srcEnd, out);
|
|
949
|
+
return out;
|
|
950
|
+
}
|
|
916
951
|
if (type instanceof StaticArray) {
|
|
917
952
|
// @ts-expect-error: type
|
|
918
953
|
return deserializeStaticArray<T>(srcStart, srcEnd, dst);
|
|
@@ -920,40 +955,32 @@ export namespace JSON {
|
|
|
920
955
|
// @ts-expect-error: type
|
|
921
956
|
return deserializeArray<T>(srcStart, srcEnd, dst);
|
|
922
957
|
} else if (type instanceof Int8Array) {
|
|
923
|
-
return deserializeTypedArray<
|
|
958
|
+
return deserializeTypedArray<nonnull<T>>(srcStart, srcEnd, dst) as T;
|
|
924
959
|
} else if (type instanceof Uint8Array) {
|
|
925
|
-
return deserializeTypedArray<
|
|
960
|
+
return deserializeTypedArray<nonnull<T>>(srcStart, srcEnd, dst) as T;
|
|
926
961
|
} else if (type instanceof Uint8ClampedArray) {
|
|
927
|
-
return deserializeTypedArray<
|
|
962
|
+
return deserializeTypedArray<nonnull<T>>(srcStart, srcEnd, dst) as T;
|
|
928
963
|
} else if (type instanceof Int16Array) {
|
|
929
|
-
return deserializeTypedArray<
|
|
964
|
+
return deserializeTypedArray<nonnull<T>>(srcStart, srcEnd, dst) as T;
|
|
930
965
|
} else if (type instanceof Uint16Array) {
|
|
931
|
-
return deserializeTypedArray<
|
|
966
|
+
return deserializeTypedArray<nonnull<T>>(srcStart, srcEnd, dst) as T;
|
|
932
967
|
} else if (type instanceof Int32Array) {
|
|
933
|
-
return deserializeTypedArray<
|
|
968
|
+
return deserializeTypedArray<nonnull<T>>(srcStart, srcEnd, dst) as T;
|
|
934
969
|
} else if (type instanceof Uint32Array) {
|
|
935
|
-
return deserializeTypedArray<
|
|
970
|
+
return deserializeTypedArray<nonnull<T>>(srcStart, srcEnd, dst) as T;
|
|
936
971
|
} else if (type instanceof Int64Array) {
|
|
937
|
-
return deserializeTypedArray<
|
|
972
|
+
return deserializeTypedArray<nonnull<T>>(srcStart, srcEnd, dst) as T;
|
|
938
973
|
} else if (type instanceof Uint64Array) {
|
|
939
|
-
return deserializeTypedArray<
|
|
974
|
+
return deserializeTypedArray<nonnull<T>>(srcStart, srcEnd, dst) as T;
|
|
940
975
|
} else if (type instanceof Float32Array) {
|
|
941
|
-
return deserializeTypedArray<
|
|
976
|
+
return deserializeTypedArray<nonnull<T>>(srcStart, srcEnd, dst) as T;
|
|
942
977
|
} else if (type instanceof Float64Array) {
|
|
943
|
-
return deserializeTypedArray<
|
|
978
|
+
return deserializeTypedArray<nonnull<T>>(srcStart, srcEnd, dst) as T;
|
|
944
979
|
} else if (type instanceof ArrayBuffer) {
|
|
945
980
|
return deserializeArrayBuffer(srcStart, srcEnd, dst) as T;
|
|
946
981
|
} else if (type instanceof Set) {
|
|
947
982
|
// @ts-expect-error: type
|
|
948
983
|
return deserializeSet<T>(srcStart, srcEnd, dst);
|
|
949
|
-
// @ts-expect-error: Defined by transform
|
|
950
|
-
} else if (isDefined(type.__DESERIALIZE)) {
|
|
951
|
-
const out = changetype<nonnull<T>>(dst || __new(offsetof<nonnull<T>>(), idof<nonnull<T>>()));
|
|
952
|
-
// @ts-expect-error: Defined by transform
|
|
953
|
-
if (isDefined(type.__INITIALIZE)) out.__INITIALIZE();
|
|
954
|
-
// @ts-expect-error: Defined by transform
|
|
955
|
-
out.__DESERIALIZE(srcStart, srcEnd, out);
|
|
956
|
-
return out;
|
|
957
984
|
} else if (type instanceof Map) {
|
|
958
985
|
// @ts-expect-error: type
|
|
959
986
|
return deserializeMap<T>(srcStart, srcEnd, dst);
|
|
@@ -991,7 +1018,8 @@ export namespace JSON {
|
|
|
991
1018
|
}
|
|
992
1019
|
}
|
|
993
1020
|
/**
|
|
994
|
-
* Methods for use when using JSON methods inside another JSON method or custom serializer/deserializer
|
|
1021
|
+
* Methods for use when using JSON methods inside another JSON method or custom serializer/deserializer
|
|
1022
|
+
* Transform will automatically convert JSON.x calls to JSON.internal.x when in a custom (de)serializer
|
|
995
1023
|
*/
|
|
996
1024
|
export namespace internal {
|
|
997
1025
|
/**
|
|
@@ -1002,102 +1030,30 @@ export namespace JSON {
|
|
|
1002
1030
|
*/
|
|
1003
1031
|
// @ts-expect-error: inline
|
|
1004
1032
|
@inline export function stringify<T>(data: T, out: string | null = null): string {
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
store<u16>(changetype<usize>(out), 101, 8);
|
|
1014
|
-
}
|
|
1015
|
-
return out;
|
|
1016
|
-
}
|
|
1017
|
-
return data ? "true" : "false";
|
|
1018
|
-
} else if (isInteger<T>() && !isSigned<T>() && nameof<T>() == "usize" && data == 0) {
|
|
1019
|
-
if (out) {
|
|
1020
|
-
out = changetype<string>(__renew(changetype<usize>(out), 8));
|
|
1021
|
-
store<u64>(changetype<usize>(out), NULL_WORD_U64);
|
|
1022
|
-
return out;
|
|
1023
|
-
}
|
|
1024
|
-
return NULL_WORD;
|
|
1025
|
-
} else if (isInteger<T>(data)) {
|
|
1026
|
-
if (out) {
|
|
1027
|
-
out = changetype<string>(__renew(changetype<usize>(out), sizeof<T>() << 3));
|
|
1028
|
-
|
|
1029
|
-
const bytes = itoa_buffered(changetype<usize>(out), data) << 1;
|
|
1030
|
-
return (out = changetype<string>(__renew(changetype<usize>(out), bytes)));
|
|
1031
|
-
}
|
|
1032
|
-
return data.toString();
|
|
1033
|
-
} else if (isFloat<T>(data)) {
|
|
1034
|
-
if (out) {
|
|
1035
|
-
out = changetype<string>(__renew(changetype<usize>(out), 64));
|
|
1036
|
-
|
|
1037
|
-
const bytes = dtoa_buffered(changetype<usize>(out), data) << 1;
|
|
1038
|
-
return (out = changetype<string>(__renew(changetype<usize>(out), bytes)));
|
|
1039
|
-
}
|
|
1040
|
-
return data.toString();
|
|
1041
|
-
} else if (isNullable<T>() && changetype<usize>(data) == <usize>0) {
|
|
1042
|
-
if (out) {
|
|
1043
|
-
out = changetype<string>(__renew(changetype<usize>(out), 8));
|
|
1044
|
-
store<u64>(changetype<usize>(out), NULL_WORD_U64);
|
|
1045
|
-
return out;
|
|
1046
|
-
}
|
|
1047
|
-
return NULL_WORD;
|
|
1048
|
-
} else if (isString<nonnull<T>>()) {
|
|
1049
|
-
serializeString(data as string);
|
|
1050
|
-
return bs.cpyOut<string>();
|
|
1051
|
-
// @ts-expect-error: Supplied by transform
|
|
1052
|
-
} else if (isDefined(data.__SERIALIZE)) {
|
|
1053
|
-
bs.saveState();
|
|
1054
|
-
// @ts-expect-error
|
|
1055
|
-
inline.always(data.__SERIALIZE(changetype<usize>(data)));
|
|
1056
|
-
return bs.cpyOut<string>();
|
|
1057
|
-
} else if (data instanceof Date) {
|
|
1058
|
-
out = out ? changetype<string>(__renew(changetype<usize>(out), 52)) : changetype<string>(__new(52, idof<string>()));
|
|
1059
|
-
|
|
1060
|
-
store<u16>(changetype<usize>(out), QUOTE);
|
|
1061
|
-
memory.copy(changetype<usize>(out) + 2, changetype<usize>(data.toISOString()), 48);
|
|
1062
|
-
store<u16>(changetype<usize>(out), QUOTE, 50);
|
|
1063
|
-
return changetype<string>(out);
|
|
1064
|
-
} else if (data instanceof Array) {
|
|
1065
|
-
bs.saveState();
|
|
1066
|
-
// @ts-expect-error
|
|
1067
|
-
inline.always(serializeArray(changetype<nonnull<T>>(data)));
|
|
1068
|
-
return bs.cpyOut<string>();
|
|
1069
|
-
} else if (data instanceof StaticArray) {
|
|
1070
|
-
bs.saveState();
|
|
1071
|
-
// @ts-expect-error
|
|
1072
|
-
inline.always(serializeStaticArray(changetype<nonnull<T>>(data)));
|
|
1073
|
-
return bs.cpyOut<string>();
|
|
1074
|
-
} else if (data instanceof Set) {
|
|
1075
|
-
bs.saveState();
|
|
1076
|
-
// @ts-expect-error
|
|
1077
|
-
inline.always(serializeSet(changetype<nonnull<T>>(data)));
|
|
1078
|
-
return bs.cpyOut<string>();
|
|
1079
|
-
} else if (data instanceof Map) {
|
|
1080
|
-
bs.saveState();
|
|
1081
|
-
// @ts-expect-error
|
|
1082
|
-
inline.always(serializeMap(changetype<nonnull<T>>(data)));
|
|
1083
|
-
return bs.cpyOut<string>();
|
|
1084
|
-
} else if (data instanceof JSON.Raw) {
|
|
1085
|
-
bs.saveState();
|
|
1086
|
-
serializeRaw(data);
|
|
1087
|
-
return bs.cpyOut<string>();
|
|
1088
|
-
} else if (data instanceof JSON.Value) {
|
|
1089
|
-
bs.saveState();
|
|
1090
|
-
inline.always(serializeArbitrary(data));
|
|
1091
|
-
return bs.cpyOut<string>();
|
|
1092
|
-
} else if (data instanceof JSON.Obj) {
|
|
1093
|
-
bs.saveState();
|
|
1094
|
-
inline.always(serializeObject(data));
|
|
1095
|
-
return bs.cpyOut<string>();
|
|
1096
|
-
} else if (data instanceof JSON.Box) {
|
|
1097
|
-
return JSON.internal.stringify(data.value);
|
|
1098
|
-
} else {
|
|
1099
|
-
throw new Error(`Could not serialize data of type '${nameof<T>()}'. ` + `If this is a custom class, add the @json decorator: @json class ${nameof<T>()} { ... }. ` + `Supported types: primitives, string, Array, Map, Date, and @json decorated classes.`);
|
|
1033
|
+
bs.saveState();
|
|
1034
|
+
JSON.__serialize<T>(data);
|
|
1035
|
+
const result = bs.cpyOut<string>();
|
|
1036
|
+
if (out) {
|
|
1037
|
+
const len = bytes(result);
|
|
1038
|
+
out = changetype<string>(__renew(changetype<usize>(out), len));
|
|
1039
|
+
memory.copy(changetype<usize>(out), changetype<usize>(result), len);
|
|
1040
|
+
return out;
|
|
1100
1041
|
}
|
|
1042
|
+
return result;
|
|
1043
|
+
}
|
|
1044
|
+
|
|
1045
|
+
/**
|
|
1046
|
+
* Parses JSON data without mutating the caller's active serialization buffer state.
|
|
1047
|
+
* Don't use this directly, use `JSON.parse` instead.
|
|
1048
|
+
* @param data - string
|
|
1049
|
+
* @returns - T
|
|
1050
|
+
*/
|
|
1051
|
+
// @ts-expect-error: inline
|
|
1052
|
+
@inline export function parse<T>(data: string): T {
|
|
1053
|
+
bs.saveState();
|
|
1054
|
+
const result = JSON.parse<T>(data);
|
|
1055
|
+
bs.loadState();
|
|
1056
|
+
return result;
|
|
1101
1057
|
}
|
|
1102
1058
|
}
|
|
1103
1059
|
}
|