goscript 0.2.3 → 0.2.5
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/README.md +8 -8
- package/cmd/go_js_wasm_exec/main.go +1 -1
- package/cmd/go_js_wasm_exec/main_test.go +1 -1
- package/cmd/goscript/cmd-compile.go +9 -1
- package/cmd/goscript/cmd-test.go +1 -1
- package/cmd/goscript/cmd_compile_test.go +44 -0
- package/cmd/goscript/deps.go +1 -1
- package/cmd/goscript-wasm/main.go +2 -2
- package/compiler/compile-request.go +19 -0
- package/compiler/compile_bench_test.go +121 -0
- package/compiler/compliance_test.go +17 -1
- package/compiler/config.go +2 -0
- package/compiler/gotest/result.go +1 -1
- package/compiler/gotest/runner.go +2 -2
- package/compiler/gotest/runner_test.go +4 -7
- package/compiler/index.test.ts +28 -0
- package/compiler/index.ts +32 -16
- package/compiler/lowering.go +1236 -143
- package/compiler/lowering_bench_test.go +4 -0
- package/compiler/override-facts.go +1 -1
- package/compiler/override-registry_test.go +125 -0
- package/compiler/package-graph.go +92 -0
- package/compiler/package-graph_test.go +113 -0
- package/compiler/runtime-contract.go +1 -1
- package/compiler/semantic-model.go +32 -0
- package/compiler/skeleton_test.go +284 -11
- package/compiler/wasm/compile.go +1 -1
- package/compiler/wasm/compile_test.go +1 -1
- package/dist/compiler/index.d.ts +4 -0
- package/dist/compiler/index.js +26 -15
- package/dist/compiler/index.js.map +1 -1
- package/dist/gs/compress/gzip/index.d.ts +41 -0
- package/dist/gs/compress/gzip/index.js +235 -0
- package/dist/gs/compress/gzip/index.js.map +1 -0
- package/dist/gs/database/sql/driver/index.d.ts +165 -0
- package/dist/gs/database/sql/driver/index.js +432 -0
- package/dist/gs/database/sql/driver/index.js.map +1 -0
- package/dist/gs/encoding/binary/index.d.ts +71 -0
- package/dist/gs/encoding/binary/index.js +778 -0
- package/dist/gs/encoding/binary/index.js.map +1 -0
- package/dist/gs/fmt/fmt.js +156 -57
- package/dist/gs/fmt/fmt.js.map +1 -1
- package/dist/gs/github.com/klauspost/cpuid/v2/index.d.ts +11 -0
- package/dist/gs/github.com/klauspost/cpuid/v2/index.js +28 -0
- package/dist/gs/github.com/klauspost/cpuid/v2/index.js.map +1 -0
- package/dist/gs/github.com/pkg/errors/errors.d.ts +0 -2
- package/dist/gs/github.com/pkg/errors/errors.js.map +1 -1
- package/dist/gs/github.com/pkg/errors/index.d.ts +2 -1
- package/dist/gs/github.com/pkg/errors/index.js +1 -1
- package/dist/gs/github.com/pkg/errors/index.js.map +1 -1
- package/dist/gs/github.com/pkg/errors/stack.d.ts +8 -19
- package/dist/gs/github.com/pkg/errors/stack.js +26 -61
- package/dist/gs/github.com/pkg/errors/stack.js.map +1 -1
- package/dist/gs/golang.org/x/crypto/cryptobyte/asn1/index.d.ts +19 -0
- package/dist/gs/golang.org/x/crypto/cryptobyte/asn1/index.js +25 -0
- package/dist/gs/golang.org/x/crypto/cryptobyte/asn1/index.js.map +1 -0
- package/dist/gs/golang.org/x/crypto/cryptobyte/index.d.ts +104 -0
- package/dist/gs/golang.org/x/crypto/cryptobyte/index.js +1107 -0
- package/dist/gs/golang.org/x/crypto/cryptobyte/index.js.map +1 -0
- package/dist/gs/golang.org/x/crypto/internal/alias/index.d.ts +3 -0
- package/dist/gs/golang.org/x/crypto/internal/alias/index.js +39 -0
- package/dist/gs/golang.org/x/crypto/internal/alias/index.js.map +1 -0
- package/dist/gs/io/fs/glob.js +1 -1
- package/dist/gs/io/fs/glob.js.map +1 -1
- package/dist/gs/io/fs/readlink.d.ts +1 -1
- package/dist/gs/io/fs/readlink.js +2 -2
- package/dist/gs/io/fs/readlink.js.map +1 -1
- package/dist/gs/io/fs/stat.d.ts +4 -2
- package/dist/gs/io/fs/stat.js +12 -73
- package/dist/gs/io/fs/stat.js.map +1 -1
- package/dist/gs/io/fs/sub.d.ts +2 -2
- package/dist/gs/io/fs/sub.js +7 -7
- package/dist/gs/io/fs/sub.js.map +1 -1
- package/dist/gs/io/fs/walk.js +1 -1
- package/dist/gs/io/fs/walk.js.map +1 -1
- package/dist/gs/net/http/index.d.ts +18 -14
- package/dist/gs/net/http/index.js +44 -23
- package/dist/gs/net/http/index.js.map +1 -1
- package/dist/gs/net/http/pprof/index.d.ts +5 -5
- package/dist/gs/net/http/pprof/index.js +21 -21
- package/dist/gs/net/http/pprof/index.js.map +1 -1
- package/dist/gs/runtime/runtime.d.ts +6 -1
- package/dist/gs/runtime/runtime.js +15 -8
- package/dist/gs/runtime/runtime.js.map +1 -1
- package/dist/gs/runtime/trace/index.d.ts +8 -5
- package/dist/gs/runtime/trace/index.js +324 -23
- package/dist/gs/runtime/trace/index.js.map +1 -1
- package/dist/gs/slices/slices.d.ts +2 -1
- package/dist/gs/slices/slices.js +9 -3
- package/dist/gs/slices/slices.js.map +1 -1
- package/dist/gs/sort/search.gs.d.ts +3 -1
- package/dist/gs/sort/search.gs.js +18 -53
- package/dist/gs/sort/search.gs.js.map +1 -1
- package/dist/gs/sync/sync.d.ts +1 -1
- package/dist/gs/sync/sync.js +3 -0
- package/dist/gs/sync/sync.js.map +1 -1
- package/dist/gs/time/time.d.ts +22 -29
- package/dist/gs/time/time.js +111 -32
- package/dist/gs/time/time.js.map +1 -1
- package/dist/gs/unsafe/unsafe.d.ts +3 -2
- package/dist/gs/unsafe/unsafe.js.map +1 -1
- package/go.mod +7 -5
- package/go.sum +12 -26
- package/gs/builtin/runtime-contract.test.ts +25 -0
- package/gs/compress/gzip/index.test.ts +86 -0
- package/gs/compress/gzip/index.ts +297 -0
- package/gs/compress/gzip/meta.json +6 -0
- package/gs/compress/gzip/parity.json +45 -0
- package/gs/database/sql/driver/index.test.ts +88 -0
- package/gs/database/sql/driver/index.ts +675 -0
- package/gs/database/sql/driver/meta.json +3 -0
- package/gs/database/sql/driver/parity.json +144 -0
- package/gs/embed/index.test.ts +1 -1
- package/gs/encoding/binary/index.test.ts +239 -0
- package/gs/encoding/binary/index.ts +999 -0
- package/gs/encoding/binary/meta.json +9 -0
- package/gs/encoding/binary/parity.json +72 -0
- package/gs/fmt/fmt.test.ts +28 -0
- package/gs/fmt/fmt.ts +198 -61
- package/gs/fmt/meta.json +2 -1
- package/gs/github.com/klauspost/cpuid/v2/index.ts +38 -0
- package/gs/github.com/klauspost/cpuid/v2/meta.json +3 -0
- package/gs/github.com/pkg/errors/errors.ts +1 -2
- package/gs/github.com/pkg/errors/index.ts +2 -1
- package/gs/github.com/pkg/errors/stack.ts +34 -62
- package/gs/golang.org/x/crypto/cryptobyte/asn1/index.test.ts +19 -0
- package/gs/golang.org/x/crypto/cryptobyte/asn1/index.ts +29 -0
- package/gs/golang.org/x/crypto/cryptobyte/index.test.ts +255 -0
- package/gs/golang.org/x/crypto/cryptobyte/index.ts +1441 -0
- package/gs/golang.org/x/crypto/cryptobyte/meta.json +3 -0
- package/gs/golang.org/x/crypto/internal/alias/index.test.ts +40 -0
- package/gs/golang.org/x/crypto/internal/alias/index.ts +40 -0
- package/gs/io/fs/glob.ts +1 -1
- package/gs/io/fs/meta.json +3 -0
- package/gs/io/fs/readlink.test.ts +2 -2
- package/gs/io/fs/readlink.ts +5 -2
- package/gs/io/fs/stat.test.ts +79 -0
- package/gs/io/fs/stat.ts +24 -10
- package/gs/io/fs/sub.test.ts +93 -0
- package/gs/io/fs/sub.ts +9 -9
- package/gs/io/fs/walk.ts +1 -1
- package/gs/net/http/index.test.ts +207 -2
- package/gs/net/http/index.ts +68 -37
- package/gs/net/http/meta.json +3 -1
- package/gs/net/http/pprof/index.test.ts +4 -4
- package/gs/net/http/pprof/index.ts +30 -27
- package/gs/runtime/runtime.test.ts +16 -0
- package/gs/runtime/runtime.ts +17 -9
- package/gs/runtime/trace/index.test.ts +113 -14
- package/gs/runtime/trace/index.ts +384 -34
- package/gs/runtime/trace/meta.json +1 -0
- package/gs/slices/slices.test.ts +24 -1
- package/gs/slices/slices.ts +14 -4
- package/gs/sort/meta.json +1 -0
- package/gs/sort/search.gs.ts +20 -5
- package/gs/sync/sync.ts +4 -1
- package/gs/time/time.test.ts +79 -2
- package/gs/time/time.ts +133 -33
- package/gs/unsafe/unsafe.ts +4 -2
- package/package.json +2 -2
|
@@ -0,0 +1,778 @@
|
|
|
1
|
+
import * as $ from '@goscript/builtin/index.js';
|
|
2
|
+
import * as io from '@goscript/io/index.js';
|
|
3
|
+
const errBufferTooSmall = $.newError('buffer too small');
|
|
4
|
+
const errOverflow = $.newError('binary: varint overflows a 64-bit integer');
|
|
5
|
+
export const MaxVarintLen16 = 3;
|
|
6
|
+
export const MaxVarintLen32 = 5;
|
|
7
|
+
export const MaxVarintLen64 = 10;
|
|
8
|
+
class littleEndian {
|
|
9
|
+
Uint(b) {
|
|
10
|
+
return this.Uint64(b);
|
|
11
|
+
}
|
|
12
|
+
Uint16(b) {
|
|
13
|
+
requireLen(b, 2);
|
|
14
|
+
return byteAt(b, 0) | (byteAt(b, 1) << 8);
|
|
15
|
+
}
|
|
16
|
+
Uint32(b) {
|
|
17
|
+
requireLen(b, 4);
|
|
18
|
+
return ((byteAt(b, 0) |
|
|
19
|
+
(byteAt(b, 1) << 8) |
|
|
20
|
+
(byteAt(b, 2) << 16) |
|
|
21
|
+
(byteAt(b, 3) << 24)) >>>
|
|
22
|
+
0);
|
|
23
|
+
}
|
|
24
|
+
Uint64(b) {
|
|
25
|
+
requireLen(b, 8);
|
|
26
|
+
let value = 0n;
|
|
27
|
+
for (let i = 0; i < 8; i++) {
|
|
28
|
+
value |= BigInt(byteAt(b, i)) << BigInt(8 * i);
|
|
29
|
+
}
|
|
30
|
+
return uint64Result(value);
|
|
31
|
+
}
|
|
32
|
+
PutUint16(b, v) {
|
|
33
|
+
requireLen(b, 2);
|
|
34
|
+
setByte(b, 0, v);
|
|
35
|
+
setByte(b, 1, v >> 8);
|
|
36
|
+
}
|
|
37
|
+
PutUint32(b, v) {
|
|
38
|
+
requireLen(b, 4);
|
|
39
|
+
const value = Number($.uint(v, 32));
|
|
40
|
+
setByte(b, 0, value);
|
|
41
|
+
setByte(b, 1, value >>> 8);
|
|
42
|
+
setByte(b, 2, value >>> 16);
|
|
43
|
+
setByte(b, 3, value >>> 24);
|
|
44
|
+
}
|
|
45
|
+
PutUint64(b, v) {
|
|
46
|
+
requireLen(b, 8);
|
|
47
|
+
const value = toUint64(v);
|
|
48
|
+
for (let i = 0; i < 8; i++) {
|
|
49
|
+
setByte(b, i, Number((value >> BigInt(8 * i)) & 0xffn));
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
AppendUint16(b, v) {
|
|
53
|
+
return $.append(b, v, v >> 8);
|
|
54
|
+
}
|
|
55
|
+
AppendUint32(b, v) {
|
|
56
|
+
const value = Number($.uint(v, 32));
|
|
57
|
+
return $.append(b, value, value >>> 8, value >>> 16, value >>> 24);
|
|
58
|
+
}
|
|
59
|
+
AppendUint64(b, v) {
|
|
60
|
+
let out = b;
|
|
61
|
+
const value = toUint64(v);
|
|
62
|
+
for (let i = 0; i < 8; i++) {
|
|
63
|
+
out = $.append(out, Number((value >> BigInt(8 * i)) & 0xffn));
|
|
64
|
+
}
|
|
65
|
+
return out;
|
|
66
|
+
}
|
|
67
|
+
String() {
|
|
68
|
+
return 'LittleEndian';
|
|
69
|
+
}
|
|
70
|
+
GoString() {
|
|
71
|
+
return 'binary.LittleEndian';
|
|
72
|
+
}
|
|
73
|
+
clone() {
|
|
74
|
+
return $.markAsStructValue(new littleEndian());
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
class bigEndian {
|
|
78
|
+
Uint(b) {
|
|
79
|
+
return this.Uint64(b);
|
|
80
|
+
}
|
|
81
|
+
Uint16(b) {
|
|
82
|
+
requireLen(b, 2);
|
|
83
|
+
return byteAt(b, 1) | (byteAt(b, 0) << 8);
|
|
84
|
+
}
|
|
85
|
+
Uint32(b) {
|
|
86
|
+
requireLen(b, 4);
|
|
87
|
+
return ((byteAt(b, 3) |
|
|
88
|
+
(byteAt(b, 2) << 8) |
|
|
89
|
+
(byteAt(b, 1) << 16) |
|
|
90
|
+
(byteAt(b, 0) << 24)) >>>
|
|
91
|
+
0);
|
|
92
|
+
}
|
|
93
|
+
Uint64(b) {
|
|
94
|
+
requireLen(b, 8);
|
|
95
|
+
let value = 0n;
|
|
96
|
+
for (let i = 0; i < 8; i++) {
|
|
97
|
+
value = (value << 8n) | BigInt(byteAt(b, i));
|
|
98
|
+
}
|
|
99
|
+
return uint64Result(value);
|
|
100
|
+
}
|
|
101
|
+
PutUint16(b, v) {
|
|
102
|
+
requireLen(b, 2);
|
|
103
|
+
setByte(b, 0, v >> 8);
|
|
104
|
+
setByte(b, 1, v);
|
|
105
|
+
}
|
|
106
|
+
PutUint32(b, v) {
|
|
107
|
+
requireLen(b, 4);
|
|
108
|
+
const value = Number($.uint(v, 32));
|
|
109
|
+
setByte(b, 0, value >>> 24);
|
|
110
|
+
setByte(b, 1, value >>> 16);
|
|
111
|
+
setByte(b, 2, value >>> 8);
|
|
112
|
+
setByte(b, 3, value);
|
|
113
|
+
}
|
|
114
|
+
PutUint64(b, v) {
|
|
115
|
+
requireLen(b, 8);
|
|
116
|
+
const value = toUint64(v);
|
|
117
|
+
for (let i = 0; i < 8; i++) {
|
|
118
|
+
setByte(b, i, Number((value >> BigInt(56 - 8 * i)) & 0xffn));
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
AppendUint16(b, v) {
|
|
122
|
+
return $.append(b, v >> 8, v);
|
|
123
|
+
}
|
|
124
|
+
AppendUint32(b, v) {
|
|
125
|
+
const value = Number($.uint(v, 32));
|
|
126
|
+
return $.append(b, value >>> 24, value >>> 16, value >>> 8, value);
|
|
127
|
+
}
|
|
128
|
+
AppendUint64(b, v) {
|
|
129
|
+
let out = b;
|
|
130
|
+
const value = toUint64(v);
|
|
131
|
+
for (let i = 0; i < 8; i++) {
|
|
132
|
+
out = $.append(out, Number((value >> BigInt(56 - 8 * i)) & 0xffn));
|
|
133
|
+
}
|
|
134
|
+
return out;
|
|
135
|
+
}
|
|
136
|
+
String() {
|
|
137
|
+
return 'BigEndian';
|
|
138
|
+
}
|
|
139
|
+
GoString() {
|
|
140
|
+
return 'binary.BigEndian';
|
|
141
|
+
}
|
|
142
|
+
clone() {
|
|
143
|
+
return $.markAsStructValue(new bigEndian());
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
export const LittleEndian = new littleEndian();
|
|
147
|
+
export const BigEndian = new bigEndian();
|
|
148
|
+
export const NativeEndian = LittleEndian;
|
|
149
|
+
$.registerInterfaceType('binary.ByteOrder', null, [
|
|
150
|
+
{
|
|
151
|
+
name: 'Uint16',
|
|
152
|
+
args: [byteSliceArg('b')],
|
|
153
|
+
returns: [numberReturn()],
|
|
154
|
+
},
|
|
155
|
+
{
|
|
156
|
+
name: 'Uint32',
|
|
157
|
+
args: [byteSliceArg('b')],
|
|
158
|
+
returns: [numberReturn()],
|
|
159
|
+
},
|
|
160
|
+
{
|
|
161
|
+
name: 'Uint64',
|
|
162
|
+
args: [byteSliceArg('b')],
|
|
163
|
+
returns: [numberReturn()],
|
|
164
|
+
},
|
|
165
|
+
{
|
|
166
|
+
name: 'PutUint16',
|
|
167
|
+
args: [byteSliceArg('b'), numberArg('v')],
|
|
168
|
+
returns: [],
|
|
169
|
+
},
|
|
170
|
+
{
|
|
171
|
+
name: 'PutUint32',
|
|
172
|
+
args: [byteSliceArg('b'), numberArg('v')],
|
|
173
|
+
returns: [],
|
|
174
|
+
},
|
|
175
|
+
{
|
|
176
|
+
name: 'PutUint64',
|
|
177
|
+
args: [byteSliceArg('b'), numberArg('v')],
|
|
178
|
+
returns: [],
|
|
179
|
+
},
|
|
180
|
+
{
|
|
181
|
+
name: 'String',
|
|
182
|
+
args: [],
|
|
183
|
+
returns: [{ type: { kind: $.TypeKind.Basic, name: 'string' } }],
|
|
184
|
+
},
|
|
185
|
+
]);
|
|
186
|
+
$.registerInterfaceType('binary.AppendByteOrder', null, [
|
|
187
|
+
{
|
|
188
|
+
name: 'AppendUint16',
|
|
189
|
+
args: [byteSliceArg('b'), numberArg('v')],
|
|
190
|
+
returns: [{ type: byteSliceType() }],
|
|
191
|
+
},
|
|
192
|
+
{
|
|
193
|
+
name: 'AppendUint32',
|
|
194
|
+
args: [byteSliceArg('b'), numberArg('v')],
|
|
195
|
+
returns: [{ type: byteSliceType() }],
|
|
196
|
+
},
|
|
197
|
+
{
|
|
198
|
+
name: 'AppendUint64',
|
|
199
|
+
args: [byteSliceArg('b'), numberArg('v')],
|
|
200
|
+
returns: [{ type: byteSliceType() }],
|
|
201
|
+
},
|
|
202
|
+
{
|
|
203
|
+
name: 'String',
|
|
204
|
+
args: [],
|
|
205
|
+
returns: [{ type: { kind: $.TypeKind.Basic, name: 'string' } }],
|
|
206
|
+
},
|
|
207
|
+
]);
|
|
208
|
+
export function AppendUvarint(buf, x) {
|
|
209
|
+
let out = buf;
|
|
210
|
+
let value = toUint64(x);
|
|
211
|
+
while (value >= 0x80n) {
|
|
212
|
+
out = $.append(out, Number(value & 0x7fn) | 0x80);
|
|
213
|
+
value >>= 7n;
|
|
214
|
+
}
|
|
215
|
+
return $.append(out, Number(value));
|
|
216
|
+
}
|
|
217
|
+
export function PutUvarint(buf, x) {
|
|
218
|
+
let value = toUint64(x);
|
|
219
|
+
let i = 0;
|
|
220
|
+
while (value >= 0x80n) {
|
|
221
|
+
setByte(buf, i, Number(value & 0x7fn) | 0x80);
|
|
222
|
+
value >>= 7n;
|
|
223
|
+
i++;
|
|
224
|
+
}
|
|
225
|
+
setByte(buf, i, Number(value));
|
|
226
|
+
return i + 1;
|
|
227
|
+
}
|
|
228
|
+
export function Uvarint(buf) {
|
|
229
|
+
let x = 0n;
|
|
230
|
+
let s = 0n;
|
|
231
|
+
for (let i = 0; i < $.len(buf); i++) {
|
|
232
|
+
const b = byteAt(buf, i);
|
|
233
|
+
if (i === MaxVarintLen64) {
|
|
234
|
+
return [0, -(i + 1)];
|
|
235
|
+
}
|
|
236
|
+
if (b < 0x80) {
|
|
237
|
+
if (i === MaxVarintLen64 - 1 && b > 1) {
|
|
238
|
+
return [0, -(i + 1)];
|
|
239
|
+
}
|
|
240
|
+
return [uint64Result(x | (BigInt(b) << s)), i + 1];
|
|
241
|
+
}
|
|
242
|
+
x |= BigInt(b & 0x7f) << s;
|
|
243
|
+
s += 7n;
|
|
244
|
+
}
|
|
245
|
+
return [0, 0];
|
|
246
|
+
}
|
|
247
|
+
export function AppendVarint(buf, x) {
|
|
248
|
+
return AppendUvarint(buf, encodeSignedVarint(x));
|
|
249
|
+
}
|
|
250
|
+
export function PutVarint(buf, x) {
|
|
251
|
+
return PutUvarint(buf, encodeSignedVarint(x));
|
|
252
|
+
}
|
|
253
|
+
export function Varint(buf) {
|
|
254
|
+
const [ux, n] = Uvarint(buf);
|
|
255
|
+
return [decodeSignedVarint(ux), n];
|
|
256
|
+
}
|
|
257
|
+
export async function ReadUvarint(r) {
|
|
258
|
+
let x = 0n;
|
|
259
|
+
let s = 0n;
|
|
260
|
+
for (let i = 0; i < MaxVarintLen64; i++) {
|
|
261
|
+
const [b, err] = await r.ReadByte();
|
|
262
|
+
if (err !== null) {
|
|
263
|
+
return [uint64Result(x), i > 0 && isEOF(err) ? io.ErrUnexpectedEOF : err];
|
|
264
|
+
}
|
|
265
|
+
if (b < 0x80) {
|
|
266
|
+
if (i === MaxVarintLen64 - 1 && b > 1) {
|
|
267
|
+
return [uint64Result(x), errOverflow];
|
|
268
|
+
}
|
|
269
|
+
return [uint64Result(x | (BigInt(b) << s)), null];
|
|
270
|
+
}
|
|
271
|
+
x |= BigInt(b & 0x7f) << s;
|
|
272
|
+
s += 7n;
|
|
273
|
+
}
|
|
274
|
+
return [uint64Result(x), errOverflow];
|
|
275
|
+
}
|
|
276
|
+
export async function ReadVarint(r) {
|
|
277
|
+
const [ux, err] = await ReadUvarint(r);
|
|
278
|
+
return [decodeSignedVarint(ux), err];
|
|
279
|
+
}
|
|
280
|
+
export async function Read(r, order, data) {
|
|
281
|
+
const byteOrder = requireByteOrder(order);
|
|
282
|
+
const target = decodeTarget(data);
|
|
283
|
+
if (target === null) {
|
|
284
|
+
return unsupportedError('Read', data);
|
|
285
|
+
}
|
|
286
|
+
const size = fixedSize(target.kind, target.value);
|
|
287
|
+
if (size < 0) {
|
|
288
|
+
return unsupportedError('Read', data);
|
|
289
|
+
}
|
|
290
|
+
const buf = $.makeSlice(size, undefined, 'byte');
|
|
291
|
+
const [, err] = await readFull(r, buf);
|
|
292
|
+
if (err !== null) {
|
|
293
|
+
return err;
|
|
294
|
+
}
|
|
295
|
+
decodeFixed(buf, byteOrder, target);
|
|
296
|
+
return null;
|
|
297
|
+
}
|
|
298
|
+
export function Decode(buf, order, data) {
|
|
299
|
+
const byteOrder = requireByteOrder(order);
|
|
300
|
+
const target = decodeTarget(data);
|
|
301
|
+
if (target === null) {
|
|
302
|
+
return [0, unsupportedError('Decode', data)];
|
|
303
|
+
}
|
|
304
|
+
const size = fixedSize(target.kind, target.value);
|
|
305
|
+
if (size < 0) {
|
|
306
|
+
return [0, unsupportedError('Decode', data)];
|
|
307
|
+
}
|
|
308
|
+
if ($.len(buf) < size) {
|
|
309
|
+
return [0, errBufferTooSmall];
|
|
310
|
+
}
|
|
311
|
+
decodeFixed($.goSlice(buf, 0, size), byteOrder, target);
|
|
312
|
+
return [size, null];
|
|
313
|
+
}
|
|
314
|
+
export async function Write(w, order, data) {
|
|
315
|
+
const encoded = encodeData(requireByteOrder(order), data);
|
|
316
|
+
if (encoded === null) {
|
|
317
|
+
return unsupportedError('Write', data);
|
|
318
|
+
}
|
|
319
|
+
const [, err] = await w.Write(encoded);
|
|
320
|
+
return err;
|
|
321
|
+
}
|
|
322
|
+
export function Encode(buf, order, data) {
|
|
323
|
+
const encoded = encodeData(requireByteOrder(order), data);
|
|
324
|
+
if (encoded === null) {
|
|
325
|
+
return [0, unsupportedError('Encode', data)];
|
|
326
|
+
}
|
|
327
|
+
if ($.len(buf) < $.len(encoded)) {
|
|
328
|
+
return [0, errBufferTooSmall];
|
|
329
|
+
}
|
|
330
|
+
$.copy(buf, encoded);
|
|
331
|
+
return [$.len(encoded), null];
|
|
332
|
+
}
|
|
333
|
+
export function Append(buf, order, data) {
|
|
334
|
+
const encoded = encodeData(requireByteOrder(order), data);
|
|
335
|
+
if (encoded === null) {
|
|
336
|
+
return [null, unsupportedError('Append', data)];
|
|
337
|
+
}
|
|
338
|
+
return [$.appendSlice(buf, encoded), null];
|
|
339
|
+
}
|
|
340
|
+
export function Size(v) {
|
|
341
|
+
const target = decodeTarget(v);
|
|
342
|
+
if (target === null) {
|
|
343
|
+
return -1;
|
|
344
|
+
}
|
|
345
|
+
return fixedSize(target.kind, target.value);
|
|
346
|
+
}
|
|
347
|
+
function requireByteOrder(order) {
|
|
348
|
+
if (order === null) {
|
|
349
|
+
throw new Error('runtime error: invalid memory address or nil pointer dereference');
|
|
350
|
+
}
|
|
351
|
+
return order;
|
|
352
|
+
}
|
|
353
|
+
function decodeFixed(buf, order, target) {
|
|
354
|
+
if (target.kind.startsWith('[]')) {
|
|
355
|
+
decodeFixedSlice(buf, order, target.kind, target.value);
|
|
356
|
+
return;
|
|
357
|
+
}
|
|
358
|
+
if (target.settable === null) {
|
|
359
|
+
return;
|
|
360
|
+
}
|
|
361
|
+
target.settable(decodeScalar(buf, order, target.kind));
|
|
362
|
+
}
|
|
363
|
+
function decodeFixedSlice(buf, order, kind, target) {
|
|
364
|
+
const elemKind = kind.slice(2);
|
|
365
|
+
const width = fixedSize(elemKind, 0);
|
|
366
|
+
const count = $.len(target);
|
|
367
|
+
for (let i = 0; i < count; i++) {
|
|
368
|
+
;
|
|
369
|
+
target[i] = decodeScalar($.goSlice(buf, i * width, i * width + width), order, elemKind);
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
function encodeData(order, data) {
|
|
373
|
+
const target = decodeTarget(data);
|
|
374
|
+
if (target === null) {
|
|
375
|
+
return null;
|
|
376
|
+
}
|
|
377
|
+
const size = fixedSize(target.kind, target.value);
|
|
378
|
+
if (size < 0) {
|
|
379
|
+
return null;
|
|
380
|
+
}
|
|
381
|
+
const out = $.makeSlice(size, undefined, 'byte');
|
|
382
|
+
encodeFixed(out, order, target.kind, target.value);
|
|
383
|
+
return out;
|
|
384
|
+
}
|
|
385
|
+
function encodeFixed(buf, order, kind, value) {
|
|
386
|
+
if (kind.startsWith('[]')) {
|
|
387
|
+
const elemKind = kind.slice(2);
|
|
388
|
+
const width = fixedSize(elemKind, 0);
|
|
389
|
+
const count = $.len(value);
|
|
390
|
+
for (let i = 0; i < count; i++) {
|
|
391
|
+
encodeScalar($.goSlice(buf, i * width, i * width + width), order, elemKind, value[i]);
|
|
392
|
+
}
|
|
393
|
+
return;
|
|
394
|
+
}
|
|
395
|
+
encodeScalar(buf, order, kind, value);
|
|
396
|
+
}
|
|
397
|
+
function decodeScalar(buf, order, kind) {
|
|
398
|
+
switch (kind) {
|
|
399
|
+
case 'bool':
|
|
400
|
+
return byteAt(buf, 0) !== 0;
|
|
401
|
+
case 'int8':
|
|
402
|
+
return intN(byteAt(buf, 0), 8);
|
|
403
|
+
case 'uint8':
|
|
404
|
+
return byteAt(buf, 0);
|
|
405
|
+
case 'int16':
|
|
406
|
+
return intN(order.Uint16(buf), 16);
|
|
407
|
+
case 'uint16':
|
|
408
|
+
return order.Uint16(buf);
|
|
409
|
+
case 'int32':
|
|
410
|
+
return intN(order.Uint32(buf), 32);
|
|
411
|
+
case 'uint32':
|
|
412
|
+
return order.Uint32(buf);
|
|
413
|
+
case 'int64':
|
|
414
|
+
return int64Result(toUint64(order.Uint64(buf)));
|
|
415
|
+
case 'uint64':
|
|
416
|
+
return order.Uint64(buf);
|
|
417
|
+
case 'float32':
|
|
418
|
+
return float32FromBits(order.Uint32(buf));
|
|
419
|
+
case 'float64':
|
|
420
|
+
return float64FromBits(order.Uint64(buf));
|
|
421
|
+
default:
|
|
422
|
+
return 0;
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
function encodeScalar(buf, order, kind, value) {
|
|
426
|
+
switch (kind) {
|
|
427
|
+
case 'bool':
|
|
428
|
+
setByte(buf, 0, value ? 1 : 0);
|
|
429
|
+
return;
|
|
430
|
+
case 'int8':
|
|
431
|
+
case 'uint8':
|
|
432
|
+
setByte(buf, 0, Number(value));
|
|
433
|
+
return;
|
|
434
|
+
case 'int16':
|
|
435
|
+
case 'uint16':
|
|
436
|
+
order.PutUint16(buf, Number(value));
|
|
437
|
+
return;
|
|
438
|
+
case 'int32':
|
|
439
|
+
case 'uint32':
|
|
440
|
+
order.PutUint32(buf, Number(value));
|
|
441
|
+
return;
|
|
442
|
+
case 'int64':
|
|
443
|
+
case 'uint64':
|
|
444
|
+
order.PutUint64(buf, value);
|
|
445
|
+
return;
|
|
446
|
+
case 'float32':
|
|
447
|
+
order.PutUint32(buf, float32Bits(Number(value)));
|
|
448
|
+
return;
|
|
449
|
+
case 'float64':
|
|
450
|
+
order.PutUint64(buf, float64Bits(Number(value)));
|
|
451
|
+
return;
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
function decodeTarget(data) {
|
|
455
|
+
const typeName = goTypeName(data);
|
|
456
|
+
const value = goValue(data);
|
|
457
|
+
if (typeName !== '') {
|
|
458
|
+
const kind = fixedKindFromType(typeName) ?? fixedKindFromTypeInfo(goTypeInfo(data));
|
|
459
|
+
if (kind !== null) {
|
|
460
|
+
return {
|
|
461
|
+
kind,
|
|
462
|
+
value: pointerValueForKind(typeName, value),
|
|
463
|
+
settable: setterFor(typeName, value),
|
|
464
|
+
};
|
|
465
|
+
}
|
|
466
|
+
return null;
|
|
467
|
+
}
|
|
468
|
+
if (value instanceof Uint8Array) {
|
|
469
|
+
return { kind: '[]uint8', value, settable: null };
|
|
470
|
+
}
|
|
471
|
+
if (Array.isArray(value)) {
|
|
472
|
+
if (value.every((item) => typeof item === 'boolean')) {
|
|
473
|
+
return { kind: '[]bool', value, settable: null };
|
|
474
|
+
}
|
|
475
|
+
if (value.every((item) => typeof item === 'number')) {
|
|
476
|
+
return { kind: '[]uint8', value, settable: null };
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
return null;
|
|
480
|
+
}
|
|
481
|
+
function pointerValueForKind(typeName, value) {
|
|
482
|
+
if (typeName.startsWith('*')) {
|
|
483
|
+
return $.pointerValue(value);
|
|
484
|
+
}
|
|
485
|
+
return value;
|
|
486
|
+
}
|
|
487
|
+
function setterFor(typeName, value) {
|
|
488
|
+
if (!typeName.startsWith('*')) {
|
|
489
|
+
return null;
|
|
490
|
+
}
|
|
491
|
+
if ($.isVarRef(value)) {
|
|
492
|
+
return (next) => {
|
|
493
|
+
value.value = next;
|
|
494
|
+
};
|
|
495
|
+
}
|
|
496
|
+
if (isBoxedValue(value) && $.isVarRef(value.__goValue)) {
|
|
497
|
+
const ref = value.__goValue;
|
|
498
|
+
return (next) => {
|
|
499
|
+
ref.value = next;
|
|
500
|
+
};
|
|
501
|
+
}
|
|
502
|
+
return null;
|
|
503
|
+
}
|
|
504
|
+
function goTypeName(value) {
|
|
505
|
+
if (isBoxedValue(value) && typeof value.__goType === 'string') {
|
|
506
|
+
return value.__goType;
|
|
507
|
+
}
|
|
508
|
+
if (value !== null &&
|
|
509
|
+
typeof value === 'object' &&
|
|
510
|
+
typeof value.__goType === 'string') {
|
|
511
|
+
return value.__goType;
|
|
512
|
+
}
|
|
513
|
+
return '';
|
|
514
|
+
}
|
|
515
|
+
function goTypeInfo(value) {
|
|
516
|
+
if (isBoxedValue(value)) {
|
|
517
|
+
return value.__goTypeInfo;
|
|
518
|
+
}
|
|
519
|
+
if (value !== null && typeof value === 'object') {
|
|
520
|
+
return value.__goTypeInfo;
|
|
521
|
+
}
|
|
522
|
+
return undefined;
|
|
523
|
+
}
|
|
524
|
+
function goValue(value) {
|
|
525
|
+
if (isBoxedValue(value) && '__goValue' in value) {
|
|
526
|
+
return value.__goValue;
|
|
527
|
+
}
|
|
528
|
+
return value;
|
|
529
|
+
}
|
|
530
|
+
function isBoxedValue(value) {
|
|
531
|
+
return value !== null && typeof value === 'object' && '__goValue' in value;
|
|
532
|
+
}
|
|
533
|
+
function fixedKindFromType(typeName) {
|
|
534
|
+
let name = typeName;
|
|
535
|
+
while (name.startsWith('*')) {
|
|
536
|
+
name = name.slice(1);
|
|
537
|
+
}
|
|
538
|
+
if (name === 'byte') {
|
|
539
|
+
return 'uint8';
|
|
540
|
+
}
|
|
541
|
+
if (name === '[]byte') {
|
|
542
|
+
return '[]uint8';
|
|
543
|
+
}
|
|
544
|
+
if (fixedKinds.has(name)) {
|
|
545
|
+
return name;
|
|
546
|
+
}
|
|
547
|
+
return null;
|
|
548
|
+
}
|
|
549
|
+
function fixedKindFromTypeInfo(info) {
|
|
550
|
+
if (info === undefined) {
|
|
551
|
+
return null;
|
|
552
|
+
}
|
|
553
|
+
if (typeof info === 'string') {
|
|
554
|
+
return fixedKindFromType(info);
|
|
555
|
+
}
|
|
556
|
+
switch (info.kind) {
|
|
557
|
+
case $.TypeKind.Pointer:
|
|
558
|
+
return fixedKindFromTypeInfo(info.elemType);
|
|
559
|
+
case $.TypeKind.Basic:
|
|
560
|
+
return fixedKindFromBasicName(info.name);
|
|
561
|
+
case $.TypeKind.Slice: {
|
|
562
|
+
const elemKind = fixedKindFromTypeInfo(info.elemType);
|
|
563
|
+
return elemKind === null ? null : fixedSliceKind(elemKind);
|
|
564
|
+
}
|
|
565
|
+
case $.TypeKind.Array: {
|
|
566
|
+
const elemKind = fixedKindFromTypeInfo(info.elemType);
|
|
567
|
+
return elemKind === null ? null : fixedSliceKind(elemKind);
|
|
568
|
+
}
|
|
569
|
+
default:
|
|
570
|
+
return null;
|
|
571
|
+
}
|
|
572
|
+
}
|
|
573
|
+
function fixedKindFromBasicName(name) {
|
|
574
|
+
if (name === undefined) {
|
|
575
|
+
return null;
|
|
576
|
+
}
|
|
577
|
+
if (name === 'byte') {
|
|
578
|
+
return 'uint8';
|
|
579
|
+
}
|
|
580
|
+
if (name === 'rune') {
|
|
581
|
+
return 'int32';
|
|
582
|
+
}
|
|
583
|
+
return fixedKinds.has(name) && !name.startsWith('[]') ?
|
|
584
|
+
name
|
|
585
|
+
: null;
|
|
586
|
+
}
|
|
587
|
+
function fixedSliceKind(elemKind) {
|
|
588
|
+
return elemKind.startsWith('[]') ? null : `[]${elemKind}`;
|
|
589
|
+
}
|
|
590
|
+
function fixedSize(kind, value) {
|
|
591
|
+
if (kind.startsWith('[]')) {
|
|
592
|
+
const elemKind = kind.slice(2);
|
|
593
|
+
const elemSize = fixedSize(elemKind, 0);
|
|
594
|
+
return elemSize < 0 ? -1 : elemSize * $.len(value);
|
|
595
|
+
}
|
|
596
|
+
switch (kind) {
|
|
597
|
+
case 'bool':
|
|
598
|
+
case 'int8':
|
|
599
|
+
case 'uint8':
|
|
600
|
+
return 1;
|
|
601
|
+
case 'int16':
|
|
602
|
+
case 'uint16':
|
|
603
|
+
return 2;
|
|
604
|
+
case 'int32':
|
|
605
|
+
case 'uint32':
|
|
606
|
+
case 'float32':
|
|
607
|
+
return 4;
|
|
608
|
+
case 'int64':
|
|
609
|
+
case 'uint64':
|
|
610
|
+
case 'float64':
|
|
611
|
+
return 8;
|
|
612
|
+
}
|
|
613
|
+
return -1;
|
|
614
|
+
}
|
|
615
|
+
function unsupportedError(fn, data) {
|
|
616
|
+
return $.newError(`encoding/binary: ${fn} of ${unsupportedKind(data)} is not supported in the GoScript browser build`);
|
|
617
|
+
}
|
|
618
|
+
function unsupportedKind(data) {
|
|
619
|
+
const typeName = goTypeName(data);
|
|
620
|
+
if (typeName !== '') {
|
|
621
|
+
return typeName;
|
|
622
|
+
}
|
|
623
|
+
if ($.isVarRef(data)) {
|
|
624
|
+
return 'untyped pointer';
|
|
625
|
+
}
|
|
626
|
+
if (Array.isArray(data)) {
|
|
627
|
+
return 'array';
|
|
628
|
+
}
|
|
629
|
+
if (data === null || data === undefined) {
|
|
630
|
+
return '<nil>';
|
|
631
|
+
}
|
|
632
|
+
return typeof data;
|
|
633
|
+
}
|
|
634
|
+
async function readFull(r, buf) {
|
|
635
|
+
let offset = 0;
|
|
636
|
+
while (offset < $.len(buf)) {
|
|
637
|
+
const [n, err] = await r.Read($.goSlice(buf, offset));
|
|
638
|
+
offset += n;
|
|
639
|
+
if (err !== null) {
|
|
640
|
+
return [offset, offset > 0 && isEOF(err) ? io.ErrUnexpectedEOF : err];
|
|
641
|
+
}
|
|
642
|
+
if (n === 0) {
|
|
643
|
+
return [offset, io.ErrUnexpectedEOF];
|
|
644
|
+
}
|
|
645
|
+
}
|
|
646
|
+
return [offset, null];
|
|
647
|
+
}
|
|
648
|
+
function requireLen(b, n) {
|
|
649
|
+
const length = $.len(b);
|
|
650
|
+
if (length < n) {
|
|
651
|
+
$.panic(`runtime error: index out of range [${n - 1}] with length ${length}`);
|
|
652
|
+
}
|
|
653
|
+
}
|
|
654
|
+
function byteAt(b, i) {
|
|
655
|
+
return Number(b[i]) & 0xff;
|
|
656
|
+
}
|
|
657
|
+
function setByte(b, i, v) {
|
|
658
|
+
requireLen(b, i + 1);
|
|
659
|
+
b[i] = Number(v) & 0xff;
|
|
660
|
+
}
|
|
661
|
+
function encodeSignedVarint(x) {
|
|
662
|
+
let ux = BigInt.asUintN(64, BigInt.asIntN(64, toBigInt(x)) << 1n);
|
|
663
|
+
if (isNegativeInt64(x)) {
|
|
664
|
+
ux = BigInt.asUintN(64, ~ux);
|
|
665
|
+
}
|
|
666
|
+
return uint64Result(ux);
|
|
667
|
+
}
|
|
668
|
+
function decodeSignedVarint(uxValue) {
|
|
669
|
+
const ux = toUint64(uxValue);
|
|
670
|
+
let x = ux >> 1n;
|
|
671
|
+
if ((ux & 1n) !== 0n) {
|
|
672
|
+
x = BigInt.asIntN(64, ~x);
|
|
673
|
+
}
|
|
674
|
+
return int64Result(x);
|
|
675
|
+
}
|
|
676
|
+
function toUint64(value) {
|
|
677
|
+
if (typeof value === 'bigint') {
|
|
678
|
+
return BigInt.asUintN(64, value);
|
|
679
|
+
}
|
|
680
|
+
return BigInt.asUintN(64, BigInt(Math.trunc(value)));
|
|
681
|
+
}
|
|
682
|
+
function toBigInt(value) {
|
|
683
|
+
if (typeof value === 'bigint') {
|
|
684
|
+
return value;
|
|
685
|
+
}
|
|
686
|
+
return BigInt(Math.trunc(value));
|
|
687
|
+
}
|
|
688
|
+
function isNegativeInt64(value) {
|
|
689
|
+
return typeof value === 'bigint' ? value < 0n : value < 0;
|
|
690
|
+
}
|
|
691
|
+
function uint64Result(value) {
|
|
692
|
+
const normalized = BigInt.asUintN(64, value);
|
|
693
|
+
if (normalized <= BigInt(Number.MAX_SAFE_INTEGER)) {
|
|
694
|
+
return Number(normalized);
|
|
695
|
+
}
|
|
696
|
+
return normalized;
|
|
697
|
+
}
|
|
698
|
+
function int64Result(value) {
|
|
699
|
+
const normalized = BigInt.asIntN(64, value);
|
|
700
|
+
if (normalized >= BigInt(Number.MIN_SAFE_INTEGER) &&
|
|
701
|
+
normalized <= BigInt(Number.MAX_SAFE_INTEGER)) {
|
|
702
|
+
return Number(normalized);
|
|
703
|
+
}
|
|
704
|
+
return normalized;
|
|
705
|
+
}
|
|
706
|
+
function intN(value, bits) {
|
|
707
|
+
const sign = 1 << (bits - 1);
|
|
708
|
+
const mask = bits === 32 ? 0xffffffff : (1 << bits) - 1;
|
|
709
|
+
const normalized = value & mask;
|
|
710
|
+
return (normalized & sign) !== 0 ? normalized - (1 << bits) : normalized;
|
|
711
|
+
}
|
|
712
|
+
function float32Bits(value) {
|
|
713
|
+
const bytes = new ArrayBuffer(4);
|
|
714
|
+
const view = new DataView(bytes);
|
|
715
|
+
view.setFloat32(0, value, true);
|
|
716
|
+
return view.getUint32(0, true);
|
|
717
|
+
}
|
|
718
|
+
function float32FromBits(value) {
|
|
719
|
+
const bytes = new ArrayBuffer(4);
|
|
720
|
+
const view = new DataView(bytes);
|
|
721
|
+
view.setUint32(0, Number($.uint(value, 32)), true);
|
|
722
|
+
return view.getFloat32(0, true);
|
|
723
|
+
}
|
|
724
|
+
function float64Bits(value) {
|
|
725
|
+
const bytes = new ArrayBuffer(8);
|
|
726
|
+
const view = new DataView(bytes);
|
|
727
|
+
view.setFloat64(0, value, true);
|
|
728
|
+
return view.getBigUint64(0, true);
|
|
729
|
+
}
|
|
730
|
+
function float64FromBits(value) {
|
|
731
|
+
const bytes = new ArrayBuffer(8);
|
|
732
|
+
const view = new DataView(bytes);
|
|
733
|
+
view.setBigUint64(0, toUint64(value), true);
|
|
734
|
+
return view.getFloat64(0, true);
|
|
735
|
+
}
|
|
736
|
+
function isEOF(err) {
|
|
737
|
+
return err === io.EOF || err?.Error?.() === 'EOF';
|
|
738
|
+
}
|
|
739
|
+
function byteSliceArg(name) {
|
|
740
|
+
return { name, type: byteSliceType() };
|
|
741
|
+
}
|
|
742
|
+
function numberArg(name) {
|
|
743
|
+
return { name, type: { kind: $.TypeKind.Basic, name: 'number' } };
|
|
744
|
+
}
|
|
745
|
+
function numberReturn() {
|
|
746
|
+
return { type: { kind: $.TypeKind.Basic, name: 'number' } };
|
|
747
|
+
}
|
|
748
|
+
function byteSliceType() {
|
|
749
|
+
return {
|
|
750
|
+
kind: $.TypeKind.Slice,
|
|
751
|
+
elemType: { kind: $.TypeKind.Basic, name: 'uint8' },
|
|
752
|
+
};
|
|
753
|
+
}
|
|
754
|
+
const fixedKinds = new Set([
|
|
755
|
+
'bool',
|
|
756
|
+
'int8',
|
|
757
|
+
'uint8',
|
|
758
|
+
'int16',
|
|
759
|
+
'uint16',
|
|
760
|
+
'int32',
|
|
761
|
+
'uint32',
|
|
762
|
+
'int64',
|
|
763
|
+
'uint64',
|
|
764
|
+
'float32',
|
|
765
|
+
'float64',
|
|
766
|
+
'[]bool',
|
|
767
|
+
'[]int8',
|
|
768
|
+
'[]uint8',
|
|
769
|
+
'[]int16',
|
|
770
|
+
'[]uint16',
|
|
771
|
+
'[]int32',
|
|
772
|
+
'[]uint32',
|
|
773
|
+
'[]int64',
|
|
774
|
+
'[]uint64',
|
|
775
|
+
'[]float32',
|
|
776
|
+
'[]float64',
|
|
777
|
+
]);
|
|
778
|
+
//# sourceMappingURL=index.js.map
|