json-as 1.0.8 → 1.0.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/assembly/test.ts CHANGED
@@ -1,225 +1,66 @@
1
1
  import { JSON } from ".";
2
2
  import { bytes } from "./util";
3
-
4
-
5
- // @json
6
- // class Obj {
7
- // public a: string = "hello";
8
- // public b: string = "world";
9
- // public c: string = '"\t\f\u0000\u0001';
10
- // }
11
-
12
-
13
- // @json
14
- // class Vec3 {
15
- // x: f32 = 0.0;
16
- // y: f32 = 0.0;
17
- // z: f32 = 0.0;
18
- // }
19
-
20
-
21
- // @json
22
- // class Player {
23
- // @alias("first name")
24
- // firstName!: string;
25
- // lastName!: string;
26
- // lastActive!: i32[];
27
- // // Drop in a code block, function, or expression that evaluates to a boolean
28
- // @omitif((self: Player) => self.age < 18)
29
- // age!: i32;
30
-
31
-
32
- // @omitnull()
33
- // pos!: Vec3 | null;
34
- // isVerified!: boolean;
35
- // }
36
-
37
-
38
- // @json
39
- // class Point {}
40
-
41
- // @json
42
- // class NewPoint {
43
- // x: f64 = 0.0;
44
- // y: f64 = 0.0;
45
- // constructor(x: f64, y: f64) {
46
- // this.x = x;
47
- // this.y = y;
48
- // }
49
-
50
-
51
- // @serializer
52
- // serializer(self: NewPoint): string {
53
- // return `x=${self.x},y=${self.y}`;
54
- // }
55
-
56
-
57
- // @deserializer
58
- // deserializer(data: string): NewPoint {
59
- // const dataSize = bytes(data);
60
-
61
- // const c = data.indexOf(",");
62
- // const x = data.slice(2, c);
63
- // const y = data.slice(c + 3);
64
-
65
- // return new NewPoint(f64.parse(x), f64.parse(y));
66
- // }
67
- // }
68
-
69
- // @json
70
- // class InnerObj<T> {
71
- // obj: T = instantiate<T>();
72
- // }
73
-
74
-
75
- // @json
76
- // class ObjWithBracketString {
77
- // data: string = "";
78
- // }
79
-
80
- // const player: Player = {
81
- // firstName: "Jairus",
82
- // lastName: "Tanaka",
83
- // lastActive: [2, 7, 2025],
84
- // age: 18,
85
- // pos: {
86
- // x: 3.4,
87
- // y: 1.2,
88
- // z: 8.3,
89
- // },
90
- // isVerified: true,
91
- // };
92
-
93
- // const a1 = JSON.stringify("\u0000\u0001\u0002\u0003\u0004\u0005\u0006\u0007\u0008\u0009\u000a\u000b\u000c\u000d\u000e\u000f\u000f\u0011\u0012\u0013\u0014\u0015\u0016\u0017\u0018\u0019\u001a\u001b\u001c\u001d\u001e\u001f");
94
- // // console.log("Bytes " + bytes(a1).toString());
95
- // console.log("a1: " + a1);
96
-
97
- // const obj = new Obj();
98
- // const a2 = JSON.stringify(obj);
99
- // // console.log("Bytes " + bytes(a2).toString());
100
- // console.log("a2: " + a2);
101
-
102
- // const a3 = JSON.stringify(player);
103
- // // console.log("Bytes " + bytes(a3).toString());
104
- // console.log("a3: " + a3);
105
-
106
- // const a4 = new JSON.Obj();
107
-
108
- // a4.set("x", 1.5);
109
- // a4.set("y", 5.4);
110
- // a4.set("z", 9.8);
111
- // a4.set("obj", obj);
112
- // a4.set<boolean>("bool", false);
113
-
114
- // console.log("a4: " + JSON.stringify(a4));
115
-
116
- // const a5 = JSON.parse<JSON.Obj>('{"foo":"bar"}');
117
-
118
- // console.log("a5: " + JSON.stringify(a5));
119
-
120
- // const a6 = JSON.parse<JSON.Obj>('{"x":1.5,"y":5.4,"z":9.8,"obj":{"foo":"bar"}}');
121
-
122
- // console.log("a6: " + JSON.stringify(a6));
123
-
124
- // const a7 = JSON.parse<JSON.Value[]>('["string",true,3.14,{"x":1.0,"y":2.0,"z":3.0},[1,2,3,true]]');
125
-
126
- // console.log("a7: " + JSON.stringify(a7));
127
-
128
- // const a8 = JSON.stringify(["hello", JSON.stringify("world"), "working?"]);
129
-
130
- // console.log("a8: " + a8);
131
-
132
- // const a9 = JSON.stringify<JSON.Raw>(JSON.Raw.from('"hello world"'));
133
-
134
- // console.log("a9: " + a9);
135
-
136
- // const m10 = new Map<string, JSON.Raw>();
137
- // m10.set("hello", new JSON.Raw('"world"'));
138
- // m10.set("pos", new JSON.Raw('{"x":1.0,"y":2.0,"z":3.0}'));
139
- // ole
140
- // console.log("a12: " + JSON.stringify(a12));
141
-
142
- // const a13 = JSON.stringify<JSON.Obj>(new JSON.Obj());
143
-
144
- // console.log("a13: " + a13);
145
-
146
- // const a14 = JSON.stringify(new Point());
147
- // console.log("a14: " + a14);
148
-
149
- // const a15 = JSON.parse<Point>(a14);
150
- // console.log("a15: " + JSON.stringify(a15));
151
-
152
- // const a16 = JSON.stringify(new NewPoint(1.0, 2.0));
153
- // console.log("a16: " + a16);
154
-
155
- // const a17 = JSON.parse<NewPoint>(a16);
156
- // console.log("a17: " + JSON.stringify(a17));
157
-
158
- // const a18 = JSON.parse<JSON.Obj[]>('[{"x":1.0,"y":2.0,"z":3.0},{"x":4.0,"y":5.0,"z":6.0},{"x":7.0,"y":8.0,"z":9.0}]');
159
- // console.log("a18: " + JSON.stringify(a18));
160
-
161
- // const a19 = JSON.stringify<JSON.Obj[]>(a18);
162
- // console.log("a19: " + a19);
163
-
164
- // const a20 = JSON.parse<JSON.Box<f64>[]>("[1.3,4.7,9.5]");
165
- // console.log("a20: " + JSON.stringify(a20));
166
-
167
- // const a21 = JSON.stringify<JSON.Box<f64>[]>(a20);
168
- // console.log("a21: " + a21);
169
-
170
- /**
171
- * A request message object that can be sent to the chat model.
172
- */
173
3
  @json
174
- export abstract class RequestMessage {
175
- /**
176
- * Creates a new request message object.
177
- *
178
- * @param role The role of the author of this message.
179
- */
180
- constructor(role: string) {
181
- this._role = role;
182
- }
183
-
4
+ class GenericEnum<T> {
5
+ private tag: string = ""
6
+ private value: T | null = null
184
7
 
185
- @alias("role")
186
- protected _role: string;
187
-
188
- /**
189
- * The role of the author of this message.
190
- */
191
- get role(): string {
192
- return this._role;
8
+ constructor() {
9
+ this.tag = ""
10
+ this.value = null
193
11
  }
194
- }
195
-
196
- @json
197
- class RawMessage extends RequestMessage {
198
- constructor(data: string) {
199
- const obj = JSON.parse<JSON.Obj>(data);
200
- if (!obj.has("role")) {
201
- throw new Error("Missing role field in message JSON.");
202
- }
203
-
204
- const role = obj.get("role")!.get<string>();
205
- super(role);
206
12
 
207
- this._data = data;
13
+ static create<T>(tag: string, value: T): GenericEnum<T> {
14
+ const item = new GenericEnum<T>()
15
+ item.tag = tag
16
+ item.value = value
17
+ return item
208
18
  }
209
19
 
210
- private _data: string;
20
+ getTag(): string {
21
+ return this.tag
22
+ }
211
23
 
24
+ getValue(): T | null {
25
+ return this.value
26
+ }
212
27
 
213
28
  @serializer
214
- serialize(self: RawMessage): string {
215
- return self._data;
29
+ serialize(self: GenericEnum<T>): string {
30
+ const tagJson = JSON.stringify(self.tag)
31
+ const valueJson = JSON.stringify(self.value)
32
+ return `{"tag":${tagJson},"value":${valueJson}}`
216
33
  }
217
34
 
218
-
219
35
  @deserializer
220
- deserialize(data: string): RawMessage {
221
- return new RawMessage(data);
36
+ deserialize(data: string): GenericEnum<T> {
37
+ const parsed = JSON.parse<Map<string, JSON.Raw>>(data);
38
+ const result = new GenericEnum<T>();
39
+
40
+ if (parsed.has("tag")) {
41
+ result.tag = JSON.parse<string>(parsed.get("tag").data);
42
+ }
43
+
44
+ if (parsed.has("value")) {
45
+ result.value = JSON.parse<T>(parsed.get("value").data);
46
+ }
47
+
48
+ return result;
222
49
  }
223
50
  }
224
51
 
225
- console.log("RawMessage: " + JSON.stringify(new RawMessage('{"role":"user","content":"Hello, how are you?"}')));
52
+ export function test(): void {
53
+ const myEnum = GenericEnum.create<string>("test_tag", "test_value");
54
+ // Serialize it
55
+ const serialized = JSON.stringify<GenericEnum<string>>(myEnum);
56
+ console.log("=== Serialized ===");
57
+ console.log(serialized);
58
+ console.log("=== Attempting to deserialize (this will crash) ===");
59
+
60
+ // This line crashes
61
+ const parsed = JSON.parse<GenericEnum<string>>(serialized);
62
+ console.log("=== Deserialized ===");
63
+ console.log(JSON.stringify(parsed))
64
+ }
65
+
66
+ test();
@@ -0,0 +1,6 @@
1
+ import { OBJECT, TOTAL_OVERHEAD } from "rt/common";
2
+
3
+ // @ts-ignore: decorator
4
+ @inline export function idofD<T>(value: T): usize {
5
+ return changetype<OBJECT>(changetype<usize>(value) - TOTAL_OVERHEAD).rtId;
6
+ }
@@ -1,4 +1,4 @@
1
- if (typeof console === 'undefined') {
1
+ if (typeof console === "undefined") {
2
2
  console = {
3
3
  log: print,
4
4
  error: print,
@@ -8,7 +8,7 @@ if (typeof console === 'undefined') {
8
8
 
9
9
  export function bench(description: string, routine: () => void, ops: number = 1_000_000): void {
10
10
  console.log(" - Benchmarking " + description);
11
- let warmup = ops/10;
11
+ let warmup = ops / 10;
12
12
  while (--warmup) {
13
13
  routine();
14
14
  }
@@ -24,4 +24,4 @@ export function bench(description: string, routine: () => void, ops: number = 1_
24
24
  const format = new Intl.NumberFormat("en-US");
25
25
 
26
26
  console.log(` Completed benchmark in ${format.format(elapsed)}ms at ${format.format(opsPerSecond)} ops/s\n`);
27
- }
27
+ }
@@ -2,29 +2,27 @@ const bytes = readbuffer("./build/" + arguments[0]);
2
2
  const module = new WebAssembly.Module(bytes);
3
3
  let memory = null;
4
4
  const { exports } = new WebAssembly.Instance(module, {
5
- env: {
6
- abort: (msg, file, line) => {
7
- console.log("abort: " + __liftString(msg) + " in " + __liftString(file) + ":" + __liftString(line))
8
- },
9
- "console.log": (ptr) => {
10
- console.log(__liftString(ptr));
11
- },
12
- "Date.now": () => Date.now()
13
- }
5
+ env: {
6
+ abort: (msg, file, line) => {
7
+ console.log("abort: " + __liftString(msg) + " in " + __liftString(file) + ":" + __liftString(line));
8
+ },
9
+ "console.log": (ptr) => {
10
+ console.log(__liftString(ptr));
11
+ },
12
+ "Date.now": () => Date.now(),
13
+ },
14
14
  });
15
15
 
16
16
  memory = exports.memory;
17
17
 
18
18
  function __liftString(pointer) {
19
- if (!pointer) return null;
20
- const
21
- end = pointer + new Uint32Array(memory.buffer)[pointer - 4 >>> 2] >>> 1,
22
- memoryU16 = new Uint16Array(memory.buffer);
23
- let
24
- start = pointer >>> 1,
25
- string = "";
26
- while (end - start > 1024) string += String.fromCharCode(...memoryU16.subarray(start, start += 1024));
27
- return string + String.fromCharCode(...memoryU16.subarray(start, end));
19
+ if (!pointer) return null;
20
+ const end = (pointer + new Uint32Array(memory.buffer)[(pointer - 4) >>> 2]) >>> 1,
21
+ memoryU16 = new Uint16Array(memory.buffer);
22
+ let start = pointer >>> 1,
23
+ string = "";
24
+ while (end - start > 1024) string += String.fromCharCode(...memoryU16.subarray(start, (start += 1024)));
25
+ return string + String.fromCharCode(...memoryU16.subarray(start, end));
28
26
  }
29
27
 
30
- exports.start();
28
+ exports.start();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "json-as",
3
- "version": "1.0.8",
3
+ "version": "1.0.9",
4
4
  "author": "Jairus Tanaka",
5
5
  "description": "The only JSON library you'll need for AssemblyScript. SIMD enabled",
6
6
  "types": "assembly/index.ts",