Skip to content

Commit f92d292

Browse files
committed
feat: support deserialization of nullables in JSON.Value
1 parent c7000e0 commit f92d292

5 files changed

Lines changed: 25 additions & 12 deletions

File tree

assembly/deserialize/simple/arbitrary.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ import { deserializeFloat } from "./float";
55
import { deserializeString } from "./string";
66
import { deserializeObject } from "./object";
77
import { BRACE_LEFT, BRACKET_LEFT, CHAR_N, QUOTE } from "../../custom/chars";
8-
import { bs } from "../../../lib/as-bs";
9-
import { deserializeString_SWAR } from "../swar/string";
108

119
export function deserializeArbitrary(srcStart: usize, srcEnd: usize, dst: usize): JSON.Value {
1210
const firstChar = load<u16>(srcStart);
@@ -21,4 +19,4 @@ export function deserializeArbitrary(srcStart: usize, srcEnd: usize, dst: usize)
2119
return JSON.Value.from<usize>(0);
2220
}
2321
return unreachable();
24-
}
22+
}

assembly/deserialize/simple/array/arbitrary.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ export function deserializeArbitraryArray(srcStart: usize, srcEnd: usize, dst: u
121121
}
122122
} else if (code == CHAR_N) {
123123
if (load<u64>(srcStart) == 30399761348886638) {
124-
console.log("Value (null): " + ptrToStr(srcStart, srcStart + 8));
124+
// console.log("Value (null): " + ptrToStr(srcStart, srcStart + 8));
125125
// @ts-ignore: type
126126
out.push(JSON.__deserialize<JSON.Value>(srcStart, (srcStart += 8)));
127127
// while (isSpace(load<u16>((srcStart += 2)))) {

assembly/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,7 @@ export namespace JSON {
308308
*/
309309
@inline set<T>(value: T): void {
310310
if (isNullable<T>()) {
311+
311312
if (value instanceof JSON.Box) {
312313
if (isBoolean<T>()) {
313314
this.type = JSON.Types.BoolNull;
@@ -331,8 +332,6 @@ export namespace JSON {
331332
this.type = JSON.Types.F64Null;
332333
store<T>(changetype<usize>(this), value, STORAGE);
333334
}
334-
335-
this.isNull = changetype<usize>(value) === 0;
336335
} else if (isBoolean<T>()) {
337336
this.type = JSON.Types.BoolNull;
338337
store<T>(changetype<usize>(this), value, STORAGE);
@@ -382,6 +381,7 @@ export namespace JSON {
382381
this.type = JSON.Types.ArrayNull;
383382
store<T>(changetype<usize>(this), value, STORAGE);
384383
}
384+
this.isNull = changetype<usize>(value) === 0;
385385
} else {
386386
if (value instanceof JSON.Box) {
387387
return this.set(value.value);

assembly/test.ts

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,30 @@
11
import { JSON } from ".";
22

3+
@json
4+
class Vec3 {
5+
x!: f32;
6+
y!: f32;
7+
z!: f32;
8+
}
9+
10+
const vec: Vec3 = {
11+
x: 1.0,
12+
y: 2.0,
13+
z: 3.0
14+
}
315
const str: JSON.Value[] = [
416
JSON.Value.from<string>("foo"),
517
JSON.Value.from("bar"),
618
JSON.Value.from(1),
719
JSON.Value.from(2),
820
JSON.Value.from(true),
9-
JSON.Value.from<JSON.Box<i32> | null>(null)
21+
JSON.Value.from<JSON.Box<i32> | null>(null),
22+
JSON.Value.from(vec),
23+
JSON.Value.from<Vec3 | null>(null)
1024
];
1125

12-
console.log(JSON.stringify(str));
13-
// for (let i = 0; i < str.length; i++) {
14-
// console.log(str.toString());
15-
// }
26+
const serialized = JSON.stringify(str);
27+
console.log("Serialized: " + serialized);
28+
29+
const deserialized = JSON.parse<JSON.Value[]>(serialized);
30+
console.log("Deserialized: " + JSON.stringify(deserialized).toString());

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
"test": "bash ./run-tests.sh",
6161
"bench:as": "bash ./run-bench.as.sh",
6262
"bench:js": "bash ./run-bench.js.sh",
63-
"build:test": "JSON_DEBUG=2 asc assembly/test.ts --transform ./transform -o ./build/test.wasm --textFile ./build/test.wat --enable simd --debug --config ./node_modules/@assemblyscript/wasi-shim/asconfig.json",
63+
"build:test": "JSON_DEBUG=0 asc assembly/test.ts --transform ./transform -o ./build/test.wasm --textFile ./build/test.wat --enable simd --debug --config ./node_modules/@assemblyscript/wasi-shim/asconfig.json",
6464
"build:tmp:test": "JSON_DEBUG=1 asc assembly/test.tmp.ts -o ./build/test.wasm --textFile ./build/test.wat --enable simd --debug --config ./node_modules/@assemblyscript/wasi-shim/asconfig.json",
6565
"build:test:wine": "JSON_DEBUG=1 JSON_WRITE=assembly/test.ts NODE_SKIP_PLATFORM_CHECK=1 wine ~/.win-bin/node/node.exe ./node_modules/assemblyscript/bin/asc.js assembly/test.ts --transform ./transform -o ./build/test.wasm --textFile ./build/test.wat --debug --config ./node_modules/@assemblyscript/wasi-shim/asconfig.json",
6666
"test:wasmtime": "wasmtime ./build/test.wasm",

0 commit comments

Comments
 (0)