json-as 1.2.6 → 1.3.1
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/CHANGELOG.md +417 -0
- package/README.md +135 -36
- package/assembly/custom/util.ts +24 -70
- package/assembly/deserialize/float.ts +181 -0
- package/assembly/deserialize/helpers/uint.ts +12 -0
- package/assembly/deserialize/index/arbitrary.ts +25 -0
- package/assembly/deserialize/index/array.ts +61 -0
- package/assembly/deserialize/index/bool.ts +1 -0
- package/assembly/deserialize/index/date.ts +1 -0
- package/assembly/deserialize/index/float.ts +1 -0
- package/assembly/deserialize/index/integer.ts +1 -0
- package/assembly/deserialize/index/map.ts +1 -0
- package/assembly/deserialize/index/object.ts +1 -0
- package/assembly/deserialize/index/raw.ts +1 -0
- package/assembly/deserialize/index/set.ts +1 -0
- package/assembly/deserialize/index/staticarray.ts +1 -0
- package/assembly/deserialize/index/string.ts +15 -0
- package/assembly/deserialize/index/struct.ts +1 -0
- package/assembly/deserialize/index/typedarray.ts +15 -0
- package/assembly/deserialize/index/unsigned.ts +1 -0
- package/assembly/deserialize/index.ts +14 -0
- package/assembly/deserialize/integer.ts +42 -0
- package/assembly/deserialize/simd/array/integer.ts +307 -0
- package/assembly/deserialize/simd/string.ts +130 -11
- package/assembly/deserialize/simple/arbitrary.ts +5 -12
- package/assembly/deserialize/simple/array/arbitrary.ts +12 -36
- package/assembly/deserialize/simple/array/array.ts +2 -8
- package/assembly/deserialize/simple/array/bool.ts +2 -8
- package/assembly/deserialize/simple/array/box.ts +2 -8
- package/assembly/deserialize/simple/array/float.ts +2 -8
- package/assembly/deserialize/simple/array/integer.ts +2 -8
- package/assembly/deserialize/simple/array/map.ts +6 -26
- package/assembly/deserialize/simple/array/object.ts +6 -26
- package/assembly/deserialize/simple/array/raw.ts +18 -61
- package/assembly/deserialize/simple/array/string.ts +5 -10
- package/assembly/deserialize/simple/array/struct.ts +6 -26
- package/assembly/deserialize/simple/array.ts +2 -5
- package/assembly/deserialize/simple/bool.ts +2 -6
- package/assembly/deserialize/simple/map.ts +29 -102
- package/assembly/deserialize/simple/object.ts +24 -81
- package/assembly/deserialize/simple/raw.ts +1 -4
- package/assembly/deserialize/simple/set.ts +11 -37
- package/assembly/deserialize/simple/staticarray/array.ts +1 -1
- package/assembly/deserialize/simple/staticarray/bool.ts +1 -1
- package/assembly/deserialize/simple/staticarray/float.ts +1 -1
- package/assembly/deserialize/simple/staticarray/integer.ts +1 -1
- package/assembly/deserialize/simple/staticarray/string.ts +7 -14
- package/assembly/deserialize/simple/staticarray/struct.ts +1 -1
- package/assembly/deserialize/simple/staticarray.ts +57 -21
- package/assembly/deserialize/simple/string.ts +90 -10
- package/assembly/deserialize/simple/struct.ts +25 -121
- package/assembly/deserialize/simple/typedarray.ts +94 -0
- package/assembly/deserialize/swar/array/arbitrary.ts +8 -0
- package/assembly/deserialize/swar/array/array.ts +39 -0
- package/assembly/deserialize/swar/array/bool.ts +47 -0
- package/assembly/deserialize/swar/array/box.ts +8 -0
- package/assembly/deserialize/swar/array/float.ts +39 -0
- package/assembly/deserialize/swar/array/integer.ts +461 -0
- package/assembly/deserialize/swar/array/map.ts +7 -0
- package/assembly/deserialize/swar/array/object.ts +44 -0
- package/assembly/deserialize/swar/array/raw.ts +8 -0
- package/assembly/deserialize/swar/array/shared.ts +96 -0
- package/assembly/deserialize/swar/array/string.ts +39 -0
- package/assembly/deserialize/swar/array/struct.ts +44 -0
- package/assembly/deserialize/swar/array.ts +49 -0
- package/assembly/deserialize/swar/string.ts +648 -15
- package/assembly/deserialize/unsigned.ts +75 -0
- package/assembly/index.d.ts +1 -3
- package/assembly/index.ts +316 -374
- package/assembly/serialize/index/arbitrary.ts +75 -0
- package/assembly/serialize/index/array.ts +1 -0
- package/assembly/serialize/index/bool.ts +1 -0
- package/assembly/serialize/index/date.ts +1 -0
- package/assembly/serialize/index/float.ts +1 -0
- package/assembly/serialize/index/integer.ts +1 -0
- package/assembly/serialize/index/map.ts +1 -0
- package/assembly/serialize/index/object.ts +46 -0
- package/assembly/serialize/index/raw.ts +1 -0
- package/assembly/serialize/index/set.ts +1 -0
- package/assembly/serialize/index/staticarray.ts +1 -0
- package/assembly/serialize/index/string.ts +15 -0
- package/assembly/serialize/index/struct.ts +1 -0
- package/assembly/serialize/index/typedarray.ts +66 -0
- package/assembly/serialize/index.ts +13 -0
- package/assembly/serialize/simd/string.ts +4 -13
- package/assembly/serialize/simple/arbitrary.ts +6 -0
- package/assembly/serialize/simple/raw.ts +1 -5
- package/assembly/serialize/simple/string.ts +3 -11
- package/assembly/serialize/simple/typedarray.ts +63 -0
- package/assembly/serialize/swar/string.ts +6 -21
- package/assembly/util/concat.ts +1 -5
- package/assembly/util/index.ts +1 -0
- package/assembly/util/masks.ts +12 -18
- package/assembly/util/memory.ts +0 -0
- package/assembly/util/snp.ts +1 -4
- package/assembly/util/stringScan.ts +24 -0
- package/assembly/util/swar.ts +50 -6
- package/lib/as-bs.ts +137 -127
- package/package.json +26 -5
- package/transform/lib/builder.d.ts.map +1 -1
- package/transform/lib/builder.js +5 -13
- package/transform/lib/builder.js.map +1 -1
- package/transform/lib/index.d.ts +1 -0
- package/transform/lib/index.d.ts.map +1 -1
- package/transform/lib/index.js +672 -757
- package/transform/lib/index.js.map +1 -1
- package/transform/lib/linkers/alias.d.ts.map +1 -1
- package/transform/lib/linkers/alias.js.map +1 -1
- package/transform/lib/linkers/custom.d.ts.map +1 -1
- package/transform/lib/linkers/custom.js +8 -9
- package/transform/lib/linkers/custom.js.map +1 -1
- package/transform/lib/linkers/imports.d.ts.map +1 -1
- package/transform/lib/linkers/imports.js.map +1 -1
- package/transform/lib/types.d.ts +6 -0
- package/transform/lib/types.d.ts.map +1 -1
- package/transform/lib/types.js +83 -21
- package/transform/lib/types.js.map +1 -1
- package/transform/lib/util.d.ts.map +1 -1
- package/transform/lib/util.js +1 -1
- package/transform/lib/util.js.map +1 -1
- package/transform/lib/visitor.d.ts.map +1 -1
- package/transform/lib/visitor.js +1 -2
- package/transform/lib/visitor.js.map +1 -1
- package/.prettierrc +0 -3
- package/ARCHITECTURE.md +0 -320
- package/CONTRIBUTING.md +0 -238
- package/TODO +0 -1
- package/assembly/deserialize/simple/float.ts +0 -11
- package/assembly/deserialize/simple/integer.ts +0 -9
- package/assembly/test.ts +0 -30
- package/eslint.config.js +0 -60
- package/lib/tsconfig.json +0 -8
- package/tools/assemblyscript-eslint-local.js +0 -29
- package/tools/assemblyscript-eslint.js +0 -29
- package/transform/tsconfig.json +0 -35
package/lib/as-bs.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { OBJECT, TOTAL_OVERHEAD } from "rt/common";
|
|
2
|
+
import { heap } from "memory";
|
|
2
3
|
|
|
3
4
|
// Buffer management constants
|
|
4
|
-
const
|
|
5
|
-
const MIN_BUFFER_SIZE: usize =
|
|
5
|
+
const SHRINK_EVERY_N_MASK: usize = 255; // check every 256 outputs
|
|
6
|
+
const MIN_BUFFER_SIZE: usize = 1024;
|
|
6
7
|
|
|
7
8
|
// Exponential moving average smoothing factor (0.0 to 1.0)
|
|
8
9
|
// Higher values = more responsive to recent sizes, lower = more stable
|
|
@@ -13,11 +14,11 @@ const EMA_ALPHA_SHIFT: usize = 3; // 1/8 = 0.125
|
|
|
13
14
|
* Central buffer namespace for managing memory operations.
|
|
14
15
|
*/
|
|
15
16
|
export namespace bs {
|
|
16
|
-
/** Current
|
|
17
|
-
export let buffer:
|
|
17
|
+
/** Current unmanaged backing store pointer. */
|
|
18
|
+
export let buffer: usize = heap.alloc(MIN_BUFFER_SIZE);
|
|
18
19
|
|
|
19
20
|
/** Current offset within the buffer. */
|
|
20
|
-
export let offset: usize =
|
|
21
|
+
export let offset: usize = buffer;
|
|
21
22
|
|
|
22
23
|
/** Byte length of the buffer. */
|
|
23
24
|
let bufferSize: usize = MIN_BUFFER_SIZE;
|
|
@@ -25,8 +26,8 @@ export namespace bs {
|
|
|
25
26
|
/** Proposed size of output */
|
|
26
27
|
export let stackSize: usize = 0;
|
|
27
28
|
|
|
28
|
-
let
|
|
29
|
-
let
|
|
29
|
+
let pauseOffsets = new Array<usize>();
|
|
30
|
+
let pauseStackSizes = new Array<usize>();
|
|
30
31
|
|
|
31
32
|
// Exponential moving average of output sizes for adaptive buffer sizing
|
|
32
33
|
// This provides smoother adaptation than simple averaging
|
|
@@ -46,33 +47,46 @@ export namespace bs {
|
|
|
46
47
|
typicalSize += (newSize - typicalSize) >> EMA_ALPHA_SHIFT;
|
|
47
48
|
}
|
|
48
49
|
|
|
49
|
-
|
|
50
|
-
|
|
50
|
+
// @ts-expect-error: @inline is a valid decorator
|
|
51
|
+
@inline function renewBuffer(newSize: usize): void {
|
|
52
|
+
const oldPtr = buffer;
|
|
53
|
+
const relOffset = offset - oldPtr;
|
|
54
|
+
const newPtr = heap.realloc(oldPtr, newSize);
|
|
55
|
+
offset = newPtr + relOffset;
|
|
56
|
+
buffer = newPtr;
|
|
57
|
+
bufferSize = newSize;
|
|
58
|
+
}
|
|
51
59
|
|
|
52
60
|
// @ts-expect-error: @inline is a valid decorator
|
|
53
|
-
@inline
|
|
54
|
-
if (
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
61
|
+
@inline function reserve(requiredSize: usize, extra: usize): void {
|
|
62
|
+
if (requiredSize <= bufferSize) return;
|
|
63
|
+
// Grow aggressively (2x) to minimize realloc frequency in hot serialization paths.
|
|
64
|
+
let next = bufferSize << 1;
|
|
65
|
+
const minNext = requiredSize + extra;
|
|
66
|
+
if (next < minNext) next = minNext;
|
|
67
|
+
renewBuffer(next);
|
|
58
68
|
}
|
|
59
|
-
|
|
60
|
-
* Stores the state of the buffer, allowing further changes to be reset
|
|
61
|
-
*/
|
|
69
|
+
|
|
62
70
|
// @ts-expect-error: @inline is a valid decorator
|
|
63
|
-
@inline
|
|
64
|
-
|
|
65
|
-
|
|
71
|
+
@inline function finalizeDynamicOutput(len: usize): void {
|
|
72
|
+
counter += 1;
|
|
73
|
+
updateTypicalSize(len);
|
|
74
|
+
if ((counter & SHRINK_EVERY_N_MASK) == 0 && bufferSize > typicalSize << 2) {
|
|
75
|
+
resize(u32(typicalSize << 1));
|
|
76
|
+
}
|
|
77
|
+
offset = buffer;
|
|
78
|
+
stackSize = 0;
|
|
66
79
|
}
|
|
67
80
|
|
|
81
|
+
export let cacheOutput: usize = 0;
|
|
82
|
+
export let cacheOutputLen: usize = 0;
|
|
68
83
|
/**
|
|
69
|
-
*
|
|
70
|
-
* This allows for changes made after the pause to be discarded.
|
|
84
|
+
* Stores the state of the buffer, allowing further changes to be reset
|
|
71
85
|
*/
|
|
72
86
|
// @ts-expect-error: @inline is a valid decorator
|
|
73
|
-
@inline export function
|
|
74
|
-
offset
|
|
75
|
-
stackSize
|
|
87
|
+
@inline export function saveState(): void {
|
|
88
|
+
pauseOffsets.push(offset - buffer);
|
|
89
|
+
pauseStackSizes.push(stackSize);
|
|
76
90
|
}
|
|
77
91
|
|
|
78
92
|
/**
|
|
@@ -80,10 +94,14 @@ export namespace bs {
|
|
|
80
94
|
* This allows for changes made after the pause to be discarded.
|
|
81
95
|
*/
|
|
82
96
|
// @ts-expect-error: @inline is a valid decorator
|
|
83
|
-
@inline export function
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
97
|
+
@inline export function loadState(): void {
|
|
98
|
+
const length = pauseOffsets.length;
|
|
99
|
+
if (length == 0) return;
|
|
100
|
+
const index = length - 1;
|
|
101
|
+
offset = buffer + unchecked(pauseOffsets[index]);
|
|
102
|
+
stackSize = unchecked(pauseStackSizes[index]);
|
|
103
|
+
pauseOffsets.length = index;
|
|
104
|
+
pauseStackSizes.length = index;
|
|
87
105
|
}
|
|
88
106
|
|
|
89
107
|
/**
|
|
@@ -93,16 +111,7 @@ export namespace bs {
|
|
|
93
111
|
*/
|
|
94
112
|
// @ts-expect-error: @inline is a valid decorator
|
|
95
113
|
@inline export function ensureSize(size: u32): void {
|
|
96
|
-
|
|
97
|
-
const deltaBytes = usize(size) + MIN_BUFFER_SIZE;
|
|
98
|
-
bufferSize += deltaBytes;
|
|
99
|
-
// @ts-expect-error: __renew is a runtime builtin
|
|
100
|
-
const newPtr = changetype<ArrayBuffer>(
|
|
101
|
-
__renew(changetype<usize>(buffer), bufferSize),
|
|
102
|
-
);
|
|
103
|
-
offset = offset + changetype<usize>(newPtr) - changetype<usize>(buffer);
|
|
104
|
-
buffer = newPtr;
|
|
105
|
-
}
|
|
114
|
+
reserve(offset - buffer + usize(size), MIN_BUFFER_SIZE);
|
|
106
115
|
}
|
|
107
116
|
|
|
108
117
|
/**
|
|
@@ -112,16 +121,8 @@ export namespace bs {
|
|
|
112
121
|
*/
|
|
113
122
|
// @ts-expect-error: @inline is a valid decorator
|
|
114
123
|
@inline export function proposeSize(size: u32): void {
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
bufferSize += deltaBytes;
|
|
118
|
-
// @ts-expect-error: __renew is a runtime builtin
|
|
119
|
-
const newPtr = changetype<ArrayBuffer>(
|
|
120
|
-
__renew(changetype<usize>(buffer), bufferSize),
|
|
121
|
-
);
|
|
122
|
-
offset = offset + changetype<usize>(newPtr) - changetype<usize>(buffer);
|
|
123
|
-
buffer = newPtr;
|
|
124
|
-
}
|
|
124
|
+
stackSize += size;
|
|
125
|
+
reserve(stackSize, 0);
|
|
125
126
|
}
|
|
126
127
|
|
|
127
128
|
/**
|
|
@@ -131,16 +132,8 @@ export namespace bs {
|
|
|
131
132
|
*/
|
|
132
133
|
// @ts-expect-error: @inline is a valid decorator
|
|
133
134
|
@inline export function growSize(size: u32): void {
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
bufferSize += deltaBytes;
|
|
137
|
-
// @ts-expect-error: __renew is a runtime builtin
|
|
138
|
-
const newPtr = changetype<ArrayBuffer>(
|
|
139
|
-
__renew(changetype<usize>(buffer), bufferSize),
|
|
140
|
-
);
|
|
141
|
-
offset = offset + changetype<usize>(newPtr) - changetype<usize>(buffer);
|
|
142
|
-
buffer = newPtr;
|
|
143
|
-
}
|
|
135
|
+
stackSize += size;
|
|
136
|
+
reserve(stackSize, MIN_BUFFER_SIZE);
|
|
144
137
|
}
|
|
145
138
|
|
|
146
139
|
/**
|
|
@@ -149,14 +142,29 @@ export namespace bs {
|
|
|
149
142
|
*/
|
|
150
143
|
// @ts-expect-error: @inline is a valid decorator
|
|
151
144
|
@inline export function resize(newSize: u32): void {
|
|
152
|
-
|
|
153
|
-
const
|
|
154
|
-
|
|
155
|
-
);
|
|
156
|
-
bufferSize = newSize;
|
|
157
|
-
offset = changetype<usize>(newPtr);
|
|
145
|
+
const oldPtr = buffer;
|
|
146
|
+
const relOffset = offset - oldPtr;
|
|
147
|
+
const newPtr = heap.realloc(buffer, newSize);
|
|
158
148
|
buffer = newPtr;
|
|
159
|
-
|
|
149
|
+
bufferSize = newSize;
|
|
150
|
+
offset = buffer + relOffset;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Shrinks the buffer using the same adaptive sizing policy as normal output
|
|
155
|
+
* finalization. Keeps enough capacity for the recent typical output size while
|
|
156
|
+
* releasing clearly excess memory.
|
|
157
|
+
*/
|
|
158
|
+
// @ts-expect-error: @inline is a valid decorator
|
|
159
|
+
@inline export function shrink(): void {
|
|
160
|
+
let next = typicalSize << 1;
|
|
161
|
+
if (next < MIN_BUFFER_SIZE) next = MIN_BUFFER_SIZE;
|
|
162
|
+
if (bufferSize > next) {
|
|
163
|
+
resize(u32(next));
|
|
164
|
+
} else {
|
|
165
|
+
offset = buffer;
|
|
166
|
+
stackSize = 0;
|
|
167
|
+
}
|
|
160
168
|
}
|
|
161
169
|
|
|
162
170
|
/**
|
|
@@ -165,13 +173,14 @@ export namespace bs {
|
|
|
165
173
|
*/
|
|
166
174
|
// @ts-expect-error: @inline is a valid decorator
|
|
167
175
|
@inline export function cpyOut<T>(): T {
|
|
168
|
-
if (
|
|
169
|
-
const len = offset -
|
|
176
|
+
if (pauseOffsets.length == 0) {
|
|
177
|
+
const len = offset - buffer;
|
|
170
178
|
// @ts-expect-error: __new is a runtime builtin
|
|
171
179
|
const _out = __new(len, idof<T>());
|
|
172
|
-
memory.copy(_out,
|
|
180
|
+
memory.copy(_out, buffer, len);
|
|
173
181
|
return changetype<T>(_out);
|
|
174
182
|
} else {
|
|
183
|
+
const pauseOffset = buffer + unchecked(pauseOffsets[pauseOffsets.length - 1]);
|
|
175
184
|
const len = offset - pauseOffset;
|
|
176
185
|
// @ts-expect-error: __new is a runtime builtin
|
|
177
186
|
const _out = __new(len, idof<T>());
|
|
@@ -181,6 +190,59 @@ export namespace bs {
|
|
|
181
190
|
}
|
|
182
191
|
}
|
|
183
192
|
|
|
193
|
+
/**
|
|
194
|
+
* Copies the slice starting at a caller-provided relative buffer offset and restores
|
|
195
|
+
* `offset` back to that slice start.
|
|
196
|
+
*
|
|
197
|
+
* This is intended for deserializers that borrow `bs` as temporary scratch space and
|
|
198
|
+
* already know their local slice start as `bs.offset - bs.buffer`.
|
|
199
|
+
*
|
|
200
|
+
* Note: this restores only `offset`. Deserialization paths do not currently depend on
|
|
201
|
+
* `stackSize`, which is tracked for serialization growth heuristics.
|
|
202
|
+
*/
|
|
203
|
+
// @ts-expect-error: @inline is a valid decorator
|
|
204
|
+
@inline export function sliceOut<T>(start: usize): T {
|
|
205
|
+
const sliceStart = buffer + start;
|
|
206
|
+
const len = offset - sliceStart;
|
|
207
|
+
// @ts-expect-error: __new is a runtime builtin
|
|
208
|
+
const _out = __new(len, idof<T>());
|
|
209
|
+
memory.copy(_out, sliceStart, len);
|
|
210
|
+
offset = sliceStart;
|
|
211
|
+
return changetype<T>(_out);
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* Copies the slice starting at a caller-provided relative buffer offset into a string field
|
|
216
|
+
* and restores `offset` back to that slice start.
|
|
217
|
+
*/
|
|
218
|
+
// @ts-expect-error: @inline is a valid decorator
|
|
219
|
+
@inline export function toField(start: usize, dstFieldPtr: usize): void {
|
|
220
|
+
const sliceStart = buffer + start;
|
|
221
|
+
const byteLength = <u32>(offset - sliceStart);
|
|
222
|
+
if (byteLength == 0) {
|
|
223
|
+
store<usize>(dstFieldPtr, changetype<usize>(""));
|
|
224
|
+
offset = sliceStart;
|
|
225
|
+
return;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
const current = load<usize>(dstFieldPtr);
|
|
229
|
+
let stringPtr: usize;
|
|
230
|
+
if (current != 0 && changetype<OBJECT>(current - TOTAL_OVERHEAD).rtSize == byteLength) {
|
|
231
|
+
stringPtr = current;
|
|
232
|
+
} else if (current != 0 && current != changetype<usize>("")) {
|
|
233
|
+
// @ts-expect-error: __renew is a runtime builtin
|
|
234
|
+
stringPtr = __renew(current, byteLength);
|
|
235
|
+
store<usize>(dstFieldPtr, stringPtr);
|
|
236
|
+
} else {
|
|
237
|
+
// @ts-expect-error: __new is a runtime builtin
|
|
238
|
+
stringPtr = __new(byteLength, idof<string>());
|
|
239
|
+
store<usize>(dstFieldPtr, stringPtr);
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
memory.copy(stringPtr, sliceStart, byteLength);
|
|
243
|
+
offset = sliceStart;
|
|
244
|
+
}
|
|
245
|
+
|
|
184
246
|
/**
|
|
185
247
|
* Copies the buffer's content to a new object of a specified type.
|
|
186
248
|
* Uses exponential moving average to track typical output sizes for
|
|
@@ -191,19 +253,11 @@ export namespace bs {
|
|
|
191
253
|
@inline export function out<T>(): T {
|
|
192
254
|
let out: usize;
|
|
193
255
|
if (cacheOutput === 0) {
|
|
194
|
-
const len = offset -
|
|
256
|
+
const len = offset - buffer;
|
|
195
257
|
// @ts-expect-error: __new is a runtime builtin
|
|
196
258
|
out = __new(len, idof<T>());
|
|
197
|
-
memory.copy(out,
|
|
198
|
-
|
|
199
|
-
counter++;
|
|
200
|
-
// Use exponential moving average for smoother size tracking
|
|
201
|
-
updateTypicalSize(len);
|
|
202
|
-
if (counter >= SHRINK_EVERY_N) {
|
|
203
|
-
// Shrink if buffer is 4x larger than typical, resize to 2x typical
|
|
204
|
-
if (bufferSize > typicalSize << 2) resize(u32(typicalSize << 1));
|
|
205
|
-
counter = 0;
|
|
206
|
-
}
|
|
259
|
+
memory.copy(out, buffer, len);
|
|
260
|
+
finalizeDynamicOutput(len);
|
|
207
261
|
} else {
|
|
208
262
|
// zero-copy path
|
|
209
263
|
// @ts-expect-error: __new is a runtime builtin
|
|
@@ -211,56 +265,12 @@ export namespace bs {
|
|
|
211
265
|
memory.copy(out, cacheOutput, cacheOutputLen);
|
|
212
266
|
// reset arena flag
|
|
213
267
|
cacheOutput = 0;
|
|
268
|
+
cacheOutputLen = 0;
|
|
269
|
+
offset = buffer;
|
|
270
|
+
stackSize = 0;
|
|
214
271
|
}
|
|
215
|
-
|
|
216
|
-
offset = changetype<usize>(buffer);
|
|
217
|
-
stackSize = 0;
|
|
218
272
|
return changetype<T>(out);
|
|
219
273
|
}
|
|
220
|
-
|
|
221
|
-
/**
|
|
222
|
-
* Copies the buffer's content to a new object of a specified type.
|
|
223
|
-
* @returns The new object containing the buffer's content.
|
|
224
|
-
*/
|
|
225
|
-
// @ts-expect-error: @inline is a valid decorator
|
|
226
|
-
@inline export function view<T>(): T {
|
|
227
|
-
const len = offset - changetype<usize>(buffer);
|
|
228
|
-
// @ts-expect-error: __new is a runtime builtin
|
|
229
|
-
const _out = __new(len, idof<T>());
|
|
230
|
-
memory.copy(_out, changetype<usize>(buffer), len);
|
|
231
|
-
return changetype<T>(_out);
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
/**
|
|
235
|
-
* Copies the buffer's content to a given destination pointer.
|
|
236
|
-
* Uses exponential moving average for adaptive buffer sizing.
|
|
237
|
-
* @param dst - The destination pointer.
|
|
238
|
-
* @returns The destination pointer cast to the specified type.
|
|
239
|
-
*/
|
|
240
|
-
// @ts-expect-error: @inline is a valid decorator
|
|
241
|
-
@inline export function outTo<T>(dst: usize): T {
|
|
242
|
-
const len = offset - changetype<usize>(buffer);
|
|
243
|
-
// @ts-expect-error: __renew is a runtime builtin
|
|
244
|
-
if (len != changetype<OBJECT>(dst - TOTAL_OVERHEAD).rtSize)
|
|
245
|
-
__renew(len, idof<T>());
|
|
246
|
-
memory.copy(dst, changetype<usize>(buffer), len);
|
|
247
|
-
|
|
248
|
-
counter++;
|
|
249
|
-
// Use exponential moving average for smoother size tracking
|
|
250
|
-
updateTypicalSize(len);
|
|
251
|
-
|
|
252
|
-
if (counter >= SHRINK_EVERY_N) {
|
|
253
|
-
// Shrink if buffer is 4x larger than typical, resize to 2x typical
|
|
254
|
-
if (bufferSize > typicalSize << 2) {
|
|
255
|
-
resize(typicalSize << 1);
|
|
256
|
-
}
|
|
257
|
-
counter = 0;
|
|
258
|
-
}
|
|
259
|
-
|
|
260
|
-
offset = changetype<usize>(buffer);
|
|
261
|
-
stackSize = 0;
|
|
262
|
-
return changetype<T>(dst);
|
|
263
|
-
}
|
|
264
274
|
}
|
|
265
275
|
|
|
266
276
|
/**
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "json-as",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.3.1",
|
|
4
4
|
"author": "Jairus Tanaka",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -12,14 +12,13 @@
|
|
|
12
12
|
"@assemblyscript/wasi-shim": "^0.1.0",
|
|
13
13
|
"@eslint/js": "^9.0.0",
|
|
14
14
|
"@types/node": "^25.0.10",
|
|
15
|
-
"as-
|
|
15
|
+
"as-test": "^1.0.1",
|
|
16
16
|
"assemblyscript": "^0.28.9",
|
|
17
17
|
"assemblyscript-prettier": "^3.0.1",
|
|
18
18
|
"chartjs-node-canvas": "^5.0.0",
|
|
19
19
|
"chartjs-plugin-datalabels": "^2.2.0",
|
|
20
20
|
"eslint": "^10.0.0",
|
|
21
|
-
"
|
|
22
|
-
"prettier": "3.6.2",
|
|
21
|
+
"prettier": "3.8.1",
|
|
23
22
|
"tinybench": "^6.0.0",
|
|
24
23
|
"typescript": "^5.9.3",
|
|
25
24
|
"typescript-eslint": "^8.0.0"
|
|
@@ -42,6 +41,24 @@
|
|
|
42
41
|
],
|
|
43
42
|
"description": "The only JSON library you'll need for AssemblyScript with SIMD and SWAR",
|
|
44
43
|
"homepage": "https://github.com/JairusSW/json-as#readme",
|
|
44
|
+
"files": [
|
|
45
|
+
"assembly/custom/",
|
|
46
|
+
"assembly/deserialize/",
|
|
47
|
+
"assembly/globals/",
|
|
48
|
+
"assembly/serialize/",
|
|
49
|
+
"assembly/util/",
|
|
50
|
+
"assembly/index.ts",
|
|
51
|
+
"assembly/index.d.ts",
|
|
52
|
+
"assembly/types.ts",
|
|
53
|
+
"assembly/tsconfig.json",
|
|
54
|
+
"lib/as-bs.ts",
|
|
55
|
+
"transform/lib/",
|
|
56
|
+
"transform/package.json",
|
|
57
|
+
"index.ts",
|
|
58
|
+
"README.md",
|
|
59
|
+
"CHANGELOG.md",
|
|
60
|
+
"LICENSE"
|
|
61
|
+
],
|
|
45
62
|
"keywords": [
|
|
46
63
|
"assemblyscript",
|
|
47
64
|
"json",
|
|
@@ -63,9 +80,13 @@
|
|
|
63
80
|
},
|
|
64
81
|
"scripts": {
|
|
65
82
|
"ci": "act",
|
|
66
|
-
"test": "
|
|
83
|
+
"test": "ast test --disable coverage --disable try-as",
|
|
84
|
+
"test:ci": "ast test --disable coverage --clean",
|
|
85
|
+
"test:coverage": "ast test --enable coverage",
|
|
86
|
+
"bench": "npm run bench:as && npm run bench:js && ./build-charts.sh",
|
|
67
87
|
"bench:as": "bash ./run-bench.as.sh",
|
|
68
88
|
"bench:js": "bash ./run-bench.js.sh",
|
|
89
|
+
"bench:publish": "bash ./publish-benchmarks.sh",
|
|
69
90
|
"build:test": "JSON_DEBUG=0 JSON_WRITE=assembly/test.ts asc assembly/test.ts --transform ./transform -o ./build/test.wasm --textFile ./build/test.wat --enable simd --debug --config ./node_modules/@assemblyscript/wasi-shim/asconfig.json",
|
|
70
91
|
"build:tmp:test": "JSON_DEBUG=1 asc assembly/test.tmp.ts -o ./build/test.wasm --textFile ./build/test.wat --enable simd --debug --config ./node_modules/@assemblyscript/wasi-shim/asconfig.json",
|
|
71
92
|
"build:test:wine": "JSON_DEBUG=1 JSON_WRITE=assembly/test.ts NODE_SKIP_PLATFORM_CHECK=1 wine ~/.win-bin/node/node.exe ./node_modules/assemblyscript/bin/asc.js assembly/test.ts --transform ./transform -o ./build/test.wasm --textFile ./build/test.wat --debug --config ./node_modules/@assemblyscript/wasi-shim/asconfig.json",
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"builder.d.ts","sourceRoot":"","sources":["../src/builder.ts"],"names":[],"mappings":"AAGA,OAAO,
|
|
1
|
+
{"version":3,"file":"builder.d.ts","sourceRoot":"","sources":["../src/builder.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,sBAAsB,EAAa,mBAAmB,EAAiB,gBAAgB,EAAE,cAAc,EAAE,cAAc,EAAE,cAAc,EAAE,gBAAgB,EAAE,eAAe,EAAE,eAAe,EAAe,iBAAiB,EAAE,oBAAoB,EAAE,aAAa,EAAE,WAAW,EAAE,uBAAuB,EAAE,cAAc,EAAE,eAAe,EAAE,oBAAoB,EAAE,sBAAsB,EAAE,qBAAqB,EAAE,YAAY,EAAE,eAAe,EAAE,UAAU,EAAE,mBAAmB,EAAE,eAAe,EAAE,gBAAgB,EAAE,sBAAsB,EAAE,cAAc,EAAE,YAAY,EAAE,mBAAmB,EAAE,kBAAkB,EAAE,gBAAgB,EAAE,oBAAoB,EAAE,WAAW,EAAE,iBAAiB,EAAE,eAAe,EAAE,kBAAkB,EAAE,oBAAoB,EAAE,wBAAwB,EAAE,oBAAoB,EAAiB,iBAAiB,EAAe,iBAAiB,EAAE,aAAa,EAAE,oBAAoB,EAAE,aAAa,EAAE,IAAI,EAAY,cAAc,EAAE,uBAAuB,EAAwC,aAAa,EAAE,uBAAuB,EAAE,wBAAwB,EAAE,uBAAuB,EAAE,eAAe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Omitted, operatorTokenToString, util } from "assemblyscript/dist/assemblyscript.js";
|
|
2
2
|
import { Visitor } from "./visitor.js";
|
|
3
3
|
function assert(isTruish, message = "assertion error") {
|
|
4
4
|
if (!isTruish)
|
|
@@ -522,9 +522,7 @@ export class ASTBuilder extends Visitor {
|
|
|
522
522
|
else {
|
|
523
523
|
const last = sb[sb.length - 1];
|
|
524
524
|
const lastCharPos = last.length - 1;
|
|
525
|
-
if (lastCharPos >= 0 &&
|
|
526
|
-
(last.charCodeAt(lastCharPos) == 125 ||
|
|
527
|
-
last.charCodeAt(lastCharPos) == 59)) {
|
|
525
|
+
if (lastCharPos >= 0 && (last.charCodeAt(lastCharPos) == 125 || last.charCodeAt(lastCharPos) == 59)) {
|
|
528
526
|
sb.push("\n");
|
|
529
527
|
}
|
|
530
528
|
else {
|
|
@@ -632,8 +630,7 @@ export class ASTBuilder extends Visitor {
|
|
|
632
630
|
}
|
|
633
631
|
for (let i = 0, k = members.length; i < k; ++i) {
|
|
634
632
|
const member = members[i];
|
|
635
|
-
if (member.kind != 54 ||
|
|
636
|
-
member.parameterIndex < 0) {
|
|
633
|
+
if (member.kind != 54 || member.parameterIndex < 0) {
|
|
637
634
|
util.indent(sb, indentLevel);
|
|
638
635
|
this.visitNodeAndTerminate(member);
|
|
639
636
|
}
|
|
@@ -928,8 +925,7 @@ export class ASTBuilder extends Visitor {
|
|
|
928
925
|
}
|
|
929
926
|
}
|
|
930
927
|
else {
|
|
931
|
-
if (!isTypeOmitted(returnType) &&
|
|
932
|
-
!node.isAny(524288 | 4096)) {
|
|
928
|
+
if (!isTypeOmitted(returnType) && !node.isAny(524288 | 4096)) {
|
|
933
929
|
sb.push("): ");
|
|
934
930
|
this.visitTypeNode(returnType);
|
|
935
931
|
}
|
|
@@ -1250,11 +1246,7 @@ export class ASTBuilder extends Visitor {
|
|
|
1250
1246
|
const numDeclarations = declarations.length;
|
|
1251
1247
|
const firstDeclaration = declarations[0];
|
|
1252
1248
|
this.serializeExternalModifiers(firstDeclaration);
|
|
1253
|
-
sb.push(firstDeclaration.is(8)
|
|
1254
|
-
? "const "
|
|
1255
|
-
: firstDeclaration.is(16)
|
|
1256
|
-
? "let "
|
|
1257
|
-
: "var ");
|
|
1249
|
+
sb.push(firstDeclaration.is(8) ? "const " : firstDeclaration.is(16) ? "let " : "var ");
|
|
1258
1250
|
this.visitVariableDeclaration(node.declarations[0]);
|
|
1259
1251
|
for (let i = 1; i < numDeclarations; ++i) {
|
|
1260
1252
|
sb.push(", ");
|