Skip to content

Commit 8efff46

Browse files
committed
fix: splice in bs.proposeSize()
1 parent abe509c commit 8efff46

File tree

8 files changed

+134
-53
lines changed

8 files changed

+134
-53
lines changed

README.md

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,17 @@
1010
</pre>
1111
</h5>
1212

13+
## Contents
14+
- [About](#about)
15+
- [Installation](#installation)
16+
- [Usage](#usage)
17+
- [Examples](#examples)
18+
- [Performance](#performance)
19+
- [License](#license)
20+
- [Contact](#contact)
21+
22+
## About
23+
1324
## Installation
1425

1526
```bash
@@ -40,7 +51,6 @@ If you'd like to see the code that the transform generates, run with `JSON_DEBUG
4051
```js
4152
import { JSON } from "json-as";
4253

43-
// @json or @serializable work here
4454
@json
4555
class Vec3 {
4656
x: f32 = 0.0;
@@ -76,11 +86,15 @@ const player: Player = {
7686
isVerified: true
7787
};
7888

79-
const stringified = JSON.stringify<Player>(player);
80-
89+
const serialized = JSON.stringify<Player>(player);
8190
const parsed = JSON.parse<Player>(stringified);
91+
92+
console.log("Serialized: " + stringified);
93+
console.log("Parsed: " + parsed);
8294
```
8395

96+
## Examples
97+
8498
Classes can even have inheritance. Here's a nasty example
8599

86100
```js
@@ -124,16 +138,15 @@ You can also add it to your `asconfig.json`
124138

125139
If you use this project in your codebase, consider dropping a [star](https://github.com/JairusSW/as-json). I would really appreciate it!
126140

127-
## Notes
128-
129-
If you want a feature, drop an issue (and again, maybe a star). I'll likely add it in less than 7 days.
141+
## 📃 License
130142

131-
## Contact
143+
This project is distributed under an open source license. You can view the full license using the following link: [License](./LICENSE)
132144

133-
- [Email](mailto:[email protected])
134-
- [GitHub](https://github.com/JairusSW)
135-
- [Discord](discord.com/users/600700584038760448)
145+
## 📫 Contact
136146

137-
## Issues
147+
Please send all issues to [GitHub Issues](https://github.com/JairusSW/as-json/issues) and to converse, please send me an email at [[email protected]](mailto:[email protected])
138148

139-
Please submit an issue to https://github.com/JairusSW/as-json/issues if you find anything wrong with this library
149+
- **Email:** Send me inquiries, questions, or requests at [[email protected]](mailto:[email protected])
150+
- **GitHub:** Visit the official GitHub repository [Here](https://github.com/JairusSW/as-json)
151+
- **Website:** Visit my official website at [jairus.dev](https://jairus.dev/)
152+
- **Discord:** Converse with me on [My Discord](discord.com/users/600700584038760448) or on the [AssemblyScript Discord Server](https://discord.gg/assemblyscript/)

assembly/serialize/simd/string.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export function serializeString_SIMD(src: string): void {
1818
const srcSize = changetype<OBJECT>(changetype<usize>(src) - TOTAL_OVERHEAD).rtSize;
1919
let srcStart = changetype<usize>(src);
2020
const srcEnd = srcStart + srcSize;
21-
bs.ensureSize(srcSize + 4);
21+
bs.proposeSize(srcSize + 4);
2222
const srcEnd16 = srcEnd - 15;
2323

2424
store<u8>(changetype<usize>(bs.offset), 34); /* " */
@@ -45,13 +45,13 @@ export function serializeString_SIMD(src: string): void {
4545
mask &= mask - 1;
4646

4747
if ((escaped & 0xffff) != BACK_SLASH) {
48-
bs.addSize(10);
48+
bs.growSize(10);
4949
store<u64>(dst_offset, 13511005048209500);
5050
store<u32>(dst_offset, escaped, 8);
5151
v128.store(dst_offset, v128.load(src_offset, 2), 12);
5252
bs.offset += 10;
5353
} else {
54-
bs.addSize(2);
54+
bs.growSize(2);
5555
store<u32>(dst_offset, escaped);
5656
v128.store(dst_offset, v128.load(src_offset, 2), 4);
5757
bs.offset += 2;
@@ -84,7 +84,7 @@ export function serializeString_SIMD(src: string): void {
8484
mask &= mask - 1;
8585

8686
if ((escaped & 0xffff) != BACK_SLASH) {
87-
bs.addSize(10);
87+
bs.growSize(10);
8888
store<u64>(dst_offset, 13511005048209500);
8989
store<u32>(dst_offset, escaped, 8);
9090
while (lane_index < 6) {
@@ -93,7 +93,7 @@ export function serializeString_SIMD(src: string): void {
9393
}
9494
bs.offset += 10;
9595
} else {
96-
bs.addSize(2);
96+
bs.growSize(2);
9797
store<u32>(dst_offset, escaped);
9898

9999
while (lane_index < 6) {
@@ -116,12 +116,12 @@ export function serializeString_SIMD(src: string): void {
116116
const escaped = load<u32>(SERIALIZE_ESCAPE_TABLE + (codeA << 2));
117117

118118
if ((escaped & 0xffff) != BACK_SLASH) {
119-
bs.addSize(10);
119+
bs.growSize(10);
120120
store<u64>(bs.offset, 13511005048209500);
121121
store<u32>(bs.offset, escaped, 8);
122122
bs.offset += 12;
123123
} else {
124-
bs.addSize(2);
124+
bs.growSize(2);
125125
store<u32>(bs.offset, escaped);
126126
bs.offset += 4;
127127
}
@@ -134,12 +134,12 @@ export function serializeString_SIMD(src: string): void {
134134
const escaped = load<u32>(SERIALIZE_ESCAPE_TABLE + (codeB << 2));
135135

136136
if ((escaped & 0xffff) != BACK_SLASH) {
137-
bs.addSize(10);
137+
bs.growSize(10);
138138
store<u64>(bs.offset, 13511005048209500);
139139
store<u32>(bs.offset, escaped, 8);
140140
bs.offset += 12;
141141
} else {
142-
bs.addSize(2);
142+
bs.growSize(2);
143143
store<u32>(bs.offset, escaped);
144144
bs.offset += 4;
145145
}
@@ -156,12 +156,12 @@ export function serializeString_SIMD(src: string): void {
156156
const escaped = load<u32>(SERIALIZE_ESCAPE_TABLE + (code << 2));
157157

158158
if ((escaped & 0xffff) != BACK_SLASH) {
159-
bs.addSize(10);
159+
bs.growSize(10);
160160
store<u64>(bs.offset, 13511005048209500);
161161
store<u32>(bs.offset, escaped, 8);
162162
bs.offset += 12;
163163
} else {
164-
bs.addSize(2);
164+
bs.growSize(2);
165165
store<u32>(bs.offset, escaped);
166166
bs.offset += 4;
167167
}

assembly/serialize/simple/array.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,16 @@ import { COMMA, BRACKET_RIGHT, BRACKET_LEFT } from "../../custom/chars";
33
import { JSON } from "../..";
44

55
export function serializeArray<T extends any[]>(src: T): void {
6+
bs.proposeSize(4);
67
const end = src.length - 1;
78
let i = 0;
89
if (end == -1) {
9-
bs.proposeSize(4);
1010
store<u32>(bs.offset, 6094939);
1111
bs.offset += 4;
1212
return;
1313
}
14-
bs.proposeSize(end << 3);
14+
// {} = 4
15+
// xi, = n << 1
1516

1617
store<u16>(bs.offset, BRACKET_LEFT);
1718
bs.offset += 2;
@@ -26,7 +27,7 @@ export function serializeArray<T extends any[]>(src: T): void {
2627

2728
const lastBlock = unchecked(src[end]);
2829
JSON.__serialize<valueof<T>>(lastBlock);
29-
bs.proposeSize(2);
30+
bs.growSize(2);
3031
store<u16>(bs.offset, BRACKET_RIGHT);
3132
bs.offset += 2;
3233
}

assembly/serialize/simple/integer.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import { itoa_buffered } from "util/number";
22
import { bs } from "../../../modules/as-bs";
33

44
export function serializeInteger<T extends number>(data: T): void {
5-
bs.proposeSize(sizeof<T>() << 3);
6-
bs.offset += itoa_buffered(bs.offset, data) << 1;
5+
bs.ensureSize(sizeof<T>() << 3);
6+
const bytesWritten = itoa_buffered(bs.offset, data) << 1;
7+
bs.offset += bytesWritten;
8+
bs.realSize += bytesWritten;
79
}

assembly/test.ts

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,42 @@
1+
import { bs } from "../modules/as-bs/assembly";
12
import { JSON } from "./";
2-
import { describe, expect } from "../modules/test/assembly";
3+
@json
4+
class Vec3 {
5+
x: f32 = 0.0;
6+
y: f32 = 0.0;
7+
z: f32 = 0.0;
8+
}
39

4-
console.log(JSON.stringify(JSON.parse<string>('"\\u0000\\u0001\\u0002\\u0003\\u0004\\u0005\\u0006\\u0007\\b\\t\\n\\u000b\\f\\r\\u000e\\u000f\\u000f\\u0011\\u0012\\u0013\\u0014\\u0015\\u0016\\u0017\\u0018\\u0019\\u001a\\u001b\\u001c\\u001d\\u001e\\u001f"')));
5-
console.log('"\\u0000\\u0001\\u0002\\u0003\\u0004\\u0005\\u0006\\u0007\\b\\t\\n\\u000b\\f\\r\\u000e\\u000f\\u000f\\u0011\\u0012\\u0013\\u0014\\u0015\\u0016\\u0017\\u0018\\u0019\\u001a\\u001b\\u001c\\u001d\\u001e\\u001f"');
10+
@json
11+
class Player {
12+
@alias("first name")
13+
firstName: string = "";
14+
lastName: string = "";
15+
lastActive: i32[] = [];
16+
// // Drop in a code block, function, or expression that evaluates to a boolean
17+
// // @omitif((self) => self.age < 18)
18+
// // @omitif('this.age <= 0')
19+
// age: i32 = 0;
20+
// // @omitnull()
21+
// pos: Vec3 | null = new Vec3();
22+
// isVerified: boolean = false;
23+
}
24+
25+
const player: Player = {
26+
firstName: "Emmet",
27+
lastName: "West",
28+
lastActive: [8, 27, 2022],
29+
// age: 23,
30+
// pos: {
31+
// x: 3.4,
32+
// y: 1.2,
33+
// z: 8.3
34+
// },
35+
// isVerified: true
36+
};
37+
38+
// bs.proposeSize(1024);
39+
const serialized = JSON.stringify<Player>(player);
40+
console.log("Serialized: " + serialized);
41+
// const parsed = JSON.parse<Player>(serialized);
42+
// console.log("Parsed: " + JSON.stringify(parsed));

modules/as-bs/assembly/index.ts

Lines changed: 48 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,42 +11,70 @@ export namespace bs {
1111
export let offset: usize = buffer;
1212

1313
/** Byte length of the buffer. */
14-
export let byteLength: usize = 32;
14+
let bufferEnd: usize = buffer + 32;
1515

1616
/** Proposed size of output */
17-
export let realSize: usize = offset;
17+
export let realSize: usize = buffer;
1818

19+
/**
20+
* Byte length of the buffer
21+
* @returns usize
22+
*/
23+
// @ts-ignore: decorator
24+
@inline export function byteLength(): usize {
25+
return bufferEnd - buffer;
26+
}
1927
/**
2028
* Proposes that the buffer size is should be greater than or equal to the proposed size.
2129
* If necessary, reallocates the buffer to the exact new size.
2230
* @param size - The size to propose.
2331
*/
24-
// @ts-ignore: Decorator valid here
32+
// @ts-ignore: decorator
33+
@inline export function ensureSize(size: u32): void {
34+
if (offset + size > bufferEnd) {
35+
bufferEnd += nextPowerOf2(size + 32);
36+
// @ts-ignore: exists
37+
const newPtr = __renew(buffer, bufferEnd - buffer);
38+
// I don't know if this is even needed. I'll need to take a look at the runtime
39+
// offset = offset - buffer + newPtr;
40+
// buffer = newPtr;
41+
}
42+
console.log("Ensure " + (realSize - buffer).toString() + " " + size.toString());
43+
}
44+
/**
45+
* Proposes that the buffer size is should be greater than or equal to the proposed size.
46+
* If necessary, reallocates the buffer to the exact new size.
47+
* @param size - The size to propose.
48+
*/
49+
// @ts-ignore: decorator
2550
@inline export function proposeSize(size: u32): void {
26-
if ((realSize = size) > byteLength) {
27-
byteLength = nextPowerOf2(size);
28-
// @ts-ignore
29-
const newPtr = __renew(buffer, byteLength);
30-
offset = offset - buffer + newPtr;
31-
buffer = newPtr;
51+
realSize = offset + size;
52+
if (realSize > bufferEnd) {
53+
bufferEnd += nextPowerOf2(size);
54+
// @ts-ignore: exists
55+
const newPtr = __renew(buffer, bufferEnd - buffer);
56+
// I don't know if this is even needed. I'll need to take a look at the runtime
57+
// offset = offset - buffer + newPtr;
58+
// buffer = newPtr;
3259
}
60+
console.log("Propose " + (realSize - buffer).toString() + " " + size.toString());
3361
}
3462

3563
/**
3664
* Increases the proposed size by nextPowerOf2(n + 8) if necessary.
3765
* If necessary, reallocates the buffer to the exact new size.
3866
* @param size - The size to grow by.
3967
*/
40-
// @ts-ignore: Decorator valid here
68+
// @ts-ignore: decorator
4169
@inline export function growSize(size: u32): void {
42-
realSize += size;
43-
if (realSize > byteLength) {
44-
byteLength += nextPowerOf2(size + 8);
70+
if ((realSize += size) > bufferEnd) {
71+
bufferEnd += nextPowerOf2(size + 32);
4572
// @ts-ignore
46-
const newPtr = __renew(buffer, byteLength);
47-
offset = offset - buffer + newPtr;
48-
buffer = newPtr;
73+
const newPtr = __renew(buffer, bufferEnd);
74+
// offset = offset - buffer + newPtr;
75+
// buffer = newPtr;
4976
}
77+
console.log("Grow " + (realSize - buffer).toString() + " " + size.toString());
5078
}
5179

5280
/**
@@ -55,9 +83,9 @@ export namespace bs {
5583
*/
5684
// @ts-ignore: Decorator valid here
5785
@inline export function resize(newSize: u32): void {
58-
// @ts-ignore
86+
// @ts-ignore: exists
5987
const newPtr = __renew(buffer, newSize);
60-
byteLength = newSize;
88+
bufferEnd = newSize;
6189
buffer = newPtr;
6290
offset = newPtr + newSize;
6391
realSize = newPtr;
@@ -70,7 +98,7 @@ export namespace bs {
7098
// @ts-ignore: Decorator valid here
7199
@inline export function out<T>(): T {
72100
const len = offset - buffer;
73-
// @ts-ignore
101+
// @ts-ignore: exists
74102
const _out = __new(len, idof<T>());
75103
memory.copy(_out, buffer, len);
76104

@@ -89,7 +117,7 @@ export namespace bs {
89117
// @ts-ignore: Decorator valid here
90118
@inline export function outTo<T>(dst: usize): T {
91119
const len = offset - buffer;
92-
// @ts-ignore
120+
// @ts-ignore: exists
93121
if (len != changetype<OBJECT>(dst - TOTAL_OVERHEAD).rtSize) __renew(len, idof<T>());
94122
memory.copy(dst, buffer, len);
95123

transform/lib/index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

transform/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ class JSONTransform extends Visitor {
327327
SERIALIZE += indent + "bs.offset += 2;\n";
328328
SERIALIZE += "}";
329329

330-
SERIALIZE = indent + "bs.proposeSize(" + this.schema.byteSize + ");\n" + SERIALIZE;
330+
SERIALIZE = SERIALIZE.slice(0, 32) + indent + "bs.proposeSize(" + this.schema.byteSize + ");\n" + SERIALIZE.slice(32);
331331

332332
INITIALIZE += " return this;\n";
333333
INITIALIZE += "}";

0 commit comments

Comments
 (0)