Skip to content

Commit

Permalink
first working example
Browse files Browse the repository at this point in the history
  • Loading branch information
JairusSW committed Jan 20, 2025
1 parent 8f748c8 commit fc4e0f1
Show file tree
Hide file tree
Showing 14 changed files with 119 additions and 44 deletions.
4 changes: 2 additions & 2 deletions assembly/deserialize/simple/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { deserializeObjectArray } from "./array/object";
import { deserializeStringArray } from "./array/string";

// @ts-ignore: Decorator valid here
export function deserializeArray<T extends unknown[]>(srcStart: usize, srcEnd: usize, dst: usize = 0): T {
export function deserializeArray<T extends unknown[]>(srcStart: usize, srcEnd: usize, dst: usize): T {
if (isString<valueof<T>>()) {
return <T>deserializeStringArray(srcStart, srcEnd, dst);
} else if (isBoolean<valueof<T>>()) {
Expand All @@ -28,7 +28,7 @@ export function deserializeArray<T extends unknown[]>(srcStart: usize, srcEnd: u
const type = changetype<nonnull<valueof<T>>>(0);
if (type instanceof JSON.Value) {
// @ts-ignore: type
return deserializeArbitraryArray(srcStart, srcEnd, dst);
return deserializeArbitraryArray<T>(srcStart, srcEnd, dst);
} else if (type instanceof Map) {
// @ts-ignore: type
return deserializeMapArray<T>(srcStart, srcEnd, dst);
Expand Down
4 changes: 2 additions & 2 deletions assembly/deserialize/simple/array/arbitrary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { JSON } from "../../../";
import { isSpace } from "util/string";
import { ptrToStr } from "../../../util/ptrToStr";

export function deserializeArbitraryArray(srcStart: usize, srcEnd: usize, dst: usize): JSON.Value[] {
const out: JSON.Value[] = [];
export function deserializeArbitraryArray<T extends JSON.Value>(srcStart: usize, srcEnd: usize, dst: usize): JSON.Value[] {
const out = dst ? changetype<T>(dst) : instantiate<T>();
let lastIndex: usize = 0;
let depth: u32 = 0;
while (srcStart < srcEnd) {
Expand Down
2 changes: 1 addition & 1 deletion assembly/deserialize/simple/array/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { BRACKET_LEFT, BRACKET_RIGHT } from "../../../custom/chars";
import { JSON } from "../../../";

export function deserializeArrayArray<T extends unknown[][]>(srcStart: usize, srcEnd: usize, dst: usize): T {
const out = changetype<T>(dst);
const out = dst ? changetype<T>(dst) : instantiate<T>();
let lastIndex: usize = 0;
let depth: u32 = 0;
while (srcStart < srcEnd) {
Expand Down
2 changes: 1 addition & 1 deletion assembly/deserialize/simple/array/bool.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { CHAR_E, CHAR_F, CHAR_T } from "../../../custom/chars";

export function deserializeBooleanArray<T extends boolean[]>(srcStart: usize, srcEnd: usize, dst: usize): T {
const out = changetype<T>(dst);
const out = dst ? changetype<T>(dst) : instantiate<T>();
while (srcStart < srcEnd) {
const code = load<u16>(srcStart);
if (code == CHAR_T && load<u16>(srcStart, 8) == CHAR_E) {
Expand Down
2 changes: 1 addition & 1 deletion assembly/deserialize/simple/array/float.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { COMMA, BRACE_RIGHT, BRACKET_RIGHT } from "../../../custom/chars";
import { JSON } from "../../..";

export function deserializeFloatArray<T extends number[]>(srcStart: usize, srcEnd: usize, dst: usize): T {
const out = changetype<T>(dst || __new(offsetof<T>(), idof<T>()));
const out = dst ? changetype<T>(dst) : instantiate<T>();
let lastIndex: usize = 0;
while (srcStart < srcEnd) {
const code = load<u16>(srcStart);
Expand Down
6 changes: 3 additions & 3 deletions assembly/deserialize/simple/array/integer.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { isSpace } from "../../../util";
import { atoi, isSpace } from "../../../util";
import { COMMA, BRACE_RIGHT, BRACKET_RIGHT } from "../../../custom/chars";
import { JSON } from "../../..";
import { ptrToStr } from "../../../util/ptrToStr";

export function deserializeIntegerArray<T extends number[]>(srcStart: usize, srcEnd: usize, dst: usize): T {
const out = changetype<T>(dst || __new(offsetof<T>(), idof<T>()));
const out = dst ? changetype<T>(dst) : instantiate<T>();
let lastIndex: usize = 0;
while (srcStart < srcEnd) {
const code = load<u16>(srcStart);
Expand All @@ -15,7 +15,7 @@ export function deserializeIntegerArray<T extends number[]>(srcStart: usize, src
const code = load<u16>(srcStart);
if (code == COMMA || code == BRACKET_RIGHT || isSpace(code)) {
console.log("element: " + ptrToStr(lastIndex, srcStart))
// out.push(JSON.__deserialize<valueof<T>>(lastIndex, srcStart));
out.push(JSON.__deserialize<valueof<T>>(lastIndex, srcStart));
// while (isSpace(load<u16>((srcStart += 2)))) {
// /* empty */
// }
Expand Down
2 changes: 1 addition & 1 deletion assembly/deserialize/simple/array/map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { BRACE_LEFT, BRACE_RIGHT } from "../../../custom/chars";
import { JSON } from "../../..";

export function deserializeMapArray<T extends Map<any, any>[]>(srcStart: usize, srcEnd: usize, dst: usize): T {
const out = changetype<T>(dst);
const out = dst ? changetype<T>(dst) : instantiate<T>();
let lastIndex: usize = 0;
let depth: u32 = 0;
while (srcStart < srcEnd) {
Expand Down
2 changes: 1 addition & 1 deletion assembly/deserialize/simple/array/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { BRACE_LEFT, BRACE_RIGHT } from "../../../custom/chars";
import { JSON } from "../../..";

export function deserializeObjectArray<T extends unknown[]>(srcStart: usize, srcEnd: usize, dst: usize): T {
const out = changetype<T>(dst);
const out = dst ? changetype<T>(dst) : instantiate<T>();
let lastIndex: usize = 0;
let depth: u32 = 0;
while (srcStart < srcEnd) {
Expand Down
2 changes: 1 addition & 1 deletion assembly/deserialize/simple/array/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { JSON } from "../../..";
import { BACK_SLASH, QUOTE } from "../../../custom/chars";

export function deserializeStringArray(srcStart: usize, srcEnd: usize, dst: usize): string[] {
const out = changetype<string[]>(dst);
const out = dst ? changetype<string[]>(dst) : new Array<string>();
let lastPos = 2;
let inString = false;
while (srcStart < srcEnd) {
Expand Down
2 changes: 1 addition & 1 deletion assembly/deserialize/simple/map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { BACK_SLASH, COMMA, CHAR_F, BRACE_LEFT, BRACKET_LEFT, CHAR_N, QUOTE, BRA
import { isSpace } from "../../util";

export function deserializeMap<T extends Map<any, any>>(srcStart: usize, srcEnd: usize, dst: usize): T {
const out = changetype<T>(dst);
const out = changetype<T>(dst || __new(offsetof<T>(), idof<T>()));
// @ts-ignore: type
if (!isString<indexof<T>>() && !isInteger<indexof<T>>() && !isFloat<indexof<T>>()) ERROR("Map key must also be a valid JSON key!");

Expand Down
60 changes: 36 additions & 24 deletions assembly/deserialize/simple/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { isSpace } from "../../util";
import { ptrToStr } from "../../util/ptrToStr";

export function deserializeObject<T>(srcStart: usize, srcEnd: usize, dst: usize): T {
const out = changetype<T>(dst);
const out = changetype<nonnull<T>>(dst || __new(offsetof<T>(), idof<T>()));

let keyStart: usize = 0;
let keyEnd: usize = 0;
Expand All @@ -22,6 +22,7 @@ export function deserializeObject<T>(srcStart: usize, srcEnd: usize, dst: usize)
keyStart = lastIndex;
keyEnd = srcStart;
console.log("Key: " + ptrToStr(lastIndex, srcStart));
console.log("Next: " + String.fromCharCode(load<u16>(srcStart + 2)));
srcStart += 2;
// while (isSpace((code = load<u16>((srcStart += 2))))) {
// /* empty */
Expand All @@ -44,12 +45,12 @@ export function deserializeObject<T>(srcStart: usize, srcEnd: usize, dst: usize)
const code = load<u16>(srcStart);
if (code == QUOTE && load<u16>(srcStart - 2) !== BACK_SLASH) {
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 + 2, dst);
// while (isSpace(load<u16>(srcStart))) srcStart += 2;
keyStart = 0;
srcStart += 4;
console.log("Next: " + String.fromCharCode(load<u16>(srcStart)));
keyStart = 0;
break;
}
srcStart += 2;
Expand All @@ -63,9 +64,11 @@ export function deserializeObject<T>(srcStart: usize, srcEnd: usize, dst: usize)
// @ts-ignore: exists
out.__DESERIALIZE(keyStart, keyEnd, lastIndex, srcStart, dst);
console.log("Value (number): " + ptrToStr(lastIndex, srcStart));
while (isSpace(load<u16>((srcStart += 2)))) {
/* empty */
}
// while (isSpace(load<u16>((srcStart += 2)))) {
// /* empty */
// }
srcStart += 2;
console.log("Next: " + String.fromCharCode(load<u16>(srcStart)));
keyStart = 0;
break;
}
Expand All @@ -79,9 +82,10 @@ export function deserializeObject<T>(srcStart: usize, srcEnd: usize, dst: usize)
const code = load<u16>(srcStart);
if (code == BRACE_RIGHT) {
if (--depth == 0) {
console.log("Value (object): " + ptrToStr(lastIndex, srcStart + 2));
// @ts-ignore: exists
out.__DESERIALIZE(keyStart, keyEnd, lastIndex, srcStart += 2, dst);
console.log("Value (object): " + ptrToStr(lastIndex, srcStart));
console.log("Next: " + String.fromCharCode(load<u16>(srcStart)));
keyStart = 0;
// while (isSpace(load<u16>(srcStart))) {
// /* empty */
Expand All @@ -99,46 +103,54 @@ export function deserializeObject<T>(srcStart: usize, srcEnd: usize, dst: usize)
const code = load<u16>(srcStart);
if (code == BRACKET_RIGHT) {
if (--depth == 0) {
console.log("Value (array): " + ptrToStr(lastIndex, srcStart + 2));
// @ts-ignore: exists
out.__DESERIALIZE(keyStart, keyEnd, lastIndex, srcStart, dst);
console.log("Value (array): " + ptrToStr(lastIndex, srcStart));
out.__DESERIALIZE(keyStart, keyEnd, lastIndex, srcStart += 2, dst);
console.log("Next: " + String.fromCharCode(load<u16>(srcStart)));
keyStart = 0;
while (isSpace(load<u16>((srcStart += 2)))) {
/* empty */
}
// while (isSpace(load<u16>((srcStart += 2)))) {
// /* empty */
// }
break;
}
} else if (code == BRACKET_LEFT) depth++;
srcStart += 2;
}
} else if (code == CHAR_T) {
if (load<u64>(srcStart) == 28429475166421108) {
console.log("Value (bool): " + ptrToStr(srcStart, srcStart + 8));
// @ts-ignore: exists
out.__DESERIALIZE(keyStart, keyEnd, srcStart, (srcStart += 8), dst);
console.log("Value (bool): " + ptrToStr(srcStart - 8, srcStart));
while (isSpace(load<u16>((srcStart += 2)))) {
/* empty */
}
// while (isSpace(load<u16>((srcStart += 2)))) {
// /* empty */
// }
srcStart += 2;
console.log("Next: " + String.fromCharCode(load<u16>(srcStart)) + " " + (srcStart < srcEnd).toString());
keyStart = 0;
}
} else if (code == CHAR_F) {
if (load<u64>(srcStart, 2) == 28429466576093281) {
console.log("Value (bool): " + ptrToStr(srcStart, srcStart + 10));
// @ts-ignore: exists
out.__DESERIALIZE(keyStart, keyEnd, srcStart, (srcStart += 10), dst);
console.log("Value (bool): " + ptrToStr(srcStart - 10, srcStart));
while (isSpace(load<u16>((srcStart += 2)))) {
/* empty */
}
// while (isSpace(load<u16>((srcStart += 2)))) {
// /* empty */
// }
srcStart += 2;
console.log("Next: " + String.fromCharCode(load<u16>(srcStart)));
keyStart = 0;
}
} else if (code == CHAR_N) {
if (load<u64>(srcStart) == 30399761348886638) {
console.log("Value (null): " + ptrToStr(srcStart, srcStart + 8));
// @ts-ignore: exists
out.__DESERIALIZE(keyStart, keyEnd, srcStart, (srcStart += 8), dst);
console.log("Value (null): " + ptrToStr(srcStart - 8, srcStart));
while (isSpace(load<u16>((srcStart += 2)))) {
/* empty */
}
// while (isSpace(load<u16>((srcStart += 2)))) {
/* empty */
// }
srcStart += 2;
console.log("Next: " + String.fromCharCode(load<u16>(srcStart)));
keyStart = 0;
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions assembly/deserialize/simple/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { BACK_SLASH } from "../../custom/chars";
import { DESERIALIZE_ESCAPE_TABLE, ESCAPE_HEX_TABLE } from "../../globals/tables";

export function deserializeString(srcStart: usize, srcEnd: usize, dst: usize): string {
srcStart += 2; srcEnd -= 2;
if (dst == 0) dst = __new(srcEnd - srcStart, idof<string>());
let dstPtr = dst;
let lastPtr = srcStart;
while (srcStart < srcEnd) {
Expand Down
8 changes: 7 additions & 1 deletion assembly/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,11 @@ export namespace JSON {
let type: nonnull<T> = changetype<nonnull<T>>(0);
// @ts-ignore: Defined by transform
if (isDefined(type.__DESERIALIZE)) {
const out = __new(offsetof<nonnull<T>>(), idof<nonnull<T>>());
// @ts-ignore
return deserializeObject<nonnull<T>>(dataPtr, dataPtr + dataSize, __new(offsetof<nonnull<T>>(), idof<nonnull<T>>()));
changetype<nonnull<T>>(out).__INITIALIZE();
// @ts-ignore
return deserializeObject<nonnull<T>>(dataPtr, dataPtr + dataSize, out);
} else if (type instanceof Map) {
// @ts-ignore
return deserializeMap<nonnull<T>>(dataPtr, dataPtr + dataSize);
Expand Down Expand Up @@ -414,6 +417,9 @@ export namespace JSON {
} else if (isString<T>()) {
// @ts-ignore: type
return deserializeString(srcStart, srcEnd, dst);
} else if (isArray<T>()) {
// @ts-ignore
return deserializeArray<T>(srcStart, srcEnd, dst);
} else {
let type: nonnull<T> = changetype<nonnull<T>>(0);
// @ts-ignore: declared by transform
Expand Down
65 changes: 60 additions & 5 deletions assembly/test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import { JSON } from ".";
import { deserializeString } from "./deserialize/simple/string";
import { bytes } from "./util";
import { ptrToStr } from "./util/ptrToStr";

// @json or @serializable work here
@json
class Vec3 {
x: f32 = 0.0;
y: f32 = 0.0;
z: f32 = 0.0;
x: f64 = 0.0;
y: f64 = 0.0;
z: f64 = 0.0;
__INITIALIZE(): Vec3 {
return this;
}
}

@json
Expand All @@ -20,6 +26,55 @@ class Player {
@omitnull()
pos!: Vec3 | null;
isVerified!: boolean;
__INITIALIZE(): Player {
this.firstName = "";
this.lastName = "";
this.lastActive = [];
this.age = null;
this.pos = changetype<Vec3>(__new(offsetof<Vec3 | null>(), idof<Vec3 | null>())).__INITIALIZE()
return this;
}
__DESERIALIZE(keyStart: usize, keyEnd: usize, valStart: usize, valEnd: usize, ptr: usize): void {
console.log(`${ptrToStr(keyStart, keyEnd)} -> ${ptrToStr(valStart, valEnd)} (${keyEnd - keyStart})`)
switch (keyEnd - keyStart) {
case 20: {
const code0 = load<u64>(keyStart, 0);
const code1 = load<u64>(keyStart, 8);
const code2 = load<u32>(keyStart, 16);
if (code0 == 32370111954878566 && code1 == 27303545189433460 && code2 == 6619245) { // first name
store<string>(ptr, JSON.__deserialize<string>(valStart, valEnd), offsetof<this>("firstName"));
return;
} else if (code0 == 32651591226032236 && code1 == 29555370777313345 && code2 == 6619254) { // lastActive
store<Array<i32>>(ptr, JSON.__deserialize<Array<i32>>(valStart, valEnd), offsetof<this>("lastActive"));
return;
} else if (code0 == 28429342022500457 && code1 == 29555310648164466 && code2 == 6553701) { // isVerified
store<boolean>(ptr, JSON.__deserialize<boolean>(valStart, valEnd), offsetof<this>("isVerified"));
return;
}
return;
}
case 6: {
let code = (<u64>load<u32>(keyStart) << 16) | <u64>load<u16>(keyStart, 4);
if (code == 493928513648) { // pos
store<Vec3 | null>(ptr, JSON.__deserialize<Vec3 | null>(valStart, valEnd), offsetof<this>("pos"));
return;
} else if (code == 433798447201) { // age
store<JSON.Box<i32> | null>(ptr, JSON.__deserialize<JSON.Box<i32> | null>(valStart, valEnd), offsetof<this>("age"));
return;
}
return;
}
case 16: {
const code0 = load<u64>(keyStart, 0);
const code1 = load<u64>(keyStart, 8);
if (code0 == 32651591226032236 && code1 == 28429440805568590) { // lastName
store<string>(ptr, JSON.__deserialize<string>(valStart, valEnd), offsetof<this>("lastName"));
return;
}
return;
}
}
}
}

const player: Player = {
Expand All @@ -37,5 +92,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));

0 comments on commit fc4e0f1

Please sign in to comment.