json-as 0.4.9 → 0.5.1
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/LICENSE +0 -0
- package/README.md +16 -46
- package/as-pect.asconfig.json +0 -0
- package/as-pect.config.js +0 -0
- package/asconfig.json +0 -0
- package/assembly/__benches__/as-json.ts +0 -3
- package/assembly/__benches__/as-tral.d.ts +0 -0
- package/assembly/__tests__/as-json.spec.ts +25 -1
- package/assembly/__tests__/as-pect.d.ts +0 -0
- package/assembly/chars.ts +1 -0
- package/assembly/index.ts +1 -457
- package/assembly/json.ts +463 -0
- package/assembly/test.ts +7 -11
- package/assembly/tsconfig.json +0 -0
- package/assembly/type.ts +10 -0
- package/assembly/util.ts +0 -0
- package/index.ts +0 -0
- package/package.json +50 -51
- package/tests/index.js +0 -0
- package/tests/test.js +0 -0
- package/transform/lib/index.js +12 -7
- package/transform/package.json +1 -1
- package/transform/src/index.ts +14 -12
- package/transform/tsconfig.json +0 -0
- package/assembly/__tests__/as-json.spec.wasm +0 -0
- package/assembly/__tests__/as-json.spec.wat +0 -18630
- package/transform/src/index.old.js +0 -430
package/transform/lib/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { ClassDecorator, registerDecorator, } from "visitor-as/dist/decorator.js";
|
|
2
|
-
import { getName } from "visitor-as/dist/utils.js";
|
|
2
|
+
import { getName, toString } from "visitor-as/dist/utils.js";
|
|
3
3
|
import { SimpleParser } from "visitor-as/dist/index.js";
|
|
4
4
|
class AsJSONTransform extends ClassDecorator {
|
|
5
5
|
constructor() {
|
|
@@ -7,28 +7,33 @@ class AsJSONTransform extends ClassDecorator {
|
|
|
7
7
|
this.sources = [];
|
|
8
8
|
this.encodeStmts = [];
|
|
9
9
|
this.decodeStmts = [];
|
|
10
|
+
this.checkDecodeStmts = [];
|
|
10
11
|
}
|
|
11
12
|
visitMethodDeclaration() { }
|
|
12
13
|
visitFieldDeclaration(node) {
|
|
14
|
+
const lineText = toString(node);
|
|
15
|
+
if (lineText.startsWith("private"))
|
|
16
|
+
return;
|
|
13
17
|
const name = getName(node);
|
|
14
18
|
if (!node.type) {
|
|
15
19
|
throw new Error(`Field ${name} is missing a type declaration`);
|
|
16
20
|
}
|
|
17
|
-
|
|
21
|
+
let type = getName(node.type);
|
|
18
22
|
// @ts-ignore
|
|
19
23
|
this.encodeStmts.push(`"${name}":\${JSON.stringify<${type}>(this.${name})},`);
|
|
20
24
|
// @ts-ignore
|
|
21
25
|
this.decodeStmts.push(`${name}: JSON.parseObjectValue<${type}>(values.get("${name}")),\n`);
|
|
26
|
+
// @ts-ignore
|
|
27
|
+
this.checkDecodeStmts.push(' if (!values.has("${name}")) throw new Error("Key "${name}" was not found. Cannot instantiate object.");\n');
|
|
22
28
|
}
|
|
23
29
|
visitClassDeclaration(node) {
|
|
24
30
|
if (!node.members) {
|
|
25
31
|
return;
|
|
26
32
|
}
|
|
27
33
|
this.currentClass = node;
|
|
28
|
-
const name = getName(node);
|
|
29
34
|
this.visit(node.members);
|
|
30
|
-
const serializedProp =
|
|
31
|
-
let serializeFunc =
|
|
35
|
+
const serializedProp = '__JSON_Serialized: string = "";';
|
|
36
|
+
let serializeFunc = "";
|
|
32
37
|
if (this.encodeStmts.length > 0) {
|
|
33
38
|
const stmt = this.encodeStmts[this.encodeStmts.length - 1];
|
|
34
39
|
this.encodeStmts[this.encodeStmts.length - 1] = stmt.slice(0, stmt.length - 1);
|
|
@@ -49,7 +54,8 @@ class AsJSONTransform extends ClassDecorator {
|
|
|
49
54
|
}
|
|
50
55
|
const deserializeFunc = `
|
|
51
56
|
@inline
|
|
52
|
-
__JSON_Deserialize(values: Map<string, string>):
|
|
57
|
+
__JSON_Deserialize<T>(values: Map<string, string>): T {
|
|
58
|
+
${process.argv.includes("--debugJSON") ? this.checkDecodeStmts.join("else") : ""}
|
|
53
59
|
return {
|
|
54
60
|
${
|
|
55
61
|
// @ts-ignore
|
|
@@ -59,7 +65,6 @@ class AsJSONTransform extends ClassDecorator {
|
|
|
59
65
|
`;
|
|
60
66
|
this.encodeStmts = [];
|
|
61
67
|
this.decodeStmts = [];
|
|
62
|
-
//console.log(serializeFunc, deserializeFunc)
|
|
63
68
|
const serializedProperty = SimpleParser.parseClassMember(serializedProp, node);
|
|
64
69
|
node.members.push(serializedProperty);
|
|
65
70
|
const serializeMethod = SimpleParser.parseClassMember(serializeFunc, node);
|
package/transform/package.json
CHANGED
package/transform/src/index.ts
CHANGED
|
@@ -7,7 +7,7 @@ import {
|
|
|
7
7
|
ClassDecorator,
|
|
8
8
|
registerDecorator,
|
|
9
9
|
} from "visitor-as/dist/decorator.js";
|
|
10
|
-
import { getName } from "visitor-as/dist/utils.js";
|
|
10
|
+
import { getName, toString } from "visitor-as/dist/utils.js";
|
|
11
11
|
import { SimpleParser } from "visitor-as/dist/index.js";
|
|
12
12
|
|
|
13
13
|
class AsJSONTransform extends ClassDecorator {
|
|
@@ -15,16 +15,18 @@ class AsJSONTransform extends ClassDecorator {
|
|
|
15
15
|
public sources: Source[] = [];
|
|
16
16
|
public encodeStmts: string[] = [];
|
|
17
17
|
public decodeStmts: string[] = [];
|
|
18
|
+
public checkDecodeStmts: string[] = [];
|
|
18
19
|
|
|
19
20
|
visitMethodDeclaration(): void {}
|
|
20
21
|
visitFieldDeclaration(node: FieldDeclaration): void {
|
|
22
|
+
const lineText = toString(node);
|
|
23
|
+
if (lineText.startsWith("private")) return;
|
|
21
24
|
const name = getName(node);
|
|
22
25
|
if (!node.type) {
|
|
23
26
|
throw new Error(`Field ${name} is missing a type declaration`);
|
|
24
27
|
}
|
|
25
28
|
|
|
26
|
-
|
|
27
|
-
|
|
29
|
+
let type = getName(node.type);
|
|
28
30
|
// @ts-ignore
|
|
29
31
|
this.encodeStmts.push(
|
|
30
32
|
`"${name}":\${JSON.stringify<${type}>(this.${name})},`
|
|
@@ -34,6 +36,9 @@ class AsJSONTransform extends ClassDecorator {
|
|
|
34
36
|
this.decodeStmts.push(
|
|
35
37
|
`${name}: JSON.parseObjectValue<${type}>(values.get("${name}")),\n`
|
|
36
38
|
);
|
|
39
|
+
|
|
40
|
+
// @ts-ignore
|
|
41
|
+
this.checkDecodeStmts.push(' if (!values.has("${name}")) throw new Error("Key "${name}" was not found. Cannot instantiate object.");\n')
|
|
37
42
|
}
|
|
38
43
|
visitClassDeclaration(node: ClassDeclaration): void {
|
|
39
44
|
if (!node.members) {
|
|
@@ -42,13 +47,11 @@ class AsJSONTransform extends ClassDecorator {
|
|
|
42
47
|
|
|
43
48
|
this.currentClass = node;
|
|
44
49
|
|
|
45
|
-
const name = getName(node);
|
|
46
|
-
|
|
47
50
|
this.visit(node.members);
|
|
48
51
|
|
|
49
|
-
const serializedProp =
|
|
52
|
+
const serializedProp = '__JSON_Serialized: string = "";';
|
|
50
53
|
|
|
51
|
-
let serializeFunc =
|
|
54
|
+
let serializeFunc = "";
|
|
52
55
|
|
|
53
56
|
if (this.encodeStmts.length > 0) {
|
|
54
57
|
const stmt = this.encodeStmts[this.encodeStmts.length - 1]!;
|
|
@@ -70,21 +73,20 @@ class AsJSONTransform extends ClassDecorator {
|
|
|
70
73
|
}
|
|
71
74
|
`;
|
|
72
75
|
}
|
|
73
|
-
|
|
74
76
|
const deserializeFunc = `
|
|
75
77
|
@inline
|
|
76
|
-
__JSON_Deserialize(values: Map<string, string>):
|
|
78
|
+
__JSON_Deserialize<T>(values: Map<string, string>): T {
|
|
79
|
+
${process.argv.includes("--debugJSON") ? this.checkDecodeStmts.join("else") : ""}
|
|
77
80
|
return {
|
|
78
81
|
${
|
|
79
|
-
|
|
80
|
-
|
|
82
|
+
// @ts-ignore
|
|
83
|
+
this.decodeStmts.join("")
|
|
81
84
|
}
|
|
82
85
|
}
|
|
83
86
|
}
|
|
84
87
|
`;
|
|
85
88
|
this.encodeStmts = [];
|
|
86
89
|
this.decodeStmts = [];
|
|
87
|
-
//console.log(serializeFunc, deserializeFunc)
|
|
88
90
|
const serializedProperty = SimpleParser.parseClassMember(
|
|
89
91
|
serializedProp,
|
|
90
92
|
node
|
package/transform/tsconfig.json
CHANGED
|
File without changes
|
|
Binary file
|