Skip to content

Commit

Permalink
codegen issues
Browse files Browse the repository at this point in the history
  • Loading branch information
JairusSW committed Aug 3, 2024
1 parent 6e34325 commit 009a4ad
Show file tree
Hide file tree
Showing 4 changed files with 224 additions and 60 deletions.
173 changes: 173 additions & 0 deletions assembly/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,179 @@ export namespace JSON {
@inline static from<T>(value: T): Box<T> {
return new Box(value);
}
@inline
@operator("==")
eq(other: this): bool {
if (isNullable<T>() && changetype<usize>(this) == <usize>0) {
if (changetype<usize>(other) == <usize>0) return true;
}
return this.value == other.value;
}

@inline
@operator("!=")
notEq(other: this): bool {
if (isNullable<T>() && changetype<usize>(this) == <usize>0) {
if (changetype<usize>(this) == changetype<usize>(other)) return true;
}
return this.value != other.value;
}

@inline
@operator(">")
gt(other: this): bool {
return this._val > other._val;
}

@inline
@operator(">=")
ge(other: this): bool {
return this._val >= other._val;
}

@inline
@operator("<")
lt(other: this): bool {
return this._val < other._val;
}

@inline
@operator("<=")
le(other: this): bool {
return this._val <= other._val;
}

@inline
@operator(">>")
shr(other: this): this {
// @ts-ignore
return instantiate<this>(this._val >> other._val);
}

@inline
@operator(">>>")
shr_u(other: this): this {
// @ts-ignore
return instantiate<this>(this._val >>> other._val);
}

@inline
@operator("<<")
shl(other: this): this {
// @ts-ignore
return instantiate<this>(this._val << other._val);
}

@inline
@operator("&")
and(other: this): this {
// @ts-ignore
return instantiate<this>(this._val & other._val);
}

@inline
@operator("|")
or(other: this): this {
// @ts-ignore
return instantiate<this>(this._val | other._val);
}

@inline
@operator("^")
xor(other: this): this {
// @ts-ignore
return instantiate<this>(this._val ^ other._val);
}

@inline
@operator("+")
add(other: this): this {
// @ts-ignore
return instantiate<this>(this._val + other._val);
}

@inline
@operator("-")
sub(other: this): this {
// @ts-ignore
return instantiate<this>(this._val - other._val);
}

@inline
@operator("*")
mul(other: this): this {
// @ts-ignore
return instantiate<this>(this._val * other._val);
}

@inline
@operator("/")
div(other: this): this {
// @ts-ignore
return instantiate<this>(this._val / other._val);
}

@inline
@operator("**")
pow(other: this): this {
// @ts-ignore
return instantiate<this>((this._val ** other._val) as T);
}

@inline
@operator("%")
rem(other: this): this {
// @ts-ignore
return instantiate<this>(this._val % other._val);
}

@inline
@operator.prefix("!")
isEmpty(): bool {
return !this._val;
}

@inline
@operator.prefix("~")
not(): this {
return instantiate<this>(~this._val);
}

@inline
@operator.prefix("+")
pos(): this {
return instantiate<this>(+this._val);
}

@inline
@operator.prefix("-")
neg(): this {
return instantiate<this>(-this._val);
}

@operator.prefix("++")
preInc(): this {
// @ts-ignore
++this._val;
return this;
}

@operator.prefix("--")
preDec(): this {
// @ts-ignore
--this._val;
return this;
}

@operator.postfix("++")
postInc(): this {
return this.clone().preInc();
}

@operator.postfix("--")
postDec(): this {
return this.clone().preDec();
}
}

/**
Expand Down
41 changes: 6 additions & 35 deletions assembly/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,13 @@
import { JSON } from ".";

@json
class Vec3 {
x: f32 = 0.0;
y: f32 = 0.0;
z: f32 = 0.0;
}

@json
class Player {
@alias("first name")
firstName!: string;
lastName!: string;
lastActive!: i32[];
// Drop in a code block, function, or expression that evaluates to a boolean
@omitif("this.age < 18")
age!: i32;
class ContentBlock {
@omitnull()
pos!: Vec3 | null;
isVerified!: boolean;
input: JSON.Raw | null = null;
}

const player: Player = {
firstName: "Emmet",
lastName: "West",
lastActive: [8, 27, 2022],
age: 23,
pos: {
x: 3.4,
y: 1.2,
z: 8.3
},
isVerified: true
};

const stringified = JSON.stringify<Player>(player);

const parsed = JSON.parse<Player>(stringified);
const foo: ContentBlock = {
input: "123"
}

console.log(stringified);
console.log(JSON.stringify(parsed))
console.log(JSON.stringify(foo))
34 changes: 22 additions & 12 deletions transform/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,12 @@ class JSONTransform extends BaseVisitor {
mem.type = type;
mem.value = value;
mem.node = member;
if (type == "JSON.Raw") {
if (type.includes("JSON.Raw")) {
mem.flags.set(PropertyFlags.JSON_Raw, []);
}
if (member.type.isNullable) {
mem.flags.set(PropertyFlags.Null, []);
}
if (member.decorators) {
for (const decorator of member.decorators) {
const decoratorName = decorator.name.text;
Expand Down Expand Up @@ -214,8 +217,8 @@ class JSONTransform extends BaseVisitor {
"`;\n store<u32>(changetype<usize>(out) + ((out.length - 2) << 1), 8192010);\n return out;\n}";
}
else {
SERIALIZE_RAW += "`;\n};";
SERIALIZE_PRETTY += "`;\n};";
SERIALIZE_RAW += "}`;\n return out;\n}";
SERIALIZE_PRETTY += "}`;\n return out;\n}";
}
INITIALIZE += " return this;\n}";
const sortedMembers = [];
Expand Down Expand Up @@ -307,12 +310,12 @@ class JSONTransform extends BaseVisitor {
else {
if (f) {
f = false;
DESERIALIZE += ` if (0 == memory.compare(changetype<usize>("${escapeQuote(escapeSlash(name))}"), changetype<usize>(data) + (key_start << 1), ${name.length << 1})) {\n ${member.deserialize}\n return true;\n }\n`;
DESERIALIZE += ` if (0 === memory.compare(changetype<usize>("${escapeQuote(escapeSlash(name))}"), changetype<usize>(data) + (key_start << 1), ${name.length << 1})) {\n ${member.deserialize}\n return true;\n }\n`;
}
else {
DESERIALIZE =
DESERIALIZE.slice(0, DESERIALIZE.length - 1) +
` else if (0 == memory.compare(changetype<usize>("${escapeQuote(escapeSlash(name))}"), changetype<usize>(data) + (key_start << 1), ${name.length << 1})) {\n ${member.deserialize}\n return true;\n }\n`;
` else if (0 === memory.compare(changetype<usize>("${escapeQuote(escapeSlash(name))}"), changetype<usize>(data) + (key_start << 1), ${name.length << 1})) {\n ${member.deserialize}\n return true;\n }\n`;
}
}
}
Expand Down Expand Up @@ -400,11 +403,12 @@ export default class Transformer extends Transform {
}
var PropertyFlags;
(function (PropertyFlags) {
PropertyFlags[PropertyFlags["Omit"] = 0] = "Omit";
PropertyFlags[PropertyFlags["OmitNull"] = 1] = "OmitNull";
PropertyFlags[PropertyFlags["OmitIf"] = 2] = "OmitIf";
PropertyFlags[PropertyFlags["Alias"] = 3] = "Alias";
PropertyFlags[PropertyFlags["JSON_Raw"] = 4] = "JSON_Raw";
PropertyFlags[PropertyFlags["Null"] = 0] = "Null";
PropertyFlags[PropertyFlags["Omit"] = 1] = "Omit";
PropertyFlags[PropertyFlags["OmitNull"] = 2] = "OmitNull";
PropertyFlags[PropertyFlags["OmitIf"] = 3] = "OmitIf";
PropertyFlags[PropertyFlags["Alias"] = 4] = "Alias";
PropertyFlags[PropertyFlags["JSON_Raw"] = 5] = "JSON_Raw";
})(PropertyFlags || (PropertyFlags = {}));
class Property {
constructor() {
Expand All @@ -426,8 +430,14 @@ class Property {
if (this.flags.has(PropertyFlags.Omit))
return;
if (this.flags.has(PropertyFlags.JSON_Raw)) {
this.right_s = "this." + name;
this.right_d = "data.substring(value_start, value_end);";
if (this.flags.has(PropertyFlags.Null)) {
this.right_s = "(this." + name + " || \"null\")";
this.right_d = "value_start === value_end - 4 && 30399761348886638 === load<u64>(changetype<usize>(data) + (value_start << 1)) ? null : data.substring(value_start, value_end)";
}
else {
this.right_s = "this." + name;
this.right_d = "data.substring(value_start, value_end);";
}
}
else {
this.right_s = "__SERIALIZE<" + type + ">(this." + name + ")";
Expand Down
Loading

0 comments on commit 009a4ad

Please sign in to comment.