Skip to content

Commit

Permalink
__DESERIALIZE should take in pointer
Browse files Browse the repository at this point in the history
  • Loading branch information
JairusSW committed Jan 16, 2025
1 parent dd144e3 commit c97f121
Show file tree
Hide file tree
Showing 17 changed files with 262 additions and 332 deletions.
7 changes: 5 additions & 2 deletions assembly/deserialize/simple/array.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { BRACKET_LEFT } from "../../custom/chars";
import { isMap } from "../../custom/util";
import { deserializeArrayArray } from "./array/array";
import { deserializeBooleanArray } from "./array/bool";
import { deserializeFloatArray } from "./array/float";
Expand Down Expand Up @@ -37,3 +35,8 @@ export function deserializeArray<T extends unknown[]>(srcStart: usize, srcEnd: u
throw new Error("Could not parse array of type " + nameof<T>() + "!");
}
}

function isMap<T>(): boolean {
let type: T = changetype<T>(0);
return type instanceof Map;
}
36 changes: 12 additions & 24 deletions assembly/deserialize/simple/array/array.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,18 @@
import { BRACKET_LEFT, BRACKET_RIGHT } from "../../../custom/chars";
import { JSON } from "../../../";
import { unsafeCharCodeAt } from "../../../custom/util";

export function deserializeArrayArray<T extends unknown[][]>(data: string): T {
const result = instantiate<T>();
let lastPos = 0;
let depth = 0;
let i = 1;
// Find start of bracket
//for (; unsafeCharCodeAt(data, i) !== leftBracketCode; i++) {}
//i++;
for (; i < data.length - 1; i++) {
const char = unsafeCharCodeAt(data, i);
if (char == BRACKET_LEFT) {
if (depth == 0) {
lastPos = i;
}
// Shifting is 6% faster than incrementing
depth++;
} else if (char == BRACKET_RIGHT) {
depth--;
if (depth == 0) {
i++;
result.push(JSON.parse<valueof<T>>(data.slice(lastPos, i)));
}
export function deserializeArrayArray<T extends unknown[][]>(srcStart: usize, srcEnd: usize, dst: usize): T {
const out = changetype<T>(dst);
let lastIndex: usize = 0;
let depth: u32 = 0;
while (srcStart < srcEnd) {
const code = load<u16>(srcStart);
if (code == BRACKET_LEFT && depth++ == 0) {
lastIndex = srcStart;
} else if (code == BRACKET_RIGHT && --depth == 0) {
out.push(JSON.__deserialize<valueof<T>>(lastIndex, srcStart));
}
srcStart += 2;
}
return result;
return out;
}
4 changes: 1 addition & 3 deletions assembly/deserialize/simple/array/float.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { isSpace } from "../../../util";
import { unsafeCharCodeAt } from "../../../custom/util";
import { COMMA, BRACKET_RIGHT, BRACE_RIGHT } from "../../../custom/chars";
import { deserializeFloat } from "../float";
import { COMMA, BRACE_RIGHT } from "../../../custom/chars";
import { JSON } from "../../..";

// @ts-ignore: Decorator valid here
Expand Down
13 changes: 9 additions & 4 deletions assembly/deserialize/simple/array/map.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
import { BRACE_LEFT, BRACE_RIGHT } from "../../../custom/chars";
import { JSON } from "../../..";
import { unsafeCharCodeAt } from "../../../custom/util";

export function deserializeMapArray<T extends Map<any, any>[]>(srcStart: usize, srcEnd: usize, dst: usize): T {
const out = instantiate<T>();
let lastPos: usize = 2;
const out = changetype<T>(dst);
let lastIndex: usize = 0;
let depth: u32 = 0;
while (srcStart < srcEnd) {

const code = load<u16>(srcStart);
if (code == BRACE_LEFT && depth++ == 0) {
lastIndex = srcStart;
} else if (code == BRACE_RIGHT && --depth == 0) {
out.push(JSON.__deserialize<valueof<T>>(lastIndex, srcStart));
}
srcStart += 2;
}
return out;
}
30 changes: 11 additions & 19 deletions assembly/deserialize/simple/array/object.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,18 @@
import { BRACE_LEFT, BRACE_RIGHT } from "../../../custom/chars";
import { JSON } from "../../..";
import { unsafeCharCodeAt } from "../../../custom/util";

export function deserializeObjectArray<T extends unknown[]>(data: string): T {
const result = instantiate<T>();
let lastPos: u32 = 1;
export function deserializeObjectArray<T extends unknown[]>(srcStart: usize, srcEnd: usize, dst: usize): T {
const out = changetype<T>(dst);
let lastIndex: usize = 0;
let depth: u32 = 0;
for (let pos: u32 = 0; pos < <u32>data.length; pos++) {
const char = unsafeCharCodeAt(data, pos);
if (char == BRACE_LEFT) {
if (depth == 0) {
lastPos = pos;
}
depth++;
} else if (char == BRACE_RIGHT) {
depth--;
if (depth == 0) {
pos++;
result.push(JSON.parse<valueof<T>>(data.slice(lastPos, pos)));
//lastPos = pos + 2;
}
while (srcStart < srcEnd) {
const code = load<u16>(srcStart);
if (code == BRACE_LEFT && depth++ == 0) {
lastIndex = srcStart;
} else if (code == BRACE_RIGHT && --depth == 0) {
out.push(JSON.__deserialize<valueof<T>>(lastIndex, srcStart));
}
srcStart += 2;
}
return result;
return out;
}
19 changes: 1 addition & 18 deletions assembly/deserialize/simple/bool.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,7 @@
import { CHAR_F, CHAR_T } from "../../custom/chars";
import { unsafeCharCodeAt } from "../../custom/util";

/**
* Deserialize a string to type boolean
* @param data data to parse
* @returns boolean
*/
// @ts-ignore: Decorator valid here
@inline export function deserializeBoolean(data: string, start: i32 = 0, end: i32 = 0): boolean {
if (!end) end = data.length;
const len = end - start;
const ptr = changetype<usize>(data) + <usize>(start << 1);
const firstChar = unsafeCharCodeAt(data, start);
if (len == 4 && firstChar == CHAR_T && load<u64>(ptr) == 28429475166421108) return true;
else if (len == 5 && firstChar == CHAR_F && load<u64>(ptr, 2) == 28429466576093281) return false;
return false; //ERROR(`Expected to find boolean, but found "${data.slice(0, 100)}" instead!`);
}

// @ts-ignore: Decorator valid here
@inline export function deserializeBoolean_NEW(srcStart: usize, srcEnd: usize): boolean {
@inline export function deserializeBoolean(srcStart: usize, srcEnd: usize): boolean {
const srcSize = srcEnd - srcStart;
const firstChar = load<u16>(srcStart);
if (srcSize == 4 && firstChar == CHAR_T && load<u64>(srcStart) == 28429475166421108) return true;
Expand Down
21 changes: 3 additions & 18 deletions assembly/deserialize/simple/float.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,11 @@
// @ts-ignore: Decorator valid here
@inline export function deserializeFloat<T>(data: string): T {
// @ts-ignore
const type: T = 0;
// @ts-ignore
if (type instanceof f64) return f64.parse(data);
// @ts-ignore
return f32.parse(data);
}
import { ptrToStr } from "../../util/ptrToStr";

// @ts-ignore: Decorator valid here
@inline export function deserializeFloat_NEW<T>(srcStart: usize, srcEnd: usize): T {
@inline export function deserializeFloat<T>(srcStart: usize, srcEnd: usize): T {
// @ts-ignore
const type: T = 0;
// @ts-ignore
if (type instanceof f64) return f64.parse(str(srcStart, srcEnd));
if (type instanceof f64) return f64.parse(ptrToStr(srcStart, srcEnd));
// @ts-ignore
return f32.parse(str(srcStart, srcEnd));
}

function str(start: usize, end: usize): string {
const size = end - start;
const out = __new(size, idof<string>());
memory.copy(out, start, size);
return changetype<string>(out);
}
37 changes: 16 additions & 21 deletions assembly/deserialize/simple/object.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { BACK_SLASH, COMMA, CHAR_F, BRACE_LEFT, BRACKET_LEFT, CHAR_N, QUOTE, BRACE_RIGHT, BRACKET_RIGHT, CHAR_T, COLON } from "../../custom/chars";
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 srcPtr = srcStart;

let keyStart: usize = 0;
Expand All @@ -20,7 +22,7 @@ export function deserializeObject<T>(srcStart: usize, srcEnd: usize, dst: usize)
if (isKey) {
keyStart = lastIndex;
keyEnd = srcStart;
console.log("Key: " + str(lastIndex, srcStart));
console.log("Key: " + ptrToStr(lastIndex, srcStart));
while (isSpace((code = load<u16>((srcStart += 2))))) {
/* empty */
}
Expand All @@ -43,9 +45,9 @@ export function deserializeObject<T>(srcStart: usize, srcEnd: usize, dst: usize)
while (isSpace(load<u16>((srcStart += 2)))) {
/* empty */
}
console.log("Value (string): " + str(lastIndex, srcStart));
console.log("Value (string): " + ptrToStr(lastIndex, srcStart));
// @ts-ignore: exists
dst.__DESERIALIZE(keyStart, keyEnd, lastIndex, srcStart);
out.__DESERIALIZE(keyStart, keyEnd, lastIndex, srcStart, dst);
keyStart = 0;
break;
}
Expand All @@ -58,8 +60,8 @@ export function deserializeObject<T>(srcStart: usize, srcEnd: usize, dst: usize)
const code = load<u16>(srcStart);
if (code == COMMA || code == BRACE_RIGHT || isSpace(code)) {
// @ts-ignore: exists
dst.__DESERIALIZE(keyStart, keyEnd, lastIndex, srcStart);
console.log("Value (number): " + str(lastIndex, srcStart));
out.__DESERIALIZE(keyStart, keyEnd, lastIndex, srcStart, dst);
console.log("Value (number): " + ptrToStr(lastIndex, srcStart));
while (isSpace(load<u16>((srcStart += 2)))) {
/* empty */
}
Expand All @@ -77,8 +79,8 @@ export function deserializeObject<T>(srcStart: usize, srcEnd: usize, dst: usize)
if (((code ^ BRACE_RIGHT) | (code ^ BRACKET_RIGHT)) == 32) {
if (--depth == 0) {
// @ts-ignore: exists
dst.__DESERIALIZE(keyStart, keyEnd, lastIndex, srcStart);
console.log("Value (object): " + str(lastIndex, srcStart));
out.__DESERIALIZE(keyStart, keyEnd, lastIndex, srcStart, dst);
console.log("Value (object): " + ptrToStr(lastIndex, srcStart));
keyStart = 0;
while (isSpace(load<u16>((srcStart += 2)))) {
/* empty */
Expand All @@ -91,8 +93,8 @@ export function deserializeObject<T>(srcStart: usize, srcEnd: usize, dst: usize)
} else if (code == CHAR_T) {
if (load<u64>(srcStart) == 28429475166421108) {
// @ts-ignore: exists
dst.__DESERIALIZE(keyStart, keyEnd, srcStart, (srcStart += 8));
console.log("Value (bool): " + str(srcStart - 8, srcStart));
out.__DESERIALIZE(keyStart, keyEnd, srcStart, (srcStart += 8), dst);
console.log("Value (bool): " + ptrToStr(srcStart - 8, srcStart));
while (isSpace(load<u16>((srcStart += 2)))) {
/* empty */
}
Expand All @@ -101,8 +103,8 @@ export function deserializeObject<T>(srcStart: usize, srcEnd: usize, dst: usize)
} else if (code == CHAR_F) {
if (load<u64>(srcStart, 2) == 28429466576093281) {
// @ts-ignore: exists
dst.__DESERIALIZE(keyStart, keyEnd, srcStart, (srcStart += 10));
console.log("Value (bool): " + str(srcStart - 10, srcStart));
out.__DESERIALIZE(keyStart, keyEnd, srcStart, (srcStart += 10), dst);
console.log("Value (bool): " + ptrToStr(srcStart - 10, srcStart));
while (isSpace(load<u16>((srcStart += 2)))) {
/* empty */
}
Expand All @@ -111,21 +113,14 @@ export function deserializeObject<T>(srcStart: usize, srcEnd: usize, dst: usize)
} else if (code == CHAR_N) {
if (load<u64>(srcStart) == 30399761348886638) {
// @ts-ignore: exists
dst.__DESERIALIZE(keyStart, keyEnd, srcStart, (srcStart += 8));
console.log("Value (null): " + str(srcStart - 8, srcStart));
out.__DESERIALIZE(keyStart, keyEnd, srcStart, (srcStart += 8), dst);
console.log("Value (null): " + ptrToStr(srcStart - 8, srcStart));
while (isSpace(load<u16>((srcStart += 2)))) {
/* empty */
}
}
}
}
}
return dst;
}

function str(start: usize, end: usize): string {
const size = end - start;
const out = __new(size, idof<string>());
memory.copy(out, start, size);
return changetype<string>(out);
return out;
}
Loading

0 comments on commit c97f121

Please sign in to comment.