json-as 0.5.35 → 0.5.36

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/asconfig.json CHANGED
@@ -12,9 +12,6 @@
12
12
  "options": {
13
13
  "transform": [
14
14
  "./transform"
15
- ],
16
- "bindings": "esm",
17
- "exportStart": "_start"
18
- },
19
- "extends": "./node_modules/@assemblyscript/wasi-shim/asconfig.json"
15
+ ]
16
+ }
20
17
  }
@@ -1,6 +1,6 @@
1
1
  import { JSON } from "..";
2
2
  import { backSlashCode, quoteCode } from "../src/chars";
3
- import { atoi_fast, unsafeCharCodeAt } from "../src/util";
3
+ import { parseJSONInt, unsafeCharCodeAt } from "../src/util";
4
4
  import { HASH } from "util/hash";
5
5
 
6
6
  @json
@@ -24,11 +24,11 @@ class Vec3 {
24
24
  if (inStr === false && char === quoteCode) {
25
25
  if (key != null) {
26
26
  if (unsafeCharCodeAt(key, 0) == 120) {
27
- to.x = atoi_fast<i32>(data.substring(last, pos - 1))
27
+ to.x = parseJSONInt<i32>(data.substring(last, pos - 1))
28
28
  } else if (unsafeCharCodeAt(key, 0) == 121) {
29
- to.y = atoi_fast<i32>(data.substring(last, pos - 1))
29
+ to.y = parseJSONInt<i32>(data.substring(last, pos - 1))
30
30
  } else if (unsafeCharCodeAt(key, 0) == 122) {
31
- to.z = atoi_fast<i32>(data.substring(last, pos - 1))
31
+ to.z = parseJSONInt<i32>(data.substring(last, pos - 1))
32
32
  }
33
33
  }
34
34
  last = ++pos;
@@ -41,11 +41,11 @@ class Vec3 {
41
41
  }
42
42
  if (key != null) {
43
43
  if (unsafeCharCodeAt(key, 0) == 120) {
44
- to.x = atoi_fast<i32>(data.substring(last, pos - 1))
44
+ to.x = parseJSONInt<i32>(data.substring(last, pos - 1))
45
45
  } else if (unsafeCharCodeAt(key, 0) == 121) {
46
- to.y = atoi_fast<i32>(data.substring(last, pos - 1))
46
+ to.y = parseJSONInt<i32>(data.substring(last, pos - 1))
47
47
  } else if (unsafeCharCodeAt(key, 0) == 122) {
48
- to.z = atoi_fast<i32>(data.substring(last, pos - 1))
48
+ to.z = parseJSONInt<i32>(data.substring(last, pos - 1))
49
49
  }
50
50
  }
51
51
  return to;
@@ -23,7 +23,7 @@ import {
23
23
  uCode,
24
24
  emptyArrayWord
25
25
  } from "./chars";
26
- import { atoi_fast, escapeChar, isBigNum, unsafeCharCodeAt } from "./util";
26
+ import { parseJSONInt, escapeChar, isBigNum, unsafeCharCodeAt } from "./util";
27
27
 
28
28
  /**
29
29
  * JSON Encoder/Decoder for AssemblyScript
@@ -384,7 +384,7 @@ function parseBoolean<T extends boolean>(data: string): T {
384
384
  export function parseNumber<T>(data: string): T {
385
385
  if (isInteger<T>()) {
386
386
  // @ts-ignore
387
- return atoi_fast<T>(data);
387
+ return parseJSONInt<T>(data);
388
388
  }
389
389
  // @ts-ignore
390
390
  const type: T = 0;
@@ -1,6 +1,6 @@
1
1
  import { StringSink } from "as-string-sink/assembly";
2
2
  import { CharCode, isSpace } from "util/string";
3
- import { backSlashCode, quoteCode } from "./chars";
3
+ import { backSlashCode, quoteCode, rCode } from "./chars";
4
4
  import { u128, u128Safe, u256, u256Safe, i128, i128Safe, i256Safe } from "as-bignum/assembly";
5
5
 
6
6
  // @ts-ignore
@@ -85,17 +85,74 @@ export function getArrayDepth<T>(depth: i32 = 1): i32 {
85
85
  */
86
86
  @unsafe
87
87
  @inline
88
- export function atoi_fast<T extends number>(str: string): T {
88
+ export function atoi_fast<T extends number>(str: string, offset: i32 = 0): T {
89
89
  // @ts-ignore
90
90
  let val: T = 0;
91
- for (let pos = 0; pos < (str.length << 1); pos += 2) {
91
+ for (; offset < (str.length << 1); offset += 2) {
92
92
  // @ts-ignore
93
- val = (val << 1) + (val << 3) + (load<u16>(changetype<usize>(str) + <usize>pos) - 48);
93
+ val = (val << 1) + (val << 3) + (load<u16>(changetype<usize>(str) + <usize>offset) - 48);
94
94
  // We use load because in this case, there is no need to have bounds-checking
95
95
  }
96
96
  return val;
97
97
  }
98
98
 
99
99
  /**
100
- *
101
- */
100
+ * Implementation of ATOI. Can be much much faster with SIMD.
101
+ * Its pretty fast. (173m ops (atoi_fast) vs 89 ops (parseInt))
102
+ */
103
+ @unsafe
104
+ @inline
105
+ export function parseJSONInt<T extends number>(str: string): T {
106
+ // @ts-ignore
107
+ let val: T = 0;
108
+ let char: u16 = load<u16>(changetype<usize>(str));
109
+ let pos = 2;
110
+ let neg = char === 45;
111
+ // @ts-ignore
112
+ val = (val << 1) + (val << 3) + (char - 48);
113
+ for (; pos < (str.length << 1); pos += 2) {
114
+ char = load<u16>(changetype<usize>(str) + <usize>pos);
115
+ if (char === 101 || char === 69) {
116
+ char = load<u16>(changetype<usize>(str) + <usize>(pos += 2));
117
+ if (char === 45) {
118
+ // @ts-ignore
119
+ val /= sciNote<T>(atoi_fast<T>(str, pos += 2));
120
+ if (neg === true) {
121
+ // @ts-ignore
122
+ return ~val + 1;
123
+ }
124
+ return val;
125
+ } else {
126
+ // @ts-ignore
127
+ val *= sciNote<T>(atoi_fast<T>(str, pos += 2));
128
+ if (neg === true) {
129
+ // @ts-ignore
130
+ return ~val + 1;
131
+ }
132
+ return val;
133
+ }
134
+ }
135
+ // @ts-ignore
136
+ val = (val << 1) + (val << 3) + (char - 48);
137
+ }
138
+ if (neg === true) {
139
+ // @ts-ignore
140
+ val = ~val + 1;
141
+ }
142
+ return val;
143
+ }
144
+
145
+ function sciNote<T extends number>(num: T): T {
146
+ let res = 1;
147
+ if (num > 0) {
148
+ for (let i = 0; i < num; i++) {
149
+ res *= 10;
150
+ }
151
+ } else {
152
+ for (let i = 0; i < num; i++) {
153
+ res /= 10;
154
+ }
155
+ }
156
+ // @ts-ignore
157
+ return res;
158
+ }
package/assembly/test.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { backSlashCode, quoteCode } from "./src/chars";
2
2
  import { JSON } from "./src/json";
3
- import { atoi_fast, unsafeCharCodeAt } from "./src/util";
3
+ import { atoi_fast, parseJSONInt, unsafeCharCodeAt } from "./src/util";
4
4
 
5
5
  @json
6
6
  class Vec3 {
@@ -51,4 +51,10 @@ console.log(JSON.stringify(parsedVec3));
51
51
 
52
52
  console.log(`atoi_fast("429496729"): ${atoi_fast<i32>("429496729")}`);
53
53
 
54
- console.log(`strtol("429496729"): ${i32.parse("429496729")}`);
54
+ console.log(`strtol("429496729"): ${i32.parse("429496729")}`);
55
+
56
+ console.log(parseJSONInt<i32>("321").toString());
57
+ console.log(parseJSONInt<i32>("321e1").toString());
58
+ console.log(parseJSONInt<i32>("321e2").toString());
59
+ console.log(parseJSONInt<i32>("321e3").toString());
60
+ console.log(parseJSONInt<i32>("321e-1").toString());
package/package.json CHANGED
@@ -1,13 +1,15 @@
1
1
  {
2
2
  "name": "json-as",
3
- "version": "0.5.35",
3
+ "version": "0.5.36",
4
4
  "description": "JSON encoder/decoder for AssemblyScript",
5
5
  "types": "assembly/index.ts",
6
6
  "author": "Jairus Tanaka",
7
7
  "contributors": [
8
8
  "DogWhich",
9
- "Joshua Tenner",
10
- "Rom"
9
+ "Romdotdog",
10
+ "Derek Barrera",
11
+ "Frankk Taylor",
12
+ "lekiano"
11
13
  ],
12
14
  "license": "MIT",
13
15
  "scripts": {
@@ -23,7 +25,6 @@
23
25
  "devDependencies": {
24
26
  "@as-pect/cli": "^8.0.1",
25
27
  "@as-tral/cli": "^2.0.0",
26
- "@assemblyscript/loader": "^0.27.1",
27
28
  "@assemblyscript/wasi-shim": "^0.1.0",
28
29
  "assemblyscript": "^0.27.1",
29
30
  "assemblyscript-prettier": "^1.0.7",
@@ -1,13 +1,15 @@
1
1
  {
2
2
  "name": "@json-as/transform",
3
- "version": "0.5.35",
3
+ "version": "0.5.36",
4
4
  "description": "JSON encoder/decoder for AssemblyScript",
5
5
  "main": "./lib/index.js",
6
6
  "author": "Jairus Tanaka",
7
7
  "contributors": [
8
8
  "DogWhich",
9
- "Joshua Tenner",
10
- "Rom"
9
+ "Romdotdog",
10
+ "Derek Barrera",
11
+ "Frankk Taylor",
12
+ "lekiano"
11
13
  ],
12
14
  "license": "MIT",
13
15
  "devDependencies": {
@@ -31,4 +33,4 @@
31
33
  "homepage": "https://github.com/JairusSW/as-json#readme",
32
34
  "type": "module",
33
35
  "exports": "./lib/index.js"
34
- }
36
+ }
@@ -61,7 +61,7 @@ class AsJSONTransform extends BaseVisitor {
61
61
  }
62
62
 
63
63
  const parentSchema = this.schemasList.find((v) => v.name == this.currentClass.parent);
64
- const members = [...node.members, ...(parentSchema ? parentSchema.node.members : [])]
64
+ const members = [...node.members, ...(parentSchema ? parentSchema.node.members : [])];
65
65
 
66
66
  for (const mem of members) {
67
67
  if (mem.type && mem.type.name && mem.type.name.identifier.text) {
@@ -129,7 +129,7 @@ class AsJSONTransform extends BaseVisitor {
129
129
  this.currentClass.setDataStmts.join("")
130
130
  }
131
131
  }
132
- `
132
+ `;
133
133
 
134
134
  const serializeMethod = SimpleParser.parseClassMember(serializeFunc, node);
135
135
  node.members.push(serializeMethod);
@@ -164,7 +164,7 @@ export default class Transformer extends Transform {
164
164
  } else {
165
165
  return 0;
166
166
  }
167
- })
167
+ });
168
168
 
169
169
  // Loop over every source
170
170
  for (const source of sources) {