json-as 1.0.0-beta.9 → 1.0.1

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.
Files changed (60) hide show
  1. package/.github/workflows/{nodejs.yml → tests.yml} +1 -1
  2. package/{CHANGELOG → CHANGELOG.md} +45 -0
  3. package/README.md +147 -93
  4. package/assembly/__benches__/abc.bench.ts +21 -0
  5. package/assembly/__benches__/large.bench.ts +174 -0
  6. package/assembly/__benches__/lib/index.ts +26 -0
  7. package/assembly/__benches__/medium.bench.ts +46 -0
  8. package/assembly/__benches__/small.bench.ts +33 -0
  9. package/assembly/__benches__/vec3.bench.ts +72 -0
  10. package/assembly/__tests__/array.spec.ts +42 -0
  11. package/assembly/__tests__/float.spec.ts +3 -3
  12. package/assembly/__tests__/map.spec.ts +7 -0
  13. package/assembly/__tests__/raw.spec.ts +4 -5
  14. package/assembly/__tests__/struct.spec.ts +2 -2
  15. package/assembly/deserialize/simple/arbitrary.ts +8 -4
  16. package/assembly/deserialize/simple/array/arbitrary.ts +6 -6
  17. package/assembly/deserialize/simple/array/array.ts +4 -3
  18. package/assembly/deserialize/simple/array/bool.ts +7 -7
  19. package/assembly/deserialize/simple/array/float.ts +2 -2
  20. package/assembly/deserialize/simple/array/integer.ts +1 -1
  21. package/assembly/deserialize/simple/array/map.ts +1 -1
  22. package/assembly/deserialize/simple/array/string.ts +3 -3
  23. package/assembly/deserialize/simple/array/struct.ts +14 -3
  24. package/assembly/deserialize/simple/array.ts +3 -0
  25. package/assembly/deserialize/simple/map.ts +93 -75
  26. package/assembly/deserialize/simple/object.ts +26 -16
  27. package/assembly/deserialize/simple/struct.ts +31 -19
  28. package/assembly/index.ts +14 -11
  29. package/assembly/serialize/simple/array.ts +1 -0
  30. package/assembly/serialize/simple/bool.ts +1 -0
  31. package/assembly/serialize/simple/date.ts +1 -0
  32. package/assembly/serialize/simple/float.ts +1 -0
  33. package/assembly/serialize/simple/integer.ts +1 -0
  34. package/assembly/serialize/simple/map.ts +1 -0
  35. package/assembly/serialize/simple/object.ts +1 -0
  36. package/assembly/serialize/simple/raw.ts +1 -0
  37. package/assembly/serialize/simple/string.ts +1 -0
  38. package/assembly/test.ts +18 -0
  39. package/bench/abc.bench.ts +20 -0
  40. package/bench/large.bench.ts +126 -0
  41. package/bench/medium.bench.ts +43 -0
  42. package/bench/small.bench.ts +30 -0
  43. package/bench/vec3.bench.ts +26 -0
  44. package/index.ts +1 -1
  45. package/package.json +24 -26
  46. package/run-bench.as.sh +27 -0
  47. package/run-bench.js.sh +12 -0
  48. package/run-tests.sh +14 -2
  49. package/transform/lib/index.js +29 -69
  50. package/transform/lib/index.js.map +1 -1
  51. package/transform/src/index.ts +55 -71
  52. package/.gitmodules +0 -0
  53. package/assembly/__benches__/misc.bench.ts +0 -47
  54. package/assembly/__benches__/schemas.ts +0 -25
  55. package/assembly/__benches__/string.bench.ts +0 -23
  56. package/assembly/__benches__/struct.bench.ts +0 -21
  57. package/assembly/as-bs.d.ts +0 -53
  58. package/bench/schemas.ts +0 -5
  59. package/bench/string.bench.ts +0 -16
  60. /package/bench/{bench.ts → lib/bench.ts} +0 -0
package/assembly/test.ts CHANGED
@@ -57,6 +57,16 @@ class Point {
57
57
  }
58
58
  }
59
59
 
60
+ @json
61
+ class InnerObj<T> {
62
+ obj: T = instantiate<T>()
63
+ }
64
+
65
+ @json
66
+ class ObjWithBracketString {
67
+ data: string = "";
68
+ }
69
+
60
70
  const player: Player = {
61
71
  firstName: "Jairus",
62
72
  lastName: "Tanaka",
@@ -120,3 +130,11 @@ m10.set("pos", new JSON.Raw("{\"x\":1.0,\"y\":2.0,\"z\":3.0}"));
120
130
  const a10 = JSON.stringify(m10);
121
131
 
122
132
  console.log("a10: " + a10);
133
+
134
+ const a11 = JSON.parse<Vec3>(' { "x" : 3.4 , "y" : 1.2 , "z" : 8.3 } ');
135
+
136
+ console.log("a11: " + JSON.stringify(a11));
137
+
138
+ const a12 = JSON.parse<InnerObj<ObjWithBracketString>>('{"obj":{"data":"hello} world"}}');
139
+
140
+ console.log("a12: " + JSON.stringify(a12))
@@ -0,0 +1,20 @@
1
+ import { bench } from "./lib/bench";
2
+
3
+ const v1 = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
4
+ const v2 = '"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"';
5
+
6
+ bench(
7
+ "Serialize Alphabet",
8
+ () => {
9
+ JSON.stringify(v1);
10
+ },
11
+ 64_000_00,
12
+ );
13
+
14
+ bench(
15
+ "Deserialize Alphabet",
16
+ () => {
17
+ JSON.parse(v2);
18
+ },
19
+ 64_000_00,
20
+ );
@@ -0,0 +1,126 @@
1
+ import { bench } from "./lib/bench";
2
+
3
+ class Vec3 {
4
+ public x!: number;
5
+ public y!: number;
6
+ public z!: number;
7
+ }
8
+
9
+ class LargeJSON {
10
+ public id!: number;
11
+ public name!: string;
12
+ public age!: number;
13
+ public email!: string;
14
+ public street!: string;
15
+ public city!: string;
16
+ public state!: string;
17
+ public zip!: string;
18
+ public tags!: string[];
19
+ public theme!: string;
20
+ public notifications!: boolean;
21
+ public language!: string;
22
+ public movement!: Vec3[];
23
+ }
24
+
25
+ const v1: LargeJSON = {
26
+ id: 2,
27
+ name: "Medium Object",
28
+ age: 18,
29
+ email: "me@jairus.dev",
30
+ street: "I don't want to say my street",
31
+ city: "I don't want to say this either",
32
+ state: "It really depends",
33
+ zip: "I forget what it is",
34
+ tags: ["me", "dogs", "mountains", "bar", "foo"],
35
+ theme: "Hyper Term Black",
36
+ notifications: true,
37
+ language: "en-US",
38
+ movement: [
39
+ { x: 1, y: 2, z: 3 },
40
+ { x: 1, y: 2, z: 3 },
41
+ { x: 1, y: 2, z: 3 },
42
+ { x: 1, y: 2, z: 3 },
43
+ { x: 1, y: 2, z: 3 },
44
+ { x: 1, y: 2, z: 3 },
45
+ { x: 1, y: 2, z: 3 },
46
+ { x: 1, y: 2, z: 3 },
47
+ { x: 1, y: 2, z: 3 },
48
+ { x: 1, y: 2, z: 3 },
49
+ { x: 1, y: 2, z: 3 },
50
+ { x: 1, y: 2, z: 3 },
51
+ { x: 1, y: 2, z: 3 },
52
+ { x: 1, y: 2, z: 3 },
53
+ { x: 1, y: 2, z: 3 },
54
+ { x: 1, y: 2, z: 3 },
55
+ { x: 1, y: 2, z: 3 },
56
+ { x: 1, y: 2, z: 3 },
57
+ { x: 1, y: 2, z: 3 },
58
+ { x: 1, y: 2, z: 3 },
59
+ { x: 1, y: 2, z: 3 },
60
+ { x: 1, y: 2, z: 3 },
61
+ { x: 1, y: 2, z: 3 },
62
+ { x: 1, y: 2, z: 3 },
63
+ { x: 1, y: 2, z: 3 },
64
+ { x: 1, y: 2, z: 3 },
65
+ { x: 1, y: 2, z: 3 },
66
+ { x: 1, y: 2, z: 3 },
67
+ { x: 1, y: 2, z: 3 },
68
+ { x: 1, y: 2, z: 3 },
69
+ { x: 1, y: 2, z: 3 },
70
+ { x: 1, y: 2, z: 3 },
71
+ { x: 1, y: 2, z: 3 },
72
+ { x: 1, y: 2, z: 3 },
73
+ { x: 1, y: 2, z: 3 },
74
+ { x: 1, y: 2, z: 3 },
75
+ { x: 1, y: 2, z: 3 },
76
+ { x: 1, y: 2, z: 3 },
77
+ { x: 1, y: 2, z: 3 },
78
+ { x: 1, y: 2, z: 3 },
79
+ { x: 1, y: 2, z: 3 },
80
+ { x: 1, y: 2, z: 3 },
81
+ { x: 1, y: 2, z: 3 },
82
+ { x: 1, y: 2, z: 3 },
83
+ { x: 1, y: 2, z: 3 },
84
+ { x: 1, y: 2, z: 3 },
85
+ { x: 1, y: 2, z: 3 },
86
+ { x: 1, y: 2, z: 3 },
87
+ { x: 1, y: 2, z: 3 },
88
+ { x: 1, y: 2, z: 3 },
89
+ { x: 1, y: 2, z: 3 },
90
+ { x: 1, y: 2, z: 3 },
91
+ { x: 1, y: 2, z: 3 },
92
+ { x: 1, y: 2, z: 3 },
93
+ { x: 1, y: 2, z: 3 },
94
+ { x: 1, y: 2, z: 3 },
95
+ { x: 1, y: 2, z: 3 },
96
+ { x: 1, y: 2, z: 3 },
97
+ { x: 1, y: 2, z: 3 },
98
+ { x: 1, y: 2, z: 3 },
99
+ { x: 1, y: 2, z: 3 },
100
+ { x: 1, y: 2, z: 3 },
101
+ { x: 1, y: 2, z: 3 },
102
+ { x: 1, y: 2, z: 3 },
103
+ { x: 1, y: 2, z: 3 },
104
+ { x: 1, y: 2, z: 3 },
105
+ { x: 1, y: 2, z: 3 },
106
+ { x: 1, y: 2, z: 3 },
107
+ ],
108
+ };
109
+
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
+
112
+ bench(
113
+ "Serialize Medium Object",
114
+ () => {
115
+ JSON.stringify(v1);
116
+ },
117
+ 3_000_00,
118
+ );
119
+
120
+ bench(
121
+ "Deserialize Medium Object",
122
+ () => {
123
+ JSON.parse(v2);
124
+ },
125
+ 3_000_00,
126
+ );
@@ -0,0 +1,43 @@
1
+ import { bench } from "./lib/bench";
2
+
3
+ class MediumJSON {
4
+ public id!: number;
5
+ public name!: string;
6
+ public age!: number;
7
+ public email!: string;
8
+ public street!: string;
9
+ public city!: string;
10
+ public state!: string;
11
+ public zip!: string;
12
+ public tags!: string[];
13
+ }
14
+
15
+ const v1: MediumJSON = {
16
+ id: 2,
17
+ name: "Medium Object",
18
+ age: 18,
19
+ email: "me@jairus.dev",
20
+ street: "I don't want to say my street",
21
+ city: "I don't want to say this either",
22
+ state: "It really depends",
23
+ zip: "I forget what it is",
24
+ tags: ["me", "dogs", "mountains", "bar", "foo"],
25
+ };
26
+
27
+ 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"]}`;
28
+
29
+ bench(
30
+ "Serialize Medium Object",
31
+ () => {
32
+ JSON.stringify(v1);
33
+ },
34
+ 8_000_00,
35
+ );
36
+
37
+ bench(
38
+ "Deserialize Medium Object",
39
+ () => {
40
+ JSON.parse(v2);
41
+ },
42
+ 8_000_00,
43
+ );
@@ -0,0 +1,30 @@
1
+ import { bench } from "./lib/bench";
2
+
3
+ class SmallJSON {
4
+ public id!: number;
5
+ public name!: string;
6
+ public active!: boolean;
7
+ }
8
+
9
+ const v1: SmallJSON = {
10
+ id: 1,
11
+ name: "Small Object",
12
+ active: true,
13
+ };
14
+ const v2 = '{"id":1,"name":"Small Object","active":true}';
15
+
16
+ bench(
17
+ "Serialize Small Object",
18
+ () => {
19
+ JSON.stringify(v1);
20
+ },
21
+ 16_000_00,
22
+ );
23
+
24
+ bench(
25
+ "Deserialize Small Object",
26
+ () => {
27
+ JSON.parse(v2);
28
+ },
29
+ 16_000_00,
30
+ );
@@ -0,0 +1,26 @@
1
+ import { bench } from "./lib/bench";
2
+
3
+ class Vec3 {
4
+ public x!: number;
5
+ public y!: number;
6
+ public z!: number;
7
+ }
8
+
9
+ const v1: Vec3 = { x: 1, y: 2, z: 3 };
10
+ const v2 = '{"x":1,"y":2,"z":3}';
11
+
12
+ bench(
13
+ "Serialize Vec3",
14
+ () => {
15
+ JSON.stringify(v1);
16
+ },
17
+ 16_000_00,
18
+ );
19
+
20
+ bench(
21
+ "Deserialize Vec3",
22
+ () => {
23
+ JSON.parse(v2);
24
+ },
25
+ 16_000_00,
26
+ );
package/index.ts CHANGED
@@ -1 +1 @@
1
- export * from "./assembly/index";
1
+ export { JSON } from "./assembly/index";
package/package.json CHANGED
@@ -1,25 +1,41 @@
1
1
  {
2
2
  "name": "json-as",
3
- "version": "1.0.0-beta.9",
3
+ "version": "1.0.1",
4
4
  "author": "Jairus Tanaka",
5
+ "description": "The only JSON library you'll need for AssemblyScript. SIMD enabled",
6
+ "types": "assembly/index.ts",
7
+ "main": "transform/lib/index.js",
8
+ "homepage": "https://github.com/JairusSW/json-as#readme",
9
+ "license": "MIT",
5
10
  "repository": {
6
11
  "type": "git",
7
12
  "url": "git+https://github.com/JairusSW/json-as.git"
8
13
  },
9
- "main": "transform/lib/index.js",
14
+ "bugs": {
15
+ "url": "https://github.com/JairusSW/json-as/issues"
16
+ },
17
+ "scripts": {
18
+ "test": "bash ./run-tests.sh",
19
+ "bench:as": "bash ./run-bench.as.sh",
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 --lib ./lib/ 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",
24
+ "test:wasmtime": "wasmtime ./build/test.wasm",
25
+ "test:wasmer": "wasmer ./build/test.wasm",
26
+ "build:transform": "tsc -p ./transform",
27
+ "bench:wasmer": "wasmer ./build/bench.wasm --llvm",
28
+ "prettier": "prettier -w ."
29
+ },
10
30
  "devDependencies": {
11
31
  "@assemblyscript/wasi-shim": "^0.1.0",
12
32
  "@types/node": "^22.13.1",
13
- "as-bench": "JairusSW/as-bench",
14
- "as-console": "^7.0.0",
15
33
  "assemblyscript": "^0.27.34",
16
34
  "assemblyscript-prettier": "^3.0.1",
17
35
  "prettier": "^3.5.0",
36
+ "tsx": "^4.19.3",
18
37
  "typescript": "^5.7.3"
19
38
  },
20
- "bugs": {
21
- "url": "https://github.com/JairusSW/json-as/issues"
22
- },
23
39
  "contributors": [
24
40
  "DogWhich",
25
41
  "Romdotdog",
@@ -30,8 +46,6 @@
30
46
  "Matt Johnson-Pint",
31
47
  "Tomáš Hromada"
32
48
  ],
33
- "description": "The only JSON library you'll need for AssemblyScript. SIMD enabled",
34
- "homepage": "https://github.com/JairusSW/json-as#readme",
35
49
  "keywords": [
36
50
  "assemblyscript",
37
51
  "json",
@@ -47,24 +61,8 @@
47
61
  "fast",
48
62
  "algorithm"
49
63
  ],
50
- "license": "MIT",
51
- "overrides": {
52
- "assemblyscript": "$assemblyscript"
53
- },
54
64
  "publishConfig": {
55
65
  "@JairusSW:registry": "https://npm.pkg.github.com"
56
66
  },
57
- "scripts": {
58
- "test": "bash ./run-tests.sh",
59
- "build:bench": "rm -rf ./build/ && JSON_DEBUG=true asc assembly/__benches__/misc.bench.ts -o ./build/bench.wasm --textFile ./build/bench.wat --transform ./transform --optimizeLevel 3 --shrinkLevel 0 --converge --noAssert --uncheckedBehavior always --runtime stub --enable simd --enable bulk-memory",
60
- "build:test": "rm -rf ./build/ && JSON_DEBUG=true asc --lib ./libs/ assembly/test.ts --transform ./transform -o ./build/test.wasm --textFile ./build/test.wat --optimizeLevel 3 --shrinkLevel 0",
61
- "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",
62
- "test:wasmtime": "wasmtime ./build/test.wasm",
63
- "test:wasmer": "wasmer ./build/test.wasm",
64
- "build:transform": "tsc -p ./transform",
65
- "bench:wasmer": "wasmer ./build/bench.wasm --llvm",
66
- "prettier": "prettier -w ."
67
- },
68
- "type": "module",
69
- "types": "assembly/index.ts"
67
+ "type": "module"
70
68
  }
@@ -0,0 +1,27 @@
1
+ #!/bin/bash
2
+
3
+ mkdir -p ./build
4
+
5
+ for file in ./assembly/__benches__/*.bench.ts; do
6
+ filename=$(basename -- "$file")
7
+ output="./build/${filename%.ts}.wasm"
8
+
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)
12
+
13
+ build_time=$((end_time - start_time))
14
+
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
22
+
23
+ echo -e "$filename (built in $formatted_time)\n"
24
+ wasmer "$output" --llvm || { echo "Benchmarked failed."; exit 1; }
25
+ done
26
+
27
+ echo "Finished benchmarks."
@@ -0,0 +1,12 @@
1
+ #!/bin/bash
2
+
3
+ mkdir -p ./build
4
+
5
+ for file in ./bench/*.bench.ts; do
6
+ filename=$(basename -- "$file")
7
+
8
+ echo -e "$filename\n"
9
+ npx tsx "$file" || { echo "Benchmark failed."; exit 1; }
10
+ done
11
+
12
+ echo "Finished benchmarks."
package/run-tests.sh CHANGED
@@ -6,9 +6,21 @@ for file in ./assembly/__tests__/*.spec.ts; do
6
6
  filename=$(basename -- "$file")
7
7
  output="./build/${filename%.ts}.wasm"
8
8
 
9
- asc "$file" --transform ./transform --lib ./libs/ -o "$output" || { echo "Tests failed"; exit 1; }
9
+ start_time=$(date +%s%3N)
10
+ npx asc "$file" --transform ./transform --lib ./lib/ -o "$output" || { echo "Tests failed"; exit 1; }
11
+ end_time=$(date +%s%3N)
10
12
 
11
- echo " -> $filename"
13
+ build_time=$((end_time - start_time))
14
+
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
22
+
23
+ echo " -> $filename (built in $formatted_time)"
12
24
  wasmtime "$output" || { echo "Tests failed"; exit 1; }
13
25
  done
14
26
 
@@ -1,4 +1,4 @@
1
- import { Source, Node, Tokenizer } from "assemblyscript/dist/assemblyscript.js";
1
+ import { Node } from "assemblyscript/dist/assemblyscript.js";
2
2
  import { Transform } from "assemblyscript/dist/transform.js";
3
3
  import { Visitor } from "./visitor.js";
4
4
  import { SimpleParser, toString } from "./util.js";
@@ -13,9 +13,7 @@ class JSONTransform extends Visitor {
13
13
  schema;
14
14
  sources = new Set();
15
15
  imports = [];
16
- jsonImport = null;
17
- bsImport = null;
18
- newStmts = { simd: [] };
16
+ topStatements = [];
19
17
  visitClassDeclaration(node) {
20
18
  if (!node.decorators?.length)
21
19
  return;
@@ -312,7 +310,7 @@ class JSONTransform extends Visitor {
312
310
  DESERIALIZE += `${indent}switch (load<u32>(keyStart)) {\n`;
313
311
  else if (memberLen == 6)
314
312
  DESERIALIZE += `${indent}let code = load<u64>(keyStart) & 0x0000FFFFFFFFFFFF;\n`;
315
- else if (memberLen == 6)
313
+ else if (memberLen == 8)
316
314
  DESERIALIZE += `${indent}let code = load<u64>(keyStart);\n`;
317
315
  else
318
316
  DESERIALIZE += toMemCDecl(memberLen, indent);
@@ -410,9 +408,6 @@ class JSONTransform extends Visitor {
410
408
  }
411
409
  visitImportStatement(node) {
412
410
  super.visitImportStatement(node);
413
- const source = this.parser.sources.find((src) => src.internalPath == node.internalPath);
414
- if (!source)
415
- return;
416
411
  this.imports.push(node);
417
412
  }
418
413
  visitSource(node) {
@@ -420,35 +415,29 @@ class JSONTransform extends Visitor {
420
415
  super.visitSource(node);
421
416
  }
422
417
  addRequiredImports(node) {
423
- const bsImport = this.imports.find((i) => i.declarations.find((d) => d.foreignName.text == "bs"));
424
- if (bsImport) {
425
- const txt = `import { bs } from "as-bs";`;
426
- if (!this.bsImport) {
427
- this.bsImport = txt;
428
- if (process.env["JSON_DEBUG"])
429
- console.log("Added as-bs import: " + txt + "\n");
430
- }
431
- }
432
- else {
433
- const txt = `import { bs } from "as-bs";`;
434
- if (!this.bsImport) {
435
- this.bsImport = txt;
436
- if (process.env["JSON_DEBUG"])
437
- console.log("Added as-bs import: " + txt + "\n");
438
- }
418
+ const filePath = fileURLToPath(import.meta.url);
419
+ const fileDir = path.dirname(filePath);
420
+ const bsImport = this.imports.find((i) => i.declarations?.find((d) => d.foreignName.text == "bs" || d.name.text == "bs"));
421
+ const jsonImport = this.imports.find((i) => i.declarations?.find((d) => d.foreignName.text == "JSON" || d.name.text == "JSON"));
422
+ let pkgRel = path.relative(path.dirname(node.range.source.normalizedPath), path.resolve(fileDir, "../../"));
423
+ if (!pkgRel.startsWith(".") && !pkgRel.startsWith("/"))
424
+ pkgRel = "./" + pkgRel;
425
+ pkgRel = pkgRel.replace(/^.*json-as/, "json-as");
426
+ if (!bsImport) {
427
+ const replaceNode = Node.createImportStatement([
428
+ Node.createImportDeclaration(Node.createIdentifierExpression("bs", node.range, false), null, node.range)
429
+ ], Node.createStringLiteralExpression(path.join(pkgRel, "./lib/as-bs"), node.range), node.range);
430
+ this.topStatements.push(replaceNode);
431
+ if (process.env["JSON_DEBUG"])
432
+ console.log("Added as-bs import: " + toString(replaceNode) + "\n");
439
433
  }
440
- if (!this.imports.find((i) => i.declarations.find((d) => d.foreignName.text == "JSON"))) {
441
- const __filename = fileURLToPath(import.meta.url);
442
- const __dirname = path.dirname(__filename);
443
- let relativePath = path.relative(path.dirname(node.range.source.normalizedPath), path.resolve(__dirname, "../../assembly/index.ts"));
444
- if (!relativePath.startsWith(".") && !relativePath.startsWith("/"))
445
- relativePath = "./" + relativePath;
446
- const txt = `import { JSON } from "${relativePath}";`;
447
- if (!this.jsonImport) {
448
- this.jsonImport = txt;
449
- if (process.env["JSON_DEBUG"])
450
- console.log("Added json-as import: " + txt + "\n");
451
- }
434
+ if (!jsonImport) {
435
+ const replaceNode = Node.createImportStatement([
436
+ Node.createImportDeclaration(Node.createIdentifierExpression("JSON", node.range, false), null, node.range)
437
+ ], Node.createStringLiteralExpression(path.join(pkgRel, "./assembly"), node.range), node.range);
438
+ this.topStatements.push(replaceNode);
439
+ if (process.env["JSON_DEBUG"])
440
+ console.log("Added json-as import: " + toString(replaceNode) + "\n");
452
441
  }
453
442
  }
454
443
  getStores(data, simd = false) {
@@ -456,14 +445,6 @@ class JSONTransform extends Visitor {
456
445
  const sizes = strToNum(data, simd);
457
446
  let offset = 0;
458
447
  for (const [size, num] of sizes) {
459
- if (size == "v128") {
460
- let index = this.newStmts.simd.findIndex((v) => v.includes(num));
461
- let name = "SIMD_" + (index == -1 ? this.newStmts.simd.length : index);
462
- if (index && !this.newStmts.simd.includes(`const ${name} = ${num};`))
463
- this.newStmts.simd.push(`const ${name} = ${num};`);
464
- out.push("store<v128>(bs.offset, " + name + ", " + offset + "); // " + data.slice(offset >> 1, (offset >> 1) + 8));
465
- offset += 16;
466
- }
467
448
  if (size == "u64") {
468
449
  out.push("store<u64>(bs.offset, " + num + ", " + offset + "); // " + data.slice(offset >> 1, (offset >> 1) + 4));
469
450
  offset += 8;
@@ -546,27 +527,9 @@ export default class Transformer extends Transform {
546
527
  transformer.imports = [];
547
528
  transformer.currentSource = source;
548
529
  transformer.visit(source);
549
- if (transformer.newStmts.simd) {
550
- const tokenizer = new Tokenizer(new Source(0, source.normalizedPath, transformer.newStmts.simd.join("\n")));
551
- parser.currentSource = tokenizer.source;
552
- for (let i = 0; i < transformer.newStmts.simd.length; i++)
553
- source.statements.unshift(parser.parseTopLevelStatement(tokenizer));
554
- parser.currentSource = source;
555
- transformer.newStmts.simd = [];
556
- }
557
- if (transformer.jsonImport) {
558
- const tokenizer = new Tokenizer(new Source(0, source.normalizedPath, transformer.jsonImport));
559
- parser.currentSource = tokenizer.source;
560
- source.statements.unshift(parser.parseTopLevelStatement(tokenizer));
561
- parser.currentSource = source;
562
- transformer.jsonImport = null;
563
- }
564
- if (transformer.bsImport) {
565
- const tokenizer = new Tokenizer(new Source(0, source.normalizedPath, transformer.bsImport));
566
- parser.currentSource = tokenizer.source;
567
- source.statements.unshift(parser.parseTopLevelStatement(tokenizer));
568
- parser.currentSource = source;
569
- transformer.bsImport = null;
530
+ if (transformer.topStatements.length) {
531
+ source.statements.unshift(...transformer.topStatements);
532
+ transformer.topStatements = [];
570
533
  }
571
534
  }
572
535
  const schemas = transformer.schemas;
@@ -671,7 +634,7 @@ function strToNum(data, simd = false, offset = 0) {
671
634
  }
672
635
  function isPrimitive(type) {
673
636
  const primitiveTypes = ["u8", "u16", "u32", "u64", "i8", "i16", "i32", "i64", "f32", "f64", "bool", "boolean"];
674
- return primitiveTypes.some((v) => type.includes(v));
637
+ return primitiveTypes.some((v) => type.startsWith(v));
675
638
  }
676
639
  function throwError(message, range) {
677
640
  const err = new Error();
@@ -706,7 +669,4 @@ function sizeof(type) {
706
669
  else
707
670
  return 0;
708
671
  }
709
- function allPrimitive(schema) {
710
- return !schema.members.some((p) => p.byteSize == 0);
711
- }
712
672
  //# sourceMappingURL=index.js.map