json-as 0.5.37 → 0.5.39
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/README.md +118 -116
- package/as-pect.asconfig.json +24 -24
- package/asconfig.json +18 -20
- package/assembly/__benches__/as-json.ts +46 -25
- package/assembly/__tests__/as-json.spec.ts +30 -22
- package/assembly/index.ts +1 -1
- package/assembly/src/chars.ts +1 -1
- package/assembly/src/json.ts +628 -576
- package/assembly/src/util.ts +40 -18
- package/assembly/test.ts +198 -13
- package/assembly/tsconfig.json +94 -1
- package/index.ts +1 -1
- package/package.json +1 -1
- package/transform/lib/index.js +30 -8
- package/transform/package.json +36 -36
- package/transform/src/index.ts +67 -37
- package/transform/tsconfig.json +70 -70
- package/tsconfig.json +12 -12
- package/transform/lib/hash.js +0 -74
- package/transform/lib/types.js +0 -15
- package/transform/src/hash.ts +0 -83
package/assembly/src/util.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { StringSink } from "as-string-sink/assembly";
|
|
2
|
-
import {
|
|
2
|
+
import { isSpace } from "util/string";
|
|
3
3
|
import { backSlashCode, quoteCode } from "./chars";
|
|
4
4
|
|
|
5
5
|
// @ts-ignore
|
|
@@ -8,6 +8,8 @@ export function unsafeCharCodeAt(data: string, pos: i32): i32 {
|
|
|
8
8
|
return load<u16>(changetype<usize>(data) + ((<usize>pos) << 1));
|
|
9
9
|
}
|
|
10
10
|
|
|
11
|
+
// @ts-ignore
|
|
12
|
+
@inline
|
|
11
13
|
export function removeWhitespace(data: string): string {
|
|
12
14
|
const result = new StringSink();
|
|
13
15
|
let instr = false;
|
|
@@ -34,15 +36,24 @@ export function removeWhitespace(data: string): string {
|
|
|
34
36
|
@inline
|
|
35
37
|
export function escapeChar(char: string): string {
|
|
36
38
|
switch (unsafeCharCodeAt(char, 0)) {
|
|
37
|
-
case 0x22:
|
|
38
|
-
|
|
39
|
-
case
|
|
40
|
-
|
|
41
|
-
case
|
|
42
|
-
|
|
43
|
-
case
|
|
44
|
-
|
|
45
|
-
|
|
39
|
+
case 0x22:
|
|
40
|
+
return '\\"';
|
|
41
|
+
case 0x5c:
|
|
42
|
+
return "\\\\";
|
|
43
|
+
case 0x08:
|
|
44
|
+
return "\\b";
|
|
45
|
+
case 0x0a:
|
|
46
|
+
return "\\n";
|
|
47
|
+
case 0x0d:
|
|
48
|
+
return "\\r";
|
|
49
|
+
case 0x09:
|
|
50
|
+
return "\\t";
|
|
51
|
+
case 0x0c:
|
|
52
|
+
return "\\f";
|
|
53
|
+
case 0x0b:
|
|
54
|
+
return "\\u000b";
|
|
55
|
+
default:
|
|
56
|
+
return char;
|
|
46
57
|
}
|
|
47
58
|
}
|
|
48
59
|
|
|
@@ -51,6 +62,9 @@ export function escapeChar(char: string): string {
|
|
|
51
62
|
* Suffers no overhead besides function calling and a if/else.
|
|
52
63
|
* @returns depth of array
|
|
53
64
|
*/
|
|
65
|
+
|
|
66
|
+
// @ts-ignore
|
|
67
|
+
@inline
|
|
54
68
|
export function getArrayDepth<T>(depth: i32 = 1): i32 {
|
|
55
69
|
// @ts-ignore
|
|
56
70
|
if (!isArray<T>()) {
|
|
@@ -68,15 +82,19 @@ export function getArrayDepth<T>(depth: i32 = 1): i32 {
|
|
|
68
82
|
/**
|
|
69
83
|
* Implementation of ATOI. Can be much much faster with SIMD.
|
|
70
84
|
* Benchmark: 40-46m ops/s
|
|
71
|
-
*/
|
|
72
|
-
|
|
85
|
+
*/
|
|
86
|
+
|
|
87
|
+
// @ts-ignore
|
|
73
88
|
@inline
|
|
74
89
|
export function atoi_fast<T extends number>(str: string, offset: i32 = 0): T {
|
|
75
90
|
// @ts-ignore
|
|
76
91
|
let val: T = 0;
|
|
77
|
-
for (; offset <
|
|
92
|
+
for (; offset < str.length << 1; offset += 2) {
|
|
78
93
|
// @ts-ignore
|
|
79
|
-
val =
|
|
94
|
+
val =
|
|
95
|
+
(val << 1) +
|
|
96
|
+
(val << 3) +
|
|
97
|
+
(load<u16>(changetype<usize>(str) + <usize>offset) - 48);
|
|
80
98
|
// We use load because in this case, there is no need to have bounds-checking
|
|
81
99
|
}
|
|
82
100
|
return val;
|
|
@@ -87,20 +105,22 @@ export function atoi_fast<T extends number>(str: string, offset: i32 = 0): T {
|
|
|
87
105
|
* Benchmark: Hovers around 30m ops/s
|
|
88
106
|
* Only safe if the string is valid.
|
|
89
107
|
* @param str integer to parse. example: 123e1, 123e-1, 123E100
|
|
90
|
-
* @returns
|
|
108
|
+
* @returns
|
|
91
109
|
*/
|
|
110
|
+
|
|
111
|
+
// @ts-ignore
|
|
92
112
|
@inline
|
|
93
113
|
export function parseSciInteger<T extends number>(str: string): T {
|
|
94
114
|
// @ts-ignore
|
|
95
115
|
let val: T = 0;
|
|
96
116
|
let offset = 0;
|
|
97
|
-
for (; offset <
|
|
117
|
+
for (; offset < str.length << 1; offset += 2) {
|
|
98
118
|
const char = load<u16>(changetype<usize>(str) + <usize>offset);
|
|
99
119
|
if (char === 101 || char === 69) {
|
|
100
120
|
const char = load<u16>(changetype<usize>(str) + <usize>(offset += 2));
|
|
101
121
|
if (char === 45) {
|
|
102
122
|
// @ts-ignore
|
|
103
|
-
val /= sciNote<T>(atoi_fast<T>(str, offset += 2));
|
|
123
|
+
val /= sciNote<T>(atoi_fast<T>(str, (offset += 2)));
|
|
104
124
|
// @ts-ignore
|
|
105
125
|
return val;
|
|
106
126
|
} else {
|
|
@@ -117,6 +137,8 @@ export function parseSciInteger<T extends number>(str: string): T {
|
|
|
117
137
|
return val;
|
|
118
138
|
}
|
|
119
139
|
|
|
140
|
+
// @ts-ignore
|
|
141
|
+
@inline
|
|
120
142
|
function sciNote<T extends number>(num: T): T {
|
|
121
143
|
let res = 1;
|
|
122
144
|
if (num > 0) {
|
|
@@ -130,4 +152,4 @@ function sciNote<T extends number>(num: T): T {
|
|
|
130
152
|
}
|
|
131
153
|
// @ts-ignore
|
|
132
154
|
return res;
|
|
133
|
-
}
|
|
155
|
+
}
|
package/assembly/test.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { JSON } from "./src/json";
|
|
2
2
|
import { atoi_fast, parseSciInteger } from "./src/util";
|
|
3
|
-
|
|
3
|
+
import * as a from "util/number";
|
|
4
4
|
@json
|
|
5
5
|
class Vec3 {
|
|
6
6
|
x!: f32;
|
|
@@ -26,16 +26,16 @@ const player: Player = {
|
|
|
26
26
|
pos: {
|
|
27
27
|
x: 3.4,
|
|
28
28
|
y: 1.2,
|
|
29
|
-
z: 8.3
|
|
29
|
+
z: 8.3,
|
|
30
30
|
},
|
|
31
|
-
isVerified: true
|
|
31
|
+
isVerified: true,
|
|
32
32
|
};
|
|
33
33
|
|
|
34
34
|
const vec: Vec3 = {
|
|
35
35
|
x: 3,
|
|
36
36
|
y: 1,
|
|
37
|
-
z: 8
|
|
38
|
-
}
|
|
37
|
+
z: 8,
|
|
38
|
+
};
|
|
39
39
|
|
|
40
40
|
const serializedPlayer = JSON.stringify<Player>(player);
|
|
41
41
|
console.log(serializedPlayer);
|
|
@@ -48,14 +48,199 @@ console.log(serializedVec3);
|
|
|
48
48
|
const parsedVec3 = JSON.parse<Vec3>(serializedVec3);
|
|
49
49
|
console.log(JSON.stringify(parsedVec3));
|
|
50
50
|
|
|
51
|
-
console.log(
|
|
51
|
+
console.log("atoi:");
|
|
52
|
+
console.log("123 - " + parseSciInteger<i32>("123").toString());
|
|
53
|
+
console.log("1230 - " + parseSciInteger<i32>("123e1").toString());
|
|
54
|
+
console.log("12300 - " + parseSciInteger<i32>("123e2").toString());
|
|
55
|
+
console.log("123000 - " + parseSciInteger<i32>("123e3").toString());
|
|
56
|
+
console.log("32 - " + parseSciInteger<i32>("123e-1").toString());
|
|
57
|
+
|
|
58
|
+
const str = changetype<string>(new ArrayBuffer(6));
|
|
59
|
+
console.log("istr:");
|
|
60
|
+
console.log("123 - " + istr8(123));
|
|
61
|
+
console.log("32 - " + istr8(32));
|
|
62
|
+
console.log("3 - " + istr8(3));
|
|
63
|
+
|
|
64
|
+
console.log(Uint8Array.wrap(changetype<ArrayBuffer>(istr8(12))).join(" "));
|
|
65
|
+
console.log(load<u32>(changetype<usize>(istr8(12))).toString());
|
|
66
|
+
@inline function istr8<
|
|
67
|
+
T extends number
|
|
68
|
+
>(int: T): string {
|
|
69
|
+
if (int >= 100) {
|
|
70
|
+
const str = changetype<string>(__new(6, idof<String>()));
|
|
71
|
+
store<u16>(changetype<usize>(str), ((int / 100) % 10) + 48);
|
|
72
|
+
store<u16>(changetype<usize>(str), ((int / 10) % 10) + 48, 2);
|
|
73
|
+
store<u16>(changetype<usize>(str), (int % 10) + 48, 4);
|
|
74
|
+
return str;
|
|
75
|
+
} else if (int >= 10) {
|
|
76
|
+
const str = changetype<string>(__new(4, idof<String>()));
|
|
77
|
+
store<u16>(changetype<usize>(str), ((int / 10) % 10) + 48);
|
|
78
|
+
store<u16>(changetype<usize>(str), (int % 10) + 48, 2);
|
|
79
|
+
return str;
|
|
80
|
+
} else {
|
|
81
|
+
const str = changetype<string>(__new(2, idof<String>()));
|
|
82
|
+
store<u16>(changetype<usize>(str), (int % 10) + 48);
|
|
83
|
+
return str;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
52
86
|
|
|
53
|
-
|
|
87
|
+
export function istr16<T extends number>(int: T): string {
|
|
88
|
+
if (int >= 10_000) {
|
|
89
|
+
const str = changetype<string>(__new(10, idof<String>()));
|
|
90
|
+
store<u16>(changetype<usize>(str), ((int / 10000) % 10) + 48);
|
|
91
|
+
store<u16>(changetype<usize>(str), ((int / 1000) % 10) + 48, 2);
|
|
92
|
+
store<u16>(changetype<usize>(str), ((int / 100) % 10) + 48, 4);
|
|
93
|
+
store<u16>(changetype<usize>(str), ((int / 10) % 10) + 48, 6);
|
|
94
|
+
store<u16>(changetype<usize>(str), (int % 10) + 48, 8);
|
|
95
|
+
return str;
|
|
96
|
+
} else if (int >= 1_000) {
|
|
97
|
+
const str = changetype<string>(__new(8, idof<String>()));
|
|
98
|
+
store<u16>(changetype<usize>(str), ((int / 1000) % 10) + 48);
|
|
99
|
+
store<u16>(changetype<usize>(str), ((int / 100) % 10) + 48, 2);
|
|
100
|
+
store<u16>(changetype<usize>(str), ((int / 10) % 10) + 48, 4);
|
|
101
|
+
store<u16>(changetype<usize>(str), (int % 10) + 48, 6);
|
|
102
|
+
return str;
|
|
103
|
+
} else if (int >= 100) {
|
|
104
|
+
const str = changetype<string>(__new(6, idof<String>()));
|
|
105
|
+
store<u16>(changetype<usize>(str), ((int / 100) % 10) + 48);
|
|
106
|
+
store<u16>(changetype<usize>(str), ((int / 10) % 10) + 48, 2);
|
|
107
|
+
store<u16>(changetype<usize>(str), (int % 10) + 48, 4);
|
|
108
|
+
return str;
|
|
109
|
+
} else if (int >= 10) {
|
|
110
|
+
const str = changetype<string>(__new(4, idof<String>()));
|
|
111
|
+
store<u16>(changetype<usize>(str), ((int / 10) % 10) + 48);
|
|
112
|
+
store<u16>(changetype<usize>(str), (int % 10) + 48, 2);
|
|
113
|
+
return str;
|
|
114
|
+
} else {
|
|
115
|
+
const str = changetype<string>(__new(2, idof<String>()));
|
|
116
|
+
store<u16>(changetype<usize>(str), (int % 10) + 48);
|
|
117
|
+
return str;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
export function istr32<T extends number>(int: T): string {
|
|
122
|
+
if (int >= 1_000_000_000) {
|
|
123
|
+
const str = changetype<string>(__new(22, idof<String>()));
|
|
124
|
+
store<u16>(changetype<usize>(str), ((int / 10000000000) % 10) + 48);
|
|
125
|
+
store<u16>(changetype<usize>(str), ((int / 1000000000) % 10) + 48, 2);
|
|
126
|
+
store<u16>(changetype<usize>(str), ((int / 100000000) % 10) + 48, 4);
|
|
127
|
+
store<u16>(changetype<usize>(str), ((int / 10000000) % 10) + 48, 6);
|
|
128
|
+
store<u16>(changetype<usize>(str), ((int / 1000000) % 10) + 48, 8);
|
|
129
|
+
store<u16>(changetype<usize>(str), ((int / 100000) % 10) + 48, 10);
|
|
130
|
+
store<u16>(changetype<usize>(str), ((int / 10000) % 10) + 48, 12);
|
|
131
|
+
store<u16>(changetype<usize>(str), ((int / 1000) % 10) + 48, 14);
|
|
132
|
+
store<u16>(changetype<usize>(str), ((int / 100) % 10) + 48, 16);
|
|
133
|
+
store<u16>(changetype<usize>(str), ((int / 10) % 10) + 48, 18);
|
|
134
|
+
store<u16>(changetype<usize>(str), (int % 10) + 48, 20);
|
|
135
|
+
return str;
|
|
136
|
+
} else if (int >= 100_000_000) {
|
|
137
|
+
const str = changetype<string>(__new(20, idof<String>()));
|
|
138
|
+
store<u16>(changetype<usize>(str), ((int / 1000000000) % 10) + 48);
|
|
139
|
+
store<u16>(changetype<usize>(str), ((int / 100000000) % 10) + 48, 2);
|
|
140
|
+
store<u16>(changetype<usize>(str), ((int / 10000000) % 10) + 48, 4);
|
|
141
|
+
store<u16>(changetype<usize>(str), ((int / 1000000) % 10) + 48, 6);
|
|
142
|
+
store<u16>(changetype<usize>(str), ((int / 100000) % 10) + 48, 8);
|
|
143
|
+
store<u16>(changetype<usize>(str), ((int / 10000) % 10) + 48, 10);
|
|
144
|
+
store<u16>(changetype<usize>(str), ((int / 1000) % 10) + 48, 12);
|
|
145
|
+
store<u16>(changetype<usize>(str), ((int / 100) % 10) + 48, 14);
|
|
146
|
+
store<u16>(changetype<usize>(str), ((int / 10) % 10) + 48, 16);
|
|
147
|
+
store<u16>(changetype<usize>(str), (int % 10) + 48, 18);
|
|
148
|
+
return str;
|
|
149
|
+
} else if (int >= 10_000_000) {
|
|
150
|
+
const str = changetype<string>(__new(18, idof<String>()));
|
|
151
|
+
store<u16>(changetype<usize>(str), ((int / 100000000) % 10) + 48);
|
|
152
|
+
store<u16>(changetype<usize>(str), ((int / 10000000) % 10) + 48, 2);
|
|
153
|
+
store<u16>(changetype<usize>(str), ((int / 1000000) % 10) + 48, 4);
|
|
154
|
+
store<u16>(changetype<usize>(str), ((int / 100000) % 10) + 48, 6);
|
|
155
|
+
store<u16>(changetype<usize>(str), ((int / 10000) % 10) + 48, 8);
|
|
156
|
+
store<u16>(changetype<usize>(str), ((int / 1000) % 10) + 48, 10);
|
|
157
|
+
store<u16>(changetype<usize>(str), ((int / 100) % 10) + 48, 12);
|
|
158
|
+
store<u16>(changetype<usize>(str), ((int / 10) % 10) + 48, 14);
|
|
159
|
+
store<u16>(changetype<usize>(str), (int % 10) + 48, 16);
|
|
160
|
+
return str;
|
|
161
|
+
} else if (int >= 10_000_000) {
|
|
162
|
+
const str = changetype<string>(__new(16, idof<String>()));
|
|
163
|
+
store<u16>(changetype<usize>(str), ((int / 10000000) % 10) + 48);
|
|
164
|
+
store<u16>(changetype<usize>(str), ((int / 1000000) % 10) + 48, 2);
|
|
165
|
+
store<u16>(changetype<usize>(str), ((int / 100000) % 10) + 48, 4);
|
|
166
|
+
store<u16>(changetype<usize>(str), ((int / 10000) % 10) + 48, 6);
|
|
167
|
+
store<u16>(changetype<usize>(str), ((int / 1000) % 10) + 48, 8);
|
|
168
|
+
store<u16>(changetype<usize>(str), ((int / 100) % 10) + 48, 10);
|
|
169
|
+
store<u16>(changetype<usize>(str), ((int / 10) % 10) + 48, 12);
|
|
170
|
+
store<u16>(changetype<usize>(str), (int % 10) + 48, 14);
|
|
171
|
+
return str;
|
|
172
|
+
} else if (int >= 1_000_000) {
|
|
173
|
+
const str = changetype<string>(__new(14, idof<String>()));
|
|
174
|
+
store<u16>(changetype<usize>(str), ((int / 1000000) % 10) + 48);
|
|
175
|
+
store<u16>(changetype<usize>(str), ((int / 100000) % 10) + 48, 2);
|
|
176
|
+
store<u16>(changetype<usize>(str), ((int / 10000) % 10) + 48, 4);
|
|
177
|
+
store<u16>(changetype<usize>(str), ((int / 1000) % 10) + 48, 6);
|
|
178
|
+
store<u16>(changetype<usize>(str), ((int / 100) % 10) + 48, 8);
|
|
179
|
+
store<u16>(changetype<usize>(str), ((int / 10) % 10) + 48, 10);
|
|
180
|
+
store<u16>(changetype<usize>(str), (int % 10) + 48, 12);
|
|
181
|
+
return str;
|
|
182
|
+
} else if (int >= 100_000) {
|
|
183
|
+
const str = changetype<string>(__new(12, idof<String>()));
|
|
184
|
+
store<u16>(changetype<usize>(str), ((int / 100000) % 10) + 48);
|
|
185
|
+
store<u16>(changetype<usize>(str), ((int / 10000) % 10) + 48, 2);
|
|
186
|
+
store<u16>(changetype<usize>(str), ((int / 1000) % 10) + 48, 4);
|
|
187
|
+
store<u16>(changetype<usize>(str), ((int / 100) % 10) + 48, 6);
|
|
188
|
+
store<u16>(changetype<usize>(str), ((int / 10) % 10) + 48, 8);
|
|
189
|
+
store<u16>(changetype<usize>(str), (int % 10) + 48, 10);
|
|
190
|
+
return str;
|
|
191
|
+
} else if (int >= 10_000) {
|
|
192
|
+
const str = changetype<string>(__new(10, idof<String>()));
|
|
193
|
+
store<u16>(changetype<usize>(str), ((int / 10000) % 10) + 48);
|
|
194
|
+
store<u16>(changetype<usize>(str), ((int / 1000) % 10) + 48, 2);
|
|
195
|
+
store<u16>(changetype<usize>(str), ((int / 100) % 10) + 48, 4);
|
|
196
|
+
store<u16>(changetype<usize>(str), ((int / 10) % 10) + 48, 6);
|
|
197
|
+
store<u16>(changetype<usize>(str), (int % 10) + 48, 8);
|
|
198
|
+
return str;
|
|
199
|
+
} else if (int >= 1_000) {
|
|
200
|
+
const str = changetype<string>(__new(8, idof<String>()));
|
|
201
|
+
store<u16>(changetype<usize>(str), ((int / 1000) % 10) + 48);
|
|
202
|
+
store<u16>(changetype<usize>(str), ((int / 100) % 10) + 48, 2);
|
|
203
|
+
store<u16>(changetype<usize>(str), ((int / 10) % 10) + 48, 4);
|
|
204
|
+
store<u16>(changetype<usize>(str), (int % 10) + 48, 6);
|
|
205
|
+
return str;
|
|
206
|
+
} else if (int >= 100) {
|
|
207
|
+
const str = changetype<string>(__new(6, idof<String>()));
|
|
208
|
+
store<u16>(changetype<usize>(str), ((int / 100) % 10) + 48);
|
|
209
|
+
store<u16>(changetype<usize>(str), ((int / 10) % 10) + 48, 2);
|
|
210
|
+
store<u16>(changetype<usize>(str), (int % 10) + 48, 4);
|
|
211
|
+
return str;
|
|
212
|
+
} else if (int >= 10) {
|
|
213
|
+
const str = changetype<string>(__new(4, idof<String>()));
|
|
214
|
+
store<u16>(changetype<usize>(str), ((int / 10) % 10) + 48);
|
|
215
|
+
store<u16>(changetype<usize>(str), (int % 10) + 48, 2);
|
|
216
|
+
return str;
|
|
217
|
+
} else {
|
|
218
|
+
const str = changetype<string>(__new(2, idof<String>()));
|
|
219
|
+
store<u16>(changetype<usize>(str), (int % 10) + 48);
|
|
220
|
+
return str;
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
export function istr64<T extends number>(int: T): string {
|
|
225
|
+
const val = new ArrayBuffer(6);
|
|
226
|
+
store<u16>(changetype<usize>(val), (int % 10) + 48, 4);
|
|
227
|
+
if ((int = (int / 10) as T) > 0)
|
|
228
|
+
store<u16>(changetype<usize>(val), (int % 10) + 48, 2);
|
|
229
|
+
else return changetype<string>(val);
|
|
230
|
+
if ((int = (int / 10) as T) > 0)
|
|
231
|
+
store<u16>(changetype<usize>(val), (int % 10) + 48);
|
|
232
|
+
return changetype<string>(val);
|
|
233
|
+
}
|
|
54
234
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
235
|
+
// 0 = 48
|
|
236
|
+
// 1 = 49
|
|
237
|
+
// 2 = 50
|
|
238
|
+
// 3 = 51
|
|
239
|
+
// 4 = 52
|
|
240
|
+
// 5 = 53
|
|
241
|
+
// 6 = 54
|
|
242
|
+
// 7 = 55
|
|
243
|
+
// 8 = 56
|
|
244
|
+
// 9 = 57
|
|
60
245
|
|
|
61
|
-
console.log(
|
|
246
|
+
console.log(JSON.stringify("h\\i from gray\bson"));
|
package/assembly/tsconfig.json
CHANGED
|
@@ -2,5 +2,98 @@
|
|
|
2
2
|
"extends": "assemblyscript/std/assembly.json",
|
|
3
3
|
"include": [
|
|
4
4
|
"./**/*.ts"
|
|
5
|
-
]
|
|
5
|
+
],
|
|
6
|
+
"compilerOptions": {
|
|
7
|
+
/* Visit https://aka.ms/tsconfig to read more about this file */
|
|
8
|
+
/* Projects */
|
|
9
|
+
// "incremental": true, /* Save .tsbuildinfo files to allow for incremental compilation of projects. */
|
|
10
|
+
// "composite": true, /* Enable constraints that allow a TypeScript project to be used with project references. */
|
|
11
|
+
// "tsBuildInfoFile": "./.tsbuildinfo", /* Specify the path to .tsbuildinfo incremental compilation file. */
|
|
12
|
+
// "disableSourceOfProjectReferenceRedirect": true, /* Disable preferring source files instead of declaration files when referencing composite projects. */
|
|
13
|
+
// "disableSolutionSearching": true, /* Opt a project out of multi-project reference checking when editing. */
|
|
14
|
+
// "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */
|
|
15
|
+
/* Language and Environment */
|
|
16
|
+
"target": "es2016" /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */,
|
|
17
|
+
// "lib": [], /* Specify a set of bundled library declaration files that describe the target runtime environment. */
|
|
18
|
+
// "jsx": "preserve", /* Specify what JSX code is generated. */
|
|
19
|
+
"experimentalDecorators": true, /* Enable experimental support for TC39 stage 2 draft decorators. */
|
|
20
|
+
"emitDecoratorMetadata": true, /* Emit design-type metadata for decorated declarations in source files. */
|
|
21
|
+
// "jsxFactory": "", /* Specify the JSX factory function used when targeting React JSX emit, e.g. 'React.createElement' or 'h'. */
|
|
22
|
+
// "jsxFragmentFactory": "", /* Specify the JSX Fragment reference used for fragments when targeting React JSX emit e.g. 'React.Fragment' or 'Fragment'. */
|
|
23
|
+
// "jsxImportSource": "", /* Specify module specifier used to import the JSX factory functions when using 'jsx: react-jsx*'. */
|
|
24
|
+
// "reactNamespace": "", /* Specify the object invoked for 'createElement'. This only applies when targeting 'react' JSX emit. */
|
|
25
|
+
// "noLib": true, /* Disable including any library files, including the default lib.d.ts. */
|
|
26
|
+
// "useDefineForClassFields": true, /* Emit ECMAScript-standard-compliant class fields. */
|
|
27
|
+
// "moduleDetection": "auto", /* Control what method is used to detect module-format JS files. */
|
|
28
|
+
/* Modules */
|
|
29
|
+
"module": "commonjs" /* Specify what module code is generated. */,
|
|
30
|
+
// "rootDir": "./", /* Specify the root folder within your source files. */
|
|
31
|
+
// "moduleResolution": "node", /* Specify how TypeScript looks up a file from a given module specifier. */
|
|
32
|
+
// "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */
|
|
33
|
+
// "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */
|
|
34
|
+
// "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */
|
|
35
|
+
// "typeRoots": [], /* Specify multiple folders that act like './node_modules/@types'. */
|
|
36
|
+
// "types": [], /* Specify type package names to be included without being referenced in a source file. */
|
|
37
|
+
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
|
|
38
|
+
// "moduleSuffixes": [], /* List of file name suffixes to search when resolving a module. */
|
|
39
|
+
// "resolveJsonModule": true, /* Enable importing .json files. */
|
|
40
|
+
// "noResolve": true, /* Disallow 'import's, 'require's or '<reference>'s from expanding the number of files TypeScript should add to a project. */
|
|
41
|
+
/* JavaScript Support */
|
|
42
|
+
// "allowJs": true, /* Allow JavaScript files to be a part of your program. Use the 'checkJS' option to get errors from these files. */
|
|
43
|
+
// "checkJs": true, /* Enable error reporting in type-checked JavaScript files. */
|
|
44
|
+
// "maxNodeModuleJsDepth": 1, /* Specify the maximum folder depth used for checking JavaScript files from 'node_modules'. Only applicable with 'allowJs'. */
|
|
45
|
+
/* Emit */
|
|
46
|
+
// "declaration": true, /* Generate .d.ts files from TypeScript and JavaScript files in your project. */
|
|
47
|
+
// "declarationMap": true, /* Create sourcemaps for d.ts files. */
|
|
48
|
+
// "emitDeclarationOnly": true, /* Only output d.ts files and not JavaScript files. */
|
|
49
|
+
// "sourceMap": true, /* Create source map files for emitted JavaScript files. */
|
|
50
|
+
// "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If 'declaration' is true, also designates a file that bundles all .d.ts output. */
|
|
51
|
+
// "outDir": "./", /* Specify an output folder for all emitted files. */
|
|
52
|
+
// "removeComments": true, /* Disable emitting comments. */
|
|
53
|
+
// "noEmit": true, /* Disable emitting files from a compilation. */
|
|
54
|
+
// "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */
|
|
55
|
+
// "importsNotUsedAsValues": "remove", /* Specify emit/checking behavior for imports that are only used for types. */
|
|
56
|
+
// "downlevelIteration": true, /* Emit more compliant, but verbose and less performant JavaScript for iteration. */
|
|
57
|
+
// "sourceRoot": "", /* Specify the root path for debuggers to find the reference source code. */
|
|
58
|
+
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
|
|
59
|
+
// "inlineSourceMap": true, /* Include sourcemap files inside the emitted JavaScript. */
|
|
60
|
+
// "inlineSources": true, /* Include source code in the sourcemaps inside the emitted JavaScript. */
|
|
61
|
+
// "emitBOM": true, /* Emit a UTF-8 Byte Order Mark (BOM) in the beginning of output files. */
|
|
62
|
+
// "newLine": "crlf", /* Set the newline character for emitting files. */
|
|
63
|
+
// "stripInternal": true, /* Disable emitting declarations that have '@internal' in their JSDoc comments. */
|
|
64
|
+
// "noEmitHelpers": true, /* Disable generating custom helper functions like '__extends' in compiled output. */
|
|
65
|
+
// "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */
|
|
66
|
+
// "preserveConstEnums": true, /* Disable erasing 'const enum' declarations in generated code. */
|
|
67
|
+
// "declarationDir": "./", /* Specify the output directory for generated declaration files. */
|
|
68
|
+
// "preserveValueImports": true, /* Preserve unused imported values in the JavaScript output that would otherwise be removed. */
|
|
69
|
+
/* Interop Constraints */
|
|
70
|
+
// "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */
|
|
71
|
+
// "allowSyntheticDefaultImports": true, /* Allow 'import x from y' when a module doesn't have a default export. */
|
|
72
|
+
"esModuleInterop": true /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */,
|
|
73
|
+
// "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */
|
|
74
|
+
"forceConsistentCasingInFileNames": true /* Ensure that casing is correct in imports. */,
|
|
75
|
+
/* Type Checking */
|
|
76
|
+
"strict": false/* Enable all strict type-checking options. */,
|
|
77
|
+
// "noImplicitAny": true, /* Enable error reporting for expressions and declarations with an implied 'any' type. */
|
|
78
|
+
"strictNullChecks": true, /* When type checking, take into account 'null' and 'undefined'. */
|
|
79
|
+
// "strictFunctionTypes": true, /* When assigning functions, check to ensure parameters and the return values are subtype-compatible. */
|
|
80
|
+
// "strictBindCallApply": true, /* Check that the arguments for 'bind', 'call', and 'apply' methods match the original function. */
|
|
81
|
+
// "strictPropertyInitialization": true, /* Check for class properties that are declared but not set in the constructor. */
|
|
82
|
+
// "noImplicitThis": true, /* Enable error reporting when 'this' is given the type 'any'. */
|
|
83
|
+
// "useUnknownInCatchVariables": true, /* Default catch clause variables as 'unknown' instead of 'any'. */
|
|
84
|
+
// "alwaysStrict": true, /* Ensure 'use strict' is always emitted. */
|
|
85
|
+
"noUnusedLocals": true, /* Enable error reporting when local variables aren't read. */
|
|
86
|
+
"noUnusedParameters": true, /* Raise an error when a function parameter isn't read. */
|
|
87
|
+
// "exactOptionalPropertyTypes": true, /* Interpret optional property types as written, rather than adding 'undefined'. */
|
|
88
|
+
// "noImplicitReturns": true, /* Enable error reporting for codepaths that do not explicitly return in a function. */
|
|
89
|
+
// "noFallthroughCasesInSwitch": true, /* Enable error reporting for fallthrough cases in switch statements. */
|
|
90
|
+
// "noUncheckedIndexedAccess": true, /* Add 'undefined' to a type when accessed using an index. */
|
|
91
|
+
// "noImplicitOverride": true, /* Ensure overriding members in derived classes are marked with an override modifier. */
|
|
92
|
+
// "noPropertyAccessFromIndexSignature": true, /* Enforces using indexed accessors for keys declared using an indexed type. */
|
|
93
|
+
// "allowUnusedLabels": true, /* Disable error reporting for unused labels. */
|
|
94
|
+
// "allowUnreachableCode": true, /* Disable error reporting for unreachable code. */
|
|
95
|
+
/* Completeness */
|
|
96
|
+
// "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */
|
|
97
|
+
"skipLibCheck": true /* Skip type checking all .d.ts files. */
|
|
98
|
+
}
|
|
6
99
|
}
|
package/index.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export { JSON } from "./assembly/src/json";
|
|
1
|
+
export { JSON } from "./assembly/src/json";
|
package/package.json
CHANGED
package/transform/lib/index.js
CHANGED
|
@@ -26,8 +26,11 @@ class AsJSONTransform extends BaseVisitor {
|
|
|
26
26
|
return;
|
|
27
27
|
let foundDecorator = false;
|
|
28
28
|
for (const decorator of node.decorators) {
|
|
29
|
+
if (
|
|
29
30
|
// @ts-ignore
|
|
30
|
-
|
|
31
|
+
decorator.name.text.toLowerCase() == "json" ||
|
|
32
|
+
// @ts-ignore
|
|
33
|
+
decorator.name.text.toLowerCase() == "serializable")
|
|
31
34
|
foundDecorator = true;
|
|
32
35
|
}
|
|
33
36
|
if (!foundDecorator)
|
|
@@ -45,7 +48,7 @@ class AsJSONTransform extends BaseVisitor {
|
|
|
45
48
|
parent: node.extendsType ? toString(node.extendsType) : "",
|
|
46
49
|
node: node,
|
|
47
50
|
encodeStmts: [],
|
|
48
|
-
setDataStmts: []
|
|
51
|
+
setDataStmts: [],
|
|
49
52
|
};
|
|
50
53
|
if (this.currentClass.parent.length > 0) {
|
|
51
54
|
const parentSchema = this.schemasList.find((v) => v.name == this.currentClass.parent);
|
|
@@ -54,12 +57,18 @@ class AsJSONTransform extends BaseVisitor {
|
|
|
54
57
|
this.currentClass.encodeStmts.push(...parentSchema === null || parentSchema === void 0 ? void 0 : parentSchema.encodeStmts);
|
|
55
58
|
}
|
|
56
59
|
else {
|
|
57
|
-
console.error("Class extends " +
|
|
60
|
+
console.error("Class extends " +
|
|
61
|
+
this.currentClass.parent +
|
|
62
|
+
", but parent class not found. Maybe add the @json decorator over parent class?");
|
|
58
63
|
}
|
|
59
64
|
}
|
|
60
65
|
const parentSchema = this.schemasList.find((v) => v.name == this.currentClass.parent);
|
|
61
|
-
const members = [
|
|
66
|
+
const members = [
|
|
67
|
+
...node.members,
|
|
68
|
+
...(parentSchema ? parentSchema.node.members : []),
|
|
69
|
+
];
|
|
62
70
|
for (const mem of members) {
|
|
71
|
+
// @ts-ignore
|
|
63
72
|
if (mem.type && mem.type.name && mem.type.name.identifier.text) {
|
|
64
73
|
const member = mem;
|
|
65
74
|
if (toString(member).startsWith("static"))
|
|
@@ -74,7 +83,18 @@ class AsJSONTransform extends BaseVisitor {
|
|
|
74
83
|
// @ts-ignore
|
|
75
84
|
this.currentClass.types.push(type);
|
|
76
85
|
// @ts-ignore
|
|
77
|
-
if ([
|
|
86
|
+
if ([
|
|
87
|
+
"u8",
|
|
88
|
+
"i8",
|
|
89
|
+
"u16",
|
|
90
|
+
"i16",
|
|
91
|
+
"u32",
|
|
92
|
+
"i32",
|
|
93
|
+
"f32",
|
|
94
|
+
"u64",
|
|
95
|
+
"i64",
|
|
96
|
+
"f64",
|
|
97
|
+
].includes(type.toLowerCase())) {
|
|
78
98
|
this.currentClass.encodeStmts.push(`"${name}":\${this.${name}.toString()},`);
|
|
79
99
|
}
|
|
80
100
|
else {
|
|
@@ -91,7 +111,8 @@ class AsJSONTransform extends BaseVisitor {
|
|
|
91
111
|
let serializeFunc = "";
|
|
92
112
|
if (this.currentClass.encodeStmts.length > 0) {
|
|
93
113
|
const stmt = this.currentClass.encodeStmts[this.currentClass.encodeStmts.length - 1];
|
|
94
|
-
this.currentClass.encodeStmts[this.currentClass.encodeStmts.length - 1] =
|
|
114
|
+
this.currentClass.encodeStmts[this.currentClass.encodeStmts.length - 1] =
|
|
115
|
+
stmt.slice(0, stmt.length - 1);
|
|
95
116
|
serializeFunc = `
|
|
96
117
|
@inline
|
|
97
118
|
__JSON_Serialize(): string {
|
|
@@ -131,7 +152,9 @@ export default class Transformer extends Transform {
|
|
|
131
152
|
// Create new transform
|
|
132
153
|
const transformer = new AsJSONTransform();
|
|
133
154
|
// Sort the sources so that user scripts are visited last
|
|
134
|
-
const sources = parser.sources
|
|
155
|
+
const sources = parser.sources
|
|
156
|
+
.filter((source) => !isStdlib(source))
|
|
157
|
+
.sort((_a, _b) => {
|
|
135
158
|
const a = _a.internalPath;
|
|
136
159
|
const b = _b.internalPath;
|
|
137
160
|
if (a[0] === "~" && b[0] !== "~") {
|
|
@@ -153,4 +176,3 @@ export default class Transformer extends Transform {
|
|
|
153
176
|
}
|
|
154
177
|
}
|
|
155
178
|
}
|
|
156
|
-
;
|
package/transform/package.json
CHANGED
|
@@ -1,36 +1,36 @@
|
|
|
1
|
-
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "@json-as/transform",
|
|
3
|
+
"version": "0.5.39",
|
|
4
|
+
"description": "JSON encoder/decoder for AssemblyScript",
|
|
5
|
+
"main": "./lib/index.js",
|
|
6
|
+
"author": "Jairus Tanaka",
|
|
7
|
+
"contributors": [
|
|
8
|
+
"DogWhich",
|
|
9
|
+
"Romdotdog",
|
|
10
|
+
"Derek Barrera",
|
|
11
|
+
"Frankk Taylor",
|
|
12
|
+
"lekiano"
|
|
13
|
+
],
|
|
14
|
+
"license": "MIT",
|
|
15
|
+
"devDependencies": {
|
|
16
|
+
"assemblyscript": "^0.27.1"
|
|
17
|
+
},
|
|
18
|
+
"dependencies": {},
|
|
19
|
+
"repository": {
|
|
20
|
+
"type": "git",
|
|
21
|
+
"url": "git+https://github.com/JairusSW/as-json.git"
|
|
22
|
+
},
|
|
23
|
+
"keywords": [
|
|
24
|
+
"assemblyscript",
|
|
25
|
+
"json",
|
|
26
|
+
"serialize",
|
|
27
|
+
"deserialize",
|
|
28
|
+
"serde"
|
|
29
|
+
],
|
|
30
|
+
"bugs": {
|
|
31
|
+
"url": "https://github.com/JairusSW/as-json/issues"
|
|
32
|
+
},
|
|
33
|
+
"homepage": "https://github.com/JairusSW/as-json#readme",
|
|
34
|
+
"type": "module",
|
|
35
|
+
"exports": "./lib/index.js"
|
|
36
|
+
}
|