Skip to content

Commit

Permalink
add support for @alias
Browse files Browse the repository at this point in the history
  • Loading branch information
JairusSW committed Jan 3, 2025
1 parent 87e6d2a commit d1910a6
Show file tree
Hide file tree
Showing 15 changed files with 167 additions and 91 deletions.
6 changes: 3 additions & 3 deletions assembly/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ declare function serializable(..._): void;
/**
* Property decorator that provides an alias name for JSON serialization.
*/
declare function alias(newName: string): void;
declare function alias(newName: string): Function;

/**
* Property decorator that allows omits a field, making it be ignored.
Expand All @@ -21,11 +21,11 @@ declare function omit(..._): void;
/**
* Property decorator that allows a field to be omitted when equal to an Expression.
*/
declare function omitif(condition: string | (() => boolean)): void;
declare function omitif(condition: string | ((value: any) => boolean)): Function;

/**
* Property decorator that allows a field to be omitted when a property is null.
*/
declare function omitnull(..._): void;
declare function omitnull(..._): Function;

declare type Raw = number;
11 changes: 10 additions & 1 deletion assembly/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import { NULL_WORD, QUOTE } from "./custom/chars";
import { bs } from "as-bs";
import { dtoa_buffered, itoa_buffered } from "util/number";
import { serializeBool } from "./serialize/simple/bool";
import { serializeInteger } from "./serialize/simple/integer";
import { serializeFloat } from "./serialize/simple/float";

class Nullable { }

Expand Down Expand Up @@ -88,6 +90,13 @@ export namespace JSON {
} else if (isDefined(data.__SERIALIZE)) {
// @ts-ignore
return data.__SERIALIZE(changetype<usize>(data));
// @ts-ignore: Supplied by transform
} else if (isDefined(data.__SERIALIZE_BS) && isDefined(data.__ALLOCATE)) {
// @ts-ignore
data.__ALLOCATE();
// @ts-ignore
data.__SERIALIZE_BS(changetype<usize>(data), false);
return bs.out<string>();
} else if (data instanceof Date) {
out = out
? changetype<string>(__renew(changetype<usize>(out), 52))
Expand Down Expand Up @@ -322,7 +331,7 @@ export namespace JSON {
}

// @ts-ignore: Decorator valid here
@inline export function serialize_simple<T>(src: T, staticSize: bool = false): void {
@inline function serialize_simple<T>(src: T, staticSize: bool = false): void {
if (isBoolean<T>()) {
serializeBool(src as bool, staticSize);
} else if (isInteger<T>()) {
Expand Down
49 changes: 22 additions & 27 deletions assembly/serialize/simple/arbitrary.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,36 @@
import { JSON } from "../..";
import { Sink } from "../../custom/sink";
import { serializeArray } from "./array";
import { serializeBool } from "./bool";
import { serializeInteger } from "./integer";
import { serializeString } from "./string";

export function serializeArbitrary(data: JSON.Value): string {
switch (data.type) {
export function serializeArbitrary(src: JSON.Value, staticSize: bool = false): void {
switch (src.type) {
case JSON.Types.U8:
return data.get<u8>().toString();
serializeInteger<u8>(src.get<u8>(), staticSize);
break;
case JSON.Types.U16:
return data.get<u16>().toString();
serializeInteger<u16>(src.get<u16>(), staticSize);
break;
case JSON.Types.U32:
return data.get<u32>().toString();
serializeInteger<u32>(src.get<u32>(), staticSize);
break;
case JSON.Types.U64:
return data.get<u64>().toString();
serializeInteger<u64>(src.get<u64>(), staticSize);
break;
case JSON.Types.String:
return JSON.stringify(data.get<string>());
serializeString(src.get<string>(), staticSize);
break;
case JSON.Types.Bool:
return data.get<boolean>() ? "true" : "false";
serializeBool(src.get<bool>(), staticSize);
case JSON.Types.Array: {
const arr = data.get<JSON.Value[]>();
if (!arr.length) return "[]";
const out = Sink.fromStringLiteral("[");
const end = arr.length - 1;
for (let i = 0; i < end; i++) {
const element = unchecked(arr[i]);
out.write(element.toString());
out.write(",");
}

const element = unchecked(arr[end]);
out.write(element.toString());

out.write("]");
return out.toString();
serializeArray(src.get<JSON.Value[]>(), staticSize);
break;
}
default: {
const fn = JSON.Value.METHODS.get(data.type - JSON.Types.Struct);
const value = data.get<usize>();
return call_indirect<string>(fn, 0, value);
const fn = JSON.Value.METHODS.get(src.type - JSON.Types.Struct);
const value = src.get<usize>();
call_indirect<string>(fn, 0, value);
}
}
}
6 changes: 3 additions & 3 deletions assembly/serialize/simple/array.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { serialize_simple } from ".";
import { bs } from "as-bs";
import { COMMA, BRACKET_RIGHT, BRACKET_LEFT } from "../../custom/chars";
import { JSON } from "../..";

export function serializeArray<T extends any[]>(src: T, staticSize: bool = false): void {
const srcSize = load<u32>(changetype<usize>(src), offsetof<T>("byteLength"));
Expand All @@ -23,15 +23,15 @@ export function serializeArray<T extends any[]>(src: T, staticSize: bool = false

while (srcPtr < srcEnd) {
const block = load<valueof<T>>(srcPtr);
serialize_simple<valueof<T>>(block);
JSON.serialize_simple<valueof<T>>(block);
if (!staticSize) bs.ensureSize(2);
store<u16>(bs.offset, COMMA);
bs.offset += 2;
srcPtr += sizeof<string>();
}

const lastBlock = load<valueof<T>>(srcPtr);
serialize_simple<valueof<T>>(lastBlock);
JSON.serialize_simple<valueof<T>>(lastBlock);
if (!staticSize) bs.ensureSize(2);
store<u16>(bs.offset, BRACKET_RIGHT);
bs.offset += 2;
Expand Down
1 change: 0 additions & 1 deletion assembly/serialize/simple/map.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { BRACE_LEFT, BRACE_RIGHT, COLON, COMMA } from "../../custom/chars";
import { bs } from "as-bs";
import { serialize_simple } from ".";

export function serializeMap<T extends Map<any, any>>(src: T, staticSize: bool = false): void {
const srcSize = src.size;
Expand Down
29 changes: 15 additions & 14 deletions assembly/test.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,33 @@
import { bs } from "as-bs";
import { serialize_simple } from "./serialize/simple";
import { VecBase } from "./types";
import { bytes } from "./util/bytes";
import { JSON } from ".";

@json
class Vec3 {
@omit
base: VecBase = new VecBase();
x: i32 = 1;
@omitif((v: i32) => {
if (v == 1) return true;
return false;
})
y: i32 = 2;
z: i32 = 3;
__SERIALIZE_BS(ptr: usize, staticSize: bool): void {
store<u64>(bs.offset, 27303493649956987, 0); // {"ba
store<u64>(bs.offset, 16325694684725363, 8); // se":
bs.offset += 16;
serialize_simple<VecBase>(load<VecBase>(ptr, offsetof<this>("base")), staticSize);
store<u64>(bs.offset, 9570664606466092, 0); // ,"x"
@inline __ALLOCATE(): void {
bs.ensureSize(1024);
}
@inline __SERIALIZE_BS(ptr: usize, staticSize: bool): void {
store<u64>(bs.offset, 9570664606466171, 0); // {"x"
store<u16>(bs.offset, 58, 8); // :
bs.offset += 10;
serialize_simple<i32>(load<i32>(ptr, offsetof<this>("x")), staticSize);
JSON.serialize_simple<i32>(load<i32>(ptr, offsetof<this>("x")), staticSize);
store<u64>(bs.offset, 9570668901433388, 0); // ,"y"
store<u16>(bs.offset, 58, 8); // :
bs.offset += 10;
serialize_simple<i32>(load<i32>(ptr, offsetof<this>("y")), staticSize);
JSON.serialize_simple<i32>(load<i32>(ptr, offsetof<this>("y")), staticSize);
store<u64>(bs.offset, 9570673196400684, 0); // ,"z"
store<u16>(bs.offset, 58, 8); // :
bs.offset += 10;
serialize_simple<i32>(load<i32>(ptr, offsetof<this>("z")), staticSize);
JSON.serialize_simple<i32>(load<i32>(ptr, offsetof<this>("z")), staticSize);
store<u16>(bs.offset, 125, 0); // }
bs.offset += 2;
}
Expand Down Expand Up @@ -66,10 +67,10 @@ const vec: Vec3 = {
}

bs.ensureSize(1024);
vec.__SERIALIZE_BS(changetype<usize>(vec), true);
// vec.__SERIALIZE_BS(changetype<usize>(vec), true);
let out = __new(bs.offset - bs.buffer, idof<string>())
const serialized = bs.shrinkTo<string>();
console.log("Serialized: " + serialized);
console.log("Serialized: " + JSON.stringify(vec));

bench("Serialize Object (New)", () => {
vec.__SERIALIZE_BS(changetype<usize>(vec), true);
Expand Down
8 changes: 7 additions & 1 deletion assembly/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
@json
export class VecBase {
@omit()
base: i32 = 0;
@inline __ALLOCATE(): void {
bs.ensureSize(4);
}
@inline __SERIALIZE_BS(ptr: usize, staticSize: bool): void {
store<u32>(bs.offset, 8192123);
bs.offset += 4;
}
}
9 changes: 9 additions & 0 deletions transform/lib/builder.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/builder.js.map

Large diffs are not rendered by default.

70 changes: 51 additions & 19 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.

Loading

0 comments on commit d1910a6

Please sign in to comment.