json-as 0.4.4 → 0.4.7
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 +9 -5
- package/as-pect.asconfig.json +23 -23
- package/asconfig.json +1 -16
- package/assembly/__benches__/benchmark.ts +33 -27
- package/assembly/__tests__/as-json.spec.ts +84 -0
- package/assembly/chars.ts +2 -1
- package/assembly/index.ts +297 -154
- package/assembly/test.ts +29 -55
- package/assembly/util.ts +30 -21
- package/index.ts +0 -1
- package/package.json +4 -3
- package/transform/lib/index.js +74 -73
- package/transform/package.json +2 -3
- package/transform/src/index.ts +108 -95
- package/assembly/__tests__/example.spec.ts +0 -40
package/assembly/util.ts
CHANGED
|
@@ -1,21 +1,30 @@
|
|
|
1
|
-
import { StringSink } from "as-string-sink/assembly";
|
|
2
|
-
import { isSpace } from "assemblyscript/std/assembly/util/string";
|
|
3
|
-
import { backSlashCode, quoteCode } from "./chars";
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
1
|
+
import { StringSink } from "as-string-sink/assembly";
|
|
2
|
+
import { isSpace } from "assemblyscript/std/assembly/util/string";
|
|
3
|
+
import { backSlashCode, quoteCode } from "./chars";
|
|
4
|
+
// @ts-ignore
|
|
5
|
+
@inline
|
|
6
|
+
export function unsafeCharCodeAt(data: string, pos: i32): i32 {
|
|
7
|
+
return load<u16>(changetype<usize>(data) + ((<usize>pos) << 1));
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export function removeWhitespace(data: string): string {
|
|
11
|
+
const result = new StringSink();
|
|
12
|
+
let instr = false;
|
|
13
|
+
for (let i = 0; i < data.length; i++) {
|
|
14
|
+
const char = data.charCodeAt(i);
|
|
15
|
+
if (instr === false && char === quoteCode) instr = true;
|
|
16
|
+
else if (
|
|
17
|
+
instr === true &&
|
|
18
|
+
char === quoteCode &&
|
|
19
|
+
data.charCodeAt(i - 1) !== backSlashCode
|
|
20
|
+
)
|
|
21
|
+
instr = false;
|
|
22
|
+
|
|
23
|
+
if (instr === false) {
|
|
24
|
+
if (!isSpace(char)) result.write(data.charAt(i));
|
|
25
|
+
} else {
|
|
26
|
+
result.write(data.charAt(i));
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
return result.toString();
|
|
30
|
+
}
|
package/index.ts
CHANGED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export { JSON } from "./assembly/index";
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "json-as",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.7",
|
|
4
4
|
"description": "JSON encoder/decoder for AssemblyScript",
|
|
5
5
|
"types": "assembly/index.ts",
|
|
6
6
|
"author": "Jairus Tanaka",
|
|
@@ -14,7 +14,8 @@
|
|
|
14
14
|
"build:transform": "tsc -p ./transform",
|
|
15
15
|
"test:wasmtime": "wasmtime ./build/test.wasm",
|
|
16
16
|
"test:lunatic": "lunatic ./build/test.wasm",
|
|
17
|
-
"test:wasm3": "wasm3 ./build/test.wasm"
|
|
17
|
+
"test:wasm3": "wasm3 ./build/test.wasm",
|
|
18
|
+
"prettier": "as-prettier -w ."
|
|
18
19
|
},
|
|
19
20
|
"devDependencies": {
|
|
20
21
|
"@as-pect/cli": "^7.0.7",
|
|
@@ -37,7 +38,7 @@
|
|
|
37
38
|
"json",
|
|
38
39
|
"serialize",
|
|
39
40
|
"deserialize",
|
|
40
|
-
"dynamic",
|
|
41
|
+
"dynamic",
|
|
41
42
|
"serde"
|
|
42
43
|
],
|
|
43
44
|
"bugs": {
|
package/transform/lib/index.js
CHANGED
|
@@ -1,73 +1,74 @@
|
|
|
1
|
-
import { ClassDecorator, registerDecorator } from "visitor-as/dist/decorator.js";
|
|
2
|
-
import { getName } from "visitor-as/dist/utils.js";
|
|
3
|
-
import { SimpleParser } from "visitor-as/dist/index.js";
|
|
4
|
-
class AsJSONTransform extends ClassDecorator {
|
|
5
|
-
constructor() {
|
|
6
|
-
super(...arguments);
|
|
7
|
-
this.sources = [];
|
|
8
|
-
this.encodeStmts = [];
|
|
9
|
-
this.decodeStmts = [];
|
|
10
|
-
}
|
|
11
|
-
visitMethodDeclaration(
|
|
12
|
-
visitFieldDeclaration(node) {
|
|
13
|
-
const name = getName(node);
|
|
14
|
-
if (!node.type) {
|
|
15
|
-
throw new Error(`Field ${name} is missing a type declaration`);
|
|
16
|
-
}
|
|
17
|
-
const type = getName(node.type);
|
|
18
|
-
// @ts-ignore
|
|
19
|
-
this.encodeStmts.push(`"${name}":\${JSON.stringify<${type}>(this.${name})},`);
|
|
20
|
-
// @ts-ignore
|
|
21
|
-
this.decodeStmts.push(`${name}: JSON.
|
|
22
|
-
}
|
|
23
|
-
visitClassDeclaration(node) {
|
|
24
|
-
if (!node.members) {
|
|
25
|
-
return;
|
|
26
|
-
}
|
|
27
|
-
this.currentClass = node;
|
|
28
|
-
const name = getName(node);
|
|
29
|
-
this.visit(node.members);
|
|
30
|
-
const serializedProp = `__JSON_Serialized: string = "";`;
|
|
31
|
-
let serializeFunc = ``;
|
|
32
|
-
if (this.encodeStmts.length > 0) {
|
|
33
|
-
const stmt = this.encodeStmts[this.encodeStmts.length - 1];
|
|
34
|
-
this.encodeStmts[this.encodeStmts.length - 1] = stmt.slice(0, stmt.length - 1);
|
|
35
|
-
serializeFunc = `
|
|
36
|
-
@inline
|
|
37
|
-
__JSON_Serialize(): string {
|
|
38
|
-
return \`{${this.encodeStmts.join("")}}\`;
|
|
39
|
-
}
|
|
40
|
-
`;
|
|
41
|
-
}
|
|
42
|
-
else {
|
|
43
|
-
serializeFunc = `
|
|
44
|
-
@inline
|
|
45
|
-
__JSON_Serialize(): string {
|
|
46
|
-
return "{}";
|
|
47
|
-
}
|
|
48
|
-
`;
|
|
49
|
-
}
|
|
50
|
-
const deserializeFunc = `
|
|
51
|
-
@inline
|
|
52
|
-
__JSON_Deserialize(values: Map<string, string>): ${name} {
|
|
53
|
-
return {
|
|
54
|
-
${
|
|
55
|
-
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
this.
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
}
|
|
73
|
-
|
|
1
|
+
import { ClassDecorator, registerDecorator, } from "visitor-as/dist/decorator.js";
|
|
2
|
+
import { getName } from "visitor-as/dist/utils.js";
|
|
3
|
+
import { SimpleParser } from "visitor-as/dist/index.js";
|
|
4
|
+
class AsJSONTransform extends ClassDecorator {
|
|
5
|
+
constructor() {
|
|
6
|
+
super(...arguments);
|
|
7
|
+
this.sources = [];
|
|
8
|
+
this.encodeStmts = [];
|
|
9
|
+
this.decodeStmts = [];
|
|
10
|
+
}
|
|
11
|
+
visitMethodDeclaration() { }
|
|
12
|
+
visitFieldDeclaration(node) {
|
|
13
|
+
const name = getName(node);
|
|
14
|
+
if (!node.type) {
|
|
15
|
+
throw new Error(`Field ${name} is missing a type declaration`);
|
|
16
|
+
}
|
|
17
|
+
const type = getName(node.type);
|
|
18
|
+
// @ts-ignore
|
|
19
|
+
this.encodeStmts.push(`"${name}":\${JSON.stringify<${type}>(this.${name})},`);
|
|
20
|
+
// @ts-ignore
|
|
21
|
+
this.decodeStmts.push(`${name}: JSON.parseObjectValue<${type}>(values.get("${name}")),\n`);
|
|
22
|
+
}
|
|
23
|
+
visitClassDeclaration(node) {
|
|
24
|
+
if (!node.members) {
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
this.currentClass = node;
|
|
28
|
+
const name = getName(node);
|
|
29
|
+
this.visit(node.members);
|
|
30
|
+
const serializedProp = `__JSON_Serialized: string = "";`;
|
|
31
|
+
let serializeFunc = ``;
|
|
32
|
+
if (this.encodeStmts.length > 0) {
|
|
33
|
+
const stmt = this.encodeStmts[this.encodeStmts.length - 1];
|
|
34
|
+
this.encodeStmts[this.encodeStmts.length - 1] = stmt.slice(0, stmt.length - 1);
|
|
35
|
+
serializeFunc = `
|
|
36
|
+
@inline
|
|
37
|
+
__JSON_Serialize(): string {
|
|
38
|
+
return \`{${this.encodeStmts.join("")}}\`;
|
|
39
|
+
}
|
|
40
|
+
`;
|
|
41
|
+
}
|
|
42
|
+
else {
|
|
43
|
+
serializeFunc = `
|
|
44
|
+
@inline
|
|
45
|
+
__JSON_Serialize(): string {
|
|
46
|
+
return "{}";
|
|
47
|
+
}
|
|
48
|
+
`;
|
|
49
|
+
}
|
|
50
|
+
const deserializeFunc = `
|
|
51
|
+
@inline
|
|
52
|
+
__JSON_Deserialize(values: Map<string, string>): ${name} {
|
|
53
|
+
return {
|
|
54
|
+
${
|
|
55
|
+
// @ts-ignore
|
|
56
|
+
this.decodeStmts.join("")}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
`;
|
|
60
|
+
this.encodeStmts = [];
|
|
61
|
+
this.decodeStmts = [];
|
|
62
|
+
//console.log(serializeFunc, deserializeFunc)
|
|
63
|
+
const serializedProperty = SimpleParser.parseClassMember(serializedProp, node);
|
|
64
|
+
node.members.push(serializedProperty);
|
|
65
|
+
const serializeMethod = SimpleParser.parseClassMember(serializeFunc, node);
|
|
66
|
+
node.members.push(serializeMethod);
|
|
67
|
+
const deserializeMethod = SimpleParser.parseClassMember(deserializeFunc, node);
|
|
68
|
+
node.members.push(deserializeMethod);
|
|
69
|
+
}
|
|
70
|
+
get name() {
|
|
71
|
+
return "json";
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
export default registerDecorator(new AsJSONTransform());
|
package/transform/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
{
|
|
1
|
+
7{
|
|
2
2
|
"name": "@json-as/transform",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.7",
|
|
4
4
|
"description": "JSON encoder/decoder for AssemblyScript",
|
|
5
5
|
"main": "./lib/index.js",
|
|
6
6
|
"author": "Jairus Tanaka",
|
|
@@ -8,7 +8,6 @@
|
|
|
8
8
|
"DogWhich"
|
|
9
9
|
],
|
|
10
10
|
"license": "MIT",
|
|
11
|
-
"scripts": {},
|
|
12
11
|
"devDependencies": {},
|
|
13
12
|
"dependencies": {
|
|
14
13
|
"visitor-as": "^0.10.2"
|
package/transform/src/index.ts
CHANGED
|
@@ -1,95 +1,108 @@
|
|
|
1
|
-
import {
|
|
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
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
`
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
`
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
1
|
+
import {
|
|
2
|
+
ClassDeclaration,
|
|
3
|
+
FieldDeclaration,
|
|
4
|
+
Source,
|
|
5
|
+
} from "assemblyscript/dist/assemblyscript";
|
|
6
|
+
import {
|
|
7
|
+
ClassDecorator,
|
|
8
|
+
registerDecorator,
|
|
9
|
+
} from "visitor-as/dist/decorator.js";
|
|
10
|
+
import { getName } from "visitor-as/dist/utils.js";
|
|
11
|
+
import { SimpleParser } from "visitor-as/dist/index.js";
|
|
12
|
+
|
|
13
|
+
class AsJSONTransform extends ClassDecorator {
|
|
14
|
+
public currentClass!: ClassDeclaration;
|
|
15
|
+
public sources: Source[] = [];
|
|
16
|
+
public encodeStmts: string[] = [];
|
|
17
|
+
public decodeStmts: string[] = [];
|
|
18
|
+
|
|
19
|
+
visitMethodDeclaration(): void {}
|
|
20
|
+
visitFieldDeclaration(node: FieldDeclaration): void {
|
|
21
|
+
const name = getName(node);
|
|
22
|
+
if (!node.type) {
|
|
23
|
+
throw new Error(`Field ${name} is missing a type declaration`);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const type = getName(node.type);
|
|
27
|
+
|
|
28
|
+
// @ts-ignore
|
|
29
|
+
this.encodeStmts.push(
|
|
30
|
+
`"${name}":\${JSON.stringify<${type}>(this.${name})},`
|
|
31
|
+
);
|
|
32
|
+
|
|
33
|
+
// @ts-ignore
|
|
34
|
+
this.decodeStmts.push(
|
|
35
|
+
`${name}: JSON.parseObjectValue<${type}>(values.get("${name}")),\n`
|
|
36
|
+
);
|
|
37
|
+
}
|
|
38
|
+
visitClassDeclaration(node: ClassDeclaration): void {
|
|
39
|
+
if (!node.members) {
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
this.currentClass = node;
|
|
44
|
+
|
|
45
|
+
const name = getName(node);
|
|
46
|
+
|
|
47
|
+
this.visit(node.members);
|
|
48
|
+
|
|
49
|
+
const serializedProp = `__JSON_Serialized: string = "";`;
|
|
50
|
+
|
|
51
|
+
let serializeFunc = ``;
|
|
52
|
+
|
|
53
|
+
if (this.encodeStmts.length > 0) {
|
|
54
|
+
const stmt = this.encodeStmts[this.encodeStmts.length - 1]!;
|
|
55
|
+
this.encodeStmts[this.encodeStmts.length - 1] = stmt!.slice(
|
|
56
|
+
0,
|
|
57
|
+
stmt.length - 1
|
|
58
|
+
);
|
|
59
|
+
serializeFunc = `
|
|
60
|
+
@inline
|
|
61
|
+
__JSON_Serialize(): string {
|
|
62
|
+
return \`{${this.encodeStmts.join("")}}\`;
|
|
63
|
+
}
|
|
64
|
+
`;
|
|
65
|
+
} else {
|
|
66
|
+
serializeFunc = `
|
|
67
|
+
@inline
|
|
68
|
+
__JSON_Serialize(): string {
|
|
69
|
+
return "{}";
|
|
70
|
+
}
|
|
71
|
+
`;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const deserializeFunc = `
|
|
75
|
+
@inline
|
|
76
|
+
__JSON_Deserialize(values: Map<string, string>): ${name} {
|
|
77
|
+
return {
|
|
78
|
+
${
|
|
79
|
+
// @ts-ignore
|
|
80
|
+
this.decodeStmts.join("")
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
`;
|
|
85
|
+
this.encodeStmts = [];
|
|
86
|
+
this.decodeStmts = [];
|
|
87
|
+
//console.log(serializeFunc, deserializeFunc)
|
|
88
|
+
const serializedProperty = SimpleParser.parseClassMember(
|
|
89
|
+
serializedProp,
|
|
90
|
+
node
|
|
91
|
+
);
|
|
92
|
+
node.members.push(serializedProperty);
|
|
93
|
+
|
|
94
|
+
const serializeMethod = SimpleParser.parseClassMember(serializeFunc, node);
|
|
95
|
+
node.members.push(serializeMethod);
|
|
96
|
+
|
|
97
|
+
const deserializeMethod = SimpleParser.parseClassMember(
|
|
98
|
+
deserializeFunc,
|
|
99
|
+
node
|
|
100
|
+
);
|
|
101
|
+
node.members.push(deserializeMethod);
|
|
102
|
+
}
|
|
103
|
+
get name(): string {
|
|
104
|
+
return "json";
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
export default registerDecorator(new AsJSONTransform());
|
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
import { JSON } from "../"
|
|
2
|
-
describe("JSON Stringify Test", () => {
|
|
3
|
-
it("Stringify String", () => {
|
|
4
|
-
expect<string>().toBe(42, "19 + 23 is 42");
|
|
5
|
-
});
|
|
6
|
-
|
|
7
|
-
it("should be the same reference", () => {
|
|
8
|
-
let ref = new Vec3();
|
|
9
|
-
expect<Vec3>(ref).toBe(ref, "Reference Equality");
|
|
10
|
-
});
|
|
11
|
-
|
|
12
|
-
it("should perform a memory comparison", () => {
|
|
13
|
-
let a = new Vec3(1, 2, 3);
|
|
14
|
-
let b = new Vec3(1, 2, 3);
|
|
15
|
-
|
|
16
|
-
expect<Vec3>(a).toStrictEqual(
|
|
17
|
-
b,
|
|
18
|
-
"a and b have the same values, (discluding child references)",
|
|
19
|
-
);
|
|
20
|
-
});
|
|
21
|
-
|
|
22
|
-
it("should compare strings", () => {
|
|
23
|
-
expect<string>("a=" + "200").toBe("a=200", "both strings are equal");
|
|
24
|
-
});
|
|
25
|
-
|
|
26
|
-
it("should compare values", () => {
|
|
27
|
-
expect<i32>(10).toBeLessThan(200);
|
|
28
|
-
expect<i32>(1000).toBeGreaterThan(200);
|
|
29
|
-
expect<i32>(1000).toBeGreaterThanOrEqual(1000);
|
|
30
|
-
expect<i32>(1000).toBeLessThanOrEqual(1000);
|
|
31
|
-
});
|
|
32
|
-
|
|
33
|
-
it("can log some values to the console", () => {
|
|
34
|
-
log<string>("Hello world!"); // strings!
|
|
35
|
-
log<f64>(3.1415); // floats!
|
|
36
|
-
log<u8>(244); // integers!
|
|
37
|
-
log<u64>(0xffffffff); // long values!
|
|
38
|
-
log<ArrayBuffer>(new ArrayBuffer(50)); // bytes!
|
|
39
|
-
});
|
|
40
|
-
});
|