json-as 0.9.6 → 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.
- package/.github/workflows/nodejs.yml +9 -10
- package/.github/workflows/release-package.yml +1 -1
- package/CHANGELOG +12 -2
- package/README.md +54 -11
- package/asconfig.json +24 -3
- package/assembly/__tests__/test.spec.ts +564 -0
- package/assembly/__tests__/types.ts +82 -0
- package/assembly/{src/chars.ts → chars.ts} +36 -36
- package/assembly/deserialize/array/array.ts +4 -4
- package/assembly/deserialize/array/bool.ts +4 -4
- package/assembly/deserialize/array/float.ts +4 -4
- package/assembly/deserialize/array/integer.ts +4 -4
- package/assembly/deserialize/array/map.ts +4 -4
- package/assembly/deserialize/array/object.ts +4 -4
- package/assembly/deserialize/array/string.ts +4 -4
- package/assembly/deserialize/array.ts +5 -4
- package/assembly/deserialize/bool.ts +4 -4
- package/assembly/deserialize/date.ts +2 -2
- package/assembly/deserialize/float.ts +2 -2
- package/assembly/deserialize/integer.ts +3 -3
- package/assembly/deserialize/map.ts +4 -4
- package/assembly/deserialize/mpz.ts +12 -0
- package/assembly/deserialize/object.ts +4 -4
- package/assembly/deserialize/string.ts +5 -5
- package/assembly/index.ts +25 -33
- package/assembly/serialize/array.ts +6 -5
- package/assembly/serialize/bool.ts +2 -2
- package/assembly/serialize/date.ts +2 -2
- package/assembly/serialize/float.ts +2 -2
- package/assembly/serialize/integer.ts +2 -2
- package/assembly/serialize/map.ts +8 -7
- package/assembly/serialize/mpz.ts +6 -0
- package/assembly/serialize/object.ts +2 -2
- package/assembly/serialize/string.ts +5 -5
- package/assembly/serialize/unknown.ts +5 -5
- package/assembly/test.ts +79 -45
- package/assembly/types.ts +4 -0
- package/assembly/{src/util.ts → util.ts} +3 -3
- package/bench/benchmark.ts +1 -1
- package/build/test.spec.wasm +0 -0
- package/build/test.spec.wasm.map +1 -0
- package/build/test.spec.wat +114980 -0
- package/build/test.wasm +0 -0
- package/build/test.wasm.map +1 -0
- package/build/test.wat +10948 -0
- package/package.json +12 -14
- package/transform/lib/index.js +256 -21
- package/transform/lib/visitor.js +516 -0
- package/transform/package.json +1 -1
- package/transform/src/index.ts +360 -25
- package/transform/src/visitor.ts +543 -0
- package/transform/tsconfig.json +23 -63
- package/tsconfig.json +13 -13
- package/as-pect.asconfig.json +0 -24
- package/as-pect.config.js +0 -30
- package/assembly/__tests__/deserialize.spec.ts +0 -298
- package/assembly/__tests__/serialize.spec.ts +0 -395
- package/assembly/deserialize/box.ts +0 -17
- package/assembly/serialize/box.ts +0 -11
- package/transform/lib/index.old.js +0 -257
- package/transform/lib/types.js +0 -17
- package/transform/src/index.old.ts +0 -312
- /package/assembly/{src/sink.ts → sink.ts} +0 -0
|
@@ -1,395 +0,0 @@
|
|
|
1
|
-
import { JSON } from "..";
|
|
2
|
-
import {
|
|
3
|
-
describe,
|
|
4
|
-
expect
|
|
5
|
-
} from "as-test/assembly";
|
|
6
|
-
|
|
7
|
-
@json
|
|
8
|
-
class BaseObject {
|
|
9
|
-
a: string;
|
|
10
|
-
constructor(a: string) {
|
|
11
|
-
this.a = a;
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
@json
|
|
16
|
-
class DerivedObject extends BaseObject {
|
|
17
|
-
b: string;
|
|
18
|
-
constructor(a: string, b: string) {
|
|
19
|
-
super(a);
|
|
20
|
-
this.b = b;
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
@json
|
|
25
|
-
class Map4 {
|
|
26
|
-
a: string;
|
|
27
|
-
b: string;
|
|
28
|
-
c: string;
|
|
29
|
-
d: string;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
@json
|
|
33
|
-
class Vec3 {
|
|
34
|
-
x: f64;
|
|
35
|
-
y: f64;
|
|
36
|
-
z: f64;
|
|
37
|
-
|
|
38
|
-
static shouldIgnore: string = "should not be serialized";
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
@json
|
|
42
|
-
class Player {
|
|
43
|
-
firstName: string;
|
|
44
|
-
lastName: string;
|
|
45
|
-
lastActive: i32[];
|
|
46
|
-
age: i32;
|
|
47
|
-
pos: Vec3 | null;
|
|
48
|
-
isVerified: boolean;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
@json
|
|
52
|
-
class OmitIf {
|
|
53
|
-
x: i32 = 1;
|
|
54
|
-
@omitif("this.y == -1")
|
|
55
|
-
y: i32 = -1;
|
|
56
|
-
z: i32 = 1;
|
|
57
|
-
@omitnull()
|
|
58
|
-
foo: string | null = null
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
class Nullable { }
|
|
62
|
-
type Null = Nullable | null;
|
|
63
|
-
|
|
64
|
-
describe("Should serialize strings", () => {
|
|
65
|
-
|
|
66
|
-
expect(
|
|
67
|
-
JSON.stringify("abcdefg")
|
|
68
|
-
).toBe("\"abcdefg\"");
|
|
69
|
-
|
|
70
|
-
expect(
|
|
71
|
-
JSON.stringify('st"ring" w""ith quotes"')
|
|
72
|
-
).toBe('"st\\"ring\\" w\\"\\"ith quotes\\""');
|
|
73
|
-
|
|
74
|
-
expect(
|
|
75
|
-
JSON.stringify('string \"with random spa\nces and \nnewlines\n\n\n')
|
|
76
|
-
).toBe('"string \\"with random spa\\nces and \\nnewlines\\n\\n\\n"');
|
|
77
|
-
|
|
78
|
-
expect(
|
|
79
|
-
JSON.stringify('string with colon : comma , brace [ ] bracket { } and quote " and other quote \\"')
|
|
80
|
-
).toBe('"string with colon : comma , brace [ ] bracket { } and quote \\" and other quote \\\\\\""')
|
|
81
|
-
|
|
82
|
-
});
|
|
83
|
-
|
|
84
|
-
describe("Should serialize integers", () => {
|
|
85
|
-
|
|
86
|
-
expect(
|
|
87
|
-
JSON.stringify(0)
|
|
88
|
-
).toBe("0");
|
|
89
|
-
|
|
90
|
-
expect(
|
|
91
|
-
JSON.stringify<u32>(100)
|
|
92
|
-
).toBe("100");
|
|
93
|
-
|
|
94
|
-
expect(
|
|
95
|
-
JSON.stringify<u64>(101)
|
|
96
|
-
).toBe("101");
|
|
97
|
-
|
|
98
|
-
expect(
|
|
99
|
-
JSON.stringify<i32>(-100)
|
|
100
|
-
).toBe("-100");
|
|
101
|
-
|
|
102
|
-
expect(
|
|
103
|
-
JSON.stringify<i64>(-101)
|
|
104
|
-
).toBe("-101");
|
|
105
|
-
|
|
106
|
-
});
|
|
107
|
-
|
|
108
|
-
describe("Should serialize floats", () => {
|
|
109
|
-
|
|
110
|
-
expect(
|
|
111
|
-
JSON.stringify<f64>(7.23)
|
|
112
|
-
).toBe("7.23");
|
|
113
|
-
|
|
114
|
-
expect(
|
|
115
|
-
JSON.stringify<f64>(10e2)
|
|
116
|
-
).toBe("1000.0");
|
|
117
|
-
|
|
118
|
-
expect(
|
|
119
|
-
JSON.stringify<f64>(123456e-5)
|
|
120
|
-
).toBe("1.23456");
|
|
121
|
-
|
|
122
|
-
expect(
|
|
123
|
-
JSON.stringify<f64>(0.0)
|
|
124
|
-
).toBe("0.0");
|
|
125
|
-
|
|
126
|
-
expect(
|
|
127
|
-
JSON.stringify<f64>(-7.23)
|
|
128
|
-
).toBe("-7.23");
|
|
129
|
-
|
|
130
|
-
expect(
|
|
131
|
-
JSON.stringify<f64>(1e-6)
|
|
132
|
-
).toBe("0.000001");
|
|
133
|
-
|
|
134
|
-
expect(
|
|
135
|
-
JSON.stringify<f64>(1e-7)
|
|
136
|
-
).toBe("1e-7");
|
|
137
|
-
|
|
138
|
-
expect(
|
|
139
|
-
JSON.parse<f64>("1E-7")
|
|
140
|
-
).toBe(1e-7);
|
|
141
|
-
|
|
142
|
-
expect(
|
|
143
|
-
JSON.stringify<f64>(1e20)
|
|
144
|
-
).toBe("100000000000000000000.0");
|
|
145
|
-
|
|
146
|
-
expect(
|
|
147
|
-
JSON.stringify<f64>(1e21)
|
|
148
|
-
).toBe("1e+21");
|
|
149
|
-
|
|
150
|
-
expect(
|
|
151
|
-
JSON.parse<f64>("1E+21")
|
|
152
|
-
).toBe(1e21);
|
|
153
|
-
|
|
154
|
-
expect(
|
|
155
|
-
JSON.parse<f64>("1e21")
|
|
156
|
-
).toBe(1e21);
|
|
157
|
-
|
|
158
|
-
expect(
|
|
159
|
-
JSON.parse<f64>("1E21")
|
|
160
|
-
).toBe(1e21);
|
|
161
|
-
|
|
162
|
-
});
|
|
163
|
-
|
|
164
|
-
describe("Should serialize booleans", () => {
|
|
165
|
-
|
|
166
|
-
expect(
|
|
167
|
-
JSON.stringify<bool>(true)
|
|
168
|
-
).toBe("true");
|
|
169
|
-
|
|
170
|
-
expect(
|
|
171
|
-
JSON.stringify<bool>(false)
|
|
172
|
-
).toBe("false");
|
|
173
|
-
|
|
174
|
-
expect(
|
|
175
|
-
JSON.stringify<boolean>(true)
|
|
176
|
-
).toBe("true");
|
|
177
|
-
|
|
178
|
-
expect(
|
|
179
|
-
JSON.stringify<boolean>(false)
|
|
180
|
-
).toBe("false");
|
|
181
|
-
|
|
182
|
-
});
|
|
183
|
-
|
|
184
|
-
describe("Should serialize class inheritance", () => {
|
|
185
|
-
|
|
186
|
-
const obj = new DerivedObject("1", "2");
|
|
187
|
-
|
|
188
|
-
expect(
|
|
189
|
-
JSON.stringify(obj)
|
|
190
|
-
).toBe("{\"a\":\"1\",\"b\":\"2\"}");
|
|
191
|
-
|
|
192
|
-
});
|
|
193
|
-
|
|
194
|
-
describe("Should serialize nulls", () => {
|
|
195
|
-
|
|
196
|
-
expect(
|
|
197
|
-
JSON.stringify<Null>(null)
|
|
198
|
-
).toBe("null");
|
|
199
|
-
|
|
200
|
-
});
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
describe("Should serialize integer arrays", () => {
|
|
204
|
-
|
|
205
|
-
expect(
|
|
206
|
-
JSON.stringify<u32[]>([0, 100, 101])
|
|
207
|
-
).toBe("[0,100,101]");
|
|
208
|
-
|
|
209
|
-
expect(
|
|
210
|
-
JSON.stringify<u64[]>([0, 100, 101])
|
|
211
|
-
).toBe("[0,100,101]");
|
|
212
|
-
|
|
213
|
-
expect(
|
|
214
|
-
JSON.stringify<i32[]>([0, 100, 101, -100, -101])
|
|
215
|
-
).toBe("[0,100,101,-100,-101]");
|
|
216
|
-
|
|
217
|
-
expect(
|
|
218
|
-
JSON.stringify<i64[]>([0, 100, 101, -100, -101])
|
|
219
|
-
).toBe("[0,100,101,-100,-101]");
|
|
220
|
-
|
|
221
|
-
});
|
|
222
|
-
|
|
223
|
-
describe("Should serialize float arrays", () => {
|
|
224
|
-
|
|
225
|
-
expect(
|
|
226
|
-
JSON.stringify<f64[]>([7.23, 10e2, 10e2, 123456e-5, 123456e-5, 0.0, 7.23])
|
|
227
|
-
).toBe("[7.23,1000.0,1000.0,1.23456,1.23456,0.0,7.23]");
|
|
228
|
-
|
|
229
|
-
expect(
|
|
230
|
-
JSON.stringify<f64[]>([1e21, 1e22, 1e-7, 1e-8, 1e-9])
|
|
231
|
-
).toBe("[1e+21,1e+22,1e-7,1e-8,1e-9]");
|
|
232
|
-
|
|
233
|
-
});
|
|
234
|
-
|
|
235
|
-
describe("Should serialize boolean arrays", () => {
|
|
236
|
-
|
|
237
|
-
expect(
|
|
238
|
-
JSON.stringify<bool[]>([true, false])
|
|
239
|
-
).toBe("[true,false]");
|
|
240
|
-
|
|
241
|
-
expect(
|
|
242
|
-
JSON.stringify<boolean[]>([true, false])
|
|
243
|
-
).toBe("[true,false]");
|
|
244
|
-
|
|
245
|
-
});
|
|
246
|
-
|
|
247
|
-
describe("Should serialize string arrays", () => {
|
|
248
|
-
|
|
249
|
-
expect(
|
|
250
|
-
JSON.stringify<string[]>(['string \"with random spa\nces and \nnewlines\n\n\n'])
|
|
251
|
-
).toBe('["string \\"with random spa\\nces and \\nnewlines\\n\\n\\n"]');
|
|
252
|
-
|
|
253
|
-
});
|
|
254
|
-
|
|
255
|
-
describe("Should serialize nested integer arrays", () => {
|
|
256
|
-
|
|
257
|
-
expect(
|
|
258
|
-
JSON.stringify<i64[][]>([[100, 101], [-100, -101], [0]])
|
|
259
|
-
).toBe("[[100,101],[-100,-101],[0]]");
|
|
260
|
-
|
|
261
|
-
});
|
|
262
|
-
|
|
263
|
-
describe("Should serialize nested float arrays", () => {
|
|
264
|
-
|
|
265
|
-
expect(
|
|
266
|
-
JSON.stringify<f64[][]>([
|
|
267
|
-
[7.23],
|
|
268
|
-
[10e2],
|
|
269
|
-
[10e2],
|
|
270
|
-
[123456e-5],
|
|
271
|
-
[123456e-5],
|
|
272
|
-
[0.0],
|
|
273
|
-
[7.23],
|
|
274
|
-
])
|
|
275
|
-
).toBe("[[7.23],[1000.0],[1000.0],[1.23456],[1.23456],[0.0],[7.23]]");
|
|
276
|
-
|
|
277
|
-
});
|
|
278
|
-
|
|
279
|
-
describe("Should serialize nested boolean arrays", () => {
|
|
280
|
-
|
|
281
|
-
expect(
|
|
282
|
-
JSON.stringify<bool[][]>([[true], [false]])
|
|
283
|
-
).toBe("[[true],[false]]");
|
|
284
|
-
|
|
285
|
-
expect(
|
|
286
|
-
JSON.stringify<boolean[][]>([[true], [false]])
|
|
287
|
-
).toBe("[[true],[false]]");
|
|
288
|
-
|
|
289
|
-
});
|
|
290
|
-
|
|
291
|
-
describe("Should serialize object arrays", () => {
|
|
292
|
-
|
|
293
|
-
expect(
|
|
294
|
-
JSON.stringify<Vec3[]>([
|
|
295
|
-
{
|
|
296
|
-
x: 3.4,
|
|
297
|
-
y: 1.2,
|
|
298
|
-
z: 8.3,
|
|
299
|
-
},
|
|
300
|
-
{
|
|
301
|
-
x: 3.4,
|
|
302
|
-
y: -2.1,
|
|
303
|
-
z: 9.3,
|
|
304
|
-
},
|
|
305
|
-
])
|
|
306
|
-
).toBe('[{"x":3.4,"y":1.2,"z":8.3},{"x":3.4,"y":-2.1,"z":9.3}]');
|
|
307
|
-
|
|
308
|
-
});
|
|
309
|
-
|
|
310
|
-
describe("Should serialize objects", () => {
|
|
311
|
-
|
|
312
|
-
expect(
|
|
313
|
-
JSON.stringify<Vec3>({
|
|
314
|
-
x: 3.4,
|
|
315
|
-
y: 1.2,
|
|
316
|
-
z: 8.3,
|
|
317
|
-
})
|
|
318
|
-
).toBe('{"x":3.4,"y":1.2,"z":8.3}');
|
|
319
|
-
|
|
320
|
-
expect(
|
|
321
|
-
JSON.stringify<Player>({
|
|
322
|
-
firstName: "Emmet",
|
|
323
|
-
lastName: "West",
|
|
324
|
-
lastActive: [8, 27, 2022],
|
|
325
|
-
age: 23,
|
|
326
|
-
pos: {
|
|
327
|
-
x: 3.4,
|
|
328
|
-
y: 1.2,
|
|
329
|
-
z: 8.3,
|
|
330
|
-
},
|
|
331
|
-
isVerified: true,
|
|
332
|
-
})
|
|
333
|
-
).toBe('{"firstName":"Emmet","lastName":"West","lastActive":[8,27,2022],"age":23,"pos":{"x":3.4,"y":1.2,"z":8.3},"isVerified":true}');
|
|
334
|
-
|
|
335
|
-
expect(
|
|
336
|
-
JSON.stringify<ObjectWithFloat>({ f: 7.23 })
|
|
337
|
-
).toBe('{"f":7.23}');
|
|
338
|
-
|
|
339
|
-
expect(
|
|
340
|
-
JSON.stringify<ObjectWithFloat>({ f: 0.000001 })
|
|
341
|
-
).toBe('{"f":0.000001}');
|
|
342
|
-
|
|
343
|
-
expect(
|
|
344
|
-
JSON.stringify<ObjectWithFloat>({ f: 1e-7 })
|
|
345
|
-
).toBe('{"f":1e-7}');
|
|
346
|
-
|
|
347
|
-
expect(
|
|
348
|
-
JSON.stringify<ObjectWithFloat>({ f: 1e20 })
|
|
349
|
-
).toBe('{"f":100000000000000000000.0}');
|
|
350
|
-
|
|
351
|
-
expect(
|
|
352
|
-
JSON.stringify<ObjectWithFloat>({ f: 1e21 })
|
|
353
|
-
).toBe('{"f":1e+21}');
|
|
354
|
-
|
|
355
|
-
expect(
|
|
356
|
-
JSON.stringify<ObjWithStrangeKey<string>>({ data: "foo" })
|
|
357
|
-
).toBe('{"a\\\\\\t\\"\\u0002b`c":"foo"}');
|
|
358
|
-
|
|
359
|
-
});
|
|
360
|
-
|
|
361
|
-
describe("Should serialize @omit'ed objects", () => {
|
|
362
|
-
|
|
363
|
-
expect(
|
|
364
|
-
JSON.stringify(<OmitIf>{
|
|
365
|
-
y: 1
|
|
366
|
-
})
|
|
367
|
-
).toBe('{"x":1,"y":1,"z":1}');
|
|
368
|
-
|
|
369
|
-
});
|
|
370
|
-
|
|
371
|
-
@json
|
|
372
|
-
class ObjWithString {
|
|
373
|
-
s!: string;
|
|
374
|
-
}
|
|
375
|
-
|
|
376
|
-
@json
|
|
377
|
-
class ObjectWithStringArray {
|
|
378
|
-
sa!: string[];
|
|
379
|
-
}
|
|
380
|
-
|
|
381
|
-
@json
|
|
382
|
-
class ObjectWithFloat {
|
|
383
|
-
f!: f64;
|
|
384
|
-
}
|
|
385
|
-
|
|
386
|
-
@json
|
|
387
|
-
class ObjectWithFloatArray {
|
|
388
|
-
fa!: f64[];
|
|
389
|
-
}
|
|
390
|
-
|
|
391
|
-
@json
|
|
392
|
-
class ObjWithStrangeKey<T> {
|
|
393
|
-
@alias('a\\\t"\x02b`c')
|
|
394
|
-
data!: T;
|
|
395
|
-
}
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import { JSON } from "..";
|
|
2
|
-
|
|
3
|
-
// @ts-ignore: Decorator
|
|
4
|
-
export function deserializeBox<T extends Box<any>>(data: string): T {
|
|
5
|
-
if (isNullable<T>() && data == "null") {
|
|
6
|
-
return null;
|
|
7
|
-
}
|
|
8
|
-
const instance = changetype<nonnull<T>>(__new(offsetof<nonnull<T>>(), idof<nonnull<T>>()))// as Box<usize>;
|
|
9
|
-
const val = instance._val;
|
|
10
|
-
instance._val = parseDirectInference(val, data);
|
|
11
|
-
// @ts-ignore
|
|
12
|
-
return changetype<T>(instance);
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
function parseDirectInference<T>(type: T, data: string, initializeDefaultValues: boolean = false): T {
|
|
16
|
-
return JSON.parse<T>(data, initializeDefaultValues)
|
|
17
|
-
}
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import { NULL_WORD } from "../src/chars";
|
|
2
|
-
import { JSON } from "..";
|
|
3
|
-
import { Box } from "as-container";
|
|
4
|
-
|
|
5
|
-
// @ts-ignore
|
|
6
|
-
export function serializeBox<T extends Box<any>>(data: T): string {
|
|
7
|
-
if (isNullable<T>() && changetype<usize>(data) == <usize>0) {
|
|
8
|
-
return NULL_WORD;
|
|
9
|
-
}
|
|
10
|
-
return JSON.stringify(data.unwrap());
|
|
11
|
-
}
|
|
@@ -1,257 +0,0 @@
|
|
|
1
|
-
import { Parser, Source, Tokenizer, } from "assemblyscript/dist/assemblyscript.js";
|
|
2
|
-
import { toString, isStdlib } from "visitor-as/dist/utils.js";
|
|
3
|
-
import { BaseVisitor, SimpleParser } from "visitor-as/dist/index.js";
|
|
4
|
-
import { Transform } from "assemblyscript/dist/transform.js";
|
|
5
|
-
class SchemaData {
|
|
6
|
-
constructor() {
|
|
7
|
-
this.keys = [];
|
|
8
|
-
this.values = [];
|
|
9
|
-
this.types = [];
|
|
10
|
-
this.name = "";
|
|
11
|
-
this.parent = "";
|
|
12
|
-
this.encodeStmts = [];
|
|
13
|
-
this.setDataStmts = [];
|
|
14
|
-
this.initializeStmts = [];
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
class AsJSONTransform extends BaseVisitor {
|
|
18
|
-
constructor() {
|
|
19
|
-
super(...arguments);
|
|
20
|
-
this.schemasList = [];
|
|
21
|
-
this.sources = new Set();
|
|
22
|
-
}
|
|
23
|
-
visitMethodDeclaration() { }
|
|
24
|
-
visitClassDeclaration(node) {
|
|
25
|
-
const className = node.name.text;
|
|
26
|
-
if (!node.decorators?.length)
|
|
27
|
-
return;
|
|
28
|
-
let foundDecorator = false;
|
|
29
|
-
for (const decorator of node.decorators) {
|
|
30
|
-
if (
|
|
31
|
-
// @ts-ignore
|
|
32
|
-
decorator.name.text.toLowerCase() == "json" ||
|
|
33
|
-
// @ts-ignore
|
|
34
|
-
decorator.name.text.toLowerCase() == "serializable")
|
|
35
|
-
foundDecorator = true;
|
|
36
|
-
}
|
|
37
|
-
if (!foundDecorator)
|
|
38
|
-
return;
|
|
39
|
-
// Prevent from being triggered twice.
|
|
40
|
-
for (const member of node.members) {
|
|
41
|
-
if (member.name.text == "__SERIALIZE")
|
|
42
|
-
return;
|
|
43
|
-
}
|
|
44
|
-
this.currentClass = {
|
|
45
|
-
name: className,
|
|
46
|
-
keys: [],
|
|
47
|
-
values: [],
|
|
48
|
-
types: [],
|
|
49
|
-
parent: node.extendsType ? toString(node.extendsType) : "",
|
|
50
|
-
node: node,
|
|
51
|
-
encodeStmts: [],
|
|
52
|
-
setDataStmts: [],
|
|
53
|
-
initializeStmts: []
|
|
54
|
-
};
|
|
55
|
-
if (this.currentClass.parent.length) {
|
|
56
|
-
const parentSchema = this.schemasList.find((v) => v.name == this.currentClass.parent);
|
|
57
|
-
if (parentSchema?.encodeStmts) {
|
|
58
|
-
parentSchema?.encodeStmts.push(parentSchema?.encodeStmts.pop() + ",");
|
|
59
|
-
for (let i = 0; i < parentSchema.keys.length; i++) {
|
|
60
|
-
const key = parentSchema.keys[i];
|
|
61
|
-
if (node.members.filter(v => v.name.text == key) == undefined)
|
|
62
|
-
this.currentClass.encodeStmts.unshift(parentSchema.encodeStmts[i]);
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
const parentSchema = this.schemasList.find((v) => v.name == this.currentClass.parent);
|
|
67
|
-
const members = [
|
|
68
|
-
...node.members
|
|
69
|
-
];
|
|
70
|
-
if (parentSchema) {
|
|
71
|
-
for (const mem of parentSchema.node.members) {
|
|
72
|
-
if (members.find(v => v.name === mem.name) == undefined)
|
|
73
|
-
members.unshift(mem);
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
for (const mem of members) {
|
|
77
|
-
// @ts-ignore
|
|
78
|
-
if (mem.type && mem.type.name && mem.type.name.identifier.text) {
|
|
79
|
-
const member = mem;
|
|
80
|
-
const lineText = toString(member);
|
|
81
|
-
//console.log("Member: " + lineText)
|
|
82
|
-
if (!lineText.startsWith("private ") && !lineText.startsWith("static ")) {
|
|
83
|
-
// @ts-ignore
|
|
84
|
-
let type = toString(member.type);
|
|
85
|
-
const name = member.name.text;
|
|
86
|
-
let aliasName = name;
|
|
87
|
-
// @ts-ignore
|
|
88
|
-
if (member.decorators && member.decorators[0]?.name.text === "alias") {
|
|
89
|
-
if (member.decorators[0] && member.decorators[0].args[0]) {
|
|
90
|
-
// @ts-ignore
|
|
91
|
-
aliasName = member.decorators[0].args[0].value;
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
this.currentClass.keys.push(name);
|
|
95
|
-
// @ts-ignore
|
|
96
|
-
this.currentClass.types.push(type);
|
|
97
|
-
// @ts-ignore
|
|
98
|
-
if ([
|
|
99
|
-
"u8",
|
|
100
|
-
"i8",
|
|
101
|
-
"u16",
|
|
102
|
-
"i16",
|
|
103
|
-
"u32",
|
|
104
|
-
"i32",
|
|
105
|
-
"u64",
|
|
106
|
-
"i64",
|
|
107
|
-
].includes(type.toLowerCase())) {
|
|
108
|
-
this.currentClass.encodeStmts.push(`${encodeKey(aliasName)}:\${this.${name}},`);
|
|
109
|
-
// @ts-ignore
|
|
110
|
-
this.currentClass.setDataStmts.push(`if (key.equals(${JSON.stringify(aliasName)})) {
|
|
111
|
-
this.${name} = __atoi_fast<${type}>(data, val_start << 1, val_end << 1);
|
|
112
|
-
return;
|
|
113
|
-
}`);
|
|
114
|
-
if (member.initializer) {
|
|
115
|
-
this.currentClass.initializeStmts.push(`this.${name} = ${toString(member.initializer)}`);
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
else // @ts-ignore
|
|
119
|
-
if ([
|
|
120
|
-
"f32",
|
|
121
|
-
"f64",
|
|
122
|
-
].includes(type.toLowerCase())) {
|
|
123
|
-
this.currentClass.encodeStmts.push(`${encodeKey(aliasName)}:\${this.${name}},`);
|
|
124
|
-
// @ts-ignore
|
|
125
|
-
this.currentClass.setDataStmts.push(`if (key.equals(${JSON.stringify(aliasName)})) {
|
|
126
|
-
this.${name} = __parseObjectValue<${type}>(data.slice(val_start, val_end), initializeDefaultValues);
|
|
127
|
-
return;
|
|
128
|
-
}`);
|
|
129
|
-
if (member.initializer) {
|
|
130
|
-
this.currentClass.initializeStmts.push(`this.${name} = ${toString(member.initializer)}`);
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
else {
|
|
134
|
-
this.currentClass.encodeStmts.push(`${encodeKey(aliasName)}:\${__JSON_Stringify<${type}>(this.${name})},`);
|
|
135
|
-
// @ts-ignore
|
|
136
|
-
this.currentClass.setDataStmts.push(`if (key.equals(${JSON.stringify(aliasName)})) {
|
|
137
|
-
this.${name} = __parseObjectValue<${type}>(val_start ? data.slice(val_start, val_end) : data, initializeDefaultValues);
|
|
138
|
-
return;
|
|
139
|
-
}`);
|
|
140
|
-
if (member.initializer) {
|
|
141
|
-
this.currentClass.initializeStmts.push(`this.${name} = ${toString(member.initializer)}`);
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
let serializeFunc = "";
|
|
148
|
-
if (this.currentClass.encodeStmts.length > 0) {
|
|
149
|
-
const stmt = this.currentClass.encodeStmts[this.currentClass.encodeStmts.length - 1];
|
|
150
|
-
this.currentClass.encodeStmts[this.currentClass.encodeStmts.length - 1] =
|
|
151
|
-
stmt.slice(0, stmt.length - 1);
|
|
152
|
-
serializeFunc = `
|
|
153
|
-
__SERIALIZE(): string {
|
|
154
|
-
return \`{${this.currentClass.encodeStmts.join("")}}\`;
|
|
155
|
-
}`;
|
|
156
|
-
}
|
|
157
|
-
else {
|
|
158
|
-
serializeFunc = `
|
|
159
|
-
__SERIALIZE(): string {
|
|
160
|
-
return "{}";
|
|
161
|
-
}`;
|
|
162
|
-
}
|
|
163
|
-
const setKeyFunc = `
|
|
164
|
-
__JSON_Set_Key(key: __Virtual<string>, data: string, val_start: i32, val_end: i32, initializeDefaultValues: boolean): void {
|
|
165
|
-
${this.currentClass.setDataStmts.join("\n ")}
|
|
166
|
-
}
|
|
167
|
-
`;
|
|
168
|
-
let initializeFunc = "";
|
|
169
|
-
if (this.currentClass.initializeStmts.length > 0) {
|
|
170
|
-
initializeFunc = `
|
|
171
|
-
__JSON_Initialize(): void {
|
|
172
|
-
${this.currentClass.initializeStmts.join(";\n")};
|
|
173
|
-
}
|
|
174
|
-
`;
|
|
175
|
-
}
|
|
176
|
-
else {
|
|
177
|
-
initializeFunc = `
|
|
178
|
-
__JSON_Initialize(): void {}
|
|
179
|
-
`;
|
|
180
|
-
}
|
|
181
|
-
const serializeMethod = SimpleParser.parseClassMember(serializeFunc, node);
|
|
182
|
-
node.members.push(serializeMethod);
|
|
183
|
-
const setDataMethod = SimpleParser.parseClassMember(setKeyFunc, node);
|
|
184
|
-
node.members.push(setDataMethod);
|
|
185
|
-
const initializeMethod = SimpleParser.parseClassMember(initializeFunc, node);
|
|
186
|
-
node.members.push(initializeMethod);
|
|
187
|
-
this.schemasList.push(this.currentClass);
|
|
188
|
-
this.sources.add(node.name.range.source);
|
|
189
|
-
// Uncomment to see the generated code for debugging.
|
|
190
|
-
//console.log(serializeFunc);
|
|
191
|
-
//console.log(setKeyFunc);
|
|
192
|
-
//console.log(initializeFunc);
|
|
193
|
-
}
|
|
194
|
-
visitSource(node) {
|
|
195
|
-
super.visitSource(node);
|
|
196
|
-
// Only add the import statement to sources that have JSON decorated classes.
|
|
197
|
-
if (!this.sources.has(node)) {
|
|
198
|
-
return;
|
|
199
|
-
}
|
|
200
|
-
// Note, the following one liner would be easier, but it fails with an assertion error
|
|
201
|
-
// because as-virtual's SimpleParser doesn't set the parser.currentSource correctly.
|
|
202
|
-
//
|
|
203
|
-
// const stmt = SimpleParser.parseTopLevelStatement('import { Virtual as __Virtual } from "as-virtual/assembly";');
|
|
204
|
-
// ... So we have to do it the long way:
|
|
205
|
-
const txt = 'import { Virtual as __Virtual } from "as-virtual/assembly";';
|
|
206
|
-
const tokenizer = new Tokenizer(new Source(0 /* SourceKind.User */, node.normalizedPath, txt));
|
|
207
|
-
const parser = new Parser();
|
|
208
|
-
parser.currentSource = tokenizer.source;
|
|
209
|
-
const stmt = parser.parseTopLevelStatement(tokenizer);
|
|
210
|
-
// Add the import statement to the top of the source.
|
|
211
|
-
node.statements.unshift(stmt);
|
|
212
|
-
}
|
|
213
|
-
}
|
|
214
|
-
function encodeKey(aliasName) {
|
|
215
|
-
return JSON.stringify(aliasName)
|
|
216
|
-
.replace(/\\/g, "\\\\")
|
|
217
|
-
.replace(/\`/g, '\\`');
|
|
218
|
-
}
|
|
219
|
-
export default class Transformer extends Transform {
|
|
220
|
-
// Trigger the transform after parse.
|
|
221
|
-
afterParse(parser) {
|
|
222
|
-
// Create new transform
|
|
223
|
-
const transformer = new AsJSONTransform();
|
|
224
|
-
// Sort the sources so that user scripts are visited last
|
|
225
|
-
const sources = parser.sources
|
|
226
|
-
.filter((source) => !isStdlib(source))
|
|
227
|
-
.sort((_a, _b) => {
|
|
228
|
-
const a = _a.internalPath;
|
|
229
|
-
const b = _b.internalPath;
|
|
230
|
-
if (a[0] === "~" && b[0] !== "~") {
|
|
231
|
-
return -1;
|
|
232
|
-
}
|
|
233
|
-
else if (a[0] !== "~" && b[0] === "~") {
|
|
234
|
-
return 1;
|
|
235
|
-
}
|
|
236
|
-
else {
|
|
237
|
-
return 0;
|
|
238
|
-
}
|
|
239
|
-
});
|
|
240
|
-
// Loop over every source
|
|
241
|
-
for (const source of sources) {
|
|
242
|
-
// Ignore all lib and std. Visit everything else.
|
|
243
|
-
if (!isStdlib(source)) {
|
|
244
|
-
transformer.visit(source);
|
|
245
|
-
}
|
|
246
|
-
}
|
|
247
|
-
// Check that every parent and child class is hooked up correctly
|
|
248
|
-
const schemas = transformer.schemasList;
|
|
249
|
-
for (const schema of schemas) {
|
|
250
|
-
if (schema.parent) {
|
|
251
|
-
const parent = schemas.find((v) => v.name === schema.parent);
|
|
252
|
-
if (!parent)
|
|
253
|
-
throw new Error(`Class ${schema.name} extends its parent class ${schema.parent}, but ${schema.parent} does not include a @json or @serializable decorator! Add the decorator and rebuild.`);
|
|
254
|
-
}
|
|
255
|
-
}
|
|
256
|
-
}
|
|
257
|
-
}
|
package/transform/lib/types.js
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
export class SchemaMember {
|
|
2
|
-
constructor() {
|
|
3
|
-
this.key = "";
|
|
4
|
-
this.type = "";
|
|
5
|
-
this.value = null;
|
|
6
|
-
this.serialize = null;
|
|
7
|
-
this.deserialize = null;
|
|
8
|
-
this.initialize = null;
|
|
9
|
-
}
|
|
10
|
-
}
|
|
11
|
-
export class SchemaData {
|
|
12
|
-
constructor() {
|
|
13
|
-
this.name = "";
|
|
14
|
-
this.members = [];
|
|
15
|
-
this.parent = null;
|
|
16
|
-
}
|
|
17
|
-
}
|