json-as 0.9.8 → 0.9.9-a
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/.github/workflows/nodejs.yml +9 -10
- package/CHANGELOG +13 -1
- package/README.md +56 -8
- package/asconfig.json +24 -3
- package/assembly/__tests__/test.spec.ts +564 -0
- package/assembly/__tests__/types.ts +82 -0
- package/assembly/{src/chars.ts → chars.ts} +36 -36
- package/assembly/deserialize/array/array.ts +4 -4
- package/assembly/deserialize/array/bool.ts +4 -4
- package/assembly/deserialize/array/float.ts +4 -4
- package/assembly/deserialize/array/integer.ts +4 -4
- package/assembly/deserialize/array/map.ts +4 -4
- package/assembly/deserialize/array/object.ts +4 -4
- package/assembly/deserialize/array/string.ts +4 -4
- package/assembly/deserialize/array.ts +5 -4
- package/assembly/deserialize/bool.ts +4 -4
- package/assembly/deserialize/date.ts +2 -2
- package/assembly/deserialize/float.ts +2 -2
- package/assembly/deserialize/integer.ts +3 -3
- package/assembly/deserialize/map.ts +6 -4
- package/assembly/deserialize/mpz.ts +12 -0
- package/assembly/deserialize/object.ts +4 -4
- package/assembly/deserialize/string.ts +5 -5
- package/assembly/index.ts +25 -23
- package/assembly/serialize/array.ts +6 -5
- package/assembly/serialize/bool.ts +2 -2
- package/assembly/serialize/date.ts +2 -2
- package/assembly/serialize/float.ts +2 -2
- package/assembly/serialize/integer.ts +2 -2
- package/assembly/serialize/map.ts +8 -7
- package/assembly/serialize/mpz.ts +6 -0
- package/assembly/serialize/object.ts +2 -2
- package/assembly/serialize/string.ts +5 -5
- package/assembly/serialize/unknown.ts +5 -5
- package/assembly/test.ts +70 -43
- package/assembly/types.ts +4 -0
- package/assembly/{src/util.ts → util.ts} +3 -3
- package/bench/benchmark.ts +1 -1
- package/build/test.spec.wasm +0 -0
- package/build/test.spec.wasm.map +1 -0
- package/build/test.spec.wat +112683 -0
- package/build/test.wasm +0 -0
- package/build/test.wasm.map +1 -0
- package/build/test.wat +30110 -0
- package/package.json +13 -13
- package/transform/lib/index.js +26 -0
- package/transform/lib/visitor.js +516 -0
- package/transform/package.json +1 -1
- package/transform/src/index.ts +24 -0
- package/transform/src/visitor.ts +543 -0
- package/transform/tsconfig.json +23 -63
- package/tsconfig.json +13 -13
- package/as-pect.asconfig.json +0 -24
- package/as-pect.config.js +0 -30
- package/assembly/__tests__/deserialize.spec.ts +0 -301
- package/assembly/__tests__/serialize.spec.ts +0 -398
- package/assembly/deserialize/box.ts +0 -17
- package/assembly/serialize/box.ts +0 -11
- package/develop/assembly/serialize/unknown.ts +0 -46
- package/transform/lib/index.old.js +0 -257
- package/transform/lib/types.js +0 -17
- package/transform/src/index.old.ts +0 -312
- /package/assembly/{src/sink.ts → sink.ts} +0 -0
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { JSON } from "..";
|
|
2
|
-
import { Sink } from "../
|
|
3
|
-
import { __atoi_fast } from "../
|
|
4
|
-
import { serializeUnknownArray } from "./array/unknown";
|
|
2
|
+
import { Sink } from "../sink";
|
|
3
|
+
import { __atoi_fast } from "../util";
|
|
5
4
|
import { serializeBool } from "./bool";
|
|
6
5
|
import { serializeFloat } from "./float";
|
|
7
6
|
import { serializeInteger } from "./integer";
|
|
@@ -13,7 +12,8 @@ import { serializeString } from "./string";
|
|
|
13
12
|
* @param data - The JSON.Value to be serialized.
|
|
14
13
|
* @returns The serialized result.
|
|
15
14
|
*/
|
|
16
|
-
|
|
15
|
+
// @ts-ignore: Decorator valid here
|
|
16
|
+
@inline export function serializeUnknown(data: JSON.Value): string {
|
|
17
17
|
const type = data.type;
|
|
18
18
|
switch (type) {
|
|
19
19
|
case JSON.Types.String: {
|
|
@@ -41,5 +41,5 @@ export function serializeUnknown(data: JSON.Value): string {
|
|
|
41
41
|
return serializeFloat(data.get<f64>());
|
|
42
42
|
}
|
|
43
43
|
}
|
|
44
|
-
return serializeUnknownArray(data.get<JSON.Value[]>());
|
|
44
|
+
return "ERROR"//serializeUnknownArray(data.get<JSON.Value[]>());
|
|
45
45
|
}
|
package/assembly/test.ts
CHANGED
|
@@ -1,51 +1,78 @@
|
|
|
1
|
-
import { JSON } from "
|
|
1
|
+
import { JSON } from "json-as/assembly";
|
|
2
2
|
|
|
3
|
-
// @json or @serializable work here
|
|
4
3
|
@json
|
|
5
|
-
class
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
}
|
|
4
|
+
class TokenMetaData {
|
|
5
|
+
id: u64;
|
|
6
|
+
name: string;
|
|
7
|
+
uri: string;
|
|
10
8
|
|
|
11
|
-
|
|
12
|
-
|
|
9
|
+
constructor(id: u64, name: string, uri: string) {
|
|
10
|
+
this.id = id;
|
|
11
|
+
this.name = name;
|
|
12
|
+
this.uri = uri;
|
|
13
|
+
}
|
|
13
14
|
}
|
|
14
15
|
|
|
15
16
|
@json
|
|
16
|
-
class
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
17
|
+
class NonFungibleToken {
|
|
18
|
+
owner: string = "";
|
|
19
|
+
counter: u64 = 0;
|
|
20
|
+
tokens: Map<u64, TokenMetaData> = new Map<u64, TokenMetaData>();
|
|
21
|
+
owners: Map<u64, string> = new Map<u64, string>();
|
|
22
|
+
balances: Map<string, u64[]> = new Map<string, u64[]>();
|
|
23
|
+
|
|
24
|
+
constructor() { }
|
|
25
|
+
|
|
26
|
+
mint(name: string, uri: string, toAddress: string): u64 {
|
|
27
|
+
this.counter += 1;
|
|
28
|
+
const id = this.counter;
|
|
29
|
+
|
|
30
|
+
const tokenMetaData = new TokenMetaData(id, name, uri);
|
|
31
|
+
|
|
32
|
+
this.tokens.set(id, tokenMetaData);
|
|
33
|
+
this.owners.set(id, toAddress);
|
|
34
|
+
|
|
35
|
+
if (!this.balances.has(toAddress)) {
|
|
36
|
+
this.balances.set(toAddress, []);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
this.balances.get(toAddress).push(id);
|
|
40
|
+
|
|
41
|
+
return id;
|
|
42
|
+
}
|
|
30
43
|
}
|
|
44
|
+
function readStringFromMemory(ptr: usize): string {
|
|
45
|
+
let len: i32 = load<u32>(ptr)
|
|
46
|
+
let buffer = new Uint8Array(len);
|
|
47
|
+
|
|
48
|
+
for (let i = 0; i < len; ++i) {
|
|
49
|
+
buffer[i] = load<u8>(ptr + 4 + i);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
let s = String.UTF8.decode(buffer.buffer);
|
|
53
|
+
return s
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function getLengthPrefixedString(s: string): ArrayBuffer {
|
|
57
|
+
let stringBuf = Uint8Array.wrap(String.UTF8.encode(s))
|
|
58
|
+
let newLen = stringBuf.byteLength
|
|
59
|
+
let buffer = new ArrayBuffer(4 + newLen);
|
|
60
|
+
let dataView = new DataView(buffer);
|
|
61
|
+
|
|
62
|
+
dataView.setUint32(0, newLen, true);
|
|
63
|
+
|
|
64
|
+
for (let i = 0; i < newLen; ++i) {
|
|
65
|
+
dataView.setInt8(4 + i, stringBuf[i])
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
return buffer
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const s1 = getLengthPrefixedString("hello world");
|
|
72
|
+
console.log(Uint8Array.wrap(s1).join(" "));
|
|
73
|
+
const s2 = readStringFromMemory(changetype<usize>(s1));
|
|
74
|
+
console.log(s2);
|
|
31
75
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
lastActive: [8, 27, 2022],
|
|
36
|
-
age: 23,
|
|
37
|
-
pos: {
|
|
38
|
-
x: 3.4,
|
|
39
|
-
y: 1.2,
|
|
40
|
-
z: 8.3
|
|
41
|
-
},
|
|
42
|
-
isVerified: true,
|
|
43
|
-
box: null
|
|
44
|
-
};
|
|
45
|
-
|
|
46
|
-
const stringified = JSON.stringify<Player>(player);
|
|
47
|
-
|
|
48
|
-
const parsed = JSON.parse<Player>(stringified);
|
|
49
|
-
|
|
50
|
-
console.log("Stringified: " + stringified);
|
|
51
|
-
console.log("Parsed: " + JSON.stringify(parsed));
|
|
76
|
+
let state = JSON.parse<NonFungibleToken>('{"owner":"","counter":1,"tokens":{"1":{"id":1,"name":"foo","uri":"bar"}},"owners":{"1":"baz"},"balances":{"baz":[1]}}');
|
|
77
|
+
state.mint("foo", "bar", "baz")
|
|
78
|
+
console.log(JSON.stringify(state))
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { StringSink } from "as-string-sink/assembly";
|
|
2
1
|
import { isSpace } from "util/string";
|
|
3
2
|
import { BACK_SLASH, QUOTE } from "./chars";
|
|
3
|
+
import { Sink } from "./sink";
|
|
4
4
|
|
|
5
5
|
// @ts-ignore: Decorator
|
|
6
6
|
export function isMap<T>(): bool {
|
|
@@ -15,7 +15,7 @@ export function unsafeCharCodeAt(data: string, pos: i32): i32 {
|
|
|
15
15
|
|
|
16
16
|
// @ts-ignore: Decorator
|
|
17
17
|
export function removeWhitespace(data: string): string {
|
|
18
|
-
const result = new
|
|
18
|
+
const result = new Sink();
|
|
19
19
|
let instr = false;
|
|
20
20
|
for (let i = 0; i < data.length; i++) {
|
|
21
21
|
const char = unsafeCharCodeAt(data, i);
|
|
@@ -258,7 +258,7 @@ export function snip_fast<T extends number>(str: string, len: u32 = 0, offset: u
|
|
|
258
258
|
*/
|
|
259
259
|
|
|
260
260
|
// @ts-ignore
|
|
261
|
-
|
|
261
|
+
export function __atoi_fast<T extends number>(str: string, start: u32 = 0, end: u32 = 0): T {
|
|
262
262
|
// @ts-ignore
|
|
263
263
|
let val: T = 0;
|
|
264
264
|
if (!end) end = start + u32(str.length << 1);
|
package/bench/benchmark.ts
CHANGED
|
Binary file
|