json-as 1.0.6 → 1.0.8

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
@@ -2,152 +2,224 @@ import { JSON } from ".";
2
2
  import { bytes } from "./util";
3
3
 
4
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
- }
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
+ // }
11
19
 
12
20
 
13
- @json
14
- class Vec3 {
15
- x: f32 = 0.0;
16
- y: f32 = 0.0;
17
- z: f32 = 0.0;
18
- }
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
+
19
31
 
32
+ // @omitnull()
33
+ // pos!: Vec3 | null;
34
+ // isVerified!: boolean;
35
+ // }
20
36
 
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 {}
37
40
 
38
- @json
39
- class Point {
40
- x: f64 = 0.0;
41
- y: f64 = 0.0;
42
- constructor(x: f64, y: f64) {
43
- this.x = x;
44
- this.y = y;
45
- }
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
+ // }
46
49
 
47
50
 
48
- @serializer
49
- serializer(self: Point): string {
50
- return `(${self.x},${self.y})`;
51
- }
51
+ // @serializer
52
+ // serializer(self: NewPoint): string {
53
+ // return `x=${self.x},y=${self.y}`;
54
+ // }
52
55
 
53
56
 
54
- @deserializer
55
- deserializer(data: string): Point | null {
56
- const dataSize = bytes(data);
57
- if (dataSize <= 2) return null;
57
+ // @deserializer
58
+ // deserializer(data: string): NewPoint {
59
+ // const dataSize = bytes(data);
58
60
 
59
- const c = data.indexOf(",");
60
- const x = data.slice(1, c);
61
- const y = data.slice(c + 1, data.length - 1);
61
+ // const c = data.indexOf(",");
62
+ // const x = data.slice(2, c);
63
+ // const y = data.slice(c + 3);
62
64
 
63
- return new Point(f64.parse(x), f64.parse(y));
64
- }
65
- }
65
+ // return new NewPoint(f64.parse(x), f64.parse(y));
66
+ // }
67
+ // }
66
68
 
69
+ // @json
70
+ // class InnerObj<T> {
71
+ // obj: T = instantiate<T>();
72
+ // }
67
73
 
68
- @json
69
- class InnerObj<T> {
70
- obj: T = instantiate<T>();
71
- }
72
74
 
75
+ // @json
76
+ // class ObjWithBracketString {
77
+ // data: string = "";
78
+ // }
73
79
 
74
- @json
75
- class ObjWithBracketString {
76
- data: string = "";
77
- }
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();
78
107
 
79
- const player: Player = {
80
- firstName: "Jairus",
81
- lastName: "Tanaka",
82
- lastActive: [2, 7, 2025],
83
- age: 18,
84
- pos: {
85
- x: 3.4,
86
- y: 1.2,
87
- z: 8.3,
88
- },
89
- isVerified: true,
90
- };
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);
91
113
 
92
- 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");
93
- // console.log("Bytes " + bytes(a1).toString());
94
- console.log("a1: " + a1);
114
+ // console.log("a4: " + JSON.stringify(a4));
95
115
 
96
- const obj = new Obj();
97
- const a2 = JSON.stringify(obj);
98
- // console.log("Bytes " + bytes(a2).toString());
99
- console.log("a2: " + a2);
116
+ // const a5 = JSON.parse<JSON.Obj>('{"foo":"bar"}');
100
117
 
101
- const a3 = JSON.stringify(player);
102
- // console.log("Bytes " + bytes(a3).toString());
103
- console.log("a3: " + a3);
118
+ // console.log("a5: " + JSON.stringify(a5));
104
119
 
105
- const a4 = new JSON.Obj();
120
+ // const a6 = JSON.parse<JSON.Obj>('{"x":1.5,"y":5.4,"z":9.8,"obj":{"foo":"bar"}}');
106
121
 
107
- a4.set("x", 1.5);
108
- a4.set("y", 5.4);
109
- a4.set("z", 9.8);
110
- a4.set("obj", obj);
111
- a4.set<boolean>("bool", false);
122
+ // console.log("a6: " + JSON.stringify(a6));
112
123
 
113
- console.log("a4: " + JSON.stringify(a4));
124
+ // const a7 = JSON.parse<JSON.Value[]>('["string",true,3.14,{"x":1.0,"y":2.0,"z":3.0},[1,2,3,true]]');
114
125
 
115
- const a5 = JSON.parse<JSON.Obj>('{"foo":"bar"}');
126
+ // console.log("a7: " + JSON.stringify(a7));
116
127
 
117
- console.log("a5: " + JSON.stringify(a5));
128
+ // const a8 = JSON.stringify(["hello", JSON.stringify("world"), "working?"]);
118
129
 
119
- const a6 = JSON.parse<JSON.Obj>('{"x":1.5,"y":5.4,"z":9.8,"obj":{"foo":"bar"}}');
130
+ // console.log("a8: " + a8);
120
131
 
121
- console.log("a6: " + JSON.stringify(a6));
132
+ // const a9 = JSON.stringify<JSON.Raw>(JSON.Raw.from('"hello world"'));
122
133
 
123
- const a7 = JSON.parse<JSON.Value[]>('["string",true,3.14,{"x":1.0,"y":2.0,"z":3.0},[1,2,3,true]]');
134
+ // console.log("a9: " + a9);
124
135
 
125
- console.log("a7: " + JSON.stringify(a7));
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));
126
141
 
127
- const a8 = JSON.stringify(["hello", JSON.stringify("world"), "working?"]);
142
+ // const a13 = JSON.stringify<JSON.Obj>(new JSON.Obj());
128
143
 
129
- console.log("a8: " + a8);
144
+ // console.log("a13: " + a13);
130
145
 
131
- const a9 = JSON.stringify<JSON.Raw>(JSON.Raw.from('"hello world"'));
146
+ // const a14 = JSON.stringify(new Point());
147
+ // console.log("a14: " + a14);
132
148
 
133
- console.log("a9: " + a9);
149
+ // const a15 = JSON.parse<Point>(a14);
150
+ // console.log("a15: " + JSON.stringify(a15));
134
151
 
135
- const m10 = new Map<string, JSON.Raw>();
136
- m10.set("hello", new JSON.Raw('"world"'));
137
- m10.set("pos", new JSON.Raw('{"x":1.0,"y":2.0,"z":3.0}'));
152
+ // const a16 = JSON.stringify(new NewPoint(1.0, 2.0));
153
+ // console.log("a16: " + a16);
138
154
 
139
- const a10 = JSON.stringify(m10);
155
+ // const a17 = JSON.parse<NewPoint>(a16);
156
+ // console.log("a17: " + JSON.stringify(a17));
140
157
 
141
- console.log("a10: " + a10);
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));
142
160
 
143
- const a11 = JSON.parse<JSON.Obj>(' { "x" : 3.4 , "y" : 1.2 , "z" : 8.3 } ');
161
+ // const a19 = JSON.stringify<JSON.Obj[]>(a18);
162
+ // console.log("a19: " + a19);
144
163
 
145
- console.log("a11: " + JSON.stringify(a11));
164
+ // const a20 = JSON.parse<JSON.Box<f64>[]>("[1.3,4.7,9.5]");
165
+ // console.log("a20: " + JSON.stringify(a20));
146
166
 
147
- const a12 = JSON.parse<InnerObj<ObjWithBracketString>>('{"obj":{"data":"hello} world"}}');
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
+ @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
+ }
148
183
 
149
- console.log("a12: " + JSON.stringify(a12));
150
184
 
151
- const a13 = JSON.stringify<JSON.Obj>(new JSON.Obj());
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;
193
+ }
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
+
207
+ this._data = data;
208
+ }
209
+
210
+ private _data: string;
211
+
212
+
213
+ @serializer
214
+ serialize(self: RawMessage): string {
215
+ return self._data;
216
+ }
217
+
218
+
219
+ @deserializer
220
+ deserialize(data: string): RawMessage {
221
+ return new RawMessage(data);
222
+ }
223
+ }
152
224
 
153
- console.log("a13: " + a13);
225
+ console.log("RawMessage: " + JSON.stringify(new RawMessage('{"role":"user","content":"Hello, how are you?"}')));
@@ -1,4 +1,4 @@
1
- import { bench } from "./lib/bench";
1
+ import { bench } from "./lib/bench.js";
2
2
 
3
3
  const v1 = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
4
4
  const v2 = '"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"';
@@ -1,4 +1,4 @@
1
- import { bench } from "./lib/bench";
1
+ import { bench } from "./lib/bench.js";
2
2
 
3
3
  class Vec3 {
4
4
  public x!: number;
@@ -110,7 +110,7 @@ const v1: LargeJSON = {
110
110
  const v2 = `{"id":2,"name":"Medium Object","age":18,"email":"me@jairus.dev","street":"I don't want to say my street","city":"I don't want to say this either","state":"It really depends","zip":"I forget what it is","tags":["me","dogs","mountains","bar","foo"],"theme":"Hyper Term Black","notifications":true,"language":"en-US","movement":[{"x":1,"y":2,"z":3},{"x":1,"y":2,"z":3},{"x":1,"y":2,"z":3},{"x":1,"y":2,"z":3},{"x":1,"y":2,"z":3},{"x":1,"y":2,"z":3},{"x":1,"y":2,"z":3},{"x":1,"y":2,"z":3},{"x":1,"y":2,"z":3},{"x":1,"y":2,"z":3},{"x":1,"y":2,"z":3},{"x":1,"y":2,"z":3},{"x":1,"y":2,"z":3},{"x":1,"y":2,"z":3},{"x":1,"y":2,"z":3},{"x":1,"y":2,"z":3},{"x":1,"y":2,"z":3},{"x":1,"y":2,"z":3},{"x":1,"y":2,"z":3},{"x":1,"y":2,"z":3},{"x":1,"y":2,"z":3},{"x":1,"y":2,"z":3},{"x":1,"y":2,"z":3},{"x":1,"y":2,"z":3},{"x":1,"y":2,"z":3},{"x":1,"y":2,"z":3},{"x":1,"y":2,"z":3},{"x":1,"y":2,"z":3},{"x":1,"y":2,"z":3},{"x":1,"y":2,"z":3},{"x":1,"y":2,"z":3},{"x":1,"y":2,"z":3},{"x":1,"y":2,"z":3},{"x":1,"y":2,"z":3},{"x":1,"y":2,"z":3},{"x":1,"y":2,"z":3},{"x":1,"y":2,"z":3},{"x":1,"y":2,"z":3},{"x":1,"y":2,"z":3},{"x":1,"y":2,"z":3},{"x":1,"y":2,"z":3},{"x":1,"y":2,"z":3},{"x":1,"y":2,"z":3},{"x":1,"y":2,"z":3},{"x":1,"y":2,"z":3},{"x":1,"y":2,"z":3},{"x":1,"y":2,"z":3},{"x":1,"y":2,"z":3},{"x":1,"y":2,"z":3},{"x":1,"y":2,"z":3},{"x":1,"y":2,"z":3},{"x":1,"y":2,"z":3},{"x":1,"y":2,"z":3},{"x":1,"y":2,"z":3},{"x":1,"y":2,"z":3},{"x":1,"y":2,"z":3},{"x":1,"y":2,"z":3},{"x":1,"y":2,"z":3},{"x":1,"y":2,"z":3},{"x":1,"y":2,"z":3},{"x":1,"y":2,"z":3},{"x":1,"y":2,"z":3},{"x":1,"y":2,"z":3},{"x":1,"y":2,"z":3},{"x":1,"y":2,"z":3},{"x":1,"y":2,"z":3},{"x":1,"y":2,"z":3},{"x":1,"y":2,"z":3}]}`;
111
111
 
112
112
  bench(
113
- "Serialize Medium Object",
113
+ "Serialize Large Object",
114
114
  () => {
115
115
  JSON.stringify(v1);
116
116
  },
@@ -118,7 +118,7 @@ bench(
118
118
  );
119
119
 
120
120
  bench(
121
- "Deserialize Medium Object",
121
+ "Deserialize Large Object",
122
122
  () => {
123
123
  JSON.parse(v2);
124
124
  },
@@ -1,5 +1,17 @@
1
+ if (typeof console === 'undefined') {
2
+ console = {
3
+ log: print,
4
+ error: print,
5
+ warn: print,
6
+ };
7
+ }
8
+
1
9
  export function bench(description: string, routine: () => void, ops: number = 1_000_000): void {
2
10
  console.log(" - Benchmarking " + description);
11
+ let warmup = ops/10;
12
+ while (--warmup) {
13
+ routine();
14
+ }
3
15
  const start = Date.now();
4
16
  let count = ops;
5
17
  while (count !== 0) {
@@ -12,4 +24,4 @@ export function bench(description: string, routine: () => void, ops: number = 1_
12
24
  const format = new Intl.NumberFormat("en-US");
13
25
 
14
26
  console.log(` Completed benchmark in ${format.format(elapsed)}ms at ${format.format(opsPerSecond)} ops/s\n`);
15
- }
27
+ }
@@ -1,4 +1,4 @@
1
- import { bench } from "./lib/bench";
1
+ import { bench } from "./lib/bench.js";
2
2
 
3
3
  class MediumJSON {
4
4
  public id!: number;
@@ -0,0 +1,30 @@
1
+ const bytes = readbuffer("./build/" + arguments[0]);
2
+ const module = new WebAssembly.Module(bytes);
3
+ let memory = null;
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
+ }
14
+ });
15
+
16
+ memory = exports.memory;
17
+
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));
28
+ }
29
+
30
+ exports.start();
@@ -1,4 +1,4 @@
1
- import { bench } from "./lib/bench";
1
+ import { bench } from "./lib/bench.js";
2
2
 
3
3
  class SmallJSON {
4
4
  public id!: number;
@@ -0,0 +1,12 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "ES2022",
4
+ "module": "ES2020",
5
+ "moduleResolution": "Node",
6
+ "removeComments": true,
7
+ "types": ["node"],
8
+ "outDir": "../build",
9
+ "sourceMap": false
10
+ },
11
+ "include": ["./*.ts", "./lib/*.ts"]
12
+ }
@@ -1,4 +1,4 @@
1
- import { bench } from "./lib/bench";
1
+ import { bench } from "./lib/bench.js";
2
2
 
3
3
  class Vec3 {
4
4
  public x!: number;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "json-as",
3
- "version": "1.0.6",
3
+ "version": "1.0.8",
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",
@@ -18,9 +18,8 @@
18
18
  "test": "bash ./run-tests.sh",
19
19
  "bench:as": "bash ./run-bench.as.sh",
20
20
  "bench:js": "bash ./run-bench.js.sh",
21
- "build:time": "time npx asc ./assembly/__benches__/abc.bench.ts --transform ./transform -o ./build/abc.bench.wasm --optimizeLevel 3 --shrinkLevel 0 --converge --noAssert --uncheckedBehavior always --runtime stub --enable simd --enable bulk-memory",
22
- "build:test": "rm -rf ./build/ && JSON_DEBUG=true asc assembly/test.ts --transform ./transform -o ./build/test.wasm --textFile ./build/test.wat --optimizeLevel 3 --shrinkLevel 0",
23
- "build:test:simd": "rm -rf ./build/ && JSON_DEBUG=true asc assembly/test.ts --transform ./transform -o ./build/test.wasm --textFile ./build/test.wat --optimizeLevel 3 --shrinkLevel 0 --enable simd",
21
+ "build:test": "rm -rf ./build/ && JSON_DEBUG=true asc assembly/test.ts --transform ./transform -o ./build/test.wasm --textFile ./build/test.wat --debug --config ./node_modules/@assemblyscript/wasi-shim/asconfig.json",
22
+ "build:test:simd": "rm -rf ./build/ && JSON_DEBUG=true asc assembly/test.ts --transform ./transform -o ./build/test.wasm --textFile ./build/test.wat --optimizeLevel 3 --shrinkLevel 0 --enable simd --config ./node_modules/@assemblyscript/wasi-shim/asconfig.json",
24
23
  "test:wasmtime": "wasmtime ./build/test.wasm",
25
24
  "test:wasmer": "wasmer ./build/test.wasm",
26
25
  "build:transform": "tsc -p ./transform",
package/run-bench.as.sh CHANGED
@@ -1,27 +1,53 @@
1
1
  #!/bin/bash
2
+ RUNTIMES=${RUNTIMES:-"minimal stub"}
3
+ ENGINES=${ENGINES:-"liftoff ignition sparkplug turbofan llvm"}
4
+ for file in ./assembly/__benches__/vec3.bench.ts; do
5
+ filename=$(basename -- "$file")
6
+ output_wasi=
7
+ for runtime in $RUNTIMES; do
8
+ output="./build/${filename%.ts}.${runtime}.wasm"
2
9
 
3
- mkdir -p ./build
10
+ npx asc "$file" --transform ./transform -o "${output}.1" -O3 --converge --noAssert --uncheckedBehavior always --runtime $runtime --enable simd --enable bulk-memory --exportStart start || {
11
+ echo "Build failed"
12
+ exit 1
13
+ }
4
14
 
5
- for file in ./assembly/__benches__/*.bench.ts; do
6
- filename=$(basename -- "$file")
7
- output="./build/${filename%.ts}.wasm"
15
+ wasm-opt -all -O4 "${output}.1" -o "$output"
16
+ rm "${output}.1"
8
17
 
9
- start_time=$(date +%s%3N)
10
- npx asc "$file" --transform ./transform -o "$output" --optimizeLevel 3 --shrinkLevel 0 --converge --noAssert --uncheckedBehavior always --runtime stub --enable simd --enable bulk-memory || { echo "Build failed"; exit 1; }
11
- end_time=$(date +%s%3N)
18
+ npx asc "$file" --transform ./transform -o "${output}.2" -O3 --converge --noAssert --uncheckedBehavior always --runtime $runtime --enable simd --enable bulk-memory --config ./node_modules/@assemblyscript/wasi-shim/asconfig.json || {
19
+ echo "Build failed"
20
+ exit 1
21
+ }
12
22
 
13
- build_time=$((end_time - start_time))
23
+ wasm-opt -all -O4 "${output}.2" -o "${output%.wasm}.wasi.wasm"
24
+ rm "${output}.2"
14
25
 
15
- if [ "$build_time" -ge 60000 ]; then
16
- formatted_time="$(bc <<< "scale=2; $build_time/60000")m"
17
- elif [ "$build_time" -ge 1000 ]; then
18
- formatted_time="$(bc <<< "scale=2; $build_time/1000")s"
19
- else
20
- formatted_time="${build_time}ms"
21
- fi
26
+ for engine in $ENGINES; do
27
+ echo -e "$filename (asc/$runtime/$engine)\n"
22
28
 
23
- echo -e "$filename (built in $formatted_time)\n"
24
- wasmer "$output" --llvm || { echo "Benchmarked failed."; exit 1; }
29
+ arg="${filename%.ts}.${runtime}.wasm"
30
+ if [[ "$engine" == "ignition" ]]; then
31
+ v8 --no-opt --module ./bench/runners/assemblyscript.js -- $arg
32
+ fi
33
+
34
+ if [[ "$engine" == "liftoff" ]]; then
35
+ v8 --liftoff-only --no-opt --module ./bench/runners/assemblyscript.js -- $arg
36
+ fi
37
+
38
+ if [[ "$engine" == "sparkplug" ]]; then
39
+ v8 --sparkplug --always-sparkplug --no-opt --module ./bench/runners/assemblyscript.js -- $arg
40
+ fi
41
+
42
+ if [[ "$engine" == "turbofan" ]]; then
43
+ v8 --no-liftoff --no-wasm-tier-up --module ./bench/runners/assemblyscript.js -- $arg
44
+ fi
45
+
46
+ if [[ "$engine" == "llvm" ]]; then
47
+ wasmer run "${output%.wasm}.wasi.wasm" --llvm --enable-simd --enable-bulk-memory --enable-relaxed-simd --enable-pass-params-opt
48
+ fi
49
+ done
50
+ done
25
51
  done
26
52
 
27
- echo "Finished benchmarks."
53
+ echo "Finished benchmarks"
package/run-bench.js.sh CHANGED
@@ -1,12 +1,38 @@
1
1
  #!/bin/bash
2
+ RUNTIMES=${RUNTIMES:-"v8-liftoff v8-ignition v8-sparkplug v8-turbofan jsc-default"}
3
+ npx tsc -p ./bench > /dev/null 2>&1
4
+ for file in ./bench/vec3.bench.ts; do
5
+ filename=$(basename -- "$file")
6
+ file_js="${filename%.ts}.js"
2
7
 
3
- mkdir -p ./build
8
+ output="./build/${filename%.ts}.wasm"
4
9
 
5
- for file in ./bench/*.bench.ts; do
6
- filename=$(basename -- "$file")
10
+ for rt in $RUNTIMES; do
11
+ runtime=$(echo $rt | cut -d'-' -f1)
12
+ engine=$(echo $rt | cut -d'-' -f2-)
13
+ echo -e "$filename (js/$runtime/$engine)\n"
14
+
15
+ arg="${filename%.ts}.${runtime}.wasm"
16
+ if [[ "$engine" == "ignition" ]]; then
17
+ v8 --no-opt --module ./build/$file_js
18
+ fi
19
+
20
+ if [[ "$engine" == "liftoff" ]]; then
21
+ v8 --liftoff-only --no-opt --module ./build/$file_js
22
+ fi
23
+
24
+ if [[ "$engine" == "sparkplug" ]]; then
25
+ v8 --sparkplug --always-sparkplug --no-opt --module ./build/$file_js
26
+ fi
27
+
28
+ if [[ "$engine" == "turbofan" ]]; then
29
+ v8 --no-liftoff --no-wasm-tier-up --module ./build/$file_js
30
+ fi
7
31
 
8
- echo -e "$filename\n"
9
- npx tsx "$file" || { echo "Benchmark failed."; exit 1; }
32
+ if [[ "$engine" == "default" ]]; then
33
+ jsc -m ./build/$file_js
34
+ fi
35
+ done
10
36
  done
11
37
 
12
- echo "Finished benchmarks."
38
+ echo "Finished benchmarks"
package/run-tests.sh CHANGED
@@ -7,7 +7,7 @@ for file in ./assembly/__tests__/*.spec.ts; do
7
7
  output="./build/${filename%.ts}.wasm"
8
8
 
9
9
  start_time=$(date +%s%3N)
10
- npx asc "$file" --transform ./transform -o "$output" --enable simd || { echo "Tests failed"; exit 1; }
10
+ npx asc "$file" --transform ./transform -o "$output" --enable simd --config ./node_modules/@assemblyscript/wasi-shim/asconfig.json --disableWarning 226 || { echo "Tests failed"; exit 1; }
11
11
  end_time=$(date +%s%3N)
12
12
 
13
13
  build_time=$((end_time - start_time))
@@ -316,9 +316,7 @@ class JSONTransform extends Visitor {
316
316
  for (let i = 0; i < memberGroup.length; i++) {
317
317
  const member = memberGroup[i];
318
318
  const memberName = member.alias || member.name;
319
- const dst = this.schemas.find(v => v.name == member.type)
320
- ? "load<usize>(ptr + offsetof<this>(\"" + member.name + "\"))"
321
- : "0";
319
+ const dst = this.schemas.find((v) => v.name == member.type) ? 'load<usize>(ptr + offsetof<this>("' + member.name + '"))' : "0";
322
320
  if (memberLen == 2) {
323
321
  DESERIALIZE += `${indent} case ${memberName.charCodeAt(0)}: { // ${memberName}\n`;
324
322
  DESERIALIZE += `${indent} store<${member.type}>(ptr, JSON.__deserialize<${member.type}>(valStart, valEnd, ${dst}), offsetof<this>(${JSON.stringify(member.name)}));\n`;
@@ -391,7 +389,7 @@ class JSONTransform extends Visitor {
391
389
  generateEmptyMethods(node) {
392
390
  let SERIALIZE_EMPTY = "@inline __SERIALIZE(ptr: usize): void {\n bs.proposeSize(4);\n store<u32>(bs.offset, 8192123);\n bs.offset += 4;\n}";
393
391
  let INITIALIZE_EMPTY = "@inline __INITIALIZE(): this {\n return this;\n}";
394
- let DESERIALIZE_EMPTY = "@inline __DESERIALIZE(keyStart: usize, keyEnd: usize, valStart: usize, valEnd: usize, ptr: usize): void {\n return false;\n}";
392
+ let DESERIALIZE_EMPTY = "@inline __DESERIALIZE(keyStart: usize, keyEnd: usize, valStart: usize, valEnd: usize, ptr: usize): void {}";
395
393
  if (process.env["JSON_DEBUG"]) {
396
394
  console.log(SERIALIZE_EMPTY);
397
395
  console.log(INITIALIZE_EMPTY);