json-as 0.9.7 → 0.9.8-a

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 (63) hide show
  1. package/.github/workflows/nodejs.yml +9 -10
  2. package/CHANGELOG +10 -2
  3. package/README.md +52 -7
  4. package/asconfig.json +24 -3
  5. package/assembly/__tests__/test.spec.ts +564 -0
  6. package/assembly/__tests__/types.ts +82 -0
  7. package/assembly/{src/chars.ts → chars.ts} +36 -36
  8. package/assembly/deserialize/array/array.ts +4 -4
  9. package/assembly/deserialize/array/bool.ts +4 -4
  10. package/assembly/deserialize/array/float.ts +4 -4
  11. package/assembly/deserialize/array/integer.ts +4 -4
  12. package/assembly/deserialize/array/map.ts +4 -4
  13. package/assembly/deserialize/array/object.ts +4 -4
  14. package/assembly/deserialize/array/string.ts +4 -4
  15. package/assembly/deserialize/array.ts +5 -4
  16. package/assembly/deserialize/bool.ts +4 -4
  17. package/assembly/deserialize/date.ts +2 -2
  18. package/assembly/deserialize/float.ts +2 -2
  19. package/assembly/deserialize/integer.ts +3 -3
  20. package/assembly/deserialize/map.ts +4 -4
  21. package/assembly/deserialize/mpz.ts +12 -0
  22. package/assembly/deserialize/object.ts +4 -4
  23. package/assembly/deserialize/string.ts +5 -5
  24. package/assembly/index.ts +25 -33
  25. package/assembly/serialize/array.ts +6 -5
  26. package/assembly/serialize/bool.ts +2 -2
  27. package/assembly/serialize/date.ts +2 -2
  28. package/assembly/serialize/float.ts +2 -2
  29. package/assembly/serialize/integer.ts +2 -2
  30. package/assembly/serialize/map.ts +8 -7
  31. package/assembly/serialize/mpz.ts +6 -0
  32. package/assembly/serialize/object.ts +2 -2
  33. package/assembly/serialize/string.ts +5 -5
  34. package/assembly/serialize/unknown.ts +5 -5
  35. package/assembly/test.ts +79 -45
  36. package/assembly/types.ts +4 -0
  37. package/assembly/{src/util.ts → util.ts} +3 -3
  38. package/bench/benchmark.ts +1 -1
  39. package/build/test.spec.wasm +0 -0
  40. package/build/test.spec.wasm.map +1 -0
  41. package/build/test.spec.wat +114980 -0
  42. package/build/test.wasm +0 -0
  43. package/build/test.wasm.map +1 -0
  44. package/build/test.wat +10948 -0
  45. package/package.json +12 -14
  46. package/transform/lib/index.js +256 -21
  47. package/transform/lib/visitor.js +516 -0
  48. package/transform/package.json +1 -1
  49. package/transform/src/index.ts +360 -25
  50. package/transform/src/visitor.ts +543 -0
  51. package/transform/tsconfig.json +23 -63
  52. package/tsconfig.json +13 -13
  53. package/as-pect.asconfig.json +0 -24
  54. package/as-pect.config.js +0 -30
  55. package/assembly/__tests__/deserialize.spec.ts +0 -301
  56. package/assembly/__tests__/serialize.spec.ts +0 -398
  57. package/assembly/deserialize/box.ts +0 -17
  58. package/assembly/serialize/box.ts +0 -11
  59. package/develop/assembly/serialize/unknown.ts +0 -46
  60. package/transform/lib/index.old.js +0 -257
  61. package/transform/lib/types.js +0 -17
  62. package/transform/src/index.old.ts +0 -312
  63. /package/assembly/{src/sink.ts → sink.ts} +0 -0
package/tsconfig.json CHANGED
@@ -9,11 +9,11 @@
9
9
  // "disableSolutionSearching": true, /* Opt a project out of multi-project reference checking when editing. */
10
10
  // "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */
11
11
  /* Language and Environment */
12
- "target": "es2020" /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */,
12
+ "target": "es2020" /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */,
13
13
  // "lib": [], /* Specify a set of bundled library declaration files that describe the target runtime environment. */
14
14
  // "jsx": "preserve", /* Specify what JSX code is generated. */
15
- "experimentalDecorators": true, /* Enable experimental support for TC39 stage 2 draft decorators. */
16
- "emitDecoratorMetadata": true, /* Emit design-type metadata for decorated declarations in source files. */
15
+ "experimentalDecorators": true, /* Enable experimental support for TC39 stage 2 draft decorators. */
16
+ "emitDecoratorMetadata": true, /* Emit design-type metadata for decorated declarations in source files. */
17
17
  // "jsxFactory": "", /* Specify the JSX factory function used when targeting React JSX emit, e.g. 'React.createElement' or 'h'. */
18
18
  // "jsxFragmentFactory": "", /* Specify the JSX Fragment reference used for fragments when targeting React JSX emit e.g. 'React.Fragment' or 'Fragment'. */
19
19
  // "jsxImportSource": "", /* Specify module specifier used to import the JSX factory functions when using 'jsx: react-jsx*'. */
@@ -22,7 +22,7 @@
22
22
  // "useDefineForClassFields": true, /* Emit ECMAScript-standard-compliant class fields. */
23
23
  // "moduleDetection": "auto", /* Control what method is used to detect module-format JS files. */
24
24
  /* Modules */
25
- "module": "commonjs" /* Specify what module code is generated. */,
25
+ "module": "commonjs" /* Specify what module code is generated. */,
26
26
  // "rootDir": "./", /* Specify the root folder within your source files. */
27
27
  // "moduleResolution": "node", /* Specify how TypeScript looks up a file from a given module specifier. */
28
28
  // "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */
@@ -43,7 +43,7 @@
43
43
  // "declarationMap": true, /* Create sourcemaps for d.ts files. */
44
44
  // "emitDeclarationOnly": true, /* Only output d.ts files and not JavaScript files. */
45
45
  // "sourceMap": true, /* Create source map files for emitted JavaScript files. */
46
- // "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If 'declaration' is true, also designates a file that bundles all .d.ts output. */
46
+ "outFile": "./transform/index.js", /* Specify a file that bundles all outputs into one JavaScript file. If 'declaration' is true, also designates a file that bundles all .d.ts output. */
47
47
  // "outDir": "./", /* Specify an output folder for all emitted files. */
48
48
  // "removeComments": true, /* Disable emitting comments. */
49
49
  // "noEmit": true, /* Disable emitting files from a compilation. */
@@ -65,21 +65,21 @@
65
65
  /* Interop Constraints */
66
66
  // "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */
67
67
  // "allowSyntheticDefaultImports": true, /* Allow 'import x from y' when a module doesn't have a default export. */
68
- "esModuleInterop": true /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */,
68
+ "esModuleInterop": true /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */,
69
69
  // "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */
70
- "forceConsistentCasingInFileNames": true /* Ensure that casing is correct in imports. */,
70
+ "forceConsistentCasingInFileNames": true /* Ensure that casing is correct in imports. */,
71
71
  /* Type Checking */
72
- "strict": true /* Enable all strict type-checking options. */,
72
+ "strict": true /* Enable all strict type-checking options. */,
73
73
  // "noImplicitAny": true, /* Enable error reporting for expressions and declarations with an implied 'any' type. */
74
- "strictNullChecks": true, /* When type checking, take into account 'null' and 'undefined'. */
74
+ "strictNullChecks": true, /* When type checking, take into account 'null' and 'undefined'. */
75
75
  // "strictFunctionTypes": true, /* When assigning functions, check to ensure parameters and the return values are subtype-compatible. */
76
76
  // "strictBindCallApply": true, /* Check that the arguments for 'bind', 'call', and 'apply' methods match the original function. */
77
77
  // "strictPropertyInitialization": true, /* Check for class properties that are declared but not set in the constructor. */
78
78
  // "noImplicitThis": true, /* Enable error reporting when 'this' is given the type 'any'. */
79
79
  // "useUnknownInCatchVariables": true, /* Default catch clause variables as 'unknown' instead of 'any'. */
80
- "alwaysStrict": true, /* Ensure 'use strict' is always emitted. */
81
- "noUnusedLocals": true, /* Enable error reporting when local variables aren't read. */
82
- "noUnusedParameters": true, /* Raise an error when a function parameter isn't read. */
80
+ "alwaysStrict": true, /* Ensure 'use strict' is always emitted. */
81
+ "noUnusedLocals": true, /* Enable error reporting when local variables aren't read. */
82
+ "noUnusedParameters": true, /* Raise an error when a function parameter isn't read. */
83
83
  // "exactOptionalPropertyTypes": true, /* Interpret optional property types as written, rather than adding 'undefined'. */
84
84
  // "noImplicitReturns": true, /* Enable error reporting for codepaths that do not explicitly return in a function. */
85
85
  // "noFallthroughCasesInSwitch": true, /* Enable error reporting for fallthrough cases in switch statements. */
@@ -90,6 +90,6 @@
90
90
  // "allowUnreachableCode": true, /* Disable error reporting for unreachable code. */
91
91
  /* Completeness */
92
92
  // "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */
93
- "skipLibCheck": true /* Skip type checking all .d.ts files. */
93
+ "skipLibCheck": true /* Skip type checking all .d.ts files. */
94
94
  }
95
95
  }
@@ -1,24 +0,0 @@
1
- {
2
- "targets": {
3
- "coverage": {
4
- "lib": ["@as-covers/assembly/index.ts"],
5
- "transform": ["@as-covers/transform", "@as-pect/transform"]
6
- },
7
- "noCoverage": {
8
- "transform": ["@as-pect/transform"]
9
- }
10
- },
11
- "options": {
12
- "exportMemory": true,
13
- "outFile": "output.wasm",
14
- "textFile": "output.wat",
15
- "bindings": "raw",
16
- "exportStart": "_start",
17
- "exportRuntime": true,
18
- "use": ["RTRACE=1"],
19
- "debug": true,
20
- "exportTable": true
21
- },
22
- "extends": "./asconfig.json",
23
- "entries": ["./node_modules/@as-pect/assembly/assembly/index.ts"]
24
- }
package/as-pect.config.js DELETED
@@ -1,30 +0,0 @@
1
- export default {
2
- /**
3
- * A set of globs passed to the glob package that qualify typescript files for testing.
4
- */
5
- entries: ["assembly/__tests__/*.spec.ts"],
6
- /**
7
- * A set of globs passed to the glob package that quality files to be added to each test.
8
- */
9
- include: ["assembly/__tests__/**/*.include.ts"],
10
- /**
11
- * A set of regexp that will disclude source files from testing.
12
- */
13
- disclude: [/node_modules/],
14
- /**
15
- * Add your required AssemblyScript imports here.
16
- */
17
- async instantiate(memory, createImports, instantiate, binary) {
18
- let instance; // Imports can reference this
19
- const myImports = {
20
- // put your web assembly imports here, and return the module
21
- };
22
- return instantiate(binary, createImports(myImports));
23
- },
24
- /** Enable code coverage. */
25
- // coverage: ["assembly/**/*.ts"],
26
- /**
27
- * Specify if the binary wasm file should be written to the file system.
28
- */
29
- outputBinary: false,
30
- };
@@ -1,301 +0,0 @@
1
- import { JSON } from "..";
2
- import {
3
- describe,
4
- expect,
5
- run
6
- } from "as-test/assembly";
7
-
8
- @json
9
- class BaseObject {
10
- a: string;
11
- constructor(a: string) {
12
- this.a = a;
13
- }
14
- }
15
-
16
- @json
17
- class DerivedObject extends BaseObject {
18
- b: string;
19
- constructor(a: string, b: string) {
20
- super(a);
21
- this.b = b;
22
- }
23
- }
24
-
25
- @json
26
- class Map4 {
27
- a: string;
28
- b: string;
29
- c: string;
30
- d: string;
31
- }
32
-
33
- @json
34
- class Vec3 {
35
- x: f64;
36
- y: f64;
37
- z: f64;
38
-
39
- static shouldIgnore: string = "should not be serialized";
40
- }
41
-
42
- @json
43
- class Player {
44
- firstName: string;
45
- lastName: string;
46
- lastActive: i32[];
47
- age: i32;
48
- pos: Vec3 | null;
49
- isVerified: boolean;
50
- }
51
-
52
- class Nullable { }
53
- type Null = Nullable | null;
54
-
55
- describe("Should deserialize strings", () => {
56
-
57
- expect(
58
- JSON.parse<string>("\"abcdefg\"")
59
- ).toBe("abcdefg");
60
-
61
- expect(
62
- JSON.parse<string>('"\\"st\\\\\\"ring\\\\\\" w\\\\\\"\\\\\\"ith quotes\\\\\\"\\\""')
63
- ).toBe('"st\\"ring\\" w\\"\\"ith quotes\\""');
64
-
65
- expect(
66
- JSON.parse<string>('"\\"string \\\\\\"with random spa\\\\nces and \\\\nnewlines\\\\n\\\\n\\\\n\\""')
67
- ).toBe('"string \\"with random spa\\nces and \\nnewlines\\n\\n\\n"');
68
-
69
- expect(
70
- JSON.parse<string>('"\\"string with colon : comma , brace [ ] bracket { } and quote \\\\\\" and other quote \\\\\\\\\\"\\\""')
71
- ).toBe('"string with colon : comma , brace [ ] bracket { } and quote \\" and other quote \\\\""');
72
-
73
- });
74
-
75
- describe("Should deserialize integers", () => {
76
-
77
- expect(
78
- JSON.parse<i32>("0")
79
- ).toBe(<i32>0);
80
-
81
- expect(
82
- JSON.parse<u32>("100")
83
- ).toBe(<u32>100);
84
-
85
- expect(
86
- JSON.parse<u64>("101")
87
- ).toBe(<u64>101);
88
-
89
- expect(
90
- JSON.parse<i32>("-100")
91
- ).toBe(<i32>-100);
92
-
93
- expect(
94
- JSON.parse<i64>("-101")
95
- ).toBe(<i64>-101);
96
-
97
- });
98
-
99
- describe("Should deserialize floats", () => {
100
-
101
- expect(
102
- JSON.parse<f64>("7.23")
103
- ).toBe(7.23);
104
-
105
- expect(
106
- JSON.parse<f64>("1000.0")
107
- ).toBe(1000.0);
108
-
109
- expect(
110
- JSON.parse<f64>("1.23456")
111
- ).toBe(1.23456);
112
-
113
- expect(
114
- JSON.parse<f64>("0.0")
115
- ).toBe(0.0);
116
-
117
- expect(
118
- JSON.parse<f64>("-7.23")
119
- ).toBe(-7.23);
120
-
121
- expect(
122
- JSON.parse<f64>("0.000001")
123
- ).toBe(0.000001);
124
-
125
- expect(
126
- JSON.parse<f64>("1e-7")
127
- ).toBe(1e-7);
128
-
129
- expect(
130
- JSON.parse<f64>("100000000000000000000.0")
131
- ).toBe(1e20);
132
-
133
- expect(
134
- JSON.parse<f64>("1e+21")
135
- ).toBe(1e21);
136
-
137
- });
138
-
139
- describe("Should deserialize booleans", () => {
140
-
141
- expect(
142
- JSON.parse<boolean>("true")
143
- ).toBe(true);
144
-
145
- expect(
146
- JSON.parse<boolean>("false")
147
- ).toBe(false);
148
-
149
- });
150
-
151
- describe("Should deserialize class inheritance", () => {
152
-
153
- const jsonStr = "{\"a\":\"1\",\"b\":\"2\"}";
154
- const obj = JSON.parse<DerivedObject>(jsonStr);
155
-
156
- expect(obj instanceof DerivedObject).toBe(true);
157
- expect(obj.a).toBe("1");
158
- expect(obj.b).toBe("2");
159
- });
160
-
161
- describe("Should deserialize nulls", () => {
162
-
163
- expect(
164
- JSON.stringify(JSON.parse<Null>("null"))
165
- ).toBe("null");
166
-
167
- });
168
-
169
- describe("Should deserialize integer arrays", () => {
170
-
171
- expect(
172
- JSON.stringify(JSON.parse<u32[]>("[0,100,101]"))
173
- ).toBe(JSON.stringify([0, 100, 101]));
174
-
175
- expect(
176
- JSON.stringify(JSON.parse<i32[]>("[0,100,101,-100,-101]"))
177
- ).toBe(JSON.stringify([0, 100, 101, -100, -101]));
178
-
179
- });
180
-
181
- describe("Should deserialize float arrays", () => {
182
-
183
- expect(
184
- JSON.stringify(JSON.parse<f64[]>("[7.23,1000.0,1000.0,1.23456,1.23456,0.0,7.23]"))
185
- ).toBe(JSON.stringify([7.23, 1000.0, 1000.0, 1.23456, 1.23456, 0.0, 7.23]));
186
-
187
- expect(
188
- JSON.stringify(JSON.parse<f64[]>("[1e+21,1e+22,1e-7,1e-8,1e-9]"))
189
- ).toBe(JSON.stringify([1e21, 1e22, 1e-7, 1e-8, 1e-9]));
190
-
191
- });
192
-
193
- describe("Should deserialize boolean arrays", () => {
194
-
195
- expect(
196
- JSON.stringify(JSON.parse<boolean[]>("[true,false]"))
197
- ).toBe(JSON.stringify([true, false]));
198
-
199
- });
200
-
201
- describe("Should deserialize string arrays", () => {
202
-
203
- expect(
204
- JSON.stringify(JSON.parse<string[]>('["string \\"with random spa\\nces and \\nnewlines\\n\\n\\n"]'))
205
- ).toBe(JSON.stringify([ 'string "with random spa\nces and \nnewlines\n\n\n' ]));
206
-
207
- });
208
-
209
- describe("Should deserialize nested integer arrays", () => {
210
-
211
- expect(
212
- JSON.stringify(JSON.parse<i64[][]>("[[100,101],[-100,-101],[0]]"))
213
- ).toBe(JSON.stringify([[100, 101], [-100, -101], [0]]));
214
-
215
- });
216
-
217
- describe("Should deserialize nested float arrays", () => {
218
-
219
- expect(
220
- JSON.stringify(JSON.parse<f64[][]>("[[7.23],[1000.0],[1000.0],[1.23456],[1.23456],[0.0],[7.23]]"))
221
- ).toBe(JSON.stringify([[7.23], [1000.0], [1000.0], [1.23456], [1.23456], [0.0], [7.23]]));
222
-
223
- });
224
-
225
- describe("Should deserialize nested boolean arrays", () => {
226
-
227
- expect(
228
- JSON.stringify(JSON.parse<boolean[][]>("[[true],[false]]"))
229
- ).toBe(JSON.stringify([[true], [false]]));
230
-
231
- });
232
-
233
- describe("Should deserialize object arrays", () => {
234
-
235
- expect(
236
- JSON.stringify(JSON.parse<Vec3[]>('[{"x":3.4,"y":1.2,"z":8.3},{"x":3.4,"y":-2.1,"z":9.3}]'))
237
- ).toBe(JSON.stringify(<Vec3[]>[
238
- { x: 3.4, y: 1.2, z: 8.3 },
239
- { x: 3.4, y: -2.1, z: 9.3 }
240
- ]));
241
-
242
- });
243
-
244
- describe("Should deserialize Objects", () => {
245
-
246
- expect(
247
- JSON.stringify(JSON.parse<Vec3>('{"x":3.4,"y":1.2,"z":8.3}'))
248
- ).toBe(JSON.stringify(<Vec3>{ x: 3.4, y: 1.2, z: 8.3 }));
249
-
250
- expect(
251
- JSON.stringify(JSON.parse<Player>('{"firstName":"Emmet","lastName":"West","lastActive":[8,27,2022],"age":23,"pos":{"x":3.4,"y":1.2,"z":8.3},"isVerified":true}'))
252
- ).toBe(JSON.stringify(<Player>{
253
- firstName: "Emmet",
254
- lastName: "West",
255
- lastActive: [8, 27, 2022],
256
- age: 23,
257
- pos: { x: 3.4, y: 1.2, z: 8.3 },
258
- isVerified: true
259
- }));
260
-
261
- expect(
262
- JSON.stringify(JSON.parse<ObjectWithFloat>('{"f":7.23}'))
263
- ).toBe(JSON.stringify(<ObjectWithFloat>{ f: 7.23 }));
264
-
265
- expect(
266
- JSON.stringify(JSON.parse<ObjectWithFloat>('{"f":0.000001}'))
267
- ).toBe(JSON.stringify(<ObjectWithFloat>{ f: 0.000001 }));
268
-
269
- expect(
270
- JSON.stringify(JSON.parse<ObjWithStrangeKey<string>>('{"a\\\\\\t\\"\\u0002b`c":"foo"}'))
271
- ).toBe(JSON.stringify(<ObjWithStrangeKey<string>>{ data: "foo" }));
272
-
273
- });
274
-
275
- run();
276
-
277
- @json
278
- class ObjWithString {
279
- s!: string;
280
- }
281
-
282
- @json
283
- class ObjectWithStringArray {
284
- sa!: string[];
285
- }
286
-
287
- @json
288
- class ObjectWithFloat {
289
- f!: f64;
290
- }
291
-
292
- @json
293
- class ObjectWithFloatArray {
294
- fa!: f64[];
295
- }
296
-
297
- @json
298
- class ObjWithStrangeKey<T> {
299
- @alias('a\\\t"\x02b`c')
300
- data!: T;
301
- }