Skip to content

Commit

Permalink
re-order and clean
Browse files Browse the repository at this point in the history
  • Loading branch information
JairusSW committed Jun 13, 2024
1 parent b14344e commit 529d636
Show file tree
Hide file tree
Showing 32 changed files with 1,221 additions and 880 deletions.
2 changes: 1 addition & 1 deletion assembly/__benches__/as-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ bench("Parse Number OLD", () => {
});
bench("Stringify Object (Vec3)", () => {
blackbox<string>(vec.__JSON_Serialize());
blackbox<string>(vec.__SERIALIZE());
});*/

// TODO: Make this allocate without crashing
Expand Down
37 changes: 37 additions & 0 deletions assembly/deserialize/array.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { isMap } from "../src/util";
import { deserializeArrayArray } from "./array/array";
import { deserializeBooleanArray } from "./array/bool";
import { deserializeFloatArray } from "./array/float";
import { deserializeIntegerArray } from "./array/integer";
import { deserializeMapArray } from "./array/map";
import { deserializeObjectArray } from "./array/object";
import { deserializeStringArray } from "./array/string";

// @ts-ignore: Decorator
@inline export function deserializeArray<T extends unknown[]>(data: string): T {
if (isString<valueof<T>>()) {
return <T>deserializeStringArray(data);
} else if (isBoolean<valueof<T>>()) {
// @ts-ignore
return deserializeBooleanArray<T>(data);
} else if (isInteger<valueof<T>>()) {
// @ts-ignore
return deserializeIntegerArray<T>(data);
} else if (isFloat<valueof<T>>()) {
// @ts-ignore
return deserializeFloatArray<T>(data);
} else if (isArrayLike<valueof<T>>()) {
// @ts-ignore
return deserializeArrayArray<T>(data);
} else if (isMap<valueof<T>>()) {
return deserializeMapArray<T>(data);
} else if (isManaged<valueof<T>>() || isReference<valueof<T>>()) {
const type = changetype<nonnull<valueof<T>>>(0);
// @ts-ignore
if (isDefined(type.__JSON_Set_Key)) {
return deserializeObjectArray<T>(data);
}
}

throw new Error("Could not parse array of type " + nameof<T>() + "!");
}
31 changes: 31 additions & 0 deletions assembly/deserialize/array/array.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { leftBracketCode, rightBracketCode } from "../../src/chars";
import { JSON } from "../../src/json";
import { unsafeCharCodeAt } from "../../src/util";

// @ts-ignore: Decorator
@inline 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 === leftBracketCode) {
if (depth === 0) {
lastPos = i;
}
// Shifting is 6% faster than incrementing
depth++;
} else if (char === rightBracketCode) {
depth--;
if (depth === 0) {
i++;
result.push(JSON.parse<valueof<T>>(data.slice(lastPos, i)));
}
}
}
return result;
}
19 changes: 19 additions & 0 deletions assembly/deserialize/array/bool.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { eCode, fCode, tCode } from "../../src/chars";
import { unsafeCharCodeAt } from "../../src/util";
import { deserializeBoolean } from "../bool";

// @ts-ignore: Decorator
@inline export function deserializeBooleanArray<T extends boolean[]>(data: string): T {
const result = instantiate<T>();
let lastPos = 1;
for (let i = 1; i < data.length - 1; i++) {
const char = unsafeCharCodeAt(data, i);
if (char === tCode || char === fCode) {
lastPos = i;
} else if (char === eCode) {
i++;
result.push(deserializeBoolean(data.slice(lastPos, i)));
}
}
return result;
}
24 changes: 24 additions & 0 deletions assembly/deserialize/array/float.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { isSpace } from "util/string";
import { unsafeCharCodeAt } from "../../src/util";
import { commaCode, rightBracketCode } from "../../src/chars";
import { deserializeFloat } from "../float";

// @ts-ignore: Decorator
@inline export function deserializeFloatArray<T extends number[]>(data: string): T {
const result = instantiate<T>();
let lastPos = 0;
let i = 1;
let awaitingParse = false;
for (; i < data.length; i++) {
const char = unsafeCharCodeAt(data, i);
if (lastPos === 0 && ((char >= 48 && char <= 57) || char === 45)) {
awaitingParse = true;
lastPos = i;
} else if (awaitingParse && (isSpace(char) || char == commaCode || char == rightBracketCode) && lastPos > 0) {
awaitingParse = false;
result.push(deserializeFloat<valueof<T>>(data.slice(lastPos, i)));
lastPos = 0;
}
}
return result;
}
24 changes: 24 additions & 0 deletions assembly/deserialize/array/integer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { isSpace } from "util/string";
import { unsafeCharCodeAt } from "../../src/util";
import { commaCode, rightBracketCode } from "../../src/chars";
import { deserializeInteger } from "../integer";

// @ts-ignore: Decorator
@inline export function deserializeIntegerArray<T extends number[]>(data: string): T {
const result = instantiate<T>();
let lastPos = 0;
let i = 1;
let awaitingParse = false;
for (; i < data.length; i++) {
const char = unsafeCharCodeAt(data, i);
if (lastPos === 0 && ((char >= 48 && char <= 57) || char === 45)) {
awaitingParse = true;
lastPos = i;
} else if (awaitingParse && (isSpace(char) || char == commaCode || char == rightBracketCode) && lastPos > 0) {
awaitingParse = false;
result.push(deserializeInteger<valueof<T>>(data.slice(lastPos, i)));
lastPos = 0;
}
}
return result;
}
27 changes: 27 additions & 0 deletions assembly/deserialize/array/map.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { leftBraceCode, rightBraceCode } from "../../src/chars";
import { JSON } from "../../src/json";
import { unsafeCharCodeAt } from "../../src/util";

// @ts-ignore: Decorator
@inline export function deserializeMapArray<T extends unknown[]>(data: string): T {
const result = instantiate<T>();
let lastPos: u32 = 1;
let depth: u32 = 0;
for (let pos: u32 = 0; pos < <u32>data.length; pos++) {
const char = unsafeCharCodeAt(data, pos);
if (char === leftBraceCode) {
if (depth === 0) {
lastPos = pos;
}
depth++;
} else if (char === rightBraceCode) {
depth--;
if (depth === 0) {
pos++;
result.push(JSON.parse<valueof<T>>(data.slice(lastPos, pos)));
//lastPos = pos + 2;
}
}
}
return result;
}
27 changes: 27 additions & 0 deletions assembly/deserialize/array/object.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { leftBraceCode, rightBraceCode } from "../../src/chars";
import { JSON } from "../../src/json";
import { unsafeCharCodeAt } from "../../src/util";

// @ts-ignore: Decorator
@inline export function deserializeObjectArray<T extends unknown[]>(data: string): T {
const result = instantiate<T>();
let lastPos: u32 = 1;
let depth: u32 = 0;
for (let pos: u32 = 0; pos < <u32>data.length; pos++) {
const char = unsafeCharCodeAt(data, pos);
if (char === leftBraceCode) {
if (depth === 0) {
lastPos = pos;
}
depth++;
} else if (char === rightBraceCode) {
depth--;
if (depth === 0) {
pos++;
result.push(JSON.parse<valueof<T>>(data.slice(lastPos, pos)));
//lastPos = pos + 2;
}
}
}
return result;
}
29 changes: 29 additions & 0 deletions assembly/deserialize/array/string.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { backSlashCode, quoteCode } from "../../src/chars";
import { unsafeCharCodeAt } from "../../src/util";
import { deserializeString } from "../string";

// @ts-ignore: Decorator
@inline export function deserializeStringArray(data: string): string[] {
const result: string[] = [];
let lastPos = 0;
let instr = false;
let escaping = false;
for (let i = 1; i < data.length - 1; i++) {
const char = unsafeCharCodeAt(data, i);
if (char === backSlashCode && !escaping) {
escaping = true;
} else {
if (char === quoteCode && !escaping) {
if (instr === false) {
instr = true;
lastPos = i;
} else {
instr = false;
result.push(deserializeString(data, lastPos, i));
}
}
escaping = false;
}
}
return result;
}
18 changes: 18 additions & 0 deletions assembly/deserialize/bool.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { fCode, tCode } from "../src/chars";
import { unsafeCharCodeAt } from "../src/util";

/**
* Deserialize a string to type boolean
* @param data data to parse
* @returns boolean
*/
// @ts-ignore: Decorator
@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 === tCode && load<u64>(ptr) === 28429475166421108) return true;
else if (len === 5 && firstChar === fCode && load<u64>(ptr, 2) === 28429466576093281) return false;
return false//ERROR(`Expected to find boolean, but found "${data.slice(0, 100)}" instead!`);
}
14 changes: 14 additions & 0 deletions assembly/deserialize/box.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { JSON } from "../src/json";

// @ts-ignore: Decorator
@inline export function deserializeBox<T extends Box<any>>(data: string): T {
const instance = changetype<nonnull<T>>(__new(offsetof<nonnull<T>>(), idof<nonnull<T>>()))// as Box<usize>;
const val = instance._val;
instance._val = parseDirectInference(val, data);
// @ts-ignore
return changetype<T>(instance);
}

@inline function parseDirectInference<T>(type: T, data: string, initializeDefaultValues: boolean = false): T {
return JSON.parse<T>(data, initializeDefaultValues)
}
11 changes: 11 additions & 0 deletions assembly/deserialize/date.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// @ts-ignore
@inline export function deserializeDate(dateTimeString: string): Date {
// Use AssemblyScript's date parser
const d = Date.fromString(dateTimeString);

// Return a new object instead of the one that the parser returned.
// This may seem redundant, but addreses the issue when Date
// is globally aliased to wasi_Date (or some other superclass).
return new Date(d.getTime());
}

9 changes: 9 additions & 0 deletions assembly/deserialize/float.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// @ts-ignore: Decorator
@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);
}
7 changes: 7 additions & 0 deletions assembly/deserialize/integer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { snip_fast } from "../src/util";

// @ts-ignore: Decorator
@inline export function deserializeInteger<T>(data: string): T {
// @ts-ignore
return snip_fast<T>(data);
}
Loading

0 comments on commit 529d636

Please sign in to comment.