json-as 0.8.7 → 0.8.8

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.
@@ -1,312 +0,0 @@
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
- }