json-as 0.8.5 → 0.8.7
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 +7 -1
- package/CHANGELOG +6 -1
- package/README.md +26 -16
- package/assembly/__tests__/deserialize.spec.ts +298 -0
- package/assembly/__tests__/serialize.spec.ts +375 -0
- package/assembly/deserialize/array/array.ts +31 -0
- package/assembly/deserialize/array/bool.ts +19 -0
- package/assembly/deserialize/array/float.ts +24 -0
- package/assembly/deserialize/array/integer.ts +24 -0
- package/assembly/deserialize/array/map.ts +27 -0
- package/assembly/deserialize/array/object.ts +27 -0
- package/assembly/deserialize/array/string.ts +29 -0
- package/assembly/deserialize/array.ts +37 -0
- package/assembly/deserialize/bool.ts +18 -0
- package/assembly/deserialize/box.ts +17 -0
- package/assembly/deserialize/date.ts +11 -0
- package/assembly/deserialize/float.ts +9 -0
- package/assembly/deserialize/integer.ts +7 -0
- package/assembly/deserialize/map.ts +182 -0
- package/assembly/deserialize/object.ts +136 -0
- package/assembly/deserialize/string.ts +88 -0
- package/assembly/index.d.ts +7 -1
- package/assembly/index.ts +129 -1
- package/assembly/serialize/array.ts +52 -0
- package/assembly/serialize/bool.ts +4 -0
- package/assembly/serialize/box.ts +10 -0
- package/assembly/serialize/date.ts +4 -0
- package/assembly/serialize/float.ts +4 -0
- package/assembly/serialize/integer.ts +5 -0
- package/assembly/serialize/map.ts +24 -0
- package/assembly/serialize/object.ts +7 -0
- package/assembly/serialize/string.ts +64 -0
- package/assembly/src/sink.ts +286 -0
- package/assembly/src/util.ts +6 -0
- package/assembly/test.ts +34 -16
- package/bench/benchmark.ts +7 -3
- package/bench.js +14 -3
- package/index.ts +1 -1
- package/package.json +6 -8
- package/transform/lib/index.js +296 -198
- package/transform/lib/index.old.js +257 -0
- package/transform/lib/types.js +17 -0
- package/transform/package.json +1 -1
- package/transform/src/index.old.ts +312 -0
- package/transform/src/index.ts +298 -234
- package/transform/tsconfig.json +2 -2
- package/tsconfig.json +94 -102
- package/assembly/__benches__/as-json.ts +0 -88
- package/assembly/__benches__/as-tral.d.ts +0 -1
- package/assembly/__tests__/as-json.spec.ts +0 -671
- package/assembly/__tests__/as-pect.d.ts +0 -1
- package/assembly/src/json.ts +0 -941
|
@@ -0,0 +1,257 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
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
|
+
}
|
package/transform/package.json
CHANGED
|
@@ -0,0 +1,312 @@
|
|
|
1
|
+
import {
|
|
2
|
+
ClassDeclaration,
|
|
3
|
+
FieldDeclaration,
|
|
4
|
+
Parser,
|
|
5
|
+
Source,
|
|
6
|
+
SourceKind,
|
|
7
|
+
Tokenizer,
|
|
8
|
+
} from "assemblyscript/dist/assemblyscript.js";
|
|
9
|
+
|
|
10
|
+
import { toString, isStdlib } from "visitor-as/dist/utils.js";
|
|
11
|
+
import { BaseVisitor, SimpleParser } from "visitor-as/dist/index.js";
|
|
12
|
+
import { Transform } from "assemblyscript/dist/transform.js";
|
|
13
|
+
|
|
14
|
+
class SchemaData {
|
|
15
|
+
public keys: string[] = [];
|
|
16
|
+
public values: string[] = [];
|
|
17
|
+
public types: string[] = [];
|
|
18
|
+
public name: string = "";
|
|
19
|
+
public parent: string = "";
|
|
20
|
+
public node!: ClassDeclaration;
|
|
21
|
+
public encodeStmts: string[] = [];
|
|
22
|
+
public setDataStmts: string[] = [];
|
|
23
|
+
public initializeStmts: string[] = [];
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
class AsJSONTransform extends BaseVisitor {
|
|
27
|
+
public schemasList: SchemaData[] = [];
|
|
28
|
+
public currentClass!: SchemaData;
|
|
29
|
+
public sources = new Set<Source>();
|
|
30
|
+
|
|
31
|
+
visitMethodDeclaration(): void { }
|
|
32
|
+
visitClassDeclaration(node: ClassDeclaration): void {
|
|
33
|
+
const className = node.name.text;
|
|
34
|
+
if (!node.decorators?.length) return;
|
|
35
|
+
let foundDecorator = false;
|
|
36
|
+
for (const decorator of node.decorators!) {
|
|
37
|
+
if (
|
|
38
|
+
// @ts-ignore
|
|
39
|
+
decorator.name.text.toLowerCase() == "json" ||
|
|
40
|
+
// @ts-ignore
|
|
41
|
+
decorator.name.text.toLowerCase() == "serializable"
|
|
42
|
+
)
|
|
43
|
+
foundDecorator = true;
|
|
44
|
+
}
|
|
45
|
+
if (!foundDecorator) return;
|
|
46
|
+
|
|
47
|
+
// Prevent from being triggered twice.
|
|
48
|
+
for (const member of node.members) {
|
|
49
|
+
if (member.name.text == "__SERIALIZE") return;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
this.currentClass = {
|
|
53
|
+
name: className,
|
|
54
|
+
keys: [],
|
|
55
|
+
values: [],
|
|
56
|
+
types: [],
|
|
57
|
+
parent: node.extendsType ? toString(node.extendsType) : "",
|
|
58
|
+
node: node,
|
|
59
|
+
encodeStmts: [],
|
|
60
|
+
setDataStmts: [],
|
|
61
|
+
initializeStmts: []
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
if (this.currentClass.parent.length) {
|
|
65
|
+
const parentSchema = this.schemasList.find(
|
|
66
|
+
(v) => v.name == this.currentClass.parent
|
|
67
|
+
);
|
|
68
|
+
if (parentSchema?.encodeStmts) {
|
|
69
|
+
parentSchema?.encodeStmts.push(parentSchema?.encodeStmts.pop() + ",");
|
|
70
|
+
for (let i = 0; i < parentSchema.keys.length; i++) {
|
|
71
|
+
const key = parentSchema.keys[i];
|
|
72
|
+
if (node.members.filter(v => v.name.text == key) == undefined) this.currentClass.encodeStmts.unshift(parentSchema.encodeStmts[i]!);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const parentSchema = this.schemasList.find(
|
|
78
|
+
(v) => v.name == this.currentClass.parent
|
|
79
|
+
);
|
|
80
|
+
|
|
81
|
+
const members = [
|
|
82
|
+
...node.members
|
|
83
|
+
];
|
|
84
|
+
|
|
85
|
+
if (parentSchema) {
|
|
86
|
+
for (const mem of parentSchema.node.members) {
|
|
87
|
+
if (members.find(v => v.name === mem.name) == undefined) members.unshift(mem);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
for (const mem of members) {
|
|
92
|
+
// @ts-ignore
|
|
93
|
+
if (mem.type && mem.type.name && mem.type.name.identifier.text) {
|
|
94
|
+
const member = mem as FieldDeclaration;
|
|
95
|
+
const lineText = toString(member);
|
|
96
|
+
//console.log("Member: " + lineText)
|
|
97
|
+
|
|
98
|
+
if (!lineText.startsWith("private ") && !lineText.startsWith("static ")) {
|
|
99
|
+
|
|
100
|
+
// @ts-ignore
|
|
101
|
+
let type = toString(member.type);
|
|
102
|
+
|
|
103
|
+
const name = member.name.text;
|
|
104
|
+
let aliasName = name;
|
|
105
|
+
|
|
106
|
+
// @ts-ignore
|
|
107
|
+
if (member.decorators && member.decorators[0]?.name.text === "alias") {
|
|
108
|
+
if (member.decorators[0] && member.decorators[0].args![0]) {
|
|
109
|
+
// @ts-ignore
|
|
110
|
+
aliasName = member.decorators[0].args![0].value;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
this.currentClass.keys.push(name);
|
|
114
|
+
// @ts-ignore
|
|
115
|
+
this.currentClass.types.push(type);
|
|
116
|
+
// @ts-ignore
|
|
117
|
+
if (
|
|
118
|
+
[
|
|
119
|
+
"u8",
|
|
120
|
+
"i8",
|
|
121
|
+
"u16",
|
|
122
|
+
"i16",
|
|
123
|
+
"u32",
|
|
124
|
+
"i32",
|
|
125
|
+
"u64",
|
|
126
|
+
"i64",
|
|
127
|
+
].includes(type.toLowerCase())
|
|
128
|
+
) {
|
|
129
|
+
this.currentClass.encodeStmts.push(
|
|
130
|
+
`${encodeKey(aliasName)}:\${this.${name}},`
|
|
131
|
+
);
|
|
132
|
+
// @ts-ignore
|
|
133
|
+
this.currentClass.setDataStmts.push(
|
|
134
|
+
`if (key.equals(${JSON.stringify(aliasName)})) {
|
|
135
|
+
this.${name} = __atoi_fast<${type}>(data, val_start << 1, val_end << 1);
|
|
136
|
+
return;
|
|
137
|
+
}`
|
|
138
|
+
);
|
|
139
|
+
if (member.initializer) {
|
|
140
|
+
this.currentClass.initializeStmts.push(
|
|
141
|
+
`this.${name} = ${toString(member.initializer)}`
|
|
142
|
+
);
|
|
143
|
+
}
|
|
144
|
+
} else // @ts-ignore
|
|
145
|
+
if (
|
|
146
|
+
[
|
|
147
|
+
"f32",
|
|
148
|
+
"f64",
|
|
149
|
+
].includes(type.toLowerCase())
|
|
150
|
+
) {
|
|
151
|
+
this.currentClass.encodeStmts.push(
|
|
152
|
+
`${encodeKey(aliasName)}:\${this.${name}},`
|
|
153
|
+
);
|
|
154
|
+
// @ts-ignore
|
|
155
|
+
this.currentClass.setDataStmts.push(
|
|
156
|
+
`if (key.equals(${JSON.stringify(aliasName)})) {
|
|
157
|
+
this.${name} = __parseObjectValue<${type}>(data.slice(val_start, val_end), initializeDefaultValues);
|
|
158
|
+
return;
|
|
159
|
+
}`
|
|
160
|
+
);
|
|
161
|
+
if (member.initializer) {
|
|
162
|
+
this.currentClass.initializeStmts.push(
|
|
163
|
+
`this.${name} = ${toString(member.initializer)}`
|
|
164
|
+
);
|
|
165
|
+
}
|
|
166
|
+
} else {
|
|
167
|
+
this.currentClass.encodeStmts.push(
|
|
168
|
+
`${encodeKey(aliasName)}:\${__JSON_Stringify<${type}>(this.${name})},`
|
|
169
|
+
);
|
|
170
|
+
// @ts-ignore
|
|
171
|
+
this.currentClass.setDataStmts.push(
|
|
172
|
+
`if (key.equals(${JSON.stringify(aliasName)})) {
|
|
173
|
+
this.${name} = __parseObjectValue<${type}>(val_start ? data.slice(val_start, val_end) : data, initializeDefaultValues);
|
|
174
|
+
return;
|
|
175
|
+
}`
|
|
176
|
+
);
|
|
177
|
+
if (member.initializer) {
|
|
178
|
+
this.currentClass.initializeStmts.push(
|
|
179
|
+
`this.${name} = ${toString(member.initializer)}`
|
|
180
|
+
);
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
let serializeFunc = "";
|
|
188
|
+
|
|
189
|
+
if (this.currentClass.encodeStmts.length > 0) {
|
|
190
|
+
const stmt =
|
|
191
|
+
this.currentClass.encodeStmts[
|
|
192
|
+
this.currentClass.encodeStmts.length - 1
|
|
193
|
+
]!;
|
|
194
|
+
this.currentClass.encodeStmts[this.currentClass.encodeStmts.length - 1] =
|
|
195
|
+
stmt!.slice(0, stmt.length - 1);
|
|
196
|
+
serializeFunc = `
|
|
197
|
+
__SERIALIZE(): string {
|
|
198
|
+
return \`{${this.currentClass.encodeStmts.join("")}}\`;
|
|
199
|
+
}`;
|
|
200
|
+
} else {
|
|
201
|
+
serializeFunc = `
|
|
202
|
+
__SERIALIZE(): string {
|
|
203
|
+
return "{}";
|
|
204
|
+
}`;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
const setKeyFunc = `
|
|
208
|
+
__JSON_Set_Key(key: __Virtual<string>, data: string, val_start: i32, val_end: i32, initializeDefaultValues: boolean): void {
|
|
209
|
+
${this.currentClass.setDataStmts.join("\n ")}
|
|
210
|
+
}
|
|
211
|
+
`;
|
|
212
|
+
|
|
213
|
+
let initializeFunc = "";
|
|
214
|
+
|
|
215
|
+
if (this.currentClass.initializeStmts.length > 0) {
|
|
216
|
+
initializeFunc = `
|
|
217
|
+
__JSON_Initialize(): void {
|
|
218
|
+
${this.currentClass.initializeStmts.join(";\n")};
|
|
219
|
+
}
|
|
220
|
+
`;
|
|
221
|
+
} else {
|
|
222
|
+
initializeFunc = `
|
|
223
|
+
__JSON_Initialize(): void {}
|
|
224
|
+
`;
|
|
225
|
+
}
|
|
226
|
+
const serializeMethod = SimpleParser.parseClassMember(serializeFunc, node);
|
|
227
|
+
node.members.push(serializeMethod);
|
|
228
|
+
|
|
229
|
+
const setDataMethod = SimpleParser.parseClassMember(setKeyFunc, node);
|
|
230
|
+
node.members.push(setDataMethod);
|
|
231
|
+
|
|
232
|
+
const initializeMethod = SimpleParser.parseClassMember(initializeFunc, node);
|
|
233
|
+
node.members.push(initializeMethod);
|
|
234
|
+
|
|
235
|
+
this.schemasList.push(this.currentClass);
|
|
236
|
+
this.sources.add(node.name.range.source);
|
|
237
|
+
|
|
238
|
+
// Uncomment to see the generated code for debugging.
|
|
239
|
+
//console.log(serializeFunc);
|
|
240
|
+
//console.log(setKeyFunc);
|
|
241
|
+
//console.log(initializeFunc);
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
visitSource(node: Source): void {
|
|
245
|
+
super.visitSource(node);
|
|
246
|
+
|
|
247
|
+
// Only add the import statement to sources that have JSON decorated classes.
|
|
248
|
+
if (!this.sources.has(node)) {
|
|
249
|
+
return;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
// Note, the following one liner would be easier, but it fails with an assertion error
|
|
253
|
+
// because as-virtual's SimpleParser doesn't set the parser.currentSource correctly.
|
|
254
|
+
//
|
|
255
|
+
// const stmt = SimpleParser.parseTopLevelStatement('import { Virtual as __Virtual } from "as-virtual/assembly";');
|
|
256
|
+
|
|
257
|
+
// ... So we have to do it the long way:
|
|
258
|
+
const txt = 'import { Virtual as __Virtual } from "as-virtual/assembly";'
|
|
259
|
+
const tokenizer = new Tokenizer(new Source(SourceKind.User, node.normalizedPath, txt));
|
|
260
|
+
const parser = new Parser();
|
|
261
|
+
parser.currentSource = tokenizer.source;
|
|
262
|
+
const stmt = parser.parseTopLevelStatement(tokenizer)!;
|
|
263
|
+
|
|
264
|
+
// Add the import statement to the top of the source.
|
|
265
|
+
node.statements.unshift(stmt);
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
function encodeKey(aliasName: string): string {
|
|
270
|
+
return JSON.stringify(aliasName)
|
|
271
|
+
.replace(/\\/g, "\\\\")
|
|
272
|
+
.replace(/\`/g, '\\`');
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
export default class Transformer extends Transform {
|
|
276
|
+
// Trigger the transform after parse.
|
|
277
|
+
afterParse(parser: Parser): void {
|
|
278
|
+
// Create new transform
|
|
279
|
+
const transformer = new AsJSONTransform();
|
|
280
|
+
|
|
281
|
+
// Sort the sources so that user scripts are visited last
|
|
282
|
+
const sources = parser.sources
|
|
283
|
+
.filter((source) => !isStdlib(source))
|
|
284
|
+
.sort((_a, _b) => {
|
|
285
|
+
const a = _a.internalPath;
|
|
286
|
+
const b = _b.internalPath;
|
|
287
|
+
if (a[0] === "~" && b[0] !== "~") {
|
|
288
|
+
return -1;
|
|
289
|
+
} else if (a[0] !== "~" && b[0] === "~") {
|
|
290
|
+
return 1;
|
|
291
|
+
} else {
|
|
292
|
+
return 0;
|
|
293
|
+
}
|
|
294
|
+
});
|
|
295
|
+
|
|
296
|
+
// Loop over every source
|
|
297
|
+
for (const source of sources) {
|
|
298
|
+
// Ignore all lib and std. Visit everything else.
|
|
299
|
+
if (!isStdlib(source)) {
|
|
300
|
+
transformer.visit(source);
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
// Check that every parent and child class is hooked up correctly
|
|
304
|
+
const schemas = transformer.schemasList;
|
|
305
|
+
for (const schema of schemas) {
|
|
306
|
+
if (schema.parent) {
|
|
307
|
+
const parent = schemas.find((v) => v.name === schema.parent);
|
|
308
|
+
if (!parent) 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.`);
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
}
|