toiljs 0.0.16 → 0.0.19

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.
Files changed (100) hide show
  1. package/CHANGELOG.md +111 -0
  2. package/README.md +313 -128
  3. package/as-pect.config.js +1 -1
  4. package/build/backend/.tsbuildinfo +1 -1
  5. package/build/backend/index.d.ts +1 -0
  6. package/build/backend/index.js +20 -1
  7. package/build/cli/.tsbuildinfo +1 -1
  8. package/build/cli/index.js +1320 -696
  9. package/build/client/.tsbuildinfo +1 -1
  10. package/build/client/dev/devtools.js +42 -5
  11. package/build/client/errors.d.ts +1 -0
  12. package/build/client/errors.js +3 -0
  13. package/build/client/index.d.ts +2 -0
  14. package/build/client/index.js +2 -0
  15. package/build/client/rpc.d.ts +1 -0
  16. package/build/client/rpc.js +37 -0
  17. package/build/compiler/.tsbuildinfo +1 -1
  18. package/build/compiler/config.js +3 -1
  19. package/build/compiler/docs.js +62 -5
  20. package/build/compiler/generate.js +5 -4
  21. package/build/compiler/index.d.ts +1 -0
  22. package/build/compiler/index.js +1 -1
  23. package/build/compiler/plugin.js +80 -8
  24. package/build/compiler/seo.js +15 -1
  25. package/build/compiler/ssg.js +7 -1
  26. package/build/compiler/vite.js +25 -0
  27. package/build/io/.tsbuildinfo +1 -1
  28. package/build/io/codec.d.ts +54 -0
  29. package/build/io/codec.js +143 -0
  30. package/build/io/index.d.ts +1 -2
  31. package/build/io/index.js +1 -2
  32. package/eslint.config.js +1 -1
  33. package/examples/basic/client/routes/features/index.tsx +1 -1
  34. package/examples/basic/client/routes/io.tsx +6 -7
  35. package/examples/basic/client/routes/rest.tsx +74 -0
  36. package/examples/basic/client/routes/rpc.tsx +43 -0
  37. package/package.json +19 -7
  38. package/presets/prettier-plugin.js +51 -0
  39. package/presets/prettier.json +1 -0
  40. package/server/runtime/README.md +97 -0
  41. package/server/runtime/abort/abort.ts +27 -0
  42. package/server/runtime/env/Server.ts +61 -0
  43. package/server/runtime/envelope.ts +191 -0
  44. package/server/runtime/exports/index.ts +52 -0
  45. package/server/runtime/handlers/ToilHandler.ts +34 -0
  46. package/server/runtime/index.ts +26 -0
  47. package/server/runtime/lang/Potential.ts +5 -0
  48. package/server/runtime/memory.ts +81 -0
  49. package/server/runtime/request.ts +55 -0
  50. package/server/runtime/response.ts +86 -0
  51. package/server/runtime/rest/Rest.ts +39 -0
  52. package/server/runtime/rest/RestHandler.ts +20 -0
  53. package/server/runtime/rest/RouteContext.ts +82 -0
  54. package/server/runtime/rest/match.ts +48 -0
  55. package/server/runtime/tsconfig.json +7 -0
  56. package/src/backend/index.ts +45 -3
  57. package/src/cli/create.ts +15 -5
  58. package/src/cli/diagnostics.ts +81 -0
  59. package/src/cli/doctor.ts +384 -7
  60. package/src/cli/index.ts +11 -2
  61. package/src/client/dev/devtools.tsx +49 -4
  62. package/src/client/errors.ts +11 -0
  63. package/src/client/index.ts +2 -0
  64. package/src/client/rpc.ts +64 -0
  65. package/src/compiler/config.ts +3 -1
  66. package/src/compiler/docs.ts +62 -5
  67. package/src/compiler/generate.ts +6 -5
  68. package/src/compiler/index.ts +3 -1
  69. package/src/compiler/plugin.ts +99 -11
  70. package/src/compiler/seo.ts +23 -3
  71. package/src/compiler/ssg.ts +10 -1
  72. package/src/compiler/vite.ts +34 -0
  73. package/src/io/FastMap.ts +24 -0
  74. package/src/io/FastSet.ts +15 -1
  75. package/src/io/codec.ts +217 -0
  76. package/src/io/index.ts +1 -2
  77. package/src/io/types.ts +2 -1
  78. package/test/assembly/example.spec.ts +14 -4
  79. package/test/doctor.test.ts +65 -0
  80. package/test/errors.test.ts +21 -0
  81. package/test/io.test.ts +65 -41
  82. package/test/prettier-plugin.test.ts +46 -0
  83. package/test/rpc.test.ts +50 -0
  84. package/tests/data-parity/generated-parity.ts +99 -0
  85. package/tests/data-parity/parity.ts +80 -0
  86. package/tests/data-parity/spec.ts +46 -0
  87. package/tsconfig.json +1 -1
  88. package/tsconfig.server.json +1 -1
  89. package/build/io/BinaryReader.d.ts +0 -44
  90. package/build/io/BinaryReader.js +0 -244
  91. package/build/io/BinaryWriter.d.ts +0 -44
  92. package/build/io/BinaryWriter.js +0 -297
  93. package/build/server/release.wasm +0 -0
  94. package/build/server/release.wat +0 -9
  95. package/src/io/BinaryReader.ts +0 -340
  96. package/src/io/BinaryWriter.ts +0 -385
  97. package/src/server/index.ts +0 -10
  98. package/src/server/main.ts +0 -13
  99. package/src/server/tsconfig.json +0 -4
  100. package/toilconfig.json +0 -30
@@ -1,244 +0,0 @@
1
- import { I128_BYTE_LENGTH, I16_BYTE_LENGTH, I32_BYTE_LENGTH, I64_BYTE_LENGTH, I8_BYTE_LENGTH, U128_BYTE_LENGTH, U16_BYTE_LENGTH, U256_BYTE_LENGTH, U32_BYTE_LENGTH, U64_BYTE_LENGTH, U8_BYTE_LENGTH, } from './lengths.js';
2
- export class BinaryReader {
3
- buffer;
4
- currentOffset = 0;
5
- constructor(bytes) {
6
- this.buffer = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
7
- }
8
- get byteLength() {
9
- return this.buffer.byteLength;
10
- }
11
- static stringCompare(a, b) {
12
- return a.localeCompare(b);
13
- }
14
- static bigintCompare(a, b) {
15
- if (a < b)
16
- return -1;
17
- if (a > b)
18
- return 1;
19
- return 0;
20
- }
21
- static numberCompare(a, b) {
22
- if (a < b)
23
- return -1;
24
- if (a > b)
25
- return 1;
26
- return 0;
27
- }
28
- setBuffer(bytes) {
29
- this.buffer = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
30
- this.currentOffset = 0;
31
- }
32
- length() {
33
- return this.buffer.byteLength;
34
- }
35
- bytesLeft() {
36
- return this.buffer.byteLength - this.currentOffset;
37
- }
38
- readI8() {
39
- this.verifyEnd(this.currentOffset + I8_BYTE_LENGTH);
40
- const value = this.buffer.getInt8(this.currentOffset);
41
- this.currentOffset += I8_BYTE_LENGTH;
42
- return value;
43
- }
44
- readI16(be = true) {
45
- this.verifyEnd(this.currentOffset + I16_BYTE_LENGTH);
46
- const value = this.buffer.getInt16(this.currentOffset, !be);
47
- this.currentOffset += I16_BYTE_LENGTH;
48
- return value;
49
- }
50
- readI32(be = true) {
51
- this.verifyEnd(this.currentOffset + I32_BYTE_LENGTH);
52
- const value = this.buffer.getInt32(this.currentOffset, !be);
53
- this.currentOffset += I32_BYTE_LENGTH;
54
- return value;
55
- }
56
- readI64(be = true) {
57
- this.verifyEnd(this.currentOffset + I64_BYTE_LENGTH);
58
- const value = this.buffer.getBigInt64(this.currentOffset, !be);
59
- this.currentOffset += I64_BYTE_LENGTH;
60
- return value;
61
- }
62
- readU8() {
63
- this.verifyEnd(this.currentOffset + U8_BYTE_LENGTH);
64
- const value = this.buffer.getUint8(this.currentOffset);
65
- this.currentOffset += U8_BYTE_LENGTH;
66
- return value;
67
- }
68
- readU16(be = true) {
69
- this.verifyEnd(this.currentOffset + U16_BYTE_LENGTH);
70
- const value = this.buffer.getUint16(this.currentOffset, !be);
71
- this.currentOffset += U16_BYTE_LENGTH;
72
- return value;
73
- }
74
- readU32(be = true) {
75
- this.verifyEnd(this.currentOffset + U32_BYTE_LENGTH);
76
- const value = this.buffer.getUint32(this.currentOffset, !be);
77
- this.currentOffset += U32_BYTE_LENGTH;
78
- return value;
79
- }
80
- readU64(be = true) {
81
- this.verifyEnd(this.currentOffset + U64_BYTE_LENGTH);
82
- const value = this.buffer.getBigUint64(this.currentOffset, !be);
83
- this.currentOffset += U64_BYTE_LENGTH;
84
- return value;
85
- }
86
- readU128(be = true) {
87
- const raw = this.readBytes(U128_BYTE_LENGTH);
88
- let bytes = raw;
89
- if (!be) {
90
- bytes = this.reverseBytes(raw);
91
- }
92
- return BigInt('0x' + this.toHexString(bytes));
93
- }
94
- readU256(be = true) {
95
- const raw = this.readBytes(U256_BYTE_LENGTH);
96
- let bytes = raw;
97
- if (!be) {
98
- bytes = this.reverseBytes(raw);
99
- }
100
- return BigInt('0x' + this.toHexString(bytes));
101
- }
102
- readI128(be = true) {
103
- const raw = this.readBytes(I128_BYTE_LENGTH);
104
- let bytes = raw;
105
- if (!be) {
106
- bytes = this.reverseBytes(raw);
107
- }
108
- let value = BigInt('0x' + this.toHexString(bytes));
109
- const signBitMask = 0x80;
110
- if (bytes[0] & signBitMask) {
111
- const twoTo128 = BigInt(1) << BigInt(128);
112
- value = value - twoTo128;
113
- }
114
- return value;
115
- }
116
- readBoolean() {
117
- return this.readU8() !== 0;
118
- }
119
- readSelector() {
120
- return this.readU32(true);
121
- }
122
- readBytes(length, zeroStop = false) {
123
- this.verifyEnd(this.currentOffset + length);
124
- let bytes = new Uint8Array(length);
125
- for (let i = 0; i < length; i++) {
126
- const b = this.buffer.getUint8(this.currentOffset++);
127
- if (zeroStop && b === 0) {
128
- bytes = new Uint8Array(bytes.subarray(0, i));
129
- break;
130
- }
131
- bytes[i] = b;
132
- }
133
- return bytes;
134
- }
135
- readString(length) {
136
- const textDecoder = new TextDecoder();
137
- const bytes = this.readBytes(length, false);
138
- return textDecoder.decode(bytes);
139
- }
140
- readStringWithLength(be = true) {
141
- const length = this.readU32(be);
142
- return this.readString(length);
143
- }
144
- readBytesWithLength(maxLength = 0, be = true) {
145
- const length = this.readU32(be);
146
- if (maxLength > 0 && length > maxLength) {
147
- throw new Error('Data length exceeds maximum length.');
148
- }
149
- return this.readBytes(length);
150
- }
151
- readArrayOfBuffer(be = true) {
152
- const length = this.readU16(be);
153
- const result = new Array(length);
154
- for (let i = 0; i < length; i++) {
155
- result[i] = this.readBytesWithLength();
156
- }
157
- return result;
158
- }
159
- readU256Array(be = true) {
160
- const length = this.readU16(be);
161
- const result = new Array(length);
162
- for (let i = 0; i < length; i++) {
163
- result[i] = this.readU256(be);
164
- }
165
- return result;
166
- }
167
- readU128Array(be = true) {
168
- const length = this.readU16(be);
169
- const result = new Array(length);
170
- for (let i = 0; i < length; i++) {
171
- result[i] = this.readU128(be);
172
- }
173
- return result;
174
- }
175
- readU64Array(be = true) {
176
- const length = this.readU16(be);
177
- const result = new Array(length);
178
- for (let i = 0; i < length; i++) {
179
- result[i] = this.readU64(be);
180
- }
181
- return result;
182
- }
183
- readU32Array(be = true) {
184
- const length = this.readU16(be);
185
- const result = new Array(length);
186
- for (let i = 0; i < length; i++) {
187
- result[i] = this.readU32(be);
188
- }
189
- return result;
190
- }
191
- readU16Array(be = true) {
192
- const length = this.readU16(be);
193
- const result = new Array(length);
194
- for (let i = 0; i < length; i++) {
195
- result[i] = this.readU16(be);
196
- }
197
- return result;
198
- }
199
- readU8Array() {
200
- const length = this.readU16(true);
201
- const result = new Array(length);
202
- for (let i = 0; i < length; i++) {
203
- result[i] = this.readU8();
204
- }
205
- return result;
206
- }
207
- readStringArray(be = true) {
208
- const length = this.readU16(be);
209
- const result = new Array(length);
210
- for (let i = 0; i < length; i++) {
211
- result[i] = this.readStringWithLength(be);
212
- }
213
- return result;
214
- }
215
- readBytesArray(be = true) {
216
- const length = this.readU16(be);
217
- const result = new Array(length);
218
- for (let i = 0; i < length; i++) {
219
- result[i] = this.readBytesWithLength(0, be);
220
- }
221
- return result;
222
- }
223
- getOffset() {
224
- return this.currentOffset;
225
- }
226
- setOffset(offset) {
227
- this.currentOffset = offset;
228
- }
229
- verifyEnd(size) {
230
- if (size > this.buffer.byteLength) {
231
- throw new Error(`Attempt to read beyond buffer length: requested up to byte offset ${size}, but buffer is only ${this.buffer.byteLength} bytes.`);
232
- }
233
- }
234
- reverseBytes(bytes) {
235
- const out = new Uint8Array(bytes.length);
236
- for (let i = 0; i < bytes.length; i++) {
237
- out[i] = bytes[bytes.length - 1 - i];
238
- }
239
- return out;
240
- }
241
- toHexString(bytes) {
242
- return Array.from(bytes, (b) => b.toString(16).padStart(2, '0')).join('');
243
- }
244
- }
@@ -1,44 +0,0 @@
1
- import type { i16, i32, i64, i8, Selector, u16, u32, u64, u8 } from './types.js';
2
- import { BinaryReader } from './BinaryReader.js';
3
- export declare class BinaryWriter implements Disposable {
4
- private currentOffset;
5
- private buffer;
6
- constructor(length?: number);
7
- static estimateArrayOfBufferLength(values: Uint8Array[]): u32;
8
- writeU8(value: u8): void;
9
- writeU16(value: u16, be?: boolean): void;
10
- writeU32(value: u32, be?: boolean): void;
11
- writeU64(value: u64, be?: boolean): void;
12
- writeI8(value: i8): void;
13
- writeI16(value: i16, be?: boolean): void;
14
- writeI32(value: i32, be?: boolean): void;
15
- writeI64(value: i64, be?: boolean): void;
16
- writeSelector(value: Selector): void;
17
- writeBoolean(value: boolean): void;
18
- writeI128(bigIntValue: bigint, be?: boolean): void;
19
- writeU256(bigIntValue: bigint, be?: boolean): void;
20
- writeU128(bigIntValue: bigint, be?: boolean): void;
21
- writeBytes(value: Uint8Array): void;
22
- writeString(value: string): void;
23
- writeStringWithLength(value: string): void;
24
- getBuffer(clear?: boolean): Uint8Array;
25
- reset(): void;
26
- toBytesReader(): BinaryReader;
27
- getOffset(): u32;
28
- setOffset(offset: u32): void;
29
- clear(): void;
30
- [Symbol.dispose](): void;
31
- allocSafe(size: u32): void;
32
- writeBytesWithLength(value: Uint8Array): void;
33
- writeArrayOfBuffer(values: Uint8Array[], be?: boolean): void;
34
- writeU32Array(value: u32[], be?: boolean): void;
35
- writeU256Array(value: bigint[], be?: boolean): void;
36
- writeU128Array(value: bigint[], be?: boolean): void;
37
- writeStringArray(value: string[]): void;
38
- writeU16Array(value: u16[], be?: boolean): void;
39
- writeU8Array(value: u8[]): void;
40
- writeU64Array(value: bigint[], be?: boolean): void;
41
- writeBytesArray(value: Uint8Array[]): void;
42
- private resize;
43
- private getDefaultBuffer;
44
- }
@@ -1,297 +0,0 @@
1
- import { I128_BYTE_LENGTH, I16_BYTE_LENGTH, I32_BYTE_LENGTH, I64_BYTE_LENGTH, I8_BYTE_LENGTH, U128_BYTE_LENGTH, U16_BYTE_LENGTH, U256_BYTE_LENGTH, U32_BYTE_LENGTH, U64_BYTE_LENGTH, U8_BYTE_LENGTH, } from './lengths.js';
2
- import { BinaryReader } from './BinaryReader.js';
3
- function bigIntToBytes(value, length = U256_BYTE_LENGTH) {
4
- if (value < 0n) {
5
- throw new RangeError('Value cannot be negative');
6
- }
7
- const hex = value.toString(16).padStart(length * 2, '0');
8
- if (hex.length > length * 2) {
9
- throw new RangeError(`Value exceeds ${length}-byte range`);
10
- }
11
- const out = new Uint8Array(length);
12
- for (let i = 0; i < length; i++) {
13
- out[i] = parseInt(hex.substring(i * 2, i * 2 + 2), 16);
14
- }
15
- return out;
16
- }
17
- export class BinaryWriter {
18
- currentOffset = 0;
19
- buffer;
20
- constructor(length = 0) {
21
- this.buffer = this.getDefaultBuffer(length);
22
- }
23
- static estimateArrayOfBufferLength(values) {
24
- if (values.length > 65535)
25
- throw new Error('Array size is too large');
26
- let totalLength = U16_BYTE_LENGTH;
27
- for (let i = 0; i < values.length; i++) {
28
- totalLength += U32_BYTE_LENGTH + values[i].length;
29
- }
30
- return totalLength;
31
- }
32
- writeU8(value) {
33
- if (value > 255)
34
- throw new Error('u8 value is too large.');
35
- this.allocSafe(U8_BYTE_LENGTH);
36
- this.buffer.setUint8(this.currentOffset++, value);
37
- }
38
- writeU16(value, be = true) {
39
- if (value > 65535)
40
- throw new Error('u16 value is too large.');
41
- this.allocSafe(U16_BYTE_LENGTH);
42
- this.buffer.setUint16(this.currentOffset, value, !be);
43
- this.currentOffset += 2;
44
- }
45
- writeU32(value, be = true) {
46
- if (value > 4294967295)
47
- throw new Error('u32 value is too large.');
48
- this.allocSafe(U32_BYTE_LENGTH);
49
- this.buffer.setUint32(this.currentOffset, value, !be);
50
- this.currentOffset += 4;
51
- }
52
- writeU64(value, be = true) {
53
- if (value > 18446744073709551615n)
54
- throw new Error('u64 value is too large.');
55
- this.allocSafe(U64_BYTE_LENGTH);
56
- this.buffer.setBigUint64(this.currentOffset, value, !be);
57
- this.currentOffset += 8;
58
- }
59
- writeI8(value) {
60
- if (value < -128 || value > 127)
61
- throw new Error('i8 value is out of range.');
62
- this.allocSafe(I8_BYTE_LENGTH);
63
- this.buffer.setInt8(this.currentOffset, value);
64
- this.currentOffset += I8_BYTE_LENGTH;
65
- }
66
- writeI16(value, be = true) {
67
- if (value < -32768 || value > 32767)
68
- throw new Error('i16 value is out of range.');
69
- this.allocSafe(I16_BYTE_LENGTH);
70
- this.buffer.setInt16(this.currentOffset, value, !be);
71
- this.currentOffset += I16_BYTE_LENGTH;
72
- }
73
- writeI32(value, be = true) {
74
- if (value < -2147483648 || value > 2147483647)
75
- throw new Error('i32 value is out of range.');
76
- this.allocSafe(I32_BYTE_LENGTH);
77
- this.buffer.setInt32(this.currentOffset, value, !be);
78
- this.currentOffset += I32_BYTE_LENGTH;
79
- }
80
- writeI64(value, be = true) {
81
- if (value < -9223372036854775808n || value > 9223372036854775807n) {
82
- throw new Error('i64 value is out of range.');
83
- }
84
- this.allocSafe(I64_BYTE_LENGTH);
85
- this.buffer.setBigInt64(this.currentOffset, value, !be);
86
- this.currentOffset += I64_BYTE_LENGTH;
87
- }
88
- writeSelector(value) {
89
- this.writeU32(value, true);
90
- }
91
- writeBoolean(value) {
92
- this.writeU8(value ? 1 : 0);
93
- }
94
- writeI128(bigIntValue, be = true) {
95
- if (bigIntValue > 170141183460469231731687303715884105727n ||
96
- bigIntValue < -170141183460469231731687303715884105728n) {
97
- throw new Error('i128 value is too large.');
98
- }
99
- this.allocSafe(I128_BYTE_LENGTH);
100
- const bytes = bigIntToBytes(bigIntValue, I128_BYTE_LENGTH);
101
- if (bytes.byteLength !== I128_BYTE_LENGTH) {
102
- throw new Error(`Invalid i128 value: ${bigIntValue}`);
103
- }
104
- if (be) {
105
- for (let i = 0; i < bytes.byteLength; i++) {
106
- this.writeU8(bytes[i]);
107
- }
108
- }
109
- else {
110
- for (let i = bytes.byteLength - 1; i >= 0; i--) {
111
- this.writeU8(bytes[i]);
112
- }
113
- }
114
- }
115
- writeU256(bigIntValue, be = true) {
116
- if (bigIntValue >
117
- 115792089237316195423570985008687907853269984665640564039457584007913129639935n ||
118
- bigIntValue < 0n) {
119
- throw new Error('u256 value is too large or negative.');
120
- }
121
- this.allocSafe(U256_BYTE_LENGTH);
122
- const bytes = bigIntToBytes(bigIntValue);
123
- if (bytes.byteLength !== U256_BYTE_LENGTH) {
124
- throw new Error(`Invalid u256 value: ${bigIntValue}`);
125
- }
126
- if (be) {
127
- for (let i = 0; i < bytes.byteLength; i++) {
128
- this.writeU8(bytes[i]);
129
- }
130
- }
131
- else {
132
- for (let i = bytes.byteLength - 1; i >= 0; i--) {
133
- this.writeU8(bytes[i]);
134
- }
135
- }
136
- }
137
- writeU128(bigIntValue, be = true) {
138
- if (bigIntValue > 340282366920938463463374607431768211455n || bigIntValue < 0n) {
139
- throw new Error('u128 value is too large or negative.');
140
- }
141
- this.allocSafe(U128_BYTE_LENGTH);
142
- const bytes = bigIntToBytes(bigIntValue, U128_BYTE_LENGTH);
143
- if (bytes.byteLength !== U128_BYTE_LENGTH) {
144
- throw new Error(`Invalid u128 value: ${bigIntValue}`);
145
- }
146
- if (be) {
147
- for (let i = 0; i < bytes.byteLength; i++) {
148
- this.writeU8(bytes[i]);
149
- }
150
- }
151
- else {
152
- for (let i = bytes.byteLength - 1; i >= 0; i--) {
153
- this.writeU8(bytes[i]);
154
- }
155
- }
156
- }
157
- writeBytes(value) {
158
- this.allocSafe(value.byteLength);
159
- for (let i = 0; i < value.byteLength; i++) {
160
- this.writeU8(value[i]);
161
- }
162
- }
163
- writeString(value) {
164
- const encoder = new TextEncoder();
165
- const bytes = encoder.encode(value);
166
- this.allocSafe(bytes.length);
167
- this.writeBytes(bytes);
168
- }
169
- writeStringWithLength(value) {
170
- const encoder = new TextEncoder();
171
- const bytes = encoder.encode(value);
172
- this.allocSafe(U32_BYTE_LENGTH + bytes.length);
173
- this.writeU32(bytes.length);
174
- this.writeBytes(bytes);
175
- }
176
- getBuffer(clear = true) {
177
- const buf = new Uint8Array(this.buffer.byteLength);
178
- for (let i = 0; i < this.buffer.byteLength; i++) {
179
- buf[i] = this.buffer.getUint8(i);
180
- }
181
- if (clear)
182
- this.clear();
183
- return buf;
184
- }
185
- reset() {
186
- this.currentOffset = 0;
187
- this.buffer = this.getDefaultBuffer(4);
188
- }
189
- toBytesReader() {
190
- return new BinaryReader(this.getBuffer());
191
- }
192
- getOffset() {
193
- return this.currentOffset;
194
- }
195
- setOffset(offset) {
196
- this.currentOffset = offset;
197
- }
198
- clear() {
199
- this.currentOffset = 0;
200
- this.buffer = this.getDefaultBuffer();
201
- }
202
- [Symbol.dispose]() {
203
- this.clear();
204
- }
205
- allocSafe(size) {
206
- if (this.currentOffset + size > this.buffer.byteLength) {
207
- this.resize(size);
208
- }
209
- }
210
- writeBytesWithLength(value) {
211
- this.writeU32(value.length);
212
- this.writeBytes(value);
213
- }
214
- writeArrayOfBuffer(values, be = true) {
215
- const totalLength = BinaryWriter.estimateArrayOfBufferLength(values);
216
- this.allocSafe(totalLength);
217
- this.writeU16(values.length, be);
218
- for (let i = 0; i < values.length; i++) {
219
- this.writeU32(values[i].length, be);
220
- this.writeBytes(values[i]);
221
- }
222
- }
223
- writeU32Array(value, be = true) {
224
- if (value.length > 65535)
225
- throw new Error('Array size is too large');
226
- this.writeU16(value.length, be);
227
- for (let i = 0; i < value.length; i++) {
228
- this.writeU32(value[i], be);
229
- }
230
- }
231
- writeU256Array(value, be = true) {
232
- if (value.length > 65535)
233
- throw new Error('Array size is too large');
234
- this.writeU16(value.length, be);
235
- for (let i = 0; i < value.length; i++) {
236
- this.writeU256(value[i], be);
237
- }
238
- }
239
- writeU128Array(value, be = true) {
240
- if (value.length > 65535)
241
- throw new Error('Array size is too large');
242
- this.writeU16(value.length, be);
243
- for (let i = 0; i < value.length; i++) {
244
- this.writeU128(value[i], be);
245
- }
246
- }
247
- writeStringArray(value) {
248
- if (value.length > 65535)
249
- throw new Error('Array size is too large');
250
- this.writeU16(value.length);
251
- for (let i = 0; i < value.length; i++) {
252
- this.writeStringWithLength(value[i]);
253
- }
254
- }
255
- writeU16Array(value, be = true) {
256
- if (value.length > 65535)
257
- throw new Error('Array size is too large');
258
- this.writeU16(value.length, be);
259
- for (let i = 0; i < value.length; i++) {
260
- this.writeU16(value[i], be);
261
- }
262
- }
263
- writeU8Array(value) {
264
- if (value.length > 65535)
265
- throw new Error('Array size is too large');
266
- this.writeU16(value.length);
267
- for (let i = 0; i < value.length; i++) {
268
- this.writeU8(value[i]);
269
- }
270
- }
271
- writeU64Array(value, be = true) {
272
- if (value.length > 65535)
273
- throw new Error('Array size is too large');
274
- this.writeU16(value.length, be);
275
- for (let i = 0; i < value.length; i++) {
276
- this.writeU64(value[i], be);
277
- }
278
- }
279
- writeBytesArray(value) {
280
- if (value.length > 65535)
281
- throw new Error('Array size is too large');
282
- this.writeU16(value.length);
283
- for (let i = 0; i < value.length; i++) {
284
- this.writeBytesWithLength(value[i]);
285
- }
286
- }
287
- resize(size) {
288
- const buf = new Uint8Array(this.buffer.byteLength + size);
289
- for (let i = 0; i < this.buffer.byteLength; i++) {
290
- buf[i] = this.buffer.getUint8(i);
291
- }
292
- this.buffer = new DataView(buf.buffer, buf.byteOffset, buf.byteLength);
293
- }
294
- getDefaultBuffer(length = 0) {
295
- return new DataView(new ArrayBuffer(length));
296
- }
297
- }
Binary file
@@ -1,9 +0,0 @@
1
- (module
2
- (type $0 (func (result i32)))
3
- (memory $0 1)
4
- (export "memory" (memory $0))
5
- (export "main" (func $src/server/main/run))
6
- (func $src/server/main/run (result i32)
7
- i32.const 42
8
- )
9
- )