Skip to content

Commit 4234aef

Browse files
committed
fix: 6-byte keys not deserializing
1 parent 2dd521a commit 4234aef

File tree

7 files changed

+57
-31
lines changed

7 files changed

+57
-31
lines changed

README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,10 @@ class Player {
7474
}
7575

7676
const player: Player = {
77-
firstName: "Emmet",
78-
lastName: "West",
79-
lastActive: [8, 27, 2022],
80-
age: 23,
77+
firstName: "Jairus",
78+
lastName: "Tanaka",
79+
lastActive: [2, 7, 2025],
80+
age: 18,
8181
pos: {
8282
x: 3.4,
8383
y: 1.2,
@@ -87,10 +87,10 @@ const player: Player = {
8787
};
8888

8989
const serialized = JSON.stringify<Player>(player);
90-
const parsed = JSON.parse<Player>(stringified);
90+
const parsed = JSON.parse<Player>(serialized);
9191

92-
console.log("Serialized: " + stringified);
93-
console.log("Parsed: " + parsed);
92+
console.log("Serialized: " + serialized);
93+
console.log("Parsed: " + JSON.stringify(parsed));
9494
```
9595

9696
## Examples

assembly/deserialize/simple/object.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
import { JSON } from "../..";
12
import { BACK_SLASH, COMMA, CHAR_F, BRACE_LEFT, BRACKET_LEFT, CHAR_N, QUOTE, BRACE_RIGHT, BRACKET_RIGHT, CHAR_T } from "../../custom/chars";
23
import { isSpace } from "../../util";
4+
import { ptrToStr } from "../../util/ptrToStr";
35

46
export function deserializeObject<T>(srcStart: usize, srcEnd: usize, dst: usize): T {
57
const out = changetype<nonnull<T>>(dst || __new(offsetof<T>(), idof<T>()));
@@ -60,9 +62,9 @@ export function deserializeObject<T>(srcStart: usize, srcEnd: usize, dst: usize)
6062
while (srcStart < srcEnd) {
6163
const code = load<u16>(srcStart);
6264
if (code == COMMA || code == BRACE_RIGHT || isSpace(code)) {
65+
console.log("Value (number): " + JSON.__deserialize<i32>(lastIndex, srcStart).toString());
6366
// @ts-ignore: exists
6467
out.__DESERIALIZE(keyStart, keyEnd, lastIndex, srcStart, dst);
65-
// console.log("Value (number): " + ptrToStr(lastIndex, srcStart));
6668
// while (isSpace(load<u16>((srcStart += 2)))) {
6769
// /* empty */
6870
// }

assembly/test.ts

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { bs } from "../modules/as-bs/assembly";
22
import { JSON } from "./";
3+
import { ptrToStr } from "./util/ptrToStr";
4+
35
@json
46
class Vec3 {
57
x: f32 = 0.0;
@@ -10,23 +12,22 @@ class Vec3 {
1012
@json
1113
class Player {
1214
@alias("first name")
13-
firstName: string = "";
14-
lastName: string = "";
15-
lastActive: i32[] = [];
16-
// // Drop in a code block, function, or expression that evaluates to a boolean
17-
// // @omitif((self) => self.age < 18)
18-
// // @omitif('this.age <= 0')
19-
// age: i32 = 0;
20-
// // @omitnull()
21-
// pos: Vec3 | null = new Vec3();
22-
// isVerified: boolean = false;
15+
firstName!: string;
16+
lastName!: string;
17+
lastActive!: i32[];
18+
// Drop in a code block, function, or expression that evaluates to a boolean
19+
// @omitif((self: Player) => self.age < 18)
20+
age!: i32;
21+
// @omitnull()
22+
// pos!: Vec3 | null;
23+
// isVerified!: boolean;
2324
}
2425

2526
const player: Player = {
26-
firstName: "Emmet",
27-
lastName: "West",
28-
lastActive: [8, 27, 2022],
29-
// age: 23,
27+
firstName: "Jairus",
28+
lastName: "Tanaka",
29+
lastActive: [2, 7, 2025],
30+
age: 18,
3031
// pos: {
3132
// x: 3.4,
3233
// y: 1.2,
@@ -35,8 +36,9 @@ const player: Player = {
3536
// isVerified: true
3637
};
3738

38-
// bs.proposeSize(1024);
3939
const serialized = JSON.stringify<Player>(player);
40+
4041
console.log("Serialized: " + serialized);
41-
// const parsed = JSON.parse<Player>(serialized);
42-
// console.log("Parsed: " + JSON.stringify(parsed));
42+
const parsed = JSON.parse<Player>(serialized);
43+
44+
console.log("Parsed: " + JSON.stringify(parsed));

modules/as-bs/assembly/index.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,17 @@ export namespace bs {
3131
*/
3232
// @ts-ignore: decorator
3333
@inline export function ensureSize(size: u32): void {
34+
let grew = ""
3435
if (offset + size > bufferEnd) {
3536
bufferEnd += nextPowerOf2(size + 32);
3637
// @ts-ignore: exists
3738
const newPtr = __renew(buffer, bufferEnd - buffer);
3839
// I don't know if this is even needed. I'll need to take a look at the runtime
3940
// offset = offset - buffer + newPtr;
4041
// buffer = newPtr;
42+
grew = "+";
4143
}
42-
console.log("Ensure " + (realSize - buffer).toString() + " " + size.toString());
44+
console.log("Ensure " + (realSize - buffer).toString() + " " + size.toString() + " " + grew);
4345
}
4446
/**
4547
* Proposes that the buffer size is should be greater than or equal to the proposed size.
@@ -48,6 +50,7 @@ export namespace bs {
4850
*/
4951
// @ts-ignore: decorator
5052
@inline export function proposeSize(size: u32): void {
53+
let grew = ""
5154
realSize = offset + size;
5255
if (realSize > bufferEnd) {
5356
bufferEnd += nextPowerOf2(size);
@@ -56,8 +59,9 @@ export namespace bs {
5659
// I don't know if this is even needed. I'll need to take a look at the runtime
5760
// offset = offset - buffer + newPtr;
5861
// buffer = newPtr;
62+
grew = "+";
5963
}
60-
console.log("Propose " + (realSize - buffer).toString() + " " + size.toString());
64+
console.log("Propose " + (realSize - buffer).toString() + " " + size.toString() + " " + grew);
6165
}
6266

6367
/**
@@ -67,14 +71,16 @@ export namespace bs {
6771
*/
6872
// @ts-ignore: decorator
6973
@inline export function growSize(size: u32): void {
74+
let grew = ""
7075
if ((realSize += size) > bufferEnd) {
7176
bufferEnd += nextPowerOf2(size + 32);
7277
// @ts-ignore
7378
const newPtr = __renew(buffer, bufferEnd);
7479
// offset = offset - buffer + newPtr;
7580
// buffer = newPtr;
81+
grew = "+";
7682
}
77-
console.log("Grow " + (realSize - buffer).toString() + " " + size.toString());
83+
console.log("Grow " + (realSize - buffer).toString() + " " + size.toString() + " " + grew);
7884
}
7985

8086
/**

transform/lib/index.js

Lines changed: 10 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

transform/lib/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

transform/src/index.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,8 @@ class JSONTransform extends Visitor {
278278
indentInc();
279279
if (memberLen == 2) DESERIALIZE += `${indent}switch (load<u16>(keyStart)) {\n`;
280280
else if (memberLen == 4) DESERIALIZE += `${indent}switch (load<u32>(keyStart)) {\n`;
281-
else if (memberLen == 6) DESERIALIZE += `${indent}let code = (<u64>load<u32>(keyStart) << 16) | <u64>load<u16>(keyStart, 4);\n`;
281+
else if (memberLen == 6) DESERIALIZE += `${indent}let code = load<u64>(keyStart) & 0x0000FFFFFFFFFFFF;\n`;
282+
else if (memberLen == 6) DESERIALIZE += `${indent}let code = load<u64>(keyStart);\n`;
282283
else DESERIALIZE += toMemCDecl(memberLen, indent);
283284
for (let i = 0; i < memberGroup.length; i++) {
284285
const member = memberGroup[i];
@@ -299,6 +300,12 @@ class JSONTransform extends Visitor {
299300
DESERIALIZE += `${indent} store<${member.type}>(ptr, JSON.__deserialize<${member.type}>(valStart, valEnd), offsetof<this>(${JSON.stringify(member.name)}));\n`;
300301
DESERIALIZE += `${indent} return;\n`;
301302
DESERIALIZE += `${indent}}${i < memberGroup.length - 1 ? " else " : "\n"}`;
303+
} else if (memberLen == 8) {
304+
DESERIALIZE += i == 0 ? indent : "";
305+
DESERIALIZE += `if (code == ${toU64(memberName)}) { // ${memberName}\n`;
306+
DESERIALIZE += `${indent} store<${member.type}>(ptr, JSON.__deserialize<${member.type}>(valStart, valEnd), offsetof<this>(${JSON.stringify(member.name)}));\n`;
307+
DESERIALIZE += `${indent} return;\n`;
308+
DESERIALIZE += `${indent}}${i < memberGroup.length - 1 ? " else " : "\n"}`;
302309
} else {
303310
DESERIALIZE += i == 0 ? indent : "";
304311
DESERIALIZE += `if (${toMemCCheck(memberName)}) { // ${memberName}\n`;

0 commit comments

Comments
 (0)