json-as 0.5.39 → 0.5.41

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.
@@ -114,6 +114,10 @@ export function parseSciInteger<T extends number>(str: string): T {
114
114
  // @ts-ignore
115
115
  let val: T = 0;
116
116
  let offset = 0;
117
+ let firstChar = load<u16>(changetype<usize>(str) + <usize>offset);
118
+ if (firstChar === 45) {
119
+ offset = 2;
120
+ }
117
121
  for (; offset < str.length << 1; offset += 2) {
118
122
  const char = load<u16>(changetype<usize>(str) + <usize>offset);
119
123
  if (char === 101 || char === 69) {
@@ -134,6 +138,9 @@ export function parseSciInteger<T extends number>(str: string): T {
134
138
  val = (val << 1) + (val << 3) + (char - 48);
135
139
  // We use load because in this case, there is no need to have bounds-checking
136
140
  }
141
+ if (firstChar === 45) {
142
+ val = -val;
143
+ }
137
144
  return val;
138
145
  }
139
146
 
@@ -141,15 +148,16 @@ export function parseSciInteger<T extends number>(str: string): T {
141
148
  @inline
142
149
  function sciNote<T extends number>(num: T): T {
143
150
  let res = 1;
144
- if (num > 0) {
145
- for (let i = 0; i < num; i++) {
146
- res *= 10;
147
- }
148
- } else {
149
- for (let i = 0; i < num; i++) {
150
- res /= 10;
151
+ // @ts-ignore
152
+ if (num > 0) {
153
+ for (let i: T = 0; i < num; i++) {
154
+ res *= 10;
155
+ }
156
+ } else {
157
+ for (let i: T = 0; i < num; i++) {
158
+ res /= 10;
159
+ }
151
160
  }
152
- }
153
161
  // @ts-ignore
154
162
  return res;
155
- }
163
+ }
package/assembly/test.ts CHANGED
@@ -54,7 +54,30 @@ console.log("1230 - " + parseSciInteger<i32>("123e1").toString());
54
54
  console.log("12300 - " + parseSciInteger<i32>("123e2").toString());
55
55
  console.log("123000 - " + parseSciInteger<i32>("123e3").toString());
56
56
  console.log("32 - " + parseSciInteger<i32>("123e-1").toString());
57
+ console.log(parseSciInteger<i32>("100").toString());
58
+ console.log(parseSciInteger<i32>("-100").toString());
57
59
 
60
+ console.log(
61
+ JSON.stringify([
62
+ "abcdefg",
63
+ 'st"ring" w""ith quotes"',
64
+ 'string \t\r"with ran\tdom spa\nces and \nnewlines\n\n\n',
65
+ 'string with colon : comma , brace [ ] bracket { } and quote " and other quote "',
66
+ ])
67
+ );/*
68
+ console.log(
69
+ JSON.stringify(
70
+ JSON.parse<string[]>(
71
+ JSON.stringify([
72
+ "abcdefg",
73
+ 'st"ring" w""ith quotes"',
74
+ 'string \t\r"with ran\tdom spa\nces and \nnewlines\n\n\n',
75
+ 'string with colon : comma , brace [ ] bracket { } and quote " and other quote "',
76
+ ])
77
+ )
78
+ )
79
+ );
80
+ /*
58
81
  const str = changetype<string>(new ArrayBuffer(6));
59
82
  console.log("istr:");
60
83
  console.log("123 - " + istr8(123));
@@ -244,3 +267,4 @@ export function istr64<T extends number>(int: T): string {
244
267
  // 9 = 57
245
268
 
246
269
  console.log(JSON.stringify("h\\i from gray\bson"));
270
+ */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "json-as",
3
- "version": "0.5.39",
3
+ "version": "0.5.41",
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 { toString, isStdlib } from "visitor-as/dist/utils.js";
1
+ import { getName, 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,23 +19,48 @@ 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
+ this.currentClass.encodeStmts.push(`"${name}":\${JSON.stringify<${type}>(this.${name})},`);
35
+ // @ts-ignore
36
+ //this.decodeStmts.push(
37
+ // `${name}: JSON.parseObjectValue<${type}>(values.get("${name}")),\n`
38
+ //);
39
+ // @ts-ignore
40
+ this.currentClass.setDataStmts.push(`if (key.length === ${name.length} && (memory.compare(changetype<usize>("${name}"), changetype<usize>(key), ${name.length}) == 0)) {
41
+ this.${name} = JSON.parseObjectValue<${type}>(value);
42
+ return;
43
+ }
44
+ `);
45
+ // @ts-ignore
46
+ //this.checkDecodeStmts.push(
47
+ // ' if (!values.has("${name}")) throw new Error("Key "${name}" was not found. Cannot instantiate object.");\n'
48
+ //);
49
+ }
22
50
  visitClassDeclaration(node) {
23
- var _c;
51
+ var _a;
24
52
  const className = node.name.text;
25
- if (!((_c = node.decorators) === null || _c === void 0 ? void 0 : _c.length))
53
+ if (!((_a = node.decorators) === null || _a === void 0 ? void 0 : _a.length))
26
54
  return;
27
55
  let foundDecorator = false;
28
56
  for (const decorator of node.decorators) {
29
- if (
30
57
  // @ts-ignore
31
- decorator.name.text.toLowerCase() == "json" ||
32
- // @ts-ignore
33
- decorator.name.text.toLowerCase() == "serializable")
58
+ if (decorator.name.text.toLowerCase() == "json" || decorator.name.text.toLowerCase() == "serializable")
34
59
  foundDecorator = true;
35
60
  }
36
61
  if (!foundDecorator)
37
62
  return;
38
- // Prevent from being triggered twice.
63
+ // Prevent from being triggered twice
39
64
  for (const member of node.members) {
40
65
  if (member.name.text == "__JSON_Serialize")
41
66
  return;
@@ -48,7 +73,7 @@ class AsJSONTransform extends BaseVisitor {
48
73
  parent: node.extendsType ? toString(node.extendsType) : "",
49
74
  node: node,
50
75
  encodeStmts: [],
51
- setDataStmts: [],
76
+ setDataStmts: []
52
77
  };
53
78
  if (this.currentClass.parent.length > 0) {
54
79
  const parentSchema = this.schemasList.find((v) => v.name == this.currentClass.parent);
@@ -57,64 +82,17 @@ class AsJSONTransform extends BaseVisitor {
57
82
  this.currentClass.encodeStmts.push(...parentSchema === null || parentSchema === void 0 ? void 0 : parentSchema.encodeStmts);
58
83
  }
59
84
  else {
60
- console.error("Class extends " +
61
- this.currentClass.parent +
62
- ", but parent class not found. Maybe add the @json decorator over parent class?");
85
+ console.error("Class extends " + this.currentClass.parent + ", but parent class not found. Maybe add the @json decorator over parent class?");
63
86
  }
64
87
  }
65
88
  const parentSchema = this.schemasList.find((v) => v.name == this.currentClass.parent);
66
- const members = [
67
- ...node.members,
68
- ...(parentSchema ? parentSchema.node.members : []),
69
- ];
70
- for (const mem of members) {
71
- // @ts-ignore
72
- if (mem.type && mem.type.name && mem.type.name.identifier.text) {
73
- const member = mem;
74
- if (toString(member).startsWith("static"))
75
- return;
76
- const lineText = toString(member);
77
- if (lineText.startsWith("private"))
78
- return;
79
- // @ts-ignore
80
- let type = toString(member.type);
81
- const name = member.name.text;
82
- this.currentClass.keys.push(name);
83
- // @ts-ignore
84
- this.currentClass.types.push(type);
85
- // @ts-ignore
86
- if ([
87
- "u8",
88
- "i8",
89
- "u16",
90
- "i16",
91
- "u32",
92
- "i32",
93
- "f32",
94
- "u64",
95
- "i64",
96
- "f64",
97
- ].includes(type.toLowerCase())) {
98
- this.currentClass.encodeStmts.push(`"${name}":\${this.${name}.toString()},`);
99
- }
100
- else {
101
- this.currentClass.encodeStmts.push(`"${name}":\${JSON.stringify<${type}>(this.${name})},`);
102
- }
103
- // @ts-ignore
104
- this.currentClass.setDataStmts.push(`if (key == "${name}") {
105
- this.${name} = JSON.parseObjectValue<${type}>(value);
106
- return;
107
- }
108
- `);
109
- }
110
- }
89
+ const members = [...node.members, ...(parentSchema ? parentSchema.node.members : [])];
90
+ this.visit(members);
111
91
  let serializeFunc = "";
112
92
  if (this.currentClass.encodeStmts.length > 0) {
113
93
  const stmt = this.currentClass.encodeStmts[this.currentClass.encodeStmts.length - 1];
114
- this.currentClass.encodeStmts[this.currentClass.encodeStmts.length - 1] =
115
- stmt.slice(0, stmt.length - 1);
94
+ this.currentClass.encodeStmts[this.currentClass.encodeStmts.length - 1] = stmt.slice(0, stmt.length - 1);
116
95
  serializeFunc = `
117
- @inline
118
96
  __JSON_Serialize(): string {
119
97
  return \`{${this.currentClass.encodeStmts.join("")}}\`;
120
98
  }
@@ -122,18 +100,16 @@ class AsJSONTransform extends BaseVisitor {
122
100
  }
123
101
  else {
124
102
  serializeFunc = `
125
- @inline
126
103
  __JSON_Serialize(): string {
127
104
  return "{}";
128
105
  }
129
106
  `;
130
107
  }
131
108
  const setKeyFunc = `
132
- @inline
133
109
  __JSON_Set_Key(key: string, value: string): void {
134
110
  ${
135
- // @ts-ignore
136
- this.currentClass.setDataStmts.join("")}
111
+ // @ts-ignore
112
+ this.currentClass.setDataStmts.join("")}
137
113
  }
138
114
  `;
139
115
  const serializeMethod = SimpleParser.parseClassMember(serializeFunc, node);
@@ -151,23 +127,18 @@ export default class Transformer extends Transform {
151
127
  afterParse(parser) {
152
128
  // Create new transform
153
129
  const transformer = new AsJSONTransform();
154
- // Sort the sources so that user scripts are visited last
155
- const sources = parser.sources
156
- .filter((source) => !isStdlib(source))
157
- .sort((_a, _b) => {
158
- const a = _a.internalPath;
159
- const b = _b.internalPath;
130
+ // Loop over every source
131
+ const sources = parser.sources.filter(source => !isStdlib(source)).sort((_a, _b) => {
132
+ const a = _a.internalPath
133
+ const b = _b.internalPath
160
134
  if (a[0] === "~" && b[0] !== "~") {
161
135
  return -1;
162
- }
163
- else if (a[0] !== "~" && b[0] === "~") {
136
+ } else if (a[0] !== "~" && b[0] === "~") {
164
137
  return 1;
165
- }
166
- else {
138
+ } else {
167
139
  return 0;
168
140
  }
169
- });
170
- // Loop over every source
141
+ })
171
142
  for (const source of sources) {
172
143
  // Ignore all lib and std. Visit everything else.
173
144
  if (!isStdlib(source)) {
@@ -175,4 +146,4 @@ export default class Transformer extends Transform {
175
146
  }
176
147
  }
177
148
  }
178
- }
149
+ }
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@json-as/transform",
3
- "version": "0.5.39",
3
+ "version": "0.5.41",
4
4
  "description": "JSON encoder/decoder for AssemblyScript",
5
5
  "main": "./lib/index.js",
6
6
  "author": "Jairus Tanaka",