Skip to content

Commit

Permalink
release: v0.9.12
Browse files Browse the repository at this point in the history
  • Loading branch information
JairusSW committed Jul 8, 2024
1 parent 126009e commit 67445c4
Show file tree
Hide file tree
Showing 29 changed files with 342 additions and 177 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ v0.9.8a - Fix #80 - Empty Maps were not serialized to {}, instead, threw memory
v0.9.8b - Fix #81 - Revert transform
v0.9.9 - Fix #82 - Initialize maps
v0.9.9a - Remove extraneous logs from transform
v0.9.11 - Fix transform type checks. switch to nodekind checks
v0.9.10 - Fix transform type checks. switch to nodekind checks
v0.9.11 - Remove MpZ--implement custom serializers and deserializers in the works
v0.9.12 - Add compat with aspect

[UNRELEASED] v1.0.0
- Allow nullable primitives
Expand Down
155 changes: 155 additions & 0 deletions assembly/custom/bs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
import { dtoa_buffered, itoa_buffered } from "util/number";
import { OBJECT, TOTAL_OVERHEAD } from "rt/common";
@inline const MAX_LEN: usize = 65536;
const STORE: usize[] = [];
let STORE_LEN: usize = 0;
const CACHE = memory.data(i32(MAX_LEN));
// Configurable amount of referenceable strings
let POINTER = changetype<usize>(CACHE);
@inline const MAX_CACHE = CACHE + MAX_LEN;

export namespace bs {
@inline export function write_int<T extends number>(num: T): void {
POINTER += itoa_buffered(POINTER, num) << 1;
if (MAX_CACHE <= POINTER) bs.shrink();
}
@inline export function write_int_u<T extends number>(num: T): void {
POINTER += itoa_buffered(POINTER, num) << 1;
}

@inline export function write_fl<T extends number>(num: T): void {
POINTER += dtoa_buffered(POINTER, num) << 1;
if (MAX_CACHE <= POINTER) bs.shrink();
}
@inline export function write_fl_u<T extends number>(num: T): void {
POINTER += dtoa_buffered(POINTER, num) << 1;
}

@inline export function write_b(buf: usize, bytes: usize = changetype<OBJECT>(buf - TOTAL_OVERHEAD).rtSize): void {
memory.copy(POINTER, buf, bytes);
POINTER += bytes;
if (MAX_CACHE <= POINTER) bs.shrink();
}
@inline export function write_b_u(buf: usize, bytes: usize = changetype<OBJECT>(buf - TOTAL_OVERHEAD).rtSize): void {
memory.copy(POINTER, buf, bytes);
POINTER += bytes;
}

@inline export function write_s(str: string, bytes: usize = changetype<OBJECT>(changetype<usize>(str) - TOTAL_OVERHEAD).rtSize): void {
memory.copy(POINTER, changetype<usize>(str), bytes);
POINTER += bytes;
if (MAX_CACHE <= POINTER) bs.shrink();
}
@inline export function write_s_u(str: string, bytes: usize = changetype<OBJECT>(changetype<usize>(str) - TOTAL_OVERHEAD).rtSize): void {
memory.copy(POINTER, changetype<usize>(str), bytes);
POINTER += bytes;
}

@inline export function write_s_se(str: string, start: usize, end: usize): void {
const bytes = end - start;
memory.copy(POINTER, changetype<usize>(str) + start, bytes);
POINTER += bytes;
if (MAX_CACHE <= POINTER) bs.shrink();
}
@inline export function write_s_se_u(str: string, start: usize, end: usize): void {
const bytes = end - start;
memory.copy(POINTER, changetype<usize>(str) + start, bytes);
POINTER += bytes;
}

@inline export function write_16(char: i32): void {
store<u16>(POINTER, char);
POINTER += 2;
if (MAX_CACHE <= POINTER) bs.shrink();
}
@inline export function write_16_u(char: i32): void {
store<u16>(POINTER, char);
//POINTER += 2;
}

@inline export function write_32(chars: i32): void {
store<u32>(POINTER, chars);
POINTER += 4;
if (MAX_CACHE <= POINTER) bs.shrink();
}
@inline export function write_32_u(chars: i32): void {
store<u32>(POINTER, chars);
//POINTER += 4;
}

@inline export function write_64(chars: i64): void {
store<u64>(POINTER, chars);
POINTER += 8;
if (MAX_CACHE <= POINTER) bs.shrink();
}

@inline export function write_64_u(chars: i64): void {
store<u64>(POINTER, chars);
POINTER += 8;
}

@inline export function write_128(chars: v128): void {
store<v128>(POINTER, chars);
POINTER += 16;
if (MAX_CACHE <= POINTER) bs.shrink();
}
@inline export function write_128_u(chars: v128): void {
store<v128>(POINTER, chars);
//POINTER += 16;
//if (MAX_CACHE <= POINTER) bs.shrink();
}
@inline export function shrink(): void {
const len = POINTER - CACHE;
STORE_LEN += len;
const out = __new(
len,
idof<ArrayBuffer>()
);
memory.copy(out, CACHE, len);
bs.reset();
STORE.push(out);
}
@inline export function out<T>(): T {
const len = POINTER - CACHE;
let out = __new(
len + STORE_LEN,
idof<T>()
);

memory.copy(out, CACHE, len);
if (STORE_LEN) {
out += len;
for (let i = 0; i < STORE.length; i++) {
const ptr = changetype<usize>(unchecked(STORE[i]));
const storeLen = changetype<OBJECT>(ptr - TOTAL_OVERHEAD).rtSize;
memory.copy(out, ptr, storeLen);
//__unpin(ptr);
out += storeLen;
}
STORE_LEN = 0;
}
bs.reset();

return changetype<T>(out);
}

@inline export function out_u<T>(): T {
const len = POINTER - CACHE;
const out = __new(
len + STORE_LEN,
idof<T>()
);

memory.copy(out, CACHE, len);
bs.reset();

return changetype<T>(out);
}

@inline export function _out(out: usize): void {
memory.copy(out, CACHE, POINTER - CACHE);
}
@inline export function reset(): void {
POINTER = CACHE;
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
28 changes: 28 additions & 0 deletions assembly/util.ts → assembly/custom/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -362,3 +362,31 @@ export function containsCodePoint(str: string, code: u32, start: i32, end: i32):
}
return false;
}

export function _intTo16(int: i32): i32 {
if (int < 10) {
// 0-10
return 48 + int;
} else {
// a-f
return 87 + int;
}
}

@inline export function intTo16(int: i32): i32 {
const high = int >> 4;
const low = int & 0x0F;
if (low < 10) {
if (high < 10) {
return ((48 + low) << 16) | 48 + high;
} else {
return ((48 + low) << 16) | 87 + high;
}
} else {
if (high < 10) {
return ((87 + low) << 16) | 48 + high;
} else {
return ((87 + low) << 16) | 87 + high;
}
}
}
2 changes: 1 addition & 1 deletion assembly/deserialize/array.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isMap } from "../util";
import { isMap } from "../custom/util";
import { deserializeArrayArray } from "./array/array";
import { deserializeBooleanArray } from "./array/bool";
import { deserializeFloatArray } from "./array/float";
Expand Down
4 changes: 2 additions & 2 deletions assembly/deserialize/array/array.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { BRACKET_LEFT, BRACKET_RIGHT } from "../../chars";
import { BRACKET_LEFT, BRACKET_RIGHT } from "../../custom/chars";
import { JSON } from "../..";
import { unsafeCharCodeAt } from "../../util";
import { unsafeCharCodeAt } from "../../custom/util";

// @ts-ignore: Decorator valid here
@inline export function deserializeArrayArray<T extends unknown[][]>(data: string): T {
Expand Down
4 changes: 2 additions & 2 deletions assembly/deserialize/array/bool.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { CHAR_E, CHAR_F, CHAR_T } from "../../chars";
import { unsafeCharCodeAt } from "../../util";
import { CHAR_E, CHAR_F, CHAR_T } from "../../custom/chars";
import { unsafeCharCodeAt } from "../../custom/util";
import { deserializeBoolean } from "../bool";

// @ts-ignore: Decorator valid here
Expand Down
4 changes: 2 additions & 2 deletions assembly/deserialize/array/float.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { isSpace } from "util/string";
import { unsafeCharCodeAt } from "../../util";
import { COMMA, BRACKET_RIGHT } from "../../chars";
import { unsafeCharCodeAt } from "../../custom/util";
import { COMMA, BRACKET_RIGHT } from "../../custom/chars";
import { deserializeFloat } from "../float";

// @ts-ignore: Decorator valid here
Expand Down
4 changes: 2 additions & 2 deletions assembly/deserialize/array/integer.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { isSpace } from "util/string";
import { unsafeCharCodeAt } from "../../util";
import { COMMA, BRACKET_RIGHT } from "../../chars";
import { unsafeCharCodeAt } from "../../custom/util";
import { COMMA, BRACKET_RIGHT } from "../../custom/chars";
import { deserializeInteger } from "../integer";

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

// @ts-ignore: Decorator valid here
@inline export function deserializeMapArray<T extends unknown[]>(data: string): T {
Expand Down
4 changes: 2 additions & 2 deletions assembly/deserialize/array/object.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { BRACE_LEFT, BRACE_RIGHT } from "../../chars";
import { BRACE_LEFT, BRACE_RIGHT } from "../../custom/chars";
import { JSON } from "../..";
import { unsafeCharCodeAt } from "../../util";
import { unsafeCharCodeAt } from "../../custom/util";

// @ts-ignore: Decorator valid here
@inline export function deserializeObjectArray<T extends unknown[]>(data: string): T {
Expand Down
4 changes: 2 additions & 2 deletions assembly/deserialize/array/string.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { BACK_SLASH, QUOTE } from "../../chars";
import { unsafeCharCodeAt } from "../../util";
import { BACK_SLASH, QUOTE } from "../../custom/chars";
import { unsafeCharCodeAt } from "../../custom/util";
import { deserializeString } from "../string";

// @ts-ignore: Decorator valid here
Expand Down
4 changes: 2 additions & 2 deletions assembly/deserialize/bool.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { CHAR_F, CHAR_T } from "../chars";
import { unsafeCharCodeAt } from "../util";
import { CHAR_F, CHAR_T } from "../custom/chars";
import { unsafeCharCodeAt } from "../custom/util";

/**
* Deserialize a string to type boolean
Expand Down
2 changes: 1 addition & 1 deletion assembly/deserialize/integer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { snip_fast } from "../util";
import { snip_fast } from "../custom/util";

// @ts-ignore: Decorator valid here
@inline export function deserializeInteger<T>(data: string): T {
Expand Down
8 changes: 3 additions & 5 deletions assembly/deserialize/map.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Virtual } from "as-virtual/assembly";
import { containsCodePoint, unsafeCharCodeAt } from "../util";
import { containsCodePoint, unsafeCharCodeAt } from "../custom/util";
import {
CHAR_A,
BACK_SLASH,
Expand All @@ -18,7 +18,7 @@ import {
CHAR_S,
CHAR_T,
CHAR_U
} from "../chars";
} from "../custom/chars";
import { deserializeBoolean } from "./bool";
import { JSON } from "..";
import { deserializeString } from "./string";
Expand Down Expand Up @@ -72,8 +72,6 @@ import { deserializeFloat } from "./float";
depth--;
if (depth === 0) {
++objectValueIndex;
console.log("Index: " + nameof<indexof<T>>());
console.log("Value: " + nameof<valueof<T>>());
map.set(deserializeMapKey<indexof<T>>(key), JSON.parse<valueof<T>>(data.slice(outerLoopIndex, objectValueIndex)));
outerLoopIndex = objectValueIndex;
isKey = false;
Expand Down Expand Up @@ -181,4 +179,4 @@ function deserializeMapKey<T>(key: Virtual<string>): T {
}

throw new Error(`JSON: Cannot parse JSON object to a Map with a key of type ${nameof<T>()}`);
}
}
4 changes: 2 additions & 2 deletions assembly/deserialize/object.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { unsafeCharCodeAt } from "../util";
import { CHAR_A, BACK_SLASH, COMMA, CHAR_E, CHAR_F, CHAR_L, BRACE_LEFT, BRACKET_LEFT, CHAR_N, QUOTE, CHAR_R, BRACE_RIGHT, BRACKET_RIGHT, CHAR_S, CHAR_T, CHAR_U } from "../chars";
import { unsafeCharCodeAt } from "../custom/util";
import { CHAR_A, BACK_SLASH, COMMA, CHAR_E, CHAR_F, CHAR_L, BRACE_LEFT, BRACKET_LEFT, CHAR_N, QUOTE, CHAR_R, BRACE_RIGHT, BRACKET_RIGHT, CHAR_S, CHAR_T, CHAR_U } from "../custom/chars";
import { isSpace } from "util/string";

// @ts-ignore: Decorator valid here
Expand Down
6 changes: 3 additions & 3 deletions assembly/deserialize/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ import {
CHAR_T,
TAB,
CHAR_U
} from "../chars";
import { Sink } from "../sink";
import { unsafeCharCodeAt } from "../util";
} from "../custom/chars";
import { Sink } from "../custom/sink";
import { unsafeCharCodeAt } from "../custom/util";

// @ts-ignore: Decorator valid here
@inline export function deserializeString(data: string, start: i32 = 0, end: i32 = 0): string {
Expand Down
2 changes: 1 addition & 1 deletion assembly/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { deserializeFloat } from "./deserialize/float";
import { deserializeObject } from "./deserialize/object";
import { deserializeMap } from "./deserialize/map";
import { deserializeDate } from "./deserialize/date";
import { NULL_WORD } from "./chars";
import { NULL_WORD } from "./custom/chars";
import { deserializeInteger } from "./deserialize/integer";
import { deserializeString } from "./deserialize/string";

Expand Down
4 changes: 2 additions & 2 deletions assembly/serialize/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import {
BRACKET_LEFT_WORD,
BRACKET_RIGHT,
BRACKET_RIGHT_WORD
} from "../chars";
import { Sink } from "../sink";
} from "../custom/chars";
import { Sink } from "../custom/sink";
import { serializeString } from "./string";

// @ts-ignore: Decorator valid here
Expand Down
4 changes: 2 additions & 2 deletions assembly/serialize/map.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { COLON, COMMA, BRACE_LEFT_WORD, BRACE_RIGHT } from "../chars";
import { COLON, COMMA, BRACE_LEFT_WORD, BRACE_RIGHT } from "../custom/chars";
import { JSON } from "..";
import { Sink } from "../sink";
import { Sink } from "../custom/sink";

// @ts-ignore: Decorator valid here
@inline export function serializeMap<T extends Map<any, any>>(data: T): string {
Expand Down
Loading

0 comments on commit 67445c4

Please sign in to comment.