json-as 0.4.3 → 0.4.6

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "json-as",
3
- "version": "0.4.3",
3
+ "version": "0.4.6",
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/tests/index.js CHANGED
File without changes
package/tests/test.js CHANGED
File without changes
@@ -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(node) { }
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.parse<${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
- ${ // @ts-ignore
55
- this.decodeStmts.join("")}
56
- }
57
- }
58
- `;
59
- this.encodeStmts = [];
60
- this.decodeStmts = [];
61
- console.log(serializeFunc, deserializeFunc);
62
- const serializedProperty = SimpleParser.parseClassMember(serializedProp, node);
63
- node.members.push(serializedProperty);
64
- const serializeMethod = SimpleParser.parseClassMember(serializeFunc, node);
65
- node.members.push(serializeMethod);
66
- const deserializeMethod = SimpleParser.parseClassMember(deserializeFunc, node);
67
- node.members.push(deserializeMethod);
68
- }
69
- get name() {
70
- return "json";
71
- }
72
- }
73
- export default registerDecorator(new AsJSONTransform());
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());
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@json-as/transform",
3
- "version": "0.4.3",
3
+ "version": "0.4.6",
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"
File without changes
@@ -1,95 +1,108 @@
1
- import {
2
- ClassDeclaration,
3
- FieldDeclaration,
4
- MethodDeclaration,
5
- Source
6
- } from "assemblyscript/dist/assemblyscript";
7
- import { ClassDecorator, registerDecorator } from "visitor-as/dist/decorator.js";
8
- import { getName } from "visitor-as/dist/utils.js";
9
- import { SimpleParser } from "visitor-as/dist/index.js";
10
-
11
- class AsJSONTransform extends ClassDecorator {
12
- public currentClass!: ClassDeclaration;
13
- public sources: Source[] = [];
14
- public encodeStmts: string[] = [];
15
- public decodeStmts: string[] = [];
16
-
17
- visitMethodDeclaration(node: MethodDeclaration): void {}
18
- visitFieldDeclaration(node: FieldDeclaration): void {
19
- const name = getName(node);
20
- if (!node.type) {
21
- throw new Error(`Field ${name} is missing a type declaration`);
22
- }
23
-
24
- const type = getName(node.type);
25
-
26
- // @ts-ignore
27
- this.encodeStmts.push(
28
- `"${name}":\${JSON.stringify<${type}>(this.${name})},`
29
- );
30
-
31
- // @ts-ignore
32
- this.decodeStmts.push(
33
- `${name}: JSON.parse<${type}>(values.get("${name}")),\n`
34
- );
35
- }
36
- visitClassDeclaration(node: ClassDeclaration): void {
37
- if (!node.members) {
38
- return;
39
- }
40
-
41
- this.currentClass = node;
42
-
43
- const name = getName(node);
44
-
45
- this.visit(node.members);
46
-
47
- const serializedProp = `__JSON_Serialized: string = "";`
48
-
49
- let serializeFunc = ``
50
-
51
- if (this.encodeStmts.length > 0) {
52
- const stmt = this.encodeStmts[this.encodeStmts.length - 1]!
53
- this.encodeStmts[this.encodeStmts.length - 1] = stmt!.slice(0, stmt.length - 1)
54
- serializeFunc = `
55
- @inline
56
- __JSON_Serialize(): string {
57
- return \`{${this.encodeStmts.join("")}}\`;
58
- }
59
- `
60
- } else {
61
- serializeFunc = `
62
- @inline
63
- __JSON_Serialize(): string {
64
- return "{}";
65
- }
66
- `
67
- }
68
-
69
- const deserializeFunc = `
70
- @inline
71
- __JSON_Deserialize(values: Map<string, string>): ${name} {
72
- return {
73
- ${// @ts-ignore
74
- this.decodeStmts.join("")}
75
- }
76
- }
77
- `;
78
- this.encodeStmts = [];
79
- this.decodeStmts = [];
80
- console.log(serializeFunc, deserializeFunc)
81
- const serializedProperty = SimpleParser.parseClassMember(serializedProp, node);
82
- node.members.push(serializedProperty);
83
-
84
- const serializeMethod = SimpleParser.parseClassMember(serializeFunc, node);
85
- node.members.push(serializeMethod);
86
-
87
- const deserializeMethod = SimpleParser.parseClassMember(deserializeFunc, node);
88
- node.members.push(deserializeMethod);
89
- }
90
- get name(): string {
91
- return "json";
92
- }
93
- }
94
-
95
- export default registerDecorator(new AsJSONTransform());
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());
File without changes
@@ -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
- });