json-as 1.0.4 → 1.0.6

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.
Files changed (35) hide show
  1. package/CHANGELOG.md +10 -1
  2. package/README.md +32 -22
  3. package/SECURITY.md +2 -1
  4. package/assembly/__benches__/large.bench.ts +3 -0
  5. package/assembly/__benches__/vec3.bench.ts +3 -0
  6. package/assembly/__tests__/arbitrary.spec.ts +3 -3
  7. package/assembly/__tests__/array.spec.ts +5 -8
  8. package/assembly/__tests__/box.spec.ts +10 -20
  9. package/assembly/__tests__/custom.spec.ts +7 -6
  10. package/assembly/__tests__/date.spec.ts +4 -6
  11. package/assembly/__tests__/float.spec.ts +3 -3
  12. package/assembly/__tests__/map.spec.ts +1 -1
  13. package/assembly/__tests__/raw.spec.ts +3 -3
  14. package/assembly/__tests__/struct.spec.ts +25 -14
  15. package/assembly/deserialize/simd/string.ts +1 -2
  16. package/assembly/deserialize/simple/array/struct.ts +2 -3
  17. package/assembly/deserialize/simple/array.ts +1 -1
  18. package/assembly/deserialize/simple/bool.ts +1 -1
  19. package/assembly/deserialize/simple/map.ts +3 -4
  20. package/assembly/deserialize/simple/object.ts +2 -3
  21. package/assembly/deserialize/simple/raw.ts +1 -1
  22. package/assembly/deserialize/simple/struct.ts +2 -3
  23. package/assembly/index.ts +26 -10
  24. package/assembly/serialize/simd/string.ts +1 -0
  25. package/assembly/serialize/simple/integer.ts +1 -1
  26. package/assembly/serialize/simple/object.ts +6 -6
  27. package/assembly/test.ts +26 -24
  28. package/bench.js +2 -4
  29. package/index.ts +1 -1
  30. package/lib/as-bs.ts +4 -13
  31. package/package.json +4 -2
  32. package/run-tests.sh +1 -1
  33. package/transform/lib/index.js +61 -63
  34. package/transform/lib/index.js.map +1 -1
  35. package/transform/src/index.ts +73 -138
@@ -4,4 +4,4 @@ import { ptrToStr } from "../../util/ptrToStr";
4
4
  // @ts-ignore: inline
5
5
  @inline export function deserializeRaw(srcStart: usize, srcEnd: usize): JSON.Raw {
6
6
  return JSON.Raw.from(ptrToStr(srcStart, srcEnd));
7
- }
7
+ }
@@ -16,8 +16,7 @@ export function deserializeStruct<T>(srcStart: usize, srcEnd: usize, dst: usize)
16
16
  while (srcStart < srcEnd && isSpace(load<u16>(srcStart))) srcStart += 2;
17
17
  while (srcEnd > srcStart && isSpace(load<u16>(srcEnd - 2))) srcEnd -= 2; // would like to optimize this later
18
18
 
19
- if (srcStart - srcEnd == 0)
20
- throw new Error("Input string had zero length or was all whitespace");
19
+ if (srcStart - srcEnd == 0) throw new Error("Input string had zero length or was all whitespace");
21
20
  if (load<u16>(srcStart) != BRACE_LEFT) throw new Error("Expected '{' at start of object at position " + (srcEnd - srcStart).toString());
22
21
  if (load<u16>(srcEnd - 2) != BRACE_RIGHT) throw new Error("Expected '}' at end of object at position " + (srcEnd - srcStart).toString());
23
22
 
@@ -31,7 +30,7 @@ export function deserializeStruct<T>(srcStart: usize, srcEnd: usize, dst: usize)
31
30
  keyEnd = srcStart;
32
31
  // console.log("Key: " + ptrToStr(lastIndex, srcStart));
33
32
  // console.log("Next: " + String.fromCharCode(load<u16>(srcStart + 2)));
34
- while (isSpace((code = load<u16>((srcStart += 2))))) { }
33
+ while (isSpace((code = load<u16>((srcStart += 2))))) {}
35
34
  if (code !== COLON) throw new Error("Expected ':' after key at position " + (srcEnd - srcStart).toString());
36
35
  isKey = false;
37
36
  } else {
package/assembly/index.ts CHANGED
@@ -4,6 +4,7 @@ import { bs } from "../lib/as-bs";
4
4
  import { serializeString } from "./serialize/simple/string";
5
5
  import { serializeArray } from "./serialize/simple/array";
6
6
  import { serializeMap } from "./serialize/simple/map";
7
+ import { serializeDate } from "./serialize/simple/date";
7
8
  import { deserializeBoolean } from "./deserialize/simple/bool";
8
9
  import { deserializeArray } from "./deserialize/simple/array";
9
10
  import { deserializeFloat } from "./deserialize/simple/float";
@@ -28,6 +29,8 @@ import { serializeObject } from "./serialize/simple/object";
28
29
  import { deserializeObject } from "./deserialize/simple/object";
29
30
  import { serializeRaw } from "./serialize/simple/raw";
30
31
  import { deserializeRaw } from "./deserialize/simple/raw";
32
+ import { isSpace } from "util/string";
33
+ import { deserializeString_SIMD } from "./deserialize/simd/string";
31
34
 
32
35
  /**
33
36
  * Offset of the 'storage' property in the JSON.Value class.
@@ -118,7 +121,7 @@ export namespace JSON {
118
121
  // @ts-ignore: Supplied by transform
119
122
  } else if (isDefined(data.__SERIALIZE_CUSTOM)) {
120
123
  // @ts-ignore
121
- inline.always(data.__SERIALIZE_CUSTOM(changetype<usize>(data)));
124
+ inline.always(data.__SERIALIZE_CUSTOM());
122
125
  return bs.out<string>();
123
126
  // @ts-ignore: Supplied by transform
124
127
  } else if (isDefined(data.__SERIALIZE)) {
@@ -179,8 +182,14 @@ export namespace JSON {
179
182
  // @ts-ignore
180
183
  return null;
181
184
  } else if (isString<T>()) {
185
+ if (dataSize < 4) throw new Error("Cannot parse data as string because it was formatted incorrectly!");
186
+ // if (ASC_FEATURE_SIMD) {
187
+ // // @ts-ignore
188
+ // return changetype<string>(deserializeString_SIMD(dataPtr, dataPtr + dataSize, __new(dataSize - 4, idof<string>())));
189
+ // } else {
182
190
  // @ts-ignore
183
191
  return deserializeString(dataPtr, dataPtr + dataSize, __new(dataSize - 4, idof<string>()));
192
+ // }
184
193
  } else if (isArray<T>()) {
185
194
  // @ts-ignore
186
195
  return inline.always(deserializeArray<nonnull<T>>(dataPtr, dataPtr + dataSize, changetype<usize>(instantiate<T>())));
@@ -210,6 +219,7 @@ export namespace JSON {
210
219
  // @ts-ignore: type
211
220
  return deserializeRaw(dataPtr, dataPtr + dataSize);
212
221
  } else if (type instanceof JSON.Value) {
222
+ // should cut out whitespace here
213
223
  // @ts-ignore
214
224
  return inline.always(deserializeArbitrary(dataPtr, dataPtr + dataSize, 0));
215
225
  } else if (type instanceof JSON.Obj) {
@@ -428,7 +438,7 @@ export namespace JSON {
428
438
  // @ts-ignore: type
429
439
  private storage: Map<string, JSON.Value> = new Map<string, JSON.Value>();
430
440
 
431
- constructor() { }
441
+ constructor() {}
432
442
 
433
443
  // @ts-ignore: decorator
434
444
  @inline get size(): i32 {
@@ -478,7 +488,6 @@ export namespace JSON {
478
488
  const out = changetype<JSON.Obj>(__new(offsetof<JSON.Obj>(), idof<JSON.Obj>()));
479
489
 
480
490
  if (value instanceof Map) {
481
-
482
491
  }
483
492
  return out;
484
493
  }
@@ -543,7 +552,7 @@ export namespace JSON {
543
552
  // @ts-ignore: Supplied by transform
544
553
  } else if (isDefined(src.__SERIALIZE_CUSTOM)) {
545
554
  // @ts-ignore
546
- return src.__SERIALIZE_CUSTOM(changetype<nonnull<T>>(src));
555
+ return src.__SERIALIZE_CUSTOM(changetype<usize>(src));
547
556
  // @ts-ignore: Supplied by transform
548
557
  } else if (isDefined(src.__SERIALIZE)) {
549
558
  // @ts-ignore
@@ -579,14 +588,15 @@ export namespace JSON {
579
588
  } else if (isFloat<T>()) {
580
589
  return deserializeFloat<T>(srcStart, srcEnd);
581
590
  } else if (isString<T>()) {
591
+ if (srcEnd - srcStart < 4) throw new Error("Cannot parse data as string because it was formatted incorrectly!");
582
592
  // @ts-ignore: type
583
593
  return deserializeString(srcStart, srcEnd, dst);
584
- } else if (isArray<T>()) {
585
- // @ts-ignore: type
586
- return inline.always(deserializeArray<T>(srcStart, srcEnd, dst));
587
594
  } else if (isNullable<T>() && srcEnd - srcStart == 8 && load<u64>(srcStart) == 30399761348886638) {
588
595
  // @ts-ignore
589
596
  return null;
597
+ } else if (isArray<T>()) {
598
+ // @ts-ignore: type
599
+ return inline.always(deserializeArray<T>(srcStart, srcEnd, dst));
590
600
  } else {
591
601
  let type: nonnull<T> = changetype<nonnull<T>>(0);
592
602
  // @ts-ignore: Defined by transform
@@ -630,9 +640,15 @@ export namespace JSON {
630
640
  }
631
641
 
632
642
  // @ts-ignore: inline
633
- @inline export function toRaw(data: string): JSON.Raw { return new JSON.Raw(data) }
643
+ @inline export function toRaw(data: string): JSON.Raw {
644
+ return new JSON.Raw(data);
645
+ }
634
646
  // @ts-ignore: inline
635
- @inline export function fromRaw(data: JSON.Raw): string { return data.data }
647
+ @inline export function fromRaw(data: JSON.Raw): string {
648
+ return data.data;
649
+ }
636
650
 
637
651
  // @ts-ignore: inline
638
- @inline export function toBox<T>(data: T): JSON.Box<T> { return new JSON.Box<T>(data) }
652
+ @inline export function toBox<T>(data: T): JSON.Box<T> {
653
+ return new JSON.Box<T>(data);
654
+ }
@@ -1,3 +1,4 @@
1
+ import { bs } from "../../../lib/as-bs";
1
2
  import { BACK_SLASH } from "../../custom/chars";
2
3
  import { SERIALIZE_ESCAPE_TABLE } from "../../globals/tables";
3
4
  import { OBJECT, TOTAL_OVERHEAD } from "rt/common";
@@ -10,4 +10,4 @@ import { itoa_buffered } from "util/number";
10
10
  }
11
11
 
12
12
  // 32 {"x":,"y":,"z"}
13
- // 18 3.41.28.3
13
+ // 18 3.41.28.3
@@ -5,13 +5,13 @@ import { bytes } from "../../util";
5
5
 
6
6
  export function serializeObject(data: JSON.Obj): void {
7
7
  if (!data.size) {
8
- store<u32>(bs.offset, 0);
8
+ bs.proposeSize(4);
9
+ store<u32>(bs.offset, 8192123);
9
10
  bs.offset += 4;
10
11
  return;
11
12
  }
12
13
 
13
- // This grabs `JSON.Obj.stackSize` which is private
14
- bs.ensureSize(load<u32>(changetype<usize>(data), offsetof<JSON.Obj>("stackSize")) - 2);
14
+ bs.proposeSize(load<u32>(changetype<usize>(data), offsetof<JSON.Obj>("stackSize")) - 2);
15
15
  const keys = data.keys();
16
16
  const values = data.values();
17
17
 
@@ -25,7 +25,7 @@ export function serializeObject(data: JSON.Obj): void {
25
25
  const keySize = bytes(firstKey);
26
26
  store<u16>(bs.offset, QUOTE);
27
27
  memory.copy(bs.offset + 2, changetype<usize>(firstKey), keySize);
28
- store<u32>(bs.offset += keySize + 2, 3801122); // ":
28
+ store<u32>((bs.offset += keySize + 2), 3801122); // ":
29
29
  bs.offset += 4;
30
30
  JSON.__serialize(unchecked(values[0]));
31
31
 
@@ -34,11 +34,11 @@ export function serializeObject(data: JSON.Obj): void {
34
34
  const keySize = bytes(key);
35
35
  store<u32>(bs.offset, 2228268); // ,"
36
36
  memory.copy(bs.offset + 4, changetype<usize>(key), keySize);
37
- store<u32>(bs.offset += keySize + 4, 3801122); // ":
37
+ store<u32>((bs.offset += keySize + 4), 3801122); // ":
38
38
  bs.offset += 4;
39
39
  JSON.__serialize(unchecked(values[i]));
40
40
  }
41
41
 
42
42
  store<u16>(bs.offset, BRACE_RIGHT);
43
43
  bs.offset += 2;
44
- }
44
+ }
package/assembly/test.ts CHANGED
@@ -1,13 +1,15 @@
1
1
  import { JSON } from ".";
2
2
  import { bytes } from "./util";
3
3
 
4
+
4
5
  @json
5
6
  class Obj {
6
7
  public a: string = "hello";
7
8
  public b: string = "world";
8
- public c: string = "\"\t\f\u0000\u0001";
9
+ public c: string = '"\t\f\u0000\u0001';
9
10
  }
10
11
 
12
+
11
13
  @json
12
14
  class Vec3 {
13
15
  x: f32 = 0.0;
@@ -15,6 +17,7 @@ class Vec3 {
15
17
  z: f32 = 0.0;
16
18
  }
17
19
 
20
+
18
21
  @json
19
22
  class Player {
20
23
  @alias("first name")
@@ -24,11 +27,14 @@ class Player {
24
27
  // Drop in a code block, function, or expression that evaluates to a boolean
25
28
  @omitif((self: Player) => self.age < 18)
26
29
  age!: i32;
30
+
31
+
27
32
  @omitnull()
28
33
  pos!: Vec3 | null;
29
34
  isVerified!: boolean;
30
35
  }
31
36
 
37
+
32
38
  @json
33
39
  class Point {
34
40
  x: f64 = 0.0;
@@ -37,10 +43,14 @@ class Point {
37
43
  this.x = x;
38
44
  this.y = y;
39
45
  }
46
+
47
+
40
48
  @serializer
41
49
  serializer(self: Point): string {
42
50
  return `(${self.x},${self.y})`;
43
51
  }
52
+
53
+
44
54
  @deserializer
45
55
  deserializer(data: string): Point | null {
46
56
  const dataSize = bytes(data);
@@ -50,18 +60,17 @@ class Point {
50
60
  const x = data.slice(1, c);
51
61
  const y = data.slice(c + 1, data.length - 1);
52
62
 
53
- return new Point(
54
- f64.parse(x),
55
- f64.parse(y)
56
- );
63
+ return new Point(f64.parse(x), f64.parse(y));
57
64
  }
58
65
  }
59
66
 
67
+
60
68
  @json
61
69
  class InnerObj<T> {
62
- obj: T = instantiate<T>()
70
+ obj: T = instantiate<T>();
63
71
  }
64
72
 
73
+
65
74
  @json
66
75
  class ObjWithBracketString {
67
76
  data: string = "";
@@ -75,9 +84,9 @@ const player: Player = {
75
84
  pos: {
76
85
  x: 3.4,
77
86
  y: 1.2,
78
- z: 8.3
87
+ z: 8.3,
79
88
  },
80
- isVerified: true
89
+ isVerified: true,
81
90
  };
82
91
 
83
92
  const a1 = JSON.stringify("\u0000\u0001\u0002\u0003\u0004\u0005\u0006\u0007\u0008\u0009\u000a\u000b\u000c\u000d\u000e\u000f\u000f\u0011\u0012\u0013\u0014\u0015\u0016\u0017\u0018\u0019\u001a\u001b\u001c\u001d\u001e\u001f");
@@ -98,7 +107,7 @@ const a4 = new JSON.Obj();
98
107
  a4.set("x", 1.5);
99
108
  a4.set("y", 5.4);
100
109
  a4.set("z", 9.8);
101
- a4.set("obj", obj)
110
+ a4.set("obj", obj);
102
111
  a4.set<boolean>("bool", false);
103
112
 
104
113
  console.log("a4: " + JSON.stringify(a4));
@@ -115,37 +124,30 @@ const a7 = JSON.parse<JSON.Value[]>('["string",true,3.14,{"x":1.0,"y":2.0,"z":3.
115
124
 
116
125
  console.log("a7: " + JSON.stringify(a7));
117
126
 
118
- const a8 = JSON.stringify(["hello", JSON.stringify("world"),"working?"]);
127
+ const a8 = JSON.stringify(["hello", JSON.stringify("world"), "working?"]);
119
128
 
120
129
  console.log("a8: " + a8);
121
130
 
122
- const a9 = JSON.stringify<JSON.Raw>(JSON.Raw.from("\"hello world\""));
131
+ const a9 = JSON.stringify<JSON.Raw>(JSON.Raw.from('"hello world"'));
123
132
 
124
133
  console.log("a9: " + a9);
125
134
 
126
135
  const m10 = new Map<string, JSON.Raw>();
127
- m10.set("hello", new JSON.Raw("\"world\""));
128
- m10.set("pos", new JSON.Raw("{\"x\":1.0,\"y\":2.0,\"z\":3.0}"));
136
+ m10.set("hello", new JSON.Raw('"world"'));
137
+ m10.set("pos", new JSON.Raw('{"x":1.0,"y":2.0,"z":3.0}'));
129
138
 
130
139
  const a10 = JSON.stringify(m10);
131
140
 
132
141
  console.log("a10: " + a10);
133
142
 
134
- const a11 = JSON.parse<Vec3>(' { "x" : 3.4 , "y" : 1.2 , "z" : 8.3 } ');
143
+ const a11 = JSON.parse<JSON.Obj>(' { "x" : 3.4 , "y" : 1.2 , "z" : 8.3 } ');
135
144
 
136
145
  console.log("a11: " + JSON.stringify(a11));
137
146
 
138
147
  const a12 = JSON.parse<InnerObj<ObjWithBracketString>>('{"obj":{"data":"hello} world"}}');
139
148
 
140
- console.log("a12: " + JSON.stringify(a12))
149
+ console.log("a12: " + JSON.stringify(a12));
141
150
 
151
+ const a13 = JSON.stringify<JSON.Obj>(new JSON.Obj());
142
152
 
143
- @json
144
- class NullableObj {
145
- bar: Bar | null = null;
146
- }
147
-
148
- @json
149
- class Bar {
150
- value: string = "";
151
- }
153
+ console.log("a13: " + a13);
package/bench.js CHANGED
@@ -34,9 +34,7 @@ const vec = {
34
34
  let data;
35
35
 
36
36
  const bench = new Bench({ time: 1000 })
37
- .add("serialize vec3", () =>
38
- data = JSON.stringify(vec)
39
- )
37
+ .add("serialize vec3", () => (data = JSON.stringify(vec)))
40
38
  .add("deserialize vec3", () => {
41
39
  data = JSON.parse('{"x":3,"y":1,"z":8}');
42
40
  })
@@ -44,7 +42,7 @@ const bench = new Bench({ time: 1000 })
44
42
  data = JSON.stringify("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789~`!@#$%^&*()-_=+{[}]|\\:;\"'?/>.<,'\"}");
45
43
  })
46
44
  .add("deserialize alphabet string", () => {
47
- data = JSON.parse('"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789~`!@#$%^&*()-_=+{[}]|\\\\:;\\"\'?/>.<,\'\\"}"')
45
+ data = JSON.parse('"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789~`!@#$%^&*()-_=+{[}]|\\\\:;\\"\'?/>.<,\'\\"}"');
48
46
  }) /*
49
47
  .add("parse float", () => {
50
48
  data = JSON.parse("1.2345")
package/index.ts CHANGED
@@ -1 +1 @@
1
- export { JSON } from "./assembly/index";
1
+ export { JSON } from "./assembly/index";
package/lib/as-bs.ts CHANGED
@@ -5,7 +5,7 @@ import { OBJECT, TOTAL_OVERHEAD } from "rt/common";
5
5
  */
6
6
  export namespace bs {
7
7
  /** Current buffer pointer. */ // @ts-ignore
8
- export let buffer: ArrayBuffer = new ArrayBuffer(32);//__new(32, idof<ArrayBuffer>());
8
+ export let buffer: ArrayBuffer = new ArrayBuffer(32); //__new(32, idof<ArrayBuffer>());
9
9
 
10
10
  /** Current offset within the buffer. */
11
11
  export let offset: usize = changetype<usize>(buffer);
@@ -28,10 +28,7 @@ export namespace bs {
28
28
  const deltaBytes = nextPowerOf2(size + 64);
29
29
  bufferSize += deltaBytes;
30
30
  // @ts-ignore: exists
31
- const newPtr = changetype<ArrayBuffer>(__renew(
32
- changetype<usize>(buffer),
33
- bufferSize
34
- ));
31
+ const newPtr = changetype<ArrayBuffer>(__renew(changetype<usize>(buffer), bufferSize));
35
32
  offset = offset + changetype<usize>(newPtr) - changetype<usize>(buffer);
36
33
  buffer = newPtr;
37
34
  }
@@ -49,10 +46,7 @@ export namespace bs {
49
46
  const deltaBytes = nextPowerOf2(size);
50
47
  bufferSize += deltaBytes;
51
48
  // @ts-ignore: exists
52
- const newPtr = changetype<ArrayBuffer>(__renew(
53
- changetype<usize>(buffer),
54
- bufferSize
55
- ));
49
+ const newPtr = changetype<ArrayBuffer>(__renew(changetype<usize>(buffer), bufferSize));
56
50
  offset = offset + changetype<usize>(newPtr) - changetype<usize>(buffer);
57
51
  buffer = newPtr;
58
52
  }
@@ -70,10 +64,7 @@ export namespace bs {
70
64
  const deltaBytes = nextPowerOf2(size + 64);
71
65
  bufferSize += deltaBytes;
72
66
  // @ts-ignore
73
- const newPtr = changetype<ArrayBuffer>(__renew(
74
- changetype<usize>(buffer),
75
- bufferSize
76
- ));
67
+ const newPtr = changetype<ArrayBuffer>(__renew(changetype<usize>(buffer), bufferSize));
77
68
  // if (buffer != newPtr) console.log(" Old: " + changetype<usize>(buffer).toString() + "\n New: " + changetype<usize>(newPtr).toString());
78
69
  offset = offset + changetype<usize>(newPtr) - changetype<usize>(buffer);
79
70
  buffer = newPtr;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "json-as",
3
- "version": "1.0.4",
3
+ "version": "1.0.6",
4
4
  "author": "Jairus Tanaka",
5
5
  "description": "The only JSON library you'll need for AssemblyScript. SIMD enabled",
6
6
  "types": "assembly/index.ts",
@@ -44,7 +44,9 @@
44
44
  "lekiano",
45
45
  "Florian Guitton",
46
46
  "Matt Johnson-Pint",
47
- "Tomáš Hromada"
47
+ "Tomáš Hromada",
48
+ "Loredana Cirstea",
49
+ "Accipiter Nisus"
48
50
  ],
49
51
  "keywords": [
50
52
  "assemblyscript",
package/run-tests.sh CHANGED
@@ -7,7 +7,7 @@ for file in ./assembly/__tests__/*.spec.ts; do
7
7
  output="./build/${filename%.ts}.wasm"
8
8
 
9
9
  start_time=$(date +%s%3N)
10
- npx asc "$file" --transform ./transform -o "$output" || { echo "Tests failed"; exit 1; }
10
+ npx asc "$file" --transform ./transform -o "$output" --enable simd || { echo "Tests failed"; exit 1; }
11
11
  end_time=$(date +%s%3N)
12
12
 
13
13
  build_time=$((end_time - start_time))