Skip to content

Commit

Permalink
bring back boxes
Browse files Browse the repository at this point in the history
  • Loading branch information
JairusSW committed Jan 20, 2025
1 parent 1544329 commit d9bbb26
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 25 deletions.
23 changes: 12 additions & 11 deletions assembly/deserialize/simple/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { ptrToStr } from "../../util/ptrToStr";

export function deserializeObject<T>(srcStart: usize, srcEnd: usize, dst: usize): T {
const out = changetype<T>(dst);
const srcPtr = srcStart;

let keyStart: usize = 0;
let keyEnd: usize = 0;
Expand All @@ -14,7 +13,7 @@ export function deserializeObject<T>(srcStart: usize, srcEnd: usize, dst: usize)

// while (srcStart < srcEnd && isSpace(load<u16>(srcStart))) srcStart += 2;
// while (srcEnd > srcStart && isSpace(load<u16>(srcEnd))) srcEnd -= 2;

srcStart += 2;
while (srcStart < srcEnd) {
let code = load<u16>(srcStart); // while (isSpace(code)) code = load<u16>(srcStart += 2);
if (keyStart == 0) {
Expand All @@ -23,12 +22,14 @@ export function deserializeObject<T>(srcStart: usize, srcEnd: usize, dst: usize)
keyStart = lastIndex;
keyEnd = srcStart;
console.log("Key: " + ptrToStr(lastIndex, srcStart));
while (isSpace((code = load<u16>((srcStart += 2))))) {
/* empty */
}
if (code !== COLON) throw new Error("Expected ':' after key at position " + (srcStart - srcPtr).toString());
srcStart += 2;
// while (isSpace((code = load<u16>((srcStart += 2))))) {
// /* empty */
// }
// if (code !== COLON) throw new Error("Expected ':' after key at position " + (srcStart - srcPtr).toString());
isKey = false;
} else {
console.log("Got key start");
isKey = true; // i don't like this
lastIndex = srcStart + 2;
}
Expand All @@ -42,13 +43,13 @@ export function deserializeObject<T>(srcStart: usize, srcEnd: usize, dst: usize)
while (srcStart < srcEnd) {
const code = load<u16>(srcStart);
if (code == QUOTE && load<u16>(srcStart - 2) !== BACK_SLASH) {
while (isSpace(load<u16>((srcStart += 2)))) {
/* empty */
}
console.log("Value (string): " + ptrToStr(lastIndex, srcStart));
console.log("Value (string): " + ptrToStr(lastIndex, srcStart + 2));
console.log("Next: " + String.fromCharCode(load<u16>(srcStart + 4)));
// @ts-ignore: exists
out.__DESERIALIZE(keyStart, keyEnd, lastIndex, srcStart, dst);
out.__DESERIALIZE(keyStart, keyEnd, lastIndex, srcStart + 2, dst);
// while (isSpace(load<u16>(srcStart))) srcStart += 2;
keyStart = 0;
srcStart += 4;
break;
}
srcStart += 2;
Expand Down
15 changes: 13 additions & 2 deletions assembly/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@ export namespace JSON {
} else if (data instanceof JSON.Value) {
serializeArbitrary(data);
return bs.shrinkTo<string>();
} else if (data instanceof JSON.Box) {
return JSON.stringify(data.value);
} else {
ERROR(`Could not serialize data of type ${nameof<T>()}. Make sure to add the correct decorators to classes.`);
}
Expand Down Expand Up @@ -174,13 +176,16 @@ export namespace JSON {
// @ts-ignore: Defined by transform
if (isDefined(type.__DESERIALIZE)) {
// @ts-ignore
return deserializeObject<nonnull<T>>(dataPtr, dataPtr + dataSize, __new(offsetof<T>(), idof<T>()));
return deserializeObject<nonnull<T>>(dataPtr, dataPtr + dataSize, __new(offsetof<nonnull<T>>(), idof<nonnull<T>>()));
} else if (type instanceof Map) {
// @ts-ignore
return deserializeMap<nonnull<T>>(dataPtr, dataPtr + dataSize);
} else if (type instanceof Date) {
// @ts-ignore
return deserializeDate(dataPtr, dataPtr + dataSize);
} else if (type instanceof JSON.Box) {
// @ts-ignore
return new JSON.Box(JSON.parse<indexof<T>>(data));
} else {
ERROR(`Could not deserialize data ${data} to type ${nameof<T>()}. Make sure to add the correct decorators to classes.`);
}
Expand Down Expand Up @@ -356,14 +361,18 @@ export namespace JSON {
export function __serialize<T>(src: T): void {
if (isBoolean<T>()) {
serializeBool(src as bool);
} else if (isInteger<T>() && nameof<T>() == "usize" && src == 0) {
bs.ensureSize(8);
store<u64>(bs.offset, 30399761348886638);
bs.offset += 8;
} else if (isInteger<T>()) {
// @ts-ignore
serializeInteger<T>(src);
} else if (isFloat<T>(src)) {
// @ts-ignore
serializeFloat<T>(src);
// @ts-ignore: Function is generated by transform
} else if (changetype<usize>(src) == <usize>0) {
} else if (isNullable<T>() && changetype<usize>(src) == <usize>0) {
bs.ensureSize(8);
store<u64>(bs.offset, 30399761348886638);
bs.offset += 8;
Expand All @@ -388,6 +397,8 @@ export namespace JSON {
serializeMap(changetype<nonnull<T>>(src));
} else if (src instanceof JSON.Value) {
serializeArbitrary(src);
} else if (src instanceof JSON.Box) {
__serialize(src.value);
} else {
ERROR(`Could not serialize provided data. Make sure to add the correct decorators to classes.`);
}
Expand Down
10 changes: 5 additions & 5 deletions assembly/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ class Player {
lastName!: string;
lastActive!: i32[];
// Drop in a code block, function, or expression that evaluates to a boolean
@omitif((age) => age < 18)
age!: i32;
// @omitif((self: Player) => i32(self.age < 18)
age: JSON.Box<i32> | null = null;
@omitnull()
pos!: Vec3 | null;
isVerified!: boolean;
Expand All @@ -26,7 +26,7 @@ const player: Player = {
firstName: "Emmet",
lastName: "West",
lastActive: [8, 27, 2022],
age: 23,
age: null,
pos: {
x: 3.4,
y: 1.2,
Expand All @@ -37,5 +37,5 @@ const player: Player = {

const stringified = JSON.stringify<Player>(player);
console.log("Serialized: " + stringified);
const parsed = JSON.parse<Player>(stringified);
console.log("Deserialized: " + JSON.stringify<Player>(parsed));
// const parsed = JSON.parse<Player>(stringified);
// console.log("Deserialized: " + JSON.stringify<Player>(parsed));
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@
"test": "rm -rf ./build/ && ast test",
"build:bench": "rm -rf ./build/ && asc assembly/__benches__/misc.bench.ts -o ./build/bench.wasm --textFile ./build/bench.wat --transform ./transform --optimizeLevel 3 --shrinkLevel 0 --converge --noAssert --uncheckedBehavior always --runtime incremental --enable simd --enable bulk-memory",
"build:test": "rm -rf ./build/ && JSON_DEBUG=true asc assembly/test.ts --transform ./transform -o ./build/test.wasm --textFile ./build/test.wat --optimizeLevel 3 --shrinkLevel 0 --enable simd --runtime stub",
"build:transform": "tsc -p ./transform",
"build:test:simd": "rm -rf ./build/ && JSON_DEBUG=true asc assembly/test.ts --transform ./transform -o ./build/test.wasm --textFile ./build/test.wat --optimizeLevel 3 --shrinkLevel 0 --enable simd --runtime stub",
"test:wasmtime": "wasmtime ./build/test.wasm",
"test:wasmer": "wasmer ./build/test.wasm",
"build:transform": "tsc -p ./transform",
"bench:wasmer": "wasmer ./build/bench.wasm --llvm",
"prettier": "prettier -w ."
},
Expand Down
13 changes: 10 additions & 3 deletions transform/lib/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion transform/lib/index.js.map

Large diffs are not rendered by default.

11 changes: 9 additions & 2 deletions transform/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ class JSONTransform extends Visitor {
break;
}
case "omitif": {
const arg = decorator.args[0];
let arg = decorator.args[0];
if (!decorator.args?.length) throwError("@omitif must have an argument or callback that resolves to type bool", member.range);
mem.flags.set(PropertyFlags.OmitIf, arg);
this.schema.static = false;
Expand Down Expand Up @@ -198,7 +198,14 @@ class JSONTransform extends Visitor {
this.schema.byteSize += 2;
SERIALIZE += indent + `}\n`;
} else if (member.flags.has(PropertyFlags.OmitIf)) {
SERIALIZE += indent + `if (${toString}) !== 0) {\n`;
if (member.flags.get(PropertyFlags.OmitIf).kind == NodeKind.Function) {
const arg = member.flags.get(PropertyFlags.OmitIf) as FunctionExpression;
// @ts-ignore: type
arg.declaration.signature.returnType.name = Node.createSimpleTypeName("boolean", arg.declaration.signature.returnType.name.range);
SERIALIZE += indent + `if ((${toString(member.flags.get(PropertyFlags.OmitIf))})(this)) {\n`;
} else {
SERIALIZE += indent + `if (${toString(member.flags.get(PropertyFlags.OmitIf))}) {\n`;
}
indentInc();
SERIALIZE += this.getStores(aliasName + ":").map(v => indent + v + "\n").join("");
SERIALIZE += indent + `JSON.__serialize<${member.type}>(load<${member.type}>(ptr, offsetof<this>(${JSON.stringify(realName)})));\n`;
Expand Down

0 comments on commit d9bbb26

Please sign in to comment.