json-as 0.9.19 → 0.9.20
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/CHANGELOG +1 -0
- package/README.md +1 -1
- package/package.json +1 -1
- package/transform/lib/index.js +492 -470
- package/transform/package.json +1 -1
- package/transform/src/index.ts +21 -1
package/CHANGELOG
CHANGED
|
@@ -27,6 +27,7 @@ v0.9.16 - JSON.Raw should be completely untouched
|
|
|
27
27
|
v0.9.17 - A schema's parent's fields should be included properly
|
|
28
28
|
v0.9.18 - Should be able to use @alias and @omit*** or JSON.Raw
|
|
29
29
|
v0.9.19 - Fix arguments in @omitif declarations not working properly
|
|
30
|
+
v0.9.20 - Strings were being received with quotes attached via the toString functionality. Removed that.
|
|
30
31
|
|
|
31
32
|
[UNRELEASED] v1.0.0
|
|
32
33
|
- Allow nullable primitives
|
package/README.md
CHANGED
package/package.json
CHANGED
package/transform/lib/index.js
CHANGED
|
@@ -1,512 +1,534 @@
|
|
|
1
|
-
import { FieldDeclaration } from "assemblyscript/dist/assemblyscript.js";
|
|
1
|
+
import { FieldDeclaration, IdentifierExpression, StringLiteralExpression, IntegerLiteralExpression, FloatLiteralExpression, NullExpression, TrueExpression, FalseExpression, } from "assemblyscript/dist/assemblyscript.js";
|
|
2
2
|
import { toString, isStdlib } from "visitor-as/dist/utils.js";
|
|
3
3
|
import { BaseVisitor, SimpleParser } from "visitor-as/dist/index.js";
|
|
4
4
|
import { Transform } from "assemblyscript/dist/transform.js";
|
|
5
5
|
class JSONTransform extends BaseVisitor {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
}
|
|
11
|
-
visitMethodDeclaration() {}
|
|
12
|
-
visitClassDeclaration(node) {
|
|
13
|
-
if (!node.decorators?.length) return;
|
|
14
|
-
let found = false;
|
|
15
|
-
for (const decorator of node.decorators) {
|
|
16
|
-
const name = decorator.name.text;
|
|
17
|
-
if (name === "json" || name === "serializable") {
|
|
18
|
-
found = true;
|
|
19
|
-
break;
|
|
20
|
-
}
|
|
6
|
+
constructor() {
|
|
7
|
+
super(...arguments);
|
|
8
|
+
this.schemasList = [];
|
|
9
|
+
this.sources = new Set();
|
|
21
10
|
}
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
(v) => v.name == node.extendsType?.name.identifier.text,
|
|
34
|
-
);
|
|
35
|
-
if (schema.parent?.members) {
|
|
36
|
-
for (let i = schema.parent.members.length - 1; i >= 0; i--) {
|
|
37
|
-
const replace = schema.members.find(
|
|
38
|
-
(v) => v.name == schema.parent?.members[i]?.name,
|
|
39
|
-
);
|
|
40
|
-
if (!replace) {
|
|
41
|
-
members.unshift(schema.parent?.members[i].node);
|
|
42
|
-
}
|
|
11
|
+
visitMethodDeclaration() { }
|
|
12
|
+
visitClassDeclaration(node) {
|
|
13
|
+
if (!node.decorators?.length)
|
|
14
|
+
return;
|
|
15
|
+
let found = false;
|
|
16
|
+
for (const decorator of node.decorators) {
|
|
17
|
+
const name = decorator.name.text;
|
|
18
|
+
if (name === "json" || name === "serializable") {
|
|
19
|
+
found = true;
|
|
20
|
+
break;
|
|
21
|
+
}
|
|
43
22
|
}
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
//const SERIALIZE_PRETTY_METHOD = SimpleParser.parseClassMember(SERIALIZE_PRETTY, node);
|
|
63
|
-
const INITIALIZE_METHOD_EMPTY = SimpleParser.parseClassMember(
|
|
64
|
-
INITIALIZE_EMPTY,
|
|
65
|
-
node,
|
|
66
|
-
);
|
|
67
|
-
const DESERIALIZE_METHOD_EMPTY = SimpleParser.parseClassMember(
|
|
68
|
-
DESERIALIZE_EMPTY,
|
|
69
|
-
node,
|
|
70
|
-
);
|
|
71
|
-
if (!node.members.find((v) => v.name.text == "__SERIALIZE"))
|
|
72
|
-
node.members.push(SERIALIZE_RAW_METHOD_EMPTY);
|
|
73
|
-
if (!node.members.find((v) => v.name.text == "__INITIALIZE"))
|
|
74
|
-
node.members.push(INITIALIZE_METHOD_EMPTY);
|
|
75
|
-
if (!node.members.find((v) => v.name.text == "__DESERIALIZE"))
|
|
76
|
-
node.members.push(DESERIALIZE_METHOD_EMPTY);
|
|
77
|
-
this.schemasList.push(schema);
|
|
78
|
-
}
|
|
79
|
-
for (const member of members) {
|
|
80
|
-
const name = member.name;
|
|
81
|
-
if (!(member instanceof FieldDeclaration)) continue;
|
|
82
|
-
if (!member.type) {
|
|
83
|
-
throw new Error(
|
|
84
|
-
"Fields must be strongly typed! Found " +
|
|
85
|
-
toString(member) +
|
|
86
|
-
" at " +
|
|
87
|
-
node.range.source.normalizedPath,
|
|
88
|
-
);
|
|
89
|
-
}
|
|
90
|
-
const type = toString(member.type);
|
|
91
|
-
if (type.startsWith("(") && type.includes("=>")) continue;
|
|
92
|
-
const value = member.initializer ? toString(member.initializer) : null;
|
|
93
|
-
if (member.flags == 32 /* CommonFlags.Static */) continue;
|
|
94
|
-
if (member.flags === 512 /* CommonFlags.Private */) continue;
|
|
95
|
-
if (member.flags === 1024 /* CommonFlags.Protected */) continue;
|
|
96
|
-
const mem = new Property();
|
|
97
|
-
mem.name = name.text;
|
|
98
|
-
mem.type = type;
|
|
99
|
-
mem.value = value;
|
|
100
|
-
mem.node = member;
|
|
101
|
-
if (type == "JSON.Raw") {
|
|
102
|
-
mem.flags.set(PropertyFlags.JSON_Raw, []);
|
|
103
|
-
}
|
|
104
|
-
if (member.decorators) {
|
|
105
|
-
for (const decorator of member.decorators) {
|
|
106
|
-
const decoratorName = decorator.name.text;
|
|
107
|
-
const args = getArgs(decorator.args);
|
|
108
|
-
switch (decoratorName) {
|
|
109
|
-
case "alias": {
|
|
110
|
-
if (!args.length)
|
|
111
|
-
throw new Error(
|
|
112
|
-
"Expected 1 argument but got zero at @alias in " +
|
|
113
|
-
node.range.source.normalizedPath,
|
|
114
|
-
);
|
|
115
|
-
mem.alias = args[0];
|
|
116
|
-
mem.flags.set(PropertyFlags.Alias, args);
|
|
117
|
-
break;
|
|
118
|
-
}
|
|
119
|
-
case "omit": {
|
|
120
|
-
mem.flags.set(PropertyFlags.Omit, args);
|
|
121
|
-
break;
|
|
122
|
-
}
|
|
123
|
-
case "omitif": {
|
|
124
|
-
if (!decorator.args?.length)
|
|
125
|
-
throw new Error(
|
|
126
|
-
"Expected 1 argument but got zero at @omitif in " +
|
|
127
|
-
node.range.source.normalizedPath,
|
|
128
|
-
);
|
|
129
|
-
mem.flags.set(PropertyFlags.OmitIf, args);
|
|
130
|
-
break;
|
|
131
|
-
}
|
|
132
|
-
case "omitnull": {
|
|
133
|
-
mem.flags.set(PropertyFlags.OmitNull, args);
|
|
134
|
-
break;
|
|
135
|
-
}
|
|
136
|
-
}
|
|
23
|
+
if (!found)
|
|
24
|
+
return;
|
|
25
|
+
const schema = new SchemaData();
|
|
26
|
+
schema.node = node;
|
|
27
|
+
schema.name = node.name.text;
|
|
28
|
+
const members = [
|
|
29
|
+
...node.members.filter((v) => v.kind === 54 /* NodeKind.FieldDeclaration */),
|
|
30
|
+
];
|
|
31
|
+
if (node.extendsType) {
|
|
32
|
+
schema.parent = this.schemasList.find((v) => v.name == node.extendsType?.name.identifier.text);
|
|
33
|
+
if (schema.parent?.members) {
|
|
34
|
+
for (let i = schema.parent.members.length - 1; i >= 0; i--) {
|
|
35
|
+
const replace = schema.members.find((v) => v.name == schema.parent?.members[i]?.name);
|
|
36
|
+
if (!replace) {
|
|
37
|
+
members.unshift(schema.parent?.members[i].node);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|
|
137
41
|
}
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
42
|
+
if (!members.length) {
|
|
43
|
+
let SERIALIZE_RAW_EMPTY = '__SERIALIZE(): string {\n return "{}";\n}';
|
|
44
|
+
//let SERIALIZE_PRETTY_EMPTY = "__SERIALIZE_PRETTY(): string {\n return \"{}\";\n}";
|
|
45
|
+
let INITIALIZE_EMPTY = "__INITIALIZE(): this {\n return this;\n}";
|
|
46
|
+
let DESERIALIZE_EMPTY = "__DESERIALIZE(data: string, key_start: i32, key_end: i32, value_start: i32, value_end: i32): boolean {\n return false;\n}";
|
|
47
|
+
if (process.env["JSON_DEBUG"]) {
|
|
48
|
+
console.log(SERIALIZE_RAW_EMPTY);
|
|
49
|
+
//console.log(SERIALIZE_PRETTY_EMPTY);
|
|
50
|
+
console.log(INITIALIZE_EMPTY);
|
|
51
|
+
console.log(DESERIALIZE_EMPTY);
|
|
52
|
+
}
|
|
53
|
+
const SERIALIZE_RAW_METHOD_EMPTY = SimpleParser.parseClassMember(SERIALIZE_RAW_EMPTY, node);
|
|
54
|
+
//const SERIALIZE_PRETTY_METHOD = SimpleParser.parseClassMember(SERIALIZE_PRETTY, node);
|
|
55
|
+
const INITIALIZE_METHOD_EMPTY = SimpleParser.parseClassMember(INITIALIZE_EMPTY, node);
|
|
56
|
+
const DESERIALIZE_METHOD_EMPTY = SimpleParser.parseClassMember(DESERIALIZE_EMPTY, node);
|
|
57
|
+
if (!node.members.find((v) => v.name.text == "__SERIALIZE"))
|
|
58
|
+
node.members.push(SERIALIZE_RAW_METHOD_EMPTY);
|
|
59
|
+
if (!node.members.find((v) => v.name.text == "__INITIALIZE"))
|
|
60
|
+
node.members.push(INITIALIZE_METHOD_EMPTY);
|
|
61
|
+
if (!node.members.find((v) => v.name.text == "__DESERIALIZE"))
|
|
62
|
+
node.members.push(DESERIALIZE_METHOD_EMPTY);
|
|
63
|
+
this.schemasList.push(schema);
|
|
64
|
+
}
|
|
65
|
+
for (const member of members) {
|
|
66
|
+
const name = member.name;
|
|
67
|
+
if (!(member instanceof FieldDeclaration))
|
|
68
|
+
continue;
|
|
69
|
+
if (!member.type) {
|
|
70
|
+
throw new Error("Fields must be strongly typed! Found " +
|
|
71
|
+
toString(member) +
|
|
72
|
+
" at " +
|
|
73
|
+
node.range.source.normalizedPath);
|
|
74
|
+
}
|
|
75
|
+
const type = toString(member.type);
|
|
76
|
+
if (type.startsWith("(") && type.includes("=>"))
|
|
77
|
+
continue;
|
|
78
|
+
const value = member.initializer ? toString(member.initializer) : null;
|
|
79
|
+
if (member.flags == 32 /* CommonFlags.Static */)
|
|
80
|
+
continue;
|
|
81
|
+
if (member.flags === 512 /* CommonFlags.Private */)
|
|
82
|
+
continue;
|
|
83
|
+
if (member.flags === 1024 /* CommonFlags.Protected */)
|
|
84
|
+
continue;
|
|
85
|
+
const mem = new Property();
|
|
86
|
+
mem.name = name.text;
|
|
87
|
+
mem.type = type;
|
|
88
|
+
mem.value = value;
|
|
89
|
+
mem.node = member;
|
|
90
|
+
if (type == "JSON.Raw") {
|
|
91
|
+
mem.flags.set(PropertyFlags.JSON_Raw, []);
|
|
92
|
+
}
|
|
93
|
+
if (member.decorators) {
|
|
94
|
+
for (const decorator of member.decorators) {
|
|
95
|
+
const decoratorName = decorator.name.text;
|
|
96
|
+
const args = getArgs(decorator.args);
|
|
97
|
+
switch (decoratorName) {
|
|
98
|
+
case "alias": {
|
|
99
|
+
if (!args.length)
|
|
100
|
+
throw new Error("Expected 1 argument but got zero at @alias in " +
|
|
101
|
+
node.range.source.normalizedPath);
|
|
102
|
+
mem.alias = args[0];
|
|
103
|
+
mem.flags.set(PropertyFlags.Alias, args);
|
|
104
|
+
break;
|
|
105
|
+
}
|
|
106
|
+
case "omit": {
|
|
107
|
+
mem.flags.set(PropertyFlags.Omit, args);
|
|
108
|
+
break;
|
|
109
|
+
}
|
|
110
|
+
case "omitif": {
|
|
111
|
+
if (!decorator.args?.length)
|
|
112
|
+
throw new Error("Expected 1 argument but got zero at @omitif in " +
|
|
113
|
+
node.range.source.normalizedPath);
|
|
114
|
+
mem.flags.set(PropertyFlags.OmitIf, args);
|
|
115
|
+
break;
|
|
116
|
+
}
|
|
117
|
+
case "omitnull": {
|
|
118
|
+
mem.flags.set(PropertyFlags.OmitNull, args);
|
|
119
|
+
break;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
mem.generate();
|
|
125
|
+
if (this.schemasList.find((v) => v.name == type)) {
|
|
126
|
+
mem.initialize =
|
|
127
|
+
"this." +
|
|
128
|
+
name.text +
|
|
129
|
+
" = changetype<nonnull<" +
|
|
130
|
+
mem.type +
|
|
131
|
+
">>(__new(offsetof<nonnull<" +
|
|
132
|
+
mem.type +
|
|
133
|
+
">>(), idof<nonnull<" +
|
|
134
|
+
mem.type +
|
|
135
|
+
">>()));\n changetype<nonnull<" +
|
|
136
|
+
mem.type +
|
|
137
|
+
">>(this." +
|
|
138
|
+
name.text +
|
|
139
|
+
").__INITIALIZE()";
|
|
140
|
+
}
|
|
141
|
+
else if (mem.value) {
|
|
142
|
+
mem.initialize = "this." + name.text + " = " + mem.value;
|
|
143
|
+
}
|
|
144
|
+
else if (type === "Map") {
|
|
145
|
+
mem.initialize = "this." + name.text + " = new " + mem.type + "()";
|
|
146
|
+
}
|
|
147
|
+
else if (type === "string") {
|
|
148
|
+
mem.initialize = "this." + name.text + ' = ""';
|
|
149
|
+
}
|
|
150
|
+
else if (type === "Array") {
|
|
151
|
+
mem.initialize =
|
|
152
|
+
"this." + name.text + " = instantiate<" + mem.type + ">()";
|
|
153
|
+
}
|
|
154
|
+
else if (type === "bool" || type === "boolean") {
|
|
155
|
+
mem.initialize = "this." + name.text + " = false";
|
|
156
|
+
}
|
|
157
|
+
else if (type === "JSON.Raw") {
|
|
158
|
+
mem.initialize = "this." + name.text + ' = ""';
|
|
159
|
+
}
|
|
160
|
+
else if (type === "u8" ||
|
|
161
|
+
type === "u16" ||
|
|
162
|
+
type === "u32" ||
|
|
163
|
+
type === "u64" ||
|
|
164
|
+
type === "i8" ||
|
|
165
|
+
type === "i16" ||
|
|
166
|
+
type === "i32" ||
|
|
167
|
+
type === "i64") {
|
|
168
|
+
mem.initialize = "this." + name.text + " = 0";
|
|
169
|
+
}
|
|
170
|
+
else if (type === "f32" || type === "f64") {
|
|
171
|
+
mem.initialize = "this." + name.text + " = 0.0";
|
|
172
|
+
}
|
|
173
|
+
schema.members.push(mem);
|
|
174
|
+
}
|
|
175
|
+
let SERIALIZE_RAW = "__SERIALIZE(): string {\n let out = `{";
|
|
176
|
+
let SERIALIZE_PRETTY = "__SERIALIZE_PRETTY(): string {\n let out = `{";
|
|
177
|
+
let INITIALIZE = "__INITIALIZE(): this {\n";
|
|
178
|
+
let DESERIALIZE = "__DESERIALIZE(data: string, key_start: i32, key_end: i32, value_start: i32, value_end: i32): boolean {\n const len = key_end - key_start;\n";
|
|
179
|
+
let indent = " ";
|
|
180
|
+
if (!schema.members.length)
|
|
181
|
+
return;
|
|
182
|
+
found = false;
|
|
183
|
+
if (schema.members[0]?.flags.has(PropertyFlags.OmitNull) ||
|
|
184
|
+
schema.members[0]?.flags.has(PropertyFlags.OmitIf)) {
|
|
185
|
+
SERIALIZE_RAW += schema.members[0]?.serialize;
|
|
186
|
+
SERIALIZE_PRETTY += "\\n" + schema.members[0]?.serialize;
|
|
260
187
|
}
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
first = false;
|
|
266
|
-
} else {
|
|
267
|
-
DESERIALIZE +=
|
|
268
|
-
"else if (2 === len) {\n switch (load<u32>(changetype<usize>(data) + (key_start << 1))) {\n";
|
|
188
|
+
else {
|
|
189
|
+
SERIALIZE_RAW += schema.members[0]?.serialize + ",";
|
|
190
|
+
SERIALIZE_PRETTY += "\\n" + schema.members[0]?.serialize + ",\\n";
|
|
191
|
+
found = true;
|
|
269
192
|
}
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
193
|
+
if (schema.members[0]?.initialize)
|
|
194
|
+
INITIALIZE += " " + schema.members[0]?.initialize + ";\n";
|
|
195
|
+
for (let i = 1; i < schema.members.length; i++) {
|
|
196
|
+
const member = schema.members[i];
|
|
197
|
+
if (member.initialize)
|
|
198
|
+
INITIALIZE += " " + member.initialize + ";\n";
|
|
199
|
+
if (member.flags.has(PropertyFlags.OmitNull) ||
|
|
200
|
+
member.flags.has(PropertyFlags.OmitIf)) {
|
|
201
|
+
SERIALIZE_RAW += member.serialize;
|
|
202
|
+
SERIALIZE_PRETTY += member.serialize;
|
|
203
|
+
}
|
|
204
|
+
else {
|
|
205
|
+
SERIALIZE_RAW += member.serialize + ",";
|
|
206
|
+
SERIALIZE_PRETTY += indent + member.serialize + ",\\n";
|
|
207
|
+
found = true;
|
|
208
|
+
}
|
|
278
209
|
}
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
DESERIALIZE += "else if (" + name.length + " === len) {\n";
|
|
210
|
+
if (found) {
|
|
211
|
+
SERIALIZE_RAW +=
|
|
212
|
+
"`;\n store<u16>(changetype<usize>(out) + ((out.length - 1) << 1), 125);\n return out;\n}";
|
|
213
|
+
SERIALIZE_PRETTY +=
|
|
214
|
+
"`;\n store<u32>(changetype<usize>(out) + ((out.length - 2) << 1), 8192010);\n return out;\n}";
|
|
285
215
|
}
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
const member = memberSet[i];
|
|
290
|
-
if (!member.deserialize) continue;
|
|
291
|
-
const name = encodeKey(member.alias || member.name);
|
|
292
|
-
if (name.length === 1) {
|
|
293
|
-
DESERIALIZE += ` case ${name.charCodeAt(0)}: {\n ${member.deserialize}\n return true;\n }\n`;
|
|
294
|
-
} else if (name.length === 2) {
|
|
295
|
-
DESERIALIZE += ` case ${charCodeAt32(name, 0)}: {\n ${member.deserialize}\n return true;\n }\n`;
|
|
296
|
-
} else if (name.length === 4) {
|
|
297
|
-
if (f) {
|
|
298
|
-
f = false;
|
|
299
|
-
DESERIALIZE += ` if (${charCodeAt64(name, 0)} === code) {\n ${member.deserialize}\n return true;\n }\n`;
|
|
300
|
-
} else {
|
|
301
|
-
DESERIALIZE =
|
|
302
|
-
DESERIALIZE.slice(0, DESERIALIZE.length - 1) +
|
|
303
|
-
`else if (${charCodeAt64(name, 0)} === code) {\n ${member.deserialize}\n return true;\n }\n`;
|
|
304
|
-
}
|
|
305
|
-
} else {
|
|
306
|
-
if (f) {
|
|
307
|
-
f = false;
|
|
308
|
-
DESERIALIZE += ` if (0 == memory.compare(changetype<usize>("${escapeQuote(escapeSlash(name))}"), changetype<usize>(data) + (key_start << 1), ${name.length << 1})) {\n ${member.deserialize}\n return true;\n }\n`;
|
|
309
|
-
} else {
|
|
310
|
-
DESERIALIZE =
|
|
311
|
-
DESERIALIZE.slice(0, DESERIALIZE.length - 1) +
|
|
312
|
-
` else if (0 == memory.compare(changetype<usize>("${escapeQuote(escapeSlash(name))}"), changetype<usize>(data) + (key_start << 1), ${name.length << 1})) {\n ${member.deserialize}\n return true;\n }\n`;
|
|
313
|
-
}
|
|
216
|
+
else {
|
|
217
|
+
SERIALIZE_RAW += "`;\n};";
|
|
218
|
+
SERIALIZE_PRETTY += "`;\n};";
|
|
314
219
|
}
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
220
|
+
INITIALIZE += " return this;\n}";
|
|
221
|
+
const sortedMembers = [];
|
|
222
|
+
const _sorted = schema.members.sort((a, b) => a.name.length - b.name.length);
|
|
223
|
+
let len = 0;
|
|
224
|
+
let offset = 0;
|
|
225
|
+
sortedMembers.push([_sorted[0]]);
|
|
226
|
+
len = _sorted[0]?.name.length;
|
|
227
|
+
for (let i = 1; i < _sorted.length; i++) {
|
|
228
|
+
const member = _sorted[i];
|
|
229
|
+
if (member.alias?.length || member.name.length > len) {
|
|
230
|
+
sortedMembers.push([member]);
|
|
231
|
+
len = member.alias?.length || member.name.length;
|
|
232
|
+
offset++;
|
|
233
|
+
}
|
|
234
|
+
else {
|
|
235
|
+
sortedMembers[offset].push(member);
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
let first = true;
|
|
239
|
+
for (const memberSet of sortedMembers) {
|
|
240
|
+
const firstMember = memberSet[0];
|
|
241
|
+
const name = encodeKey(firstMember.alias || firstMember.name);
|
|
242
|
+
if (name.length === 1) {
|
|
243
|
+
if (first) {
|
|
244
|
+
DESERIALIZE +=
|
|
245
|
+
" if (1 === len) {\n switch (load<u16>(changetype<usize>(data) + (key_start << 1))) {\n";
|
|
246
|
+
first = false;
|
|
247
|
+
}
|
|
248
|
+
else {
|
|
249
|
+
DESERIALIZE +=
|
|
250
|
+
"else if (1 === len) {\n switch (load<u16>(changetype<usize>(data) + (key_start << 1))) {\n";
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
else if (name.length === 2) {
|
|
254
|
+
if (first) {
|
|
255
|
+
DESERIALIZE +=
|
|
256
|
+
" if (2 === len) {\n switch (load<u32>(changetype<usize>(data) + (key_start << 1))) {\n";
|
|
257
|
+
first = false;
|
|
258
|
+
}
|
|
259
|
+
else {
|
|
260
|
+
DESERIALIZE +=
|
|
261
|
+
"else if (2 === len) {\n switch (load<u32>(changetype<usize>(data) + (key_start << 1))) {\n";
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
else if (name.length === 4) {
|
|
265
|
+
if (first) {
|
|
266
|
+
DESERIALIZE +=
|
|
267
|
+
" if (4 === len) {\n const code = load<u64>(changetype<usize>(data) + (key_start << 1));\n";
|
|
268
|
+
first = false;
|
|
269
|
+
}
|
|
270
|
+
else {
|
|
271
|
+
DESERIALIZE +=
|
|
272
|
+
"else if (4 === len) {\n const code = load<u64>(changetype<usize>(data) + (key_start << 1));\n";
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
else {
|
|
276
|
+
if (first) {
|
|
277
|
+
DESERIALIZE += " if (" + name.length + " === len) {\n";
|
|
278
|
+
first = false;
|
|
279
|
+
}
|
|
280
|
+
else {
|
|
281
|
+
DESERIALIZE += "else if (" + name.length + " === len) {\n";
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
let f = true;
|
|
285
|
+
for (let i = 0; i < memberSet.length; i++) {
|
|
286
|
+
const member = memberSet[i];
|
|
287
|
+
if (!member.deserialize)
|
|
288
|
+
continue;
|
|
289
|
+
const name = encodeKey(member.alias || member.name);
|
|
290
|
+
if (name.length === 1) {
|
|
291
|
+
DESERIALIZE += ` case ${name.charCodeAt(0)}: {\n ${member.deserialize}\n return true;\n }\n`;
|
|
292
|
+
}
|
|
293
|
+
else if (name.length === 2) {
|
|
294
|
+
DESERIALIZE += ` case ${charCodeAt32(name, 0)}: {\n ${member.deserialize}\n return true;\n }\n`;
|
|
295
|
+
}
|
|
296
|
+
else if (name.length === 4) {
|
|
297
|
+
if (f) {
|
|
298
|
+
f = false;
|
|
299
|
+
DESERIALIZE += ` if (${charCodeAt64(name, 0)} === code) {\n ${member.deserialize}\n return true;\n }\n`;
|
|
300
|
+
}
|
|
301
|
+
else {
|
|
302
|
+
DESERIALIZE =
|
|
303
|
+
DESERIALIZE.slice(0, DESERIALIZE.length - 1) +
|
|
304
|
+
`else if (${charCodeAt64(name, 0)} === code) {\n ${member.deserialize}\n return true;\n }\n`;
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
else {
|
|
308
|
+
if (f) {
|
|
309
|
+
f = false;
|
|
310
|
+
DESERIALIZE += ` if (0 == memory.compare(changetype<usize>("${escapeQuote(escapeSlash(name))}"), changetype<usize>(data) + (key_start << 1), ${name.length << 1})) {\n ${member.deserialize}\n return true;\n }\n`;
|
|
311
|
+
}
|
|
312
|
+
else {
|
|
313
|
+
DESERIALIZE =
|
|
314
|
+
DESERIALIZE.slice(0, DESERIALIZE.length - 1) +
|
|
315
|
+
` else if (0 == memory.compare(changetype<usize>("${escapeQuote(escapeSlash(name))}"), changetype<usize>(data) + (key_start << 1), ${name.length << 1})) {\n ${member.deserialize}\n return true;\n }\n`;
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
if (name.length < 3) {
|
|
320
|
+
DESERIALIZE += ` default: {\n return false;\n }\n }\n`;
|
|
321
|
+
}
|
|
322
|
+
else if (name.length == 4) {
|
|
323
|
+
DESERIALIZE =
|
|
324
|
+
DESERIALIZE.slice(0, DESERIALIZE.length - 1) +
|
|
325
|
+
` else {\n return false;\n }\n`;
|
|
326
|
+
}
|
|
327
|
+
else {
|
|
328
|
+
DESERIALIZE =
|
|
329
|
+
DESERIALIZE.slice(0, DESERIALIZE.length - 1) +
|
|
330
|
+
` else {\n return false;\n }\n`;
|
|
331
|
+
}
|
|
332
|
+
DESERIALIZE += " } ";
|
|
333
|
+
}
|
|
334
|
+
DESERIALIZE += "\n return false;\n}";
|
|
335
|
+
//console.log(sortedMembers);
|
|
336
|
+
if (process.env["JSON_DEBUG"]) {
|
|
337
|
+
console.log(SERIALIZE_RAW);
|
|
338
|
+
//console.log(SERIALIZE_PRETTY);
|
|
339
|
+
console.log(INITIALIZE);
|
|
340
|
+
console.log(DESERIALIZE);
|
|
341
|
+
}
|
|
342
|
+
const SERIALIZE_RAW_METHOD = SimpleParser.parseClassMember(SERIALIZE_RAW, node);
|
|
343
|
+
//const SERIALIZE_PRETTY_METHOD = SimpleParser.parseClassMember(SERIALIZE_PRETTY, node);
|
|
344
|
+
const INITIALIZE_METHOD = SimpleParser.parseClassMember(INITIALIZE, node);
|
|
345
|
+
const DESERIALIZE_METHOD = SimpleParser.parseClassMember(DESERIALIZE, node);
|
|
346
|
+
if (!node.members.find((v) => v.name.text == "__SERIALIZE"))
|
|
347
|
+
node.members.push(SERIALIZE_RAW_METHOD);
|
|
348
|
+
if (!node.members.find((v) => v.name.text == "__INITIALIZE"))
|
|
349
|
+
node.members.push(INITIALIZE_METHOD);
|
|
350
|
+
if (!node.members.find((v) => v.name.text == "__DESERIALIZE"))
|
|
351
|
+
node.members.push(DESERIALIZE_METHOD);
|
|
352
|
+
this.schemasList.push(schema);
|
|
336
353
|
}
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
const DESERIALIZE_METHOD = SimpleParser.parseClassMember(DESERIALIZE, node);
|
|
344
|
-
if (!node.members.find((v) => v.name.text == "__SERIALIZE"))
|
|
345
|
-
node.members.push(SERIALIZE_RAW_METHOD);
|
|
346
|
-
if (!node.members.find((v) => v.name.text == "__INITIALIZE"))
|
|
347
|
-
node.members.push(INITIALIZE_METHOD);
|
|
348
|
-
if (!node.members.find((v) => v.name.text == "__DESERIALIZE"))
|
|
349
|
-
node.members.push(DESERIALIZE_METHOD);
|
|
350
|
-
this.schemasList.push(schema);
|
|
351
|
-
}
|
|
352
|
-
visitSource(node) {
|
|
353
|
-
super.visitSource(node);
|
|
354
|
-
// Only add the import statement to sources that have JSON decorated classes.
|
|
355
|
-
if (!this.sources.has(node)) {
|
|
356
|
-
return;
|
|
354
|
+
visitSource(node) {
|
|
355
|
+
super.visitSource(node);
|
|
356
|
+
// Only add the import statement to sources that have JSON decorated classes.
|
|
357
|
+
if (!this.sources.has(node)) {
|
|
358
|
+
return;
|
|
359
|
+
}
|
|
357
360
|
}
|
|
358
|
-
}
|
|
359
361
|
}
|
|
360
362
|
export default class Transformer extends Transform {
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
363
|
+
// Trigger the transform after parse.
|
|
364
|
+
afterParse(parser) {
|
|
365
|
+
// Create new transform
|
|
366
|
+
const transformer = new JSONTransform();
|
|
367
|
+
// Sort the sources so that user scripts are visited last
|
|
368
|
+
const sources = parser.sources
|
|
369
|
+
.filter((source) => !isStdlib(source))
|
|
370
|
+
.sort((_a, _b) => {
|
|
371
|
+
const a = _a.internalPath;
|
|
372
|
+
const b = _b.internalPath;
|
|
373
|
+
if (a[0] === "~" && b[0] !== "~") {
|
|
374
|
+
return -1;
|
|
375
|
+
}
|
|
376
|
+
else if (a[0] !== "~" && b[0] === "~") {
|
|
377
|
+
return 1;
|
|
378
|
+
}
|
|
379
|
+
else {
|
|
380
|
+
return 0;
|
|
381
|
+
}
|
|
382
|
+
});
|
|
383
|
+
// Loop over every source
|
|
384
|
+
for (const source of sources) {
|
|
385
|
+
// Ignore all lib and std. Visit everything else.
|
|
386
|
+
if (!isStdlib(source)) {
|
|
387
|
+
transformer.visit(source);
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
// Check that every parent and child class is hooked up correctly
|
|
391
|
+
const schemas = transformer.schemasList;
|
|
392
|
+
for (const schema of schemas) {
|
|
393
|
+
if (schema.parent) {
|
|
394
|
+
const parent = schemas.find((v) => v.name === schema.parent?.name);
|
|
395
|
+
if (!parent)
|
|
396
|
+
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.`);
|
|
397
|
+
}
|
|
377
398
|
}
|
|
378
|
-
});
|
|
379
|
-
// Loop over every source
|
|
380
|
-
for (const source of sources) {
|
|
381
|
-
// Ignore all lib and std. Visit everything else.
|
|
382
|
-
if (!isStdlib(source)) {
|
|
383
|
-
transformer.visit(source);
|
|
384
|
-
}
|
|
385
|
-
}
|
|
386
|
-
// Check that every parent and child class is hooked up correctly
|
|
387
|
-
const schemas = transformer.schemasList;
|
|
388
|
-
for (const schema of schemas) {
|
|
389
|
-
if (schema.parent) {
|
|
390
|
-
const parent = schemas.find((v) => v.name === schema.parent?.name);
|
|
391
|
-
if (!parent)
|
|
392
|
-
throw new Error(
|
|
393
|
-
`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.`,
|
|
394
|
-
);
|
|
395
|
-
}
|
|
396
399
|
}
|
|
397
|
-
}
|
|
398
400
|
}
|
|
399
401
|
var PropertyFlags;
|
|
400
402
|
(function (PropertyFlags) {
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
403
|
+
PropertyFlags[PropertyFlags["Omit"] = 0] = "Omit";
|
|
404
|
+
PropertyFlags[PropertyFlags["OmitNull"] = 1] = "OmitNull";
|
|
405
|
+
PropertyFlags[PropertyFlags["OmitIf"] = 2] = "OmitIf";
|
|
406
|
+
PropertyFlags[PropertyFlags["Alias"] = 3] = "Alias";
|
|
407
|
+
PropertyFlags[PropertyFlags["JSON_Raw"] = 4] = "JSON_Raw";
|
|
406
408
|
})(PropertyFlags || (PropertyFlags = {}));
|
|
407
409
|
class Property {
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
}
|
|
420
|
-
generate() {
|
|
421
|
-
const name = this.name;
|
|
422
|
-
const escapedName = escapeString(JSON.stringify(this.alias || this.name));
|
|
423
|
-
const type = this.type;
|
|
424
|
-
if (this.flags.has(PropertyFlags.Omit)) return;
|
|
425
|
-
if (this.flags.has(PropertyFlags.JSON_Raw)) {
|
|
426
|
-
this.right_s = "this." + name;
|
|
427
|
-
this.right_d = "data.substring(value_start, value_end);";
|
|
428
|
-
} else {
|
|
429
|
-
this.right_s = "__SERIALIZE<" + type + ">(this." + name + ")";
|
|
430
|
-
this.right_d =
|
|
431
|
-
"__DESERIALIZE<" + type + ">(data.substring(value_start, value_end))";
|
|
410
|
+
constructor() {
|
|
411
|
+
this.name = "";
|
|
412
|
+
this.alias = null;
|
|
413
|
+
this.type = "";
|
|
414
|
+
this.value = null;
|
|
415
|
+
this.flags = new Map();
|
|
416
|
+
this.serialize = null;
|
|
417
|
+
this.deserialize = null;
|
|
418
|
+
this.initialize = null;
|
|
419
|
+
this.right_s = "";
|
|
420
|
+
this.right_d = "";
|
|
432
421
|
}
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
422
|
+
generate() {
|
|
423
|
+
const name = this.name;
|
|
424
|
+
const escapedName = escapeString(JSON.stringify(this.alias || this.name));
|
|
425
|
+
const type = this.type;
|
|
426
|
+
if (this.flags.has(PropertyFlags.Omit))
|
|
427
|
+
return;
|
|
428
|
+
if (this.flags.has(PropertyFlags.JSON_Raw)) {
|
|
429
|
+
this.right_s = "this." + name;
|
|
430
|
+
this.right_d = "data.substring(value_start, value_end);";
|
|
431
|
+
}
|
|
432
|
+
else {
|
|
433
|
+
this.right_s = "__SERIALIZE<" + type + ">(this." + name + ")";
|
|
434
|
+
this.right_d =
|
|
435
|
+
"__DESERIALIZE<" + type + ">(data.substring(value_start, value_end))";
|
|
436
|
+
}
|
|
437
|
+
if (this.flags.has(PropertyFlags.OmitIf)) {
|
|
438
|
+
const condition = this.flags.get(PropertyFlags.OmitIf)[0];
|
|
439
|
+
if (!condition)
|
|
440
|
+
throw new Error("Could not find condition when using decorator @omitif! Provide at least one condition");
|
|
441
|
+
this.serialize =
|
|
442
|
+
"${" +
|
|
443
|
+
condition +
|
|
444
|
+
' ? "" : \'' +
|
|
445
|
+
escapedName +
|
|
446
|
+
":' + " +
|
|
447
|
+
this.right_s +
|
|
448
|
+
' + ","}';
|
|
449
|
+
this.deserialize = "this." + name + " = " + this.right_d + ";";
|
|
450
|
+
}
|
|
451
|
+
else if (this.flags.has(PropertyFlags.OmitNull)) {
|
|
452
|
+
this.serialize =
|
|
453
|
+
"${changetype<usize>(this." +
|
|
454
|
+
name +
|
|
455
|
+
") == <usize>0" +
|
|
456
|
+
' ? "" : \'' +
|
|
457
|
+
escapedName +
|
|
458
|
+
":' + " +
|
|
459
|
+
this.right_s +
|
|
460
|
+
' + ","}';
|
|
461
|
+
this.deserialize = "this." + name + " = " + this.right_d + ";";
|
|
462
|
+
}
|
|
463
|
+
else {
|
|
464
|
+
this.serialize = escapedName + ":${" + this.right_s + "}";
|
|
465
|
+
this.deserialize = "this." + name + " = " + this.right_d + ";";
|
|
466
|
+
}
|
|
462
467
|
}
|
|
463
|
-
}
|
|
464
468
|
}
|
|
465
469
|
class SchemaData {
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
470
|
+
constructor() {
|
|
471
|
+
this.name = "";
|
|
472
|
+
this.members = [];
|
|
473
|
+
this.parent = null;
|
|
474
|
+
}
|
|
471
475
|
}
|
|
472
476
|
function charCodeAt32(data, offset) {
|
|
473
|
-
|
|
477
|
+
return (data.charCodeAt(offset + 1) << 16) | data.charCodeAt(offset);
|
|
474
478
|
}
|
|
475
479
|
function charCodeAt64(data, offset) {
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
);
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
(secondCharCode << 16n) |
|
|
489
|
-
firstCharCode;
|
|
490
|
-
return u64Value;
|
|
480
|
+
if (offset + 3 >= data.length) {
|
|
481
|
+
throw new Error("The string must have at least 4 characters from the specified offset.");
|
|
482
|
+
}
|
|
483
|
+
const firstCharCode = BigInt(data.charCodeAt(offset));
|
|
484
|
+
const secondCharCode = BigInt(data.charCodeAt(offset + 1));
|
|
485
|
+
const thirdCharCode = BigInt(data.charCodeAt(offset + 2));
|
|
486
|
+
const fourthCharCode = BigInt(data.charCodeAt(offset + 3));
|
|
487
|
+
const u64Value = (fourthCharCode << 48n) |
|
|
488
|
+
(thirdCharCode << 32n) |
|
|
489
|
+
(secondCharCode << 16n) |
|
|
490
|
+
firstCharCode;
|
|
491
|
+
return u64Value;
|
|
491
492
|
}
|
|
492
493
|
function encodeKey(key) {
|
|
493
|
-
|
|
494
|
-
|
|
494
|
+
const data = JSON.stringify(key);
|
|
495
|
+
return data.slice(1, data.length - 1);
|
|
495
496
|
}
|
|
496
497
|
function escapeString(data) {
|
|
497
|
-
|
|
498
|
+
return data.replace(/\\/g, "\\\\").replace(/\`/g, "\\`");
|
|
498
499
|
}
|
|
499
500
|
function escapeSlash(data) {
|
|
500
|
-
|
|
501
|
+
return data.replace(/\\/g, "\\\\").replace(/\`/g, "\\`");
|
|
501
502
|
}
|
|
502
503
|
function escapeQuote(data) {
|
|
503
|
-
|
|
504
|
+
return data.replace(/\"/g, '\\"');
|
|
504
505
|
}
|
|
505
506
|
function getArgs(args) {
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
507
|
+
if (!args)
|
|
508
|
+
return [];
|
|
509
|
+
let out = [];
|
|
510
|
+
for (const arg of args) {
|
|
511
|
+
if (arg instanceof StringLiteralExpression) {
|
|
512
|
+
out.push(arg.value);
|
|
513
|
+
}
|
|
514
|
+
else if (arg instanceof IntegerLiteralExpression) {
|
|
515
|
+
out.push(i64_to_string(arg.value));
|
|
516
|
+
}
|
|
517
|
+
else if (arg instanceof FloatLiteralExpression) {
|
|
518
|
+
out.push(arg.value.toString());
|
|
519
|
+
}
|
|
520
|
+
else if (arg instanceof NullExpression) {
|
|
521
|
+
out.push(arg.text);
|
|
522
|
+
}
|
|
523
|
+
else if (arg instanceof TrueExpression) {
|
|
524
|
+
out.push(arg.text);
|
|
525
|
+
}
|
|
526
|
+
else if (arg instanceof FalseExpression) {
|
|
527
|
+
out.push(arg.text);
|
|
528
|
+
}
|
|
529
|
+
else if (arg instanceof IdentifierExpression) {
|
|
530
|
+
out.push(arg.text);
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
return out;
|
|
512
534
|
}
|
package/transform/package.json
CHANGED
package/transform/src/index.ts
CHANGED
|
@@ -7,6 +7,12 @@ import {
|
|
|
7
7
|
NodeKind,
|
|
8
8
|
Expression,
|
|
9
9
|
CommonFlags,
|
|
10
|
+
StringLiteralExpression,
|
|
11
|
+
IntegerLiteralExpression,
|
|
12
|
+
FloatLiteralExpression,
|
|
13
|
+
NullExpression,
|
|
14
|
+
TrueExpression,
|
|
15
|
+
FalseExpression,
|
|
10
16
|
} from "assemblyscript/dist/assemblyscript.js";
|
|
11
17
|
|
|
12
18
|
import { toString, isStdlib } from "visitor-as/dist/utils.js";
|
|
@@ -577,7 +583,21 @@ function getArgs(args: Expression[] | null): string[] {
|
|
|
577
583
|
if (!args) return [];
|
|
578
584
|
let out: string[] = [];
|
|
579
585
|
for (const arg of args) {
|
|
580
|
-
|
|
586
|
+
if (arg instanceof StringLiteralExpression) {
|
|
587
|
+
out.push(arg.value);
|
|
588
|
+
} else if (arg instanceof IntegerLiteralExpression) {
|
|
589
|
+
out.push(i64_to_string(arg.value));
|
|
590
|
+
} else if (arg instanceof FloatLiteralExpression) {
|
|
591
|
+
out.push(arg.value.toString());
|
|
592
|
+
} else if (arg instanceof NullExpression) {
|
|
593
|
+
out.push(arg.text);
|
|
594
|
+
} else if (arg instanceof TrueExpression) {
|
|
595
|
+
out.push(arg.text);
|
|
596
|
+
} else if (arg instanceof FalseExpression) {
|
|
597
|
+
out.push(arg.text);
|
|
598
|
+
} else if (arg instanceof IdentifierExpression) {
|
|
599
|
+
out.push(arg.text);
|
|
600
|
+
}
|
|
581
601
|
}
|
|
582
602
|
return out;
|
|
583
603
|
}
|