json-as 0.5.32 → 0.5.33

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/asconfig.json CHANGED
@@ -12,6 +12,9 @@
12
12
  "options": {
13
13
  "transform": [
14
14
  "./transform"
15
- ]
16
- }
15
+ ],
16
+ "bindings": "esm",
17
+ "exportStart": "_start"
18
+ },
19
+ "extends": "./node_modules/@assemblyscript/wasi-shim/asconfig.json"
17
20
  }
@@ -1,6 +1,7 @@
1
1
  import { JSON } from "..";
2
2
  import { backSlashCode, quoteCode } from "../src/chars";
3
3
  import { atoi_fast, unsafeCharCodeAt } from "../src/util";
4
+ import { HASH } from "util/hash";
4
5
 
5
6
  @json
6
7
  class Vec3 {
@@ -23,28 +24,28 @@ class Vec3 {
23
24
  if (inStr === false && char === quoteCode) {
24
25
  if (key != null) {
25
26
  if (unsafeCharCodeAt(key, 0) == 120) {
26
- to.x = atoi_fast<i32>(data.slice(last, pos - 1))
27
+ to.x = atoi_fast<i32>(data.substring(last, pos - 1))
27
28
  } else if (unsafeCharCodeAt(key, 0) == 121) {
28
- to.y = atoi_fast<i32>(data.slice(last, pos - 1))
29
+ to.y = atoi_fast<i32>(data.substring(last, pos - 1))
29
30
  } else if (unsafeCharCodeAt(key, 0) == 122) {
30
- to.z = atoi_fast<i32>(data.slice(last, pos - 1))
31
+ to.z = atoi_fast<i32>(data.substring(last, pos - 1))
31
32
  }
32
33
  }
33
34
  last = ++pos;
34
35
  inStr = true;
35
36
  } else if (char === quoteCode && unsafeCharCodeAt(data, pos - 1) != backSlashCode) {
36
37
  inStr = false;
37
- key = data.slice(last, pos);
38
+ key = data.substring(last, pos);
38
39
  last = pos += 2;
39
40
  }
40
41
  }
41
42
  if (key != null) {
42
43
  if (unsafeCharCodeAt(key, 0) == 120) {
43
- to.x = atoi_fast<i32>(data.slice(last, pos - 1))
44
+ to.x = atoi_fast<i32>(data.substring(last, pos - 1))
44
45
  } else if (unsafeCharCodeAt(key, 0) == 121) {
45
- to.y = atoi_fast<i32>(data.slice(last, pos - 1))
46
+ to.y = atoi_fast<i32>(data.substring(last, pos - 1))
46
47
  } else if (unsafeCharCodeAt(key, 0) == 122) {
47
- to.z = atoi_fast<i32>(data.slice(last, pos - 1))
48
+ to.z = atoi_fast<i32>(data.substring(last, pos - 1))
48
49
  }
49
50
  }
50
51
  return to;
@@ -65,6 +66,9 @@ bench("Stringify Object (Vec3)", () => {
65
66
  blackbox<string>(vec.__JSON_Serialize(vec));
66
67
  });*/
67
68
 
69
+ bench("HASH String", () => {
70
+ blackbox<number>(HASH("Hello"));
71
+ })
68
72
  // TODO: Make this allocate without crashing
69
73
  bench("Parse Object (Vec3)", () => {
70
74
  blackbox<Vec3>(vec.__JSON_Deserialize('{"x":0,"y":0,"z":0}', vec));
package/assembly/test.ts CHANGED
@@ -5,66 +5,22 @@ import { atoi_fast, unsafeCharCodeAt } from "./src/util";
5
5
  // @json or @serializable work here
6
6
  @json
7
7
  class Vec3 {
8
- x!: f32;
9
- y!: f32;
10
- z!: f32;
11
-
12
- @inline
13
- __JSON_Serialize(): string {
14
- return `{"x":${this.x.toString()},"y":${this.y.toString()},"z":${this.z.toString()}}`;
15
- }
16
-
17
- @inline
18
- __JSON_Deserialize(data: string, to: Vec3): Vec3 {
19
- let last = 1;
20
- let char = 0;
21
- let inStr = false;
22
- let key: string | null = null;
23
- let pos = 0;
24
- for (; pos < data.length - 1; pos++) {
25
- char = unsafeCharCodeAt(data, pos);
26
- if (inStr === false && char === quoteCode) {
27
- if (key != null) {
28
- if (key == "x") {
29
- to.x = f32.parse(data.slice(last, pos - 1))
30
- } else if (key == "y") {
31
- to.y = f32.parse(data.slice(last, pos - 1))
32
- } else if (key == "z") {
33
- to.z = f32.parse(data.slice(last, pos - 1))
34
- }
35
- }
36
- last = ++pos;
37
- inStr = true;
38
- } else if (char === quoteCode && unsafeCharCodeAt(data, pos - 1) != backSlashCode) {
39
- inStr = false;
40
- key = data.slice(last, pos);
41
- last = pos += 2;
42
- }
43
- }
44
- if (key != null) {
45
- if (key == "x") {
46
- to.x = f32.parse(data.slice(last, pos - 1))
47
- } else if (key == "y") {
48
- to.y = f32.parse(data.slice(last, pos - 1))
49
- } else if (key == "z") {
50
- to.z = f32.parse(data.slice(last, pos - 1))
51
- }
52
- }
53
- return to;
54
- }
8
+ x: i32;
9
+ y: i32;
10
+ z: i32;
55
11
  }
56
12
 
57
13
  const vec: Vec3 = {
58
- x: 3.4,
59
- y: 1.2,
60
- z: 8.3
14
+ x: 3,
15
+ y: 1,
16
+ z: 8
61
17
  }
62
18
 
63
- const serializedVec3 = vec.__JSON_Serialize();
19
+ const serializedVec3 = JSON.stringify(vec);
64
20
  console.log(serializedVec3);
65
21
 
66
- const parsedVec3 = vec.__JSON_Deserialize(serializedVec3, new Vec3());
67
- console.log(parsedVec3.__JSON_Serialize());
22
+ const parsedVec3 = JSON.parse<Vec3>(serializedVec3);
23
+ console.log(JSON.stringify(parsedVec3));
68
24
 
69
25
  console.log(`atoi_fast("429496729"): ${atoi_fast<i32>("429496729")}`);
70
26
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "json-as",
3
- "version": "0.5.32",
3
+ "version": "0.5.33",
4
4
  "description": "JSON encoder/decoder for AssemblyScript",
5
5
  "types": "assembly/index.ts",
6
6
  "author": "Jairus Tanaka",
@@ -1,4 +1,4 @@
1
- import { getName, toString, isStdlib } from "visitor-as/dist/utils.js";
1
+ import { toString, isStdlib } from "visitor-as/dist/utils.js";
2
2
  import { BaseVisitor, SimpleParser } from "visitor-as/dist/index.js";
3
3
  import { Transform } from "assemblyscript/dist/transform.js";
4
4
  class SchemaData {
@@ -19,41 +19,6 @@ class AsJSONTransform extends BaseVisitor {
19
19
  this.sources = [];
20
20
  }
21
21
  visitMethodDeclaration() { }
22
- visitFieldDeclaration(node) {
23
- if (toString(node).startsWith("static"))
24
- return;
25
- const lineText = toString(node);
26
- if (lineText.startsWith("private"))
27
- return;
28
- const name = getName(node);
29
- if (!node.type) {
30
- throw new Error(`Field ${name} is missing a type declaration`);
31
- }
32
- let type = getName(node.type);
33
- // @ts-ignore
34
- if (["u8", "i8", "u16", "i16", "u32", "i32", "f32", "u64", "i64", "f64"].includes(type.toLowerCase())) {
35
- this.currentClass.encodeStmts.push(`"${name}":\${this.${name}.toString()},`);
36
- }
37
- else {
38
- this.currentClass.encodeStmts.push(`"${name}":\${JSON.stringify<${type}>(this.${name})},`);
39
- }
40
- this.currentClass.keys.push(name);
41
- this.currentClass.types.push(type);
42
- // @ts-ignore
43
- //this.decodeStmts.push(
44
- // `${name}: JSON.parseObjectValue<${type}>(values.get("${name}")),\n`
45
- //);
46
- // @ts-ignore
47
- this.currentClass.setDataStmts.push(`if (key == "${name}") {
48
- this.${name} = JSON.parseObjectValue<${type}>(value);
49
- return;
50
- }
51
- `);
52
- // @ts-ignore
53
- //this.checkDecodeStmts.push(
54
- // ' if (!values.has("${name}")) throw new Error("Key "${name}" was not found. Cannot instantiate object.");\n'
55
- //);
56
- }
57
22
  visitClassDeclaration(node) {
58
23
  var _c;
59
24
  const className = node.name.text;
@@ -67,7 +32,7 @@ class AsJSONTransform extends BaseVisitor {
67
32
  }
68
33
  if (!foundDecorator)
69
34
  return;
70
- // Prevent from being triggered twice
35
+ // Prevent from being triggered twice.
71
36
  for (const member of node.members) {
72
37
  if (member.name.text == "__JSON_Serialize")
73
38
  return;
@@ -94,7 +59,35 @@ class AsJSONTransform extends BaseVisitor {
94
59
  }
95
60
  const parentSchema = this.schemasList.find((v) => v.name == this.currentClass.parent);
96
61
  const members = [...node.members, ...(parentSchema ? parentSchema.node.members : [])];
97
- this.visit(members);
62
+ for (const mem of members) {
63
+ if (mem.type && mem.type.name && mem.type.name.identifier.text) {
64
+ const member = mem;
65
+ if (toString(member).startsWith("static"))
66
+ return;
67
+ const lineText = toString(member);
68
+ if (lineText.startsWith("private"))
69
+ return;
70
+ // @ts-ignore
71
+ const type = member.type.name.identifier.text;
72
+ const name = member.name.text;
73
+ this.currentClass.keys.push(name);
74
+ // @ts-ignore
75
+ this.currentClass.types.push(type);
76
+ // @ts-ignore
77
+ if (["u8", "i8", "u16", "i16", "u32", "i32", "f32", "u64", "i64", "f64"].includes(type.toLowerCase())) {
78
+ this.currentClass.encodeStmts.push(`"${name}":\${this.${name}.toString()},`);
79
+ }
80
+ else {
81
+ this.currentClass.encodeStmts.push(`"${name}":\${JSON.stringify<${type}>(this.${name})},`);
82
+ }
83
+ // @ts-ignore
84
+ this.currentClass.setDataStmts.push(`if (key == "${name}") {
85
+ this.${name} = JSON.parseObjectValue<${type}>(value);
86
+ return;
87
+ }
88
+ `);
89
+ }
90
+ }
98
91
  let serializeFunc = "";
99
92
  if (this.currentClass.encodeStmts.length > 0) {
100
93
  const stmt = this.currentClass.encodeStmts[this.currentClass.encodeStmts.length - 1];
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@json-as/transform",
3
- "version": "0.5.32",
3
+ "version": "0.5.33",
4
4
  "description": "JSON encoder/decoder for AssemblyScript",
5
5
  "main": "./lib/index.js",
6
6
  "author": "Jairus Tanaka",
@@ -10,7 +10,9 @@
10
10
  "Rom"
11
11
  ],
12
12
  "license": "MIT",
13
- "devDependencies": {},
13
+ "devDependencies": {
14
+ "assemblyscript": "^0.27.1"
15
+ },
14
16
  "dependencies": {},
15
17
  "repository": {
16
18
  "type": "git",
@@ -4,7 +4,7 @@ import {
4
4
  Source,
5
5
  Parser
6
6
  } from "assemblyscript/dist/assemblyscript";
7
- import { getName, toString, isStdlib } from "visitor-as/dist/utils.js";
7
+ import { toString, isStdlib } from "visitor-as/dist/utils.js";
8
8
  import { BaseVisitor, SimpleParser } from "visitor-as/dist/index.js";
9
9
  import { Transform } from "assemblyscript/dist/transform.js";
10
10
 
@@ -24,48 +24,6 @@ class AsJSONTransform extends BaseVisitor {
24
24
  public sources: Source[] = [];
25
25
 
26
26
  visitMethodDeclaration(): void { }
27
- visitFieldDeclaration(node: FieldDeclaration): void {
28
- if (toString(node).startsWith("static")) return;
29
- const lineText = toString(node);
30
- if (lineText.startsWith("private")) return;
31
- const name = getName(node);
32
- if (!node.type) {
33
- throw new Error(`Field ${name} is missing a type declaration`);
34
- }
35
-
36
- let type = getName(node.type);
37
- // @ts-ignore
38
- if (["u8", "i8", "u16", "i16", "u32", "i32", "f32", "u64", "i64", "f64"].includes(type.toLowerCase())) {
39
- this.currentClass.encodeStmts.push(
40
- `"${name}":\${this.${name}.toString()},`
41
- );
42
- } else {
43
- this.currentClass.encodeStmts.push(
44
- `"${name}":\${JSON.stringify<${type}>(this.${name})},`
45
- );
46
- }
47
-
48
- this.currentClass.keys.push(name);
49
- this.currentClass.types.push(type);
50
- // @ts-ignore
51
- //this.decodeStmts.push(
52
- // `${name}: JSON.parseObjectValue<${type}>(values.get("${name}")),\n`
53
- //);
54
-
55
- // @ts-ignore
56
- this.currentClass.setDataStmts.push(
57
- `if (key == "${name}") {
58
- this.${name} = JSON.parseObjectValue<${type}>(value);
59
- return;
60
- }
61
- `
62
- );
63
-
64
- // @ts-ignore
65
- //this.checkDecodeStmts.push(
66
- // ' if (!values.has("${name}")) throw new Error("Key "${name}" was not found. Cannot instantiate object.");\n'
67
- //);
68
- }
69
27
  visitClassDeclaration(node: ClassDeclaration): void {
70
28
  const className = node.name.text;
71
29
  if (!node.decorators?.length) return;
@@ -76,7 +34,7 @@ class AsJSONTransform extends BaseVisitor {
76
34
  }
77
35
  if (!foundDecorator) return;
78
36
 
79
- // Prevent from being triggered twice
37
+ // Prevent from being triggered twice.
80
38
  for (const member of node.members) {
81
39
  if (member.name.text == "__JSON_Serialize") return;
82
40
  }
@@ -104,7 +62,40 @@ class AsJSONTransform extends BaseVisitor {
104
62
 
105
63
  const parentSchema = this.schemasList.find((v) => v.name == this.currentClass.parent);
106
64
  const members = [...node.members, ...(parentSchema ? parentSchema.node.members : [])]
107
- this.visit(members);
65
+
66
+ for (const mem of members) {
67
+ if (mem.type && mem.type.name && mem.type.name.identifier.text) {
68
+ const member: FieldDeclaration = mem;
69
+ if (toString(member).startsWith("static")) return;
70
+ const lineText = toString(member);
71
+ if (lineText.startsWith("private")) return;
72
+
73
+ // @ts-ignore
74
+ const type = member.type.name.identifier.text;
75
+ const name = member.name.text;
76
+ this.currentClass.keys.push(name);
77
+ // @ts-ignore
78
+ this.currentClass.types.push(type);
79
+ // @ts-ignore
80
+ if (["u8", "i8", "u16", "i16", "u32", "i32", "f32", "u64", "i64", "f64"].includes(type.toLowerCase())) {
81
+ this.currentClass.encodeStmts.push(
82
+ `"${name}":\${this.${name}.toString()},`
83
+ );
84
+ } else {
85
+ this.currentClass.encodeStmts.push(
86
+ `"${name}":\${JSON.stringify<${type}>(this.${name})},`
87
+ );
88
+ }
89
+ // @ts-ignore
90
+ this.currentClass.setDataStmts.push(
91
+ `if (key == "${name}") {
92
+ this.${name} = JSON.parseObjectValue<${type}>(value);
93
+ return;
94
+ }
95
+ `
96
+ );
97
+ }
98
+ }
108
99
 
109
100
  let serializeFunc = "";
110
101
 
@@ -138,7 +129,7 @@ class AsJSONTransform extends BaseVisitor {
138
129
  }
139
130
  }
140
131
  `
141
-
132
+
142
133
  const serializeMethod = SimpleParser.parseClassMember(serializeFunc, node);
143
134
  node.members.push(serializeMethod);
144
135
 
@@ -160,7 +151,7 @@ export default class Transformer extends Transform {
160
151
  afterParse(parser: Parser): void {
161
152
  // Create new transform
162
153
  const transformer = new AsJSONTransform();
163
-
154
+
164
155
  // Sort the sources so that user scripts are visited last
165
156
  const sources = parser.sources.filter(source => !isStdlib(source)).sort((_a, _b) => {
166
157
  const a = _a.internalPath
@@ -173,7 +164,7 @@ export default class Transformer extends Transform {
173
164
  return 0;
174
165
  }
175
166
  })
176
-
167
+
177
168
  // Loop over every source
178
169
  for (const source of sources) {
179
170
  // Ignore all lib and std. Visit everything else.