json-as 0.4.4 → 0.4.5

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