json-as 0.5.38 → 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/assembly/__benches__/as-json.ts +19 -11
- package/assembly/src/json.ts +584 -556
- package/assembly/test.ts +9 -5
- package/package.json +1 -1
- package/transform/lib/index.js +5 -1
- package/transform/package.json +1 -1
- package/transform/src/index.ts +16 -10
- package/transform/lib/hash.js +0 -72
- package/transform/lib/types.js +0 -15
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
|
-
import * as a from "util/number"
|
|
3
|
+
import * as a from "util/number";
|
|
4
4
|
@json
|
|
5
5
|
class Vec3 {
|
|
6
6
|
x!: f32;
|
|
@@ -63,7 +63,9 @@ console.log("3 - " + istr8(3));
|
|
|
63
63
|
|
|
64
64
|
console.log(Uint8Array.wrap(changetype<ArrayBuffer>(istr8(12))).join(" "));
|
|
65
65
|
console.log(load<u32>(changetype<usize>(istr8(12))).toString());
|
|
66
|
-
@inline function istr8<
|
|
66
|
+
@inline function istr8<
|
|
67
|
+
T extends number
|
|
68
|
+
>(int: T): string {
|
|
67
69
|
if (int >= 100) {
|
|
68
70
|
const str = changetype<string>(__new(6, idof<String>()));
|
|
69
71
|
store<u16>(changetype<usize>(str), ((int / 100) % 10) + 48);
|
|
@@ -222,9 +224,11 @@ export function istr32<T extends number>(int: T): string {
|
|
|
222
224
|
export function istr64<T extends number>(int: T): string {
|
|
223
225
|
const val = new ArrayBuffer(6);
|
|
224
226
|
store<u16>(changetype<usize>(val), (int % 10) + 48, 4);
|
|
225
|
-
if ((int = int / 10 as T) > 0)
|
|
227
|
+
if ((int = (int / 10) as T) > 0)
|
|
228
|
+
store<u16>(changetype<usize>(val), (int % 10) + 48, 2);
|
|
226
229
|
else return changetype<string>(val);
|
|
227
|
-
if ((int = int / 10 as T) > 0)
|
|
230
|
+
if ((int = (int / 10) as T) > 0)
|
|
231
|
+
store<u16>(changetype<usize>(val), (int % 10) + 48);
|
|
228
232
|
return changetype<string>(val);
|
|
229
233
|
}
|
|
230
234
|
|
|
@@ -239,4 +243,4 @@ export function istr64<T extends number>(int: T): string {
|
|
|
239
243
|
// 8 = 56
|
|
240
244
|
// 9 = 57
|
|
241
245
|
|
|
242
|
-
console.log(JSON.stringify("h\\i from gray\bson"))
|
|
246
|
+
console.log(JSON.stringify("h\\i from gray\bson"));
|
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)
|
|
@@ -65,6 +68,7 @@ class AsJSONTransform extends BaseVisitor {
|
|
|
65
68
|
...(parentSchema ? parentSchema.node.members : []),
|
|
66
69
|
];
|
|
67
70
|
for (const mem of members) {
|
|
71
|
+
// @ts-ignore
|
|
68
72
|
if (mem.type && mem.type.name && mem.type.name.identifier.text) {
|
|
69
73
|
const member = mem;
|
|
70
74
|
if (toString(member).startsWith("static"))
|
package/transform/package.json
CHANGED
package/transform/src/index.ts
CHANGED
|
@@ -23,14 +23,19 @@ class AsJSONTransform extends BaseVisitor {
|
|
|
23
23
|
public currentClass!: SchemaData;
|
|
24
24
|
public sources: Source[] = [];
|
|
25
25
|
|
|
26
|
-
visitMethodDeclaration(): void {
|
|
26
|
+
visitMethodDeclaration(): void {}
|
|
27
27
|
visitClassDeclaration(node: ClassDeclaration): void {
|
|
28
28
|
const className = node.name.text;
|
|
29
29
|
if (!node.decorators?.length) return;
|
|
30
30
|
let foundDecorator = false;
|
|
31
31
|
for (const decorator of node.decorators!) {
|
|
32
|
-
|
|
33
|
-
|
|
32
|
+
if (
|
|
33
|
+
// @ts-ignore
|
|
34
|
+
decorator.name.text.toLowerCase() == "json" ||
|
|
35
|
+
// @ts-ignore
|
|
36
|
+
decorator.name.text.toLowerCase() == "serializable"
|
|
37
|
+
)
|
|
38
|
+
foundDecorator = true;
|
|
34
39
|
}
|
|
35
40
|
if (!foundDecorator) return;
|
|
36
41
|
|
|
@@ -60,8 +65,8 @@ class AsJSONTransform extends BaseVisitor {
|
|
|
60
65
|
} else {
|
|
61
66
|
console.error(
|
|
62
67
|
"Class extends " +
|
|
63
|
-
|
|
64
|
-
|
|
68
|
+
this.currentClass.parent +
|
|
69
|
+
", but parent class not found. Maybe add the @json decorator over parent class?"
|
|
65
70
|
);
|
|
66
71
|
}
|
|
67
72
|
}
|
|
@@ -75,8 +80,9 @@ class AsJSONTransform extends BaseVisitor {
|
|
|
75
80
|
];
|
|
76
81
|
|
|
77
82
|
for (const mem of members) {
|
|
83
|
+
// @ts-ignore
|
|
78
84
|
if (mem.type && mem.type.name && mem.type.name.identifier.text) {
|
|
79
|
-
const member
|
|
85
|
+
const member = mem as FieldDeclaration;
|
|
80
86
|
if (toString(member).startsWith("static")) return;
|
|
81
87
|
const lineText = toString(member);
|
|
82
88
|
if (lineText.startsWith("private")) return;
|
|
@@ -127,7 +133,7 @@ class AsJSONTransform extends BaseVisitor {
|
|
|
127
133
|
if (this.currentClass.encodeStmts.length > 0) {
|
|
128
134
|
const stmt =
|
|
129
135
|
this.currentClass.encodeStmts[
|
|
130
|
-
|
|
136
|
+
this.currentClass.encodeStmts.length - 1
|
|
131
137
|
]!;
|
|
132
138
|
this.currentClass.encodeStmts[this.currentClass.encodeStmts.length - 1] =
|
|
133
139
|
stmt!.slice(0, stmt.length - 1);
|
|
@@ -150,9 +156,9 @@ class AsJSONTransform extends BaseVisitor {
|
|
|
150
156
|
@inline
|
|
151
157
|
__JSON_Set_Key(key: string, value: string): void {
|
|
152
158
|
${
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
159
|
+
// @ts-ignore
|
|
160
|
+
this.currentClass.setDataStmts.join("")
|
|
161
|
+
}
|
|
156
162
|
}
|
|
157
163
|
`;
|
|
158
164
|
|
package/transform/lib/hash.js
DELETED
|
@@ -1,72 +0,0 @@
|
|
|
1
|
-
// XXHash 32-bit as a starting point, see: https://cyan4973.github.io/xxHash
|
|
2
|
-
// primes
|
|
3
|
-
// @ts-ignore: decorator
|
|
4
|
-
const XXH32_P1 = 2654435761;
|
|
5
|
-
// @ts-ignore: decorator
|
|
6
|
-
const XXH32_P2 = 2246822519;
|
|
7
|
-
// @ts-ignore: decorator
|
|
8
|
-
const XXH32_P3 = 3266489917;
|
|
9
|
-
// @ts-ignore: decorator
|
|
10
|
-
const XXH32_P4 = 668265263;
|
|
11
|
-
// @ts-ignore: decorator
|
|
12
|
-
const XXH32_P5 = 374761393;
|
|
13
|
-
// @ts-ignore: decorator
|
|
14
|
-
const XXH32_SEED = 0;
|
|
15
|
-
function hash32(key, len = 4) {
|
|
16
|
-
let h = XXH32_SEED + XXH32_P5 + len;
|
|
17
|
-
h += key * XXH32_P3;
|
|
18
|
-
h = rotl(h, 17) * XXH32_P4;
|
|
19
|
-
h ^= h >> 15;
|
|
20
|
-
h *= XXH32_P2;
|
|
21
|
-
h ^= h >> 13;
|
|
22
|
-
h *= XXH32_P3;
|
|
23
|
-
h ^= h >> 16;
|
|
24
|
-
return h;
|
|
25
|
-
}
|
|
26
|
-
function rotl(x, r) {
|
|
27
|
-
return (x << r) | (x >>> (32 - r));
|
|
28
|
-
}
|
|
29
|
-
function mix(h, key) {
|
|
30
|
-
return rotl(h + key * XXH32_P2, 13) * XXH32_P1;
|
|
31
|
-
}
|
|
32
|
-
export function hashStr(key) {
|
|
33
|
-
if (key == null) return XXH32_SEED;
|
|
34
|
-
let h = key.length;
|
|
35
|
-
let len = h;
|
|
36
|
-
let pos = 0;
|
|
37
|
-
if (len >= 16) {
|
|
38
|
-
let s1 = XXH32_SEED + XXH32_P1 + XXH32_P2;
|
|
39
|
-
let s2 = XXH32_SEED + XXH32_P2;
|
|
40
|
-
let s3 = XXH32_SEED;
|
|
41
|
-
let s4 = XXH32_SEED - XXH32_P1;
|
|
42
|
-
let end = len + pos - 16;
|
|
43
|
-
while (pos <= end) {
|
|
44
|
-
s1 = mix(s1, key.charCodeAt(pos));
|
|
45
|
-
s2 = mix(s2, key.charCodeAt(pos + 1));
|
|
46
|
-
s3 = mix(s3, key.charCodeAt(pos + 2));
|
|
47
|
-
s4 = mix(s4, load(pos, 12));
|
|
48
|
-
pos += 16;
|
|
49
|
-
}
|
|
50
|
-
h += rotl(s1, 1) + rotl(s2, 7) + rotl(s3, 12) + rotl(s4, 18);
|
|
51
|
-
} else {
|
|
52
|
-
h += XXH32_SEED + XXH32_P5;
|
|
53
|
-
}
|
|
54
|
-
let end = changetype(key) + len - 4;
|
|
55
|
-
while (pos <= end) {
|
|
56
|
-
h += load(pos) * XXH32_P3;
|
|
57
|
-
h = rotl(h, 17) * XXH32_P4;
|
|
58
|
-
pos += 4;
|
|
59
|
-
}
|
|
60
|
-
end = changetype(key) + len;
|
|
61
|
-
while (pos < end) {
|
|
62
|
-
h += load(pos) * XXH32_P5;
|
|
63
|
-
h = rotl(h, 11) * XXH32_P1;
|
|
64
|
-
pos++;
|
|
65
|
-
}
|
|
66
|
-
h ^= h >> 15;
|
|
67
|
-
h *= XXH32_P2;
|
|
68
|
-
h ^= h >> 13;
|
|
69
|
-
h *= XXH32_P3;
|
|
70
|
-
h ^= h >> 16;
|
|
71
|
-
return h;
|
|
72
|
-
}
|
package/transform/lib/types.js
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
export var Types;
|
|
2
|
-
(function (Types) {
|
|
3
|
-
Types[(Types["String"] = 0)] = "String";
|
|
4
|
-
Types[(Types["u8"] = 1)] = "u8";
|
|
5
|
-
Types[(Types["i8"] = 2)] = "i8";
|
|
6
|
-
Types[(Types["u16"] = 3)] = "u16";
|
|
7
|
-
Types[(Types["i16"] = 4)] = "i16";
|
|
8
|
-
Types[(Types["u32"] = 5)] = "u32";
|
|
9
|
-
Types[(Types["i32"] = 6)] = "i32";
|
|
10
|
-
Types[(Types["u64"] = 7)] = "u64";
|
|
11
|
-
Types[(Types["i64"] = 8)] = "i64";
|
|
12
|
-
Types[(Types["f32"] = 9)] = "f32";
|
|
13
|
-
Types[(Types["f64"] = 10)] = "f64";
|
|
14
|
-
Types[(Types["boolean"] = 11)] = "boolean";
|
|
15
|
-
})(Types || (Types = {}));
|