json-as 0.5.54 → 0.5.56

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/LICENSE CHANGED
@@ -18,4 +18,4 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
18
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
19
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
20
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
21
+ SOFTWARE.
package/asconfig.json ADDED
@@ -0,0 +1,15 @@
1
+ {
2
+ "targets": {
3
+ "test": {
4
+ "outFile": "build/test.wasm",
5
+ "sourceMap": false,
6
+ "optimizeLevel": 0,
7
+ "shrinkLevel": 0,
8
+ "converge": false,
9
+ "noAssert": false
10
+ }
11
+ },
12
+ "options": {
13
+ "transform": ["./transform"]
14
+ }
15
+ }
@@ -1,5 +1,5 @@
1
1
  import { JSON } from "..";
2
- import { snip_fast } from "../src/util";
2
+ import { __atoi_fast, snip_fast } from "../src/util";
3
3
  @json
4
4
  class Vec3 {
5
5
  x: i32;
@@ -12,13 +12,13 @@ const vec: Vec3 = {
12
12
  y: 1,
13
13
  z: 8,
14
14
  }
15
-
15
+ /*
16
16
  bench("Parse Number SNIP", () => {
17
17
  blackbox<i32>(snip_fast<i32>("12345"));
18
18
  });
19
- /*
19
+
20
20
  bench("Parse Number ATOI", () => {
21
- blackbox<i32>(atoi_fast<i32>("12345"));
21
+ blackbox<i32>(__atoi_fast<i32>("12345"));
22
22
  })
23
23
 
24
24
  bench("Parse Number STDLIB", () => {
@@ -28,16 +28,16 @@ bench("Parse Number STDLIB", () => {
28
28
  bench("Parse Number OLD", () => {
29
29
  blackbox<i32>(parseSciInteger<i32>("12345"));
30
30
  });
31
- */
31
+
32
32
  bench("Stringify Object (Vec3)", () => {
33
33
  blackbox<string>(vec.__JSON_Serialize());
34
- });
34
+ });*/
35
35
 
36
36
  // TODO: Make this allocate without crashing
37
- //bench("Parse Object (Vec3)", () => {
38
- // blackbox<Vec3>(vec.__JSON_Deserialize('{"x":0,"y":0,"z":0}', vec));
39
- //});
40
-
37
+ bench("Parse Object (Vec3)", () => {
38
+ blackbox<Vec3>(JSON.parse<Vec3>('{"x":0,"y":0,"z":0}'));
39
+ });
40
+ /*
41
41
  bench("Stringify Number Array", () => {
42
42
  blackbox(JSON.stringify<i32[]>([1, 2, 3]));
43
43
  });
@@ -1,29 +1,55 @@
1
1
  // Characters
2
- export const commaCode = ",".charCodeAt(0);
3
- export const quoteCode = '"'.charCodeAt(0);
4
- export const backSlashCode = "\\".charCodeAt(0);
5
- export const forwardSlashCode = "/".charCodeAt(0);
6
- export const leftBraceCode = "{".charCodeAt(0);
7
- export const rightBraceCode = "}".charCodeAt(0);
8
- export const leftBracketCode = "[".charCodeAt(0);
9
- export const rightBracketCode = "]".charCodeAt(0);
10
- export const colonCode = ":".charCodeAt(0);
11
- export const tCode = "t".charCodeAt(0);
12
- export const rCode = "r".charCodeAt(0);
13
- export const uCode = "u".charCodeAt(0);
14
- export const eCode = "e".charCodeAt(0);
15
- export const fCode = "f".charCodeAt(0);
16
- export const aCode = "a".charCodeAt(0);
17
- export const lCode = "l".charCodeAt(0);
18
- export const sCode = "s".charCodeAt(0);
19
- export const nCode = "n".charCodeAt(0);
2
+ // @ts-ignore = Decorator is valid here
3
+ @inline export const commaCode = 44;
4
+ // @ts-ignore = Decorator is valid here
5
+ @inline export const quoteCode = 34;
6
+ // @ts-ignore = Decorator is valid here
7
+ @inline export const backSlashCode = 92;
8
+ // @ts-ignore: Decorator is valid here
9
+ @inline export const forwardSlashCode = 47;
10
+ // @ts-ignore: Decorator is valid here
11
+ @inline export const leftBraceCode = 123;
12
+ // @ts-ignore: Decorator is valid here
13
+ @inline export const rightBraceCode = 125;
14
+ // @ts-ignore: Decorator is valid here
15
+ @inline export const leftBracketCode = 91;
16
+ // @ts-ignore: Decorator is valid here
17
+ @inline export const rightBracketCode = 93;
18
+ // @ts-ignore: Decorator is valid here
19
+ @inline export const colonCode = 58;
20
+ // @ts-ignore: Decorator is valid here
21
+ @inline export const tCode = 116;
22
+ // @ts-ignore: Decorator is valid here
23
+ @inline export const rCode = 114;
24
+ // @ts-ignore: Decorator is valid here
25
+ @inline export const uCode = 117;
26
+ // @ts-ignore: Decorator is valid here
27
+ @inline export const eCode = 101;
28
+ // @ts-ignore: Decorator is valid here
29
+ @inline export const fCode = 102;
30
+ // @ts-ignore: Decorator is valid here
31
+ @inline export const aCode = 97;
32
+ // @ts-ignore: Decorator is valid here
33
+ @inline export const lCode = 108;
34
+ // @ts-ignore: Decorator is valid here
35
+ @inline export const sCode = 115;
36
+ // @ts-ignore = Decorator is valid here
37
+ @inline export const nCode = 110;
20
38
  // Strings
21
- export const trueWord = "true";
22
- export const falseWord = "false";
23
- export const nullWord = "null";
24
- export const leftBracketWord = "[";
25
- export const emptyArrayWord = "[]";
26
- export const commaWord = ",";
27
- export const rightBracketWord = "]";
39
+ // @ts-ignore: Decorator is valid here
40
+ @inline export const trueWord = "true";
41
+ // @ts-ignore: Decorator is valid here
42
+ @inline export const falseWord = "false";
43
+ // @ts-ignore: Decorator is valid here
44
+ @inline export const nullWord = "null";
45
+ // @ts-ignore: Decorator is valid here
46
+ @inline export const leftBracketWord = "[";
47
+ // @ts-ignore: Decorator is valid here
48
+ @inline export const emptyArrayWord = "[]";
49
+ // @ts-ignore: Decorator is valid here
50
+ @inline export const commaWord = ",";
51
+ // @ts-ignore: Decorator is valid here
52
+ @inline export const rightBracketWord = "]";
28
53
  // Escape Codes
29
- export const newLineCode = "\n".charCodeAt(0);
54
+ // @ts-ignore: Decorator is valid here
55
+ @inline export const newLineCode = 10;
@@ -20,8 +20,10 @@ import {
20
20
  trueWord,
21
21
  uCode,
22
22
  emptyArrayWord,
23
+ falseWord,
23
24
  } from "./chars";
24
25
  import { snip_fast, unsafeCharCodeAt } from "./util";
26
+ import { Virtual } from "as-virtual/assembly";
25
27
 
26
28
  /**
27
29
  * JSON Encoder/Decoder for AssemblyScript
@@ -138,165 +140,132 @@ export namespace JSON {
138
140
  );
139
141
  }
140
142
  }
141
- // @ts-ignore: Decorator
142
- @inline function parseObjectValue<T>(data: string): T {
143
- let type: T;
144
- if (isString<T>()) {
145
- // @ts-ignore
146
- let result = "";
147
- let last = 0;
148
- for (let i = 0; i < data.length; i++) {
149
- // \\"
150
- if (unsafeCharCodeAt(data, i) === backSlashCode) {
151
- const char = unsafeCharCodeAt(data, ++i);
152
- result += data.slice(last, i - 1);
153
- if (char === 34) {
154
- result += '"';
143
+ }
144
+
145
+ // @ts-ignore: Decorator
146
+ @global @inline function __parseObjectValue<T>(data: string): T {
147
+ let type: T;
148
+ if (isString<T>()) {
149
+ // @ts-ignore
150
+ let result = "";
151
+ let last = 0;
152
+ for (let i = 0; i < data.length; i++) {
153
+ // \\"
154
+ if (unsafeCharCodeAt(data, i) === backSlashCode) {
155
+ const char = unsafeCharCodeAt(data, ++i);
156
+ result += data.slice(last, i - 1);
157
+ if (char === 34) {
158
+ result += '"';
159
+ last = ++i;
160
+ } else if (char === 110) {
161
+ result += "\n";
162
+ last = ++i;
163
+ // 92 98 114 116 102 117
164
+ } else if (char >= 92 && char <= 117) {
165
+ if (char === 92) {
166
+ result += "\\";
155
167
  last = ++i;
156
- } else if (char === 110) {
157
- result += "\n";
168
+ } else if (char === 98) {
169
+ result += "\b";
170
+ last = ++i;
171
+ } else if (char === 102) {
172
+ result += "\f";
173
+ last = ++i;
174
+ } else if (char === 114) {
175
+ result += "\r";
176
+ last = ++i;
177
+ } else if (char === 116) {
178
+ result += "\t";
179
+ last = ++i;
180
+ } else if (
181
+ char === 117 &&
182
+ load<u64>(changetype<usize>(data) + <usize>((i + 1) << 1)) ===
183
+ 27584753879220272
184
+ ) {
185
+ result += "\u000b";
186
+ i += 4;
158
187
  last = ++i;
159
- // 92 98 114 116 102 117
160
- } else if (char >= 92 && char <= 117) {
161
- if (char === 92) {
162
- result += "\\";
163
- last = ++i;
164
- } else if (char === 98) {
165
- result += "\b";
166
- last = ++i;
167
- } else if (char === 102) {
168
- result += "\f";
169
- last = ++i;
170
- } else if (char === 114) {
171
- result += "\r";
172
- last = ++i;
173
- } else if (char === 116) {
174
- result += "\t";
175
- last = ++i;
176
- } else if (
177
- char === 117 &&
178
- load<u64>(changetype<usize>(data) + <usize>((i + 1) << 1)) ===
179
- 27584753879220272
180
- ) {
181
- result += "\u000b";
182
- i += 4;
183
- last = ++i;
184
- }
185
188
  }
186
189
  }
187
190
  }
188
- result += data.slice(last);
189
- // @ts-ignore
190
- return result;
191
- } else if (isBoolean<T>()) {
192
- // @ts-ignore
193
- return parseBoolean<T>(data);
194
- } else if (isFloat<T>() || isInteger<T>()) {
195
- return parseNumber<T>(data);
196
- } else if (isArrayLike<T>()) {
197
- // @ts-ignore
198
- return parseArray<T>(data);
199
- // @ts-ignore
200
- } else if (isNullable<T>() && data == "null") {
201
- // @ts-ignore
202
- return null;
203
- // @ts-ignore
204
- } else if (isDefined(type.__JSON_Set_Key)) {
205
- return parseObject<T>(data.trimStart());
206
- } else if (idof<nonnull<T>>() == idof<Date>()) {
207
- // @ts-ignore
208
- return Date.fromString(data);
209
- } else {
210
- // @ts-ignore
211
- throw new Error(
212
- `Could not deserialize data ${data} to type ${nameof<T>()}. Make sure to add the correct decorators to classes.`
213
- );
214
191
  }
192
+ result += data.slice(last);
193
+ // @ts-ignore
194
+ return result;
195
+ } else if (isBoolean<T>()) {
196
+ // @ts-ignore
197
+ return parseBoolean<T>(data);
198
+ } else if (isFloat<T>() || isInteger<T>()) {
199
+ return parseNumber<T>(data);
200
+ } else if (isArrayLike<T>()) {
201
+ // @ts-ignore
202
+ return parseArray<T>(data);
203
+ // @ts-ignore
204
+ } else if (isNullable<T>() && data == "null") {
205
+ // @ts-ignore
206
+ return null;
207
+ // @ts-ignore
208
+ } else if (isDefined(type.__JSON_Set_Key)) {
209
+ return parseObject<T>(data.trimStart());
210
+ } else if (idof<nonnull<T>>() == idof<Date>()) {
211
+ // @ts-ignore
212
+ return Date.fromString(data);
213
+ } else {
214
+ // @ts-ignore
215
+ throw new Error(
216
+ `Could not deserialize data ${data} to type ${nameof<T>()}. Make sure to add the correct decorators to classes.`
217
+ );
215
218
  }
216
219
  }
217
220
 
218
221
  // @ts-ignore: Decorator
219
- @inline function serializeString(data: string): string {
220
- // @ts-ignore
221
- //if (data.length === 0) return "\"\"";
222
- /*
223
- let char: i32 = 0;
224
- if (data.length === 1) {
225
- char = unsafeCharCodeAt(data, 0);
226
- if (char === 34) {
227
- return "\\\"";
228
- } else if (char === 92) {
229
- return "\\n";
230
- } else if (char <= 13 && char >= 8) {
231
- switch (char) {
232
- case 0x5C: {
233
- return "\\\\";
234
- }
235
- case 0x08: {
236
- return "\\b";
237
- }
238
- case 0x0D: {
239
- return "\\r";
240
- }
241
- case 0x09: {
242
- return "\\t";
243
- }
244
- case 0x0C: {
245
- return "\\f";
246
- }
247
- case 0x0B: {
248
- return "\\u000b";
249
- }
250
- }
251
- } else {
252
- return data;
253
- }
254
- }*/
255
-
256
- let result = '"';
222
+ @inline export function serializeString(data: string): string {
223
+ let result = new StringSink('"');
257
224
 
258
225
  let last: i32 = 0;
259
226
  // @ts-ignore
260
227
  for (let i = 0; i < data.length; i++) {
261
228
  const char = unsafeCharCodeAt(<string>data, i);
262
229
  if (char === 34 || char === 92) {
263
- result += (<string>data).slice(last, i) + "\\";
230
+ result.write(<string>data, last, i);
231
+ result.writeCodePoint(backSlashCode);
264
232
  last = i;
265
233
  //i++;
266
234
  } else if (char <= 13 && char >= 8) {
267
- result += (<string>data).slice(last, i);
235
+ result.write(<string>data, last, i);
268
236
  last = i + 1;
269
237
  switch (char) {
270
238
  case 8: {
271
- result += "\\b";
239
+ result.write("\\b");
272
240
  break;
273
241
  }
274
242
  case 9: {
275
- result += "\\t";
243
+ result.write("\\t");
276
244
  break;
277
245
  }
278
246
  case 10: {
279
- result += "\\n";
247
+ result.write("\\n");
280
248
  break;
281
249
  }
282
250
  case 11: {
283
- result += "\\x0B"; // \\u000b
251
+ result.write("\\x0B"); // \\u000b
284
252
  break;
285
253
  }
286
254
  case 12: {
287
- result += "\\f";
255
+ result.write("\\f");
288
256
  break;
289
257
  }
290
258
  case 13: {
291
- result += "\\r";
259
+ result.write("\\r");
292
260
  break;
293
261
  }
294
262
  }
295
263
  }
296
264
  }
297
265
  if (result.length === 1) return '"' + data + '"';
298
- else result += (<string>data).slice(last);
299
- return result + '"';
266
+ else result.write(<string>data, last);
267
+ result.write("\"");
268
+ return result.toString();
300
269
  }
301
270
 
302
271
  // @ts-ignore: Decorator
@@ -389,7 +358,7 @@ export namespace JSON {
389
358
  let schema: nonnull<T> = changetype<nonnull<T>>(
390
359
  __new(offsetof<nonnull<T>>(), idof<nonnull<T>>())
391
360
  );
392
- let key = "";
361
+ let key = Virtual.createEmpty<string>();
393
362
  let isKey = false;
394
363
  let depth = 0;
395
364
  let outerLoopIndex = 1;
@@ -409,10 +378,7 @@ export namespace JSON {
409
378
  if (depth === 0) {
410
379
  ++arrayValueIndex;
411
380
  // @ts-ignore
412
- schema.__JSON_Set_Key(
413
- key,
414
- data.slice(outerLoopIndex, arrayValueIndex)
415
- );
381
+ schema.__JSON_Set_Key<Virtual<string>>(key, data, outerLoopIndex, arrayValueIndex);
416
382
  outerLoopIndex = arrayValueIndex;
417
383
  isKey = false;
418
384
  break;
@@ -433,10 +399,7 @@ export namespace JSON {
433
399
  if (depth === 0) {
434
400
  ++objectValueIndex;
435
401
  // @ts-ignore
436
- schema.__JSON_Set_Key(
437
- key,
438
- data.slice(outerLoopIndex, objectValueIndex)
439
- );
402
+ schema.__JSON_Set_Key<Virtual<string>>(key, data, outerLoopIndex, objectValueIndex);
440
403
  outerLoopIndex = objectValueIndex;
441
404
  isKey = false;
442
405
  break;
@@ -455,14 +418,11 @@ export namespace JSON {
455
418
  unsafeCharCodeAt(data, stringValueIndex - 1) !== backSlashCode
456
419
  ) {
457
420
  if (isKey === false) {
458
- key = data.slice(outerLoopIndex, stringValueIndex);
421
+ key.reinst(data, outerLoopIndex, stringValueIndex);
459
422
  isKey = true;
460
423
  } else {
461
424
  // @ts-ignore
462
- schema.__JSON_Set_Key(
463
- key,
464
- data.slice(outerLoopIndex, stringValueIndex)
465
- );
425
+ schema.__JSON_Set_Key<Virtual<string>>(key, data, outerLoopIndex, stringValueIndex);
466
426
  isKey = false;
467
427
  }
468
428
  outerLoopIndex = ++stringValueIndex;
@@ -471,7 +431,7 @@ export namespace JSON {
471
431
  }
472
432
  } else if (char == nCode) {
473
433
  // @ts-ignore
474
- schema.__JSON_Set_Key(key, nullWord);
434
+ schema.__JSON_Set_Key<Virtual<string>>(key, nullWord, 0, 4);
475
435
  isKey = false;
476
436
  } else if (
477
437
  char === tCode &&
@@ -480,7 +440,7 @@ export namespace JSON {
480
440
  unsafeCharCodeAt(data, ++outerLoopIndex) === eCode
481
441
  ) {
482
442
  // @ts-ignore
483
- schema.__JSON_Set_Key(key, trueWord);
443
+ schema.__JSON_Set_Key<Virtual<string>>(key, trueWord, 0, 4);
484
444
  isKey = false;
485
445
  } else if (
486
446
  char === fCode &&
@@ -490,7 +450,7 @@ export namespace JSON {
490
450
  unsafeCharCodeAt(data, ++outerLoopIndex) === eCode
491
451
  ) {
492
452
  // @ts-ignore
493
- schema.__JSON_Set_Key(key, "false");
453
+ schema.__JSON_Set_Key<Virtual<string>>(key, falseWord, 0, 5);
494
454
  isKey = false;
495
455
  } else if ((char >= 48 && char <= 57) || char === 45) {
496
456
  let numberValueIndex = ++outerLoopIndex;
@@ -498,10 +458,7 @@ export namespace JSON {
498
458
  const char = unsafeCharCodeAt(data, numberValueIndex);
499
459
  if (char === commaCode || char === rightBraceCode || isSpace(char)) {
500
460
  // @ts-ignore
501
- schema.__JSON_Set_Key(
502
- key,
503
- data.slice(outerLoopIndex - 1, numberValueIndex)
504
- );
461
+ schema.__JSON_Set_Key<Virtual<string>>(key, data, outerLoopIndex - 1, numberValueIndex);
505
462
  outerLoopIndex = numberValueIndex;
506
463
  isKey = false;
507
464
  break;