json-as 0.9.19 → 0.9.21

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,6 +1,6 @@
1
1
  {
2
2
  "name": "@json-as/transform",
3
- "version": "0.9.19",
3
+ "version": "0.9.21",
4
4
  "description": "The only JSON library you'll need for AssemblyScript. SIMD enabled",
5
5
  "main": "./lib/index.js",
6
6
  "author": "Jairus Tanaka",
@@ -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";
@@ -18,7 +24,7 @@ class JSONTransform extends BaseVisitor {
18
24
  public currentClass!: SchemaData;
19
25
  public sources = new Set<Source>();
20
26
 
21
- visitMethodDeclaration(): void {}
27
+ visitMethodDeclaration(): void { }
22
28
  visitClassDeclaration(node: ClassDeclaration): void {
23
29
  if (!node.decorators?.length) return;
24
30
 
@@ -103,9 +109,9 @@ class JSONTransform extends BaseVisitor {
103
109
  if (!member.type) {
104
110
  throw new Error(
105
111
  "Fields must be strongly typed! Found " +
106
- toString(member) +
107
- " at " +
108
- node.range.source.normalizedPath,
112
+ toString(member) +
113
+ " at " +
114
+ node.range.source.normalizedPath,
109
115
  );
110
116
  }
111
117
  const type = toString(member.type!);
@@ -122,10 +128,14 @@ class JSONTransform extends BaseVisitor {
122
128
  mem.value = value;
123
129
  mem.node = member;
124
130
 
125
- if (type == "JSON.Raw") {
131
+ if (type.includes("JSON.Raw")) {
126
132
  mem.flags.set(PropertyFlags.JSON_Raw, []);
127
133
  }
128
134
 
135
+ if (member.type.isNullable) {
136
+ mem.flags.set(PropertyFlags.Null, []);
137
+ }
138
+
129
139
  if (member.decorators) {
130
140
  for (const decorator of member.decorators) {
131
141
  const decoratorName = (decorator.name as IdentifierExpression).text;
@@ -137,7 +147,7 @@ class JSONTransform extends BaseVisitor {
137
147
  if (!args.length)
138
148
  throw new Error(
139
149
  "Expected 1 argument but got zero at @alias in " +
140
- node.range.source.normalizedPath,
150
+ node.range.source.normalizedPath,
141
151
  );
142
152
  mem.alias = args[0]!;
143
153
  mem.flags.set(PropertyFlags.Alias, args);
@@ -151,7 +161,7 @@ class JSONTransform extends BaseVisitor {
151
161
  if (!decorator.args?.length)
152
162
  throw new Error(
153
163
  "Expected 1 argument but got zero at @omitif in " +
154
- node.range.source.normalizedPath,
164
+ node.range.source.normalizedPath,
155
165
  );
156
166
  mem.flags.set(PropertyFlags.OmitIf, args);
157
167
  break;
@@ -262,36 +272,35 @@ class JSONTransform extends BaseVisitor {
262
272
  SERIALIZE_PRETTY +=
263
273
  "`;\n store<u32>(changetype<usize>(out) + ((out.length - 2) << 1), 8192010);\n return out;\n}";
264
274
  } else {
265
- SERIALIZE_RAW += "`;\n};";
266
- SERIALIZE_PRETTY += "`;\n};";
275
+ SERIALIZE_RAW += "}`;\n return out;\n}";
276
+ SERIALIZE_PRETTY += "}`;\n return out;\n}";
267
277
  }
268
278
 
269
279
  INITIALIZE += " return this;\n}";
270
280
 
271
281
  const sortedMembers: Property[][] = [];
272
282
  const _sorted = schema.members.sort(
273
- (a, b) => a.name.length - b.name.length,
283
+ (a, b) => (a.alias?.length! || a.name.length) - (b.alias?.length! || b.name.length),
274
284
  );
275
- let len = 0;
276
- let offset = 0;
277
- sortedMembers.push([_sorted[0]!]);
278
- len = _sorted[0]?.name.length!;
279
- for (let i = 1; i < _sorted.length; i++) {
285
+ let len = -1;
286
+ let offset = -1;
287
+ for (let i = 0; i < _sorted.length; i++) {
280
288
  const member = _sorted[i]!;
281
- if (member.alias?.length || member.name.length > len) {
289
+ const _name = member.alias || member.name;
290
+ if (_name.length === len) {
291
+ sortedMembers[offset]?.push(member);
292
+ } else {
282
293
  sortedMembers.push([member]);
283
- len = member.alias?.length || member.name.length;
294
+ len = _name.length;
284
295
  offset++;
285
- } else {
286
- sortedMembers[offset]!.push(member);
287
296
  }
288
297
  }
289
298
 
290
299
  let first = true;
291
300
  for (const memberSet of sortedMembers) {
292
301
  const firstMember = memberSet[0]!;
293
- const name = encodeKey(firstMember.alias || firstMember.name);
294
- if (name.length === 1) {
302
+ const _name = encodeKey(firstMember.alias || firstMember.name);
303
+ if (_name.length === 1) {
295
304
  if (first) {
296
305
  DESERIALIZE +=
297
306
  " if (1 === len) {\n switch (load<u16>(changetype<usize>(data) + (key_start << 1))) {\n";
@@ -300,7 +309,7 @@ class JSONTransform extends BaseVisitor {
300
309
  DESERIALIZE +=
301
310
  "else if (1 === len) {\n switch (load<u16>(changetype<usize>(data) + (key_start << 1))) {\n";
302
311
  }
303
- } else if (name.length === 2) {
312
+ } else if (_name.length === 2) {
304
313
  if (first) {
305
314
  DESERIALIZE +=
306
315
  " if (2 === len) {\n switch (load<u32>(changetype<usize>(data) + (key_start << 1))) {\n";
@@ -309,7 +318,7 @@ class JSONTransform extends BaseVisitor {
309
318
  DESERIALIZE +=
310
319
  "else if (2 === len) {\n switch (load<u32>(changetype<usize>(data) + (key_start << 1))) {\n";
311
320
  }
312
- } else if (name.length === 4) {
321
+ } else if (_name.length === 4) {
313
322
  if (first) {
314
323
  DESERIALIZE +=
315
324
  " if (4 === len) {\n const code = load<u64>(changetype<usize>(data) + (key_start << 1));\n";
@@ -320,44 +329,44 @@ class JSONTransform extends BaseVisitor {
320
329
  }
321
330
  } else {
322
331
  if (first) {
323
- DESERIALIZE += " if (" + name.length + " === len) {\n";
332
+ DESERIALIZE += " if (" + _name.length + " === len) {\n";
324
333
  first = false;
325
334
  } else {
326
- DESERIALIZE += "else if (" + name.length + " === len) {\n";
335
+ DESERIALIZE += "else if (" + _name.length + " === len) {\n";
327
336
  }
328
337
  }
329
338
  let f = true;
330
339
  for (let i = 0; i < memberSet.length; i++) {
331
340
  const member = memberSet[i]!;
332
341
  if (!member.deserialize) continue;
333
- const name = encodeKey(member.alias || member.name);
334
- if (name.length === 1) {
335
- DESERIALIZE += ` case ${name.charCodeAt(0)}: {\n ${member.deserialize}\n return true;\n }\n`;
336
- } else if (name.length === 2) {
337
- DESERIALIZE += ` case ${charCodeAt32(name, 0)}: {\n ${member.deserialize}\n return true;\n }\n`;
338
- } else if (name.length === 4) {
342
+ const _name = encodeKey(member.alias || member.name);
343
+ if (_name.length === 1) {
344
+ DESERIALIZE += ` case ${_name.charCodeAt(0)}: { /* ${_name} */\n ${member.deserialize}\n return true;\n }\n`;
345
+ } else if (_name.length === 2) {
346
+ DESERIALIZE += ` case ${charCodeAt32(_name, 0)}: { /* ${_name} */\n ${member.deserialize}\n return true;\n }\n`;
347
+ } else if (_name.length === 4) {
339
348
  if (f) {
340
349
  f = false;
341
- DESERIALIZE += ` if (${charCodeAt64(name, 0)} === code) {\n ${member.deserialize}\n return true;\n }\n`;
350
+ DESERIALIZE += ` if (${charCodeAt64(_name, 0)} === code) { /* ${_name} */\n ${member.deserialize}\n return true;\n }\n`;
342
351
  } else {
343
352
  DESERIALIZE =
344
353
  DESERIALIZE.slice(0, DESERIALIZE.length - 1) +
345
- `else if (${charCodeAt64(name, 0)} === code) {\n ${member.deserialize}\n return true;\n }\n`;
354
+ `else if (${charCodeAt64(_name, 0)} === code) {\n ${member.deserialize}\n return true;\n }\n`;
346
355
  }
347
356
  } else {
348
357
  if (f) {
349
358
  f = false;
350
- 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`;
359
+ DESERIALIZE += ` if (0 === memory.compare(changetype<usize>("${escapeQuote(escapeSlash(_name))}"), changetype<usize>(data) + (key_start << 1), ${_name.length << 1})) { /* ${_name} */\n ${member.deserialize}\n return true;\n }\n`;
351
360
  } else {
352
361
  DESERIALIZE =
353
362
  DESERIALIZE.slice(0, DESERIALIZE.length - 1) +
354
- ` 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`;
363
+ ` else if (0 === memory.compare(changetype<usize>("${escapeQuote(escapeSlash(_name))}"), changetype<usize>(data) + (key_start << 1), ${_name.length << 1})) { /* ${_name} */\n ${member.deserialize}\n return true;\n }\n`;
355
364
  }
356
365
  }
357
366
  }
358
- if (name.length < 3) {
367
+ if (_name.length < 3) {
359
368
  DESERIALIZE += ` default: {\n return false;\n }\n }\n`;
360
- } else if (name.length == 4) {
369
+ } else if (_name.length == 4) {
361
370
  DESERIALIZE =
362
371
  DESERIALIZE.slice(0, DESERIALIZE.length - 1) +
363
372
  ` else {\n return false;\n }\n`;
@@ -450,6 +459,7 @@ export default class Transformer extends Transform {
450
459
  }
451
460
 
452
461
  enum PropertyFlags {
462
+ Null,
453
463
  Omit,
454
464
  OmitNull,
455
465
  OmitIf,
@@ -483,8 +493,13 @@ class Property {
483
493
  if (this.flags.has(PropertyFlags.Omit)) return;
484
494
 
485
495
  if (this.flags.has(PropertyFlags.JSON_Raw)) {
486
- this.right_s = "this." + name;
487
- this.right_d = "data.substring(value_start, value_end);";
496
+ if (this.flags.has(PropertyFlags.Null)) {
497
+ this.right_s = "(this." + name + " || \"null\")";
498
+ this.right_d = "value_start === value_end - 4 && 30399761348886638 === load<u64>(changetype<usize>(data) + (value_start << 1)) ? null : data.substring(value_start, value_end)";
499
+ } else {
500
+ this.right_s = "this." + name;
501
+ this.right_d = "data.substring(value_start, value_end);";
502
+ }
488
503
  } else {
489
504
  this.right_s = "__SERIALIZE<" + type + ">(this." + name + ")";
490
505
  this.right_d =
@@ -577,7 +592,21 @@ function getArgs(args: Expression[] | null): string[] {
577
592
  if (!args) return [];
578
593
  let out: string[] = [];
579
594
  for (const arg of args) {
580
- out.push(toString(arg));
595
+ if (arg instanceof StringLiteralExpression) {
596
+ out.push(arg.value);
597
+ } else if (arg instanceof IntegerLiteralExpression) {
598
+ out.push(i64_to_string(arg.value));
599
+ } else if (arg instanceof FloatLiteralExpression) {
600
+ out.push(arg.value.toString());
601
+ } else if (arg instanceof NullExpression) {
602
+ out.push(arg.text);
603
+ } else if (arg instanceof TrueExpression) {
604
+ out.push(arg.text);
605
+ } else if (arg instanceof FalseExpression) {
606
+ out.push(arg.text);
607
+ } else if (arg instanceof IdentifierExpression) {
608
+ out.push(arg.text);
609
+ }
581
610
  }
582
611
  return out;
583
612
  }