goscript 0.2.0 → 0.2.2
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/cmd/goscript-wasm/main.go +38 -6
- package/compiler/diagnostic.go +104 -12
- package/compiler/diagnostic_test.go +106 -0
- package/compiler/gotest/runner.go +99 -17
- package/compiler/gotest/runner_test.go +65 -0
- package/compiler/index.test.ts +23 -0
- package/compiler/lowered-program.go +9 -7
- package/compiler/lowering.go +361 -72
- package/compiler/lowering_bench_test.go +1 -0
- package/compiler/lowering_internal_test.go +18 -0
- package/compiler/protobuf-ts-binding.go +65 -12
- package/compiler/protobuf-ts-binding_test.go +339 -0
- package/compiler/runtime-contract.go +4 -0
- package/compiler/runtime-contract_test.go +2 -0
- package/compiler/service.go +1 -0
- package/compiler/skeleton_test.go +60 -3
- package/compiler/wasm/compile_test.go +37 -4
- package/compiler/wasm-api.go +57 -7
- package/dist/gs/builtin/hostio.js +6 -1
- package/dist/gs/builtin/hostio.js.map +1 -1
- package/dist/gs/builtin/slice.d.ts +11 -1
- package/dist/gs/builtin/slice.js +158 -2
- package/dist/gs/builtin/slice.js.map +1 -1
- package/dist/gs/crypto/aes/index.d.ts +15 -0
- package/dist/gs/crypto/aes/index.js +57 -0
- package/dist/gs/crypto/aes/index.js.map +1 -0
- package/dist/gs/crypto/cipher/index.d.ts +41 -0
- package/dist/gs/crypto/cipher/index.js +255 -0
- package/dist/gs/crypto/cipher/index.js.map +1 -0
- package/dist/gs/github.com/aperturerobotics/protobuf-go-lite/index.d.ts +1 -0
- package/dist/gs/github.com/aperturerobotics/protobuf-go-lite/index.js +30 -5
- package/dist/gs/github.com/aperturerobotics/protobuf-go-lite/index.js.map +1 -1
- package/dist/gs/github.com/aperturerobotics/protobuf-go-lite/json/index.d.ts +1 -0
- package/dist/gs/github.com/aperturerobotics/protobuf-go-lite/json/index.js +17 -11
- package/dist/gs/github.com/aperturerobotics/protobuf-go-lite/json/index.js.map +1 -1
- package/dist/gs/golang.org/x/crypto/chacha20poly1305/index.d.ts +31 -0
- package/dist/gs/golang.org/x/crypto/chacha20poly1305/index.js +117 -0
- package/dist/gs/golang.org/x/crypto/chacha20poly1305/index.js.map +1 -0
- package/dist/gs/internal/byteorder/index.js +2 -2
- package/dist/gs/internal/byteorder/index.js.map +1 -1
- package/dist/gs/io/io.js +18 -2
- package/dist/gs/io/io.js.map +1 -1
- package/dist/gs/reflect/type.js +57 -0
- package/dist/gs/reflect/type.js.map +1 -1
- package/dist/gs/runtime/debug/index.js +2 -1
- package/dist/gs/runtime/debug/index.js.map +1 -1
- package/dist/gs/sync/atomic/doc_64.gs.js +7 -6
- package/dist/gs/sync/atomic/doc_64.gs.js.map +1 -1
- package/go.mod +2 -2
- package/go.sum +2 -0
- package/gs/builtin/hostio.test.ts +22 -1
- package/gs/builtin/hostio.ts +6 -1
- package/gs/builtin/runtime-contract.test.ts +28 -0
- package/gs/builtin/slice.ts +225 -20
- package/gs/crypto/aes/index.test.ts +120 -0
- package/gs/crypto/aes/index.ts +76 -0
- package/gs/crypto/cipher/index.ts +345 -0
- package/gs/crypto/cipher/meta.json +6 -0
- package/gs/github.com/aperturerobotics/protobuf-go-lite/index.test.ts +162 -0
- package/gs/github.com/aperturerobotics/protobuf-go-lite/index.ts +41 -5
- package/gs/github.com/aperturerobotics/protobuf-go-lite/json/index.test.ts +18 -0
- package/gs/github.com/aperturerobotics/protobuf-go-lite/json/index.ts +17 -11
- package/gs/golang.org/x/crypto/chacha20poly1305/index.test.ts +91 -0
- package/gs/golang.org/x/crypto/chacha20poly1305/index.ts +245 -0
- package/gs/internal/byteorder/index.test.ts +2 -2
- package/gs/internal/byteorder/index.ts +2 -2
- package/gs/io/io.test.ts +56 -1
- package/gs/io/io.ts +19 -2
- package/gs/reflect/type.ts +64 -0
- package/gs/reflect/typefor.test.ts +21 -1
- package/gs/runtime/debug/index.test.ts +32 -4
- package/gs/runtime/debug/index.ts +5 -2
- package/gs/sync/atomic/doc_64.gs.ts +6 -7
- package/gs/sync/atomic/doc_64.test.ts +43 -0
- package/package.json +10 -3
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import * as $ from "@goscript/builtin/index.js";
|
|
1
2
|
// SwapInt64 atomically stores new into *addr and returns the previous *addr value.
|
|
2
3
|
// Consider using the more ergonomic and less error-prone [Int64.Swap] instead
|
|
3
4
|
// (particularly if you target 32-bit platforms; see the bugs section).
|
|
@@ -58,7 +59,7 @@ export function CompareAndSwapUint64(addr, old, _new) {
|
|
|
58
59
|
export function AddInt64(addr, delta) {
|
|
59
60
|
if (!addr)
|
|
60
61
|
return 0;
|
|
61
|
-
addr.value = addr.value
|
|
62
|
+
addr.value = $.int64Add(addr.value, delta);
|
|
62
63
|
return addr.value;
|
|
63
64
|
}
|
|
64
65
|
// AddUint64 atomically adds delta to *addr and returns the new value.
|
|
@@ -71,7 +72,7 @@ export function AddInt64(addr, delta) {
|
|
|
71
72
|
export function AddUint64(addr, delta) {
|
|
72
73
|
if (!addr)
|
|
73
74
|
return 0;
|
|
74
|
-
addr.value = addr.value
|
|
75
|
+
addr.value = $.uint64Add(addr.value, delta);
|
|
75
76
|
return addr.value;
|
|
76
77
|
}
|
|
77
78
|
// AndInt64 atomically performs a bitwise AND operation on *addr using the bitmask provided as mask
|
|
@@ -83,7 +84,7 @@ export function AndInt64(addr, mask) {
|
|
|
83
84
|
if (!addr)
|
|
84
85
|
return 0;
|
|
85
86
|
let old = addr.value;
|
|
86
|
-
addr.value = addr.value
|
|
87
|
+
addr.value = $.int64And(addr.value, mask);
|
|
87
88
|
return old;
|
|
88
89
|
}
|
|
89
90
|
// AndUint64 atomically performs a bitwise AND operation on *addr using the bitmask provided as mask
|
|
@@ -95,7 +96,7 @@ export function AndUint64(addr, mask) {
|
|
|
95
96
|
if (!addr)
|
|
96
97
|
return 0;
|
|
97
98
|
let old = addr.value;
|
|
98
|
-
addr.value = addr.value
|
|
99
|
+
addr.value = $.uint64And(addr.value, mask);
|
|
99
100
|
return old;
|
|
100
101
|
}
|
|
101
102
|
// OrInt64 atomically performs a bitwise OR operation on *addr using the bitmask provided as mask
|
|
@@ -107,7 +108,7 @@ export function OrInt64(addr, mask) {
|
|
|
107
108
|
if (!addr)
|
|
108
109
|
return 0;
|
|
109
110
|
let old = addr.value;
|
|
110
|
-
addr.value = addr.value
|
|
111
|
+
addr.value = $.int64Or(addr.value, mask);
|
|
111
112
|
return old;
|
|
112
113
|
}
|
|
113
114
|
// OrUint64 atomically performs a bitwise OR operation on *addr using the bitmask provided as mask
|
|
@@ -119,7 +120,7 @@ export function OrUint64(addr, mask) {
|
|
|
119
120
|
if (!addr)
|
|
120
121
|
return 0;
|
|
121
122
|
let old = addr.value;
|
|
122
|
-
addr.value = addr.value
|
|
123
|
+
addr.value = $.uint64Or(addr.value, mask);
|
|
123
124
|
return old;
|
|
124
125
|
}
|
|
125
126
|
// LoadInt64 atomically loads *addr.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"doc_64.gs.js","sourceRoot":"","sources":["../../../../gs/sync/atomic/doc_64.gs.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"doc_64.gs.js","sourceRoot":"","sources":["../../../../gs/sync/atomic/doc_64.gs.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,CAAC,MAAM,4BAA4B,CAAC;AAEhD,mFAAmF;AACnF,8EAA8E;AAC9E,uEAAuE;AACvE,EAAE;AACF,aAAa;AACb,MAAM,UAAU,SAAS,CAAC,IAA6B,EAAE,IAAY;IACpE,IAAI,CAAC,IAAI;QAAE,OAAO,CAAC,CAAC;IACpB,IAAI,GAAG,GAAG,IAAI,CAAC,KAAK,CAAC;IACrB,IAAI,CAAC,KAAK,GAAG,IAAI,CAAC;IAClB,OAAO,GAAG,CAAC;AACZ,CAAC;AAED,oFAAoF;AACpF,+EAA+E;AAC/E,uEAAuE;AACvE,EAAE;AACF,aAAa;AACb,MAAM,UAAU,UAAU,CAAC,IAA6B,EAAE,IAAY;IACrE,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"}
|
package/go.mod
CHANGED
|
@@ -2,13 +2,13 @@ module github.com/aperturerobotics/goscript
|
|
|
2
2
|
|
|
3
3
|
go 1.25.3
|
|
4
4
|
|
|
5
|
-
toolchain go1.26.
|
|
5
|
+
toolchain go1.26.4
|
|
6
6
|
|
|
7
7
|
require (
|
|
8
8
|
github.com/aperturerobotics/cli v1.1.0
|
|
9
9
|
github.com/aperturerobotics/json-iterator-lite v1.1.0
|
|
10
10
|
github.com/aperturerobotics/protobuf-go-lite v0.14.0
|
|
11
|
-
github.com/aperturerobotics/util v1.34.
|
|
11
|
+
github.com/aperturerobotics/util v1.34.9
|
|
12
12
|
github.com/pkg/errors v0.9.1
|
|
13
13
|
github.com/sirupsen/logrus v1.9.5-0.20260508084601-d4a50659cfd6
|
|
14
14
|
golang.org/x/tools v0.45.0
|
package/go.sum
CHANGED
|
@@ -15,6 +15,8 @@ github.com/aperturerobotics/util v1.34.5 h1:007MaOJrrsiGm5o1c8Tt7p8nVwUAxkM6pmGf
|
|
|
15
15
|
github.com/aperturerobotics/util v1.34.5/go.mod h1:mDe7WnncVuV7yjeeVSsagyfrw4xfncu7d+f0+d70niY=
|
|
16
16
|
github.com/aperturerobotics/util v1.34.6 h1:Z5LOncJ+ooBut7Q2FIJTYZvx5KOFD/F7qFdchCYl1aY=
|
|
17
17
|
github.com/aperturerobotics/util v1.34.6/go.mod h1:4dIoZeVR1RNIgqrZxWe9F9LkDtsGEBvFTiwFAT0vxZ4=
|
|
18
|
+
github.com/aperturerobotics/util v1.34.9 h1:Eyhu1Citt+od7jKIb+Q92h/CEujWCFsvdvJavQeoRjM=
|
|
19
|
+
github.com/aperturerobotics/util v1.34.9/go.mod h1:GxjGkYQNEj8xvLFz+T8ifqANtK5cMesOlCxh50htvBI=
|
|
18
20
|
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
|
19
21
|
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
|
20
22
|
github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps=
|
|
@@ -182,18 +182,39 @@ describe('hostio text writes', () => {
|
|
|
182
182
|
expect(getHostRuntime().platform).toBe('darwin')
|
|
183
183
|
})
|
|
184
184
|
|
|
185
|
-
it('uses console fallback in browser-like hosts', () => {
|
|
185
|
+
it('uses console.log fallback for stdout and stderr text in browser-like hosts', () => {
|
|
186
186
|
const consoleLog = vi.spyOn(console, 'log').mockImplementation(() => {})
|
|
187
|
+
const consoleError = vi.spyOn(console, 'error').mockImplementation(() => {})
|
|
187
188
|
|
|
188
189
|
delete (globalThis as any).Deno
|
|
189
190
|
delete (globalThis as any).process
|
|
190
191
|
resetHostRuntimeForTests()
|
|
191
192
|
|
|
192
193
|
writeHostStdoutText('browser\n')
|
|
194
|
+
writeHostStderrText('stderr\n')
|
|
193
195
|
|
|
194
196
|
expect(getHostRuntime().platform).toBe('unknown')
|
|
195
197
|
expect(getHostRuntime().nodeFS).toBeNull()
|
|
196
198
|
expect(consoleLog).toHaveBeenCalledWith('browser')
|
|
199
|
+
expect(consoleLog).toHaveBeenCalledWith('stderr')
|
|
200
|
+
expect(consoleError).not.toHaveBeenCalled()
|
|
201
|
+
})
|
|
202
|
+
|
|
203
|
+
it('writes stdout and stderr file descriptors to console.log in browser-like hosts', () => {
|
|
204
|
+
const consoleLog = vi.spyOn(console, 'log').mockImplementation(() => {})
|
|
205
|
+
const consoleError = vi.spyOn(console, 'error').mockImplementation(() => {})
|
|
206
|
+
|
|
207
|
+
delete (globalThis as any).Deno
|
|
208
|
+
delete (globalThis as any).process
|
|
209
|
+
resetHostRuntimeForTests()
|
|
210
|
+
|
|
211
|
+
const runtime = getHostRuntime()
|
|
212
|
+
expect(runtime.writeFD(1, new Uint8Array([111, 107, 10]))).toBe(3)
|
|
213
|
+
expect(runtime.writeFD(2, new Uint8Array([101, 114, 114, 10]))).toBe(4)
|
|
214
|
+
|
|
215
|
+
expect(consoleLog).toHaveBeenCalledWith('ok')
|
|
216
|
+
expect(consoleLog).toHaveBeenCalledWith('err')
|
|
217
|
+
expect(consoleError).not.toHaveBeenCalled()
|
|
197
218
|
})
|
|
198
219
|
})
|
|
199
220
|
|
package/gs/builtin/hostio.ts
CHANGED
|
@@ -104,6 +104,7 @@ export type MainScriptMeta = {
|
|
|
104
104
|
}
|
|
105
105
|
|
|
106
106
|
const encoder = new TextEncoder()
|
|
107
|
+
const decoder = new TextDecoder()
|
|
107
108
|
|
|
108
109
|
function getDynamicRequire(): ((specifier: string) => unknown) | null {
|
|
109
110
|
try {
|
|
@@ -342,6 +343,10 @@ function detectHostRuntime(): HostRuntime {
|
|
|
342
343
|
buffer,
|
|
343
344
|
)
|
|
344
345
|
}
|
|
346
|
+
if (fd === 1 || fd === 2) {
|
|
347
|
+
fallbackConsoleWriter('log')(decoder.decode(buffer))
|
|
348
|
+
return buffer.length
|
|
349
|
+
}
|
|
345
350
|
throw new HostUnsupportedError()
|
|
346
351
|
}
|
|
347
352
|
const writeStdoutText: HostTextWrite = (data: string) => {
|
|
@@ -374,7 +379,7 @@ function detectHostRuntime(): HostRuntime {
|
|
|
374
379
|
)
|
|
375
380
|
return
|
|
376
381
|
}
|
|
377
|
-
fallbackConsoleWriter('
|
|
382
|
+
fallbackConsoleWriter('log')(data)
|
|
378
383
|
}
|
|
379
384
|
|
|
380
385
|
return {
|
|
@@ -17,6 +17,7 @@ import {
|
|
|
17
17
|
int,
|
|
18
18
|
arrayPointerFromIndexRef,
|
|
19
19
|
indexAddress,
|
|
20
|
+
indexByteAddress,
|
|
20
21
|
indexRef,
|
|
21
22
|
interfaceValue,
|
|
22
23
|
len,
|
|
@@ -62,6 +63,7 @@ import {
|
|
|
62
63
|
uintShr,
|
|
63
64
|
unref,
|
|
64
65
|
unsupportedPointerRef,
|
|
66
|
+
unsafePointerRef,
|
|
65
67
|
varRef,
|
|
66
68
|
} from './index.js'
|
|
67
69
|
|
|
@@ -423,6 +425,16 @@ describe('builtin runtime contract helpers', () => {
|
|
|
423
425
|
arrayView[2] = 19
|
|
424
426
|
expect((byteBacking as Uint8Array)[3]).toBe(19)
|
|
425
427
|
|
|
428
|
+
const words = [0x11223344, 0]
|
|
429
|
+
const wordBytes = arrayPointerFromIndexRef(indexRef(words, 0), 8, 4, 1)
|
|
430
|
+
const byteView = pointerValue(wordBytes) as number[]
|
|
431
|
+
expect(byteView[0]).toBe(0x44)
|
|
432
|
+
expect(byteView[3]).toBe(0x11)
|
|
433
|
+
byteView[4] = 0xaa
|
|
434
|
+
expect(words[1]).toBe(0xaa)
|
|
435
|
+
wordBytes.value = [1, 2, 3, 4]
|
|
436
|
+
expect(words[0]).toBe(0x04030201)
|
|
437
|
+
|
|
426
438
|
shortBytes![0] = 14
|
|
427
439
|
expect(bytesToUint8Array(shortBytes)).toEqual(new Uint8Array([14, 0]))
|
|
428
440
|
|
|
@@ -473,6 +485,22 @@ describe('builtin runtime contract helpers', () => {
|
|
|
473
485
|
expect(indexAddress(other, 0)).not.toBe(indexAddress(left, 0))
|
|
474
486
|
})
|
|
475
487
|
|
|
488
|
+
it('resolves unsafe byte addresses within numeric slice elements', () => {
|
|
489
|
+
const words = makeSlice<number>(2, undefined, 'number')
|
|
490
|
+
const first = indexByteAddress(words, 0, 8)
|
|
491
|
+
const second = indexByteAddress(words, 1, 8)
|
|
492
|
+
|
|
493
|
+
expect(second - first).toBe(8)
|
|
494
|
+
unsafePointerRef<number>(first + 1).value = 0x12
|
|
495
|
+
unsafePointerRef<number>(first + 7).value = 0x80
|
|
496
|
+
expect(unsafePointerRef<number>(first + 1).value).toBe(0x12)
|
|
497
|
+
expect(unsafePointerRef<number>(first + 7).value).toBe(0x80)
|
|
498
|
+
expect(words![0]).toBe(0x8000000000001200n as unknown as number)
|
|
499
|
+
|
|
500
|
+
unsafePointerRef<number>(second).value = 0x34
|
|
501
|
+
expect(words![1]).toBe(0x34)
|
|
502
|
+
})
|
|
503
|
+
|
|
476
504
|
it('exposes owned pointer handles for addressable collection elements', () => {
|
|
477
505
|
const values = [1, 2, 3, 4]
|
|
478
506
|
const second = indexRef(values, 1)
|
package/gs/builtin/slice.ts
CHANGED
|
@@ -76,6 +76,14 @@ interface GoSliceObject<T> {
|
|
|
76
76
|
const addressStride = 0x100000000
|
|
77
77
|
let nextAddressBase = 1
|
|
78
78
|
const addressBases = new WeakMap<object, number>()
|
|
79
|
+
const byteAddressBases = new WeakMap<object, number>()
|
|
80
|
+
const byteAddressSources = new globalThis.Map<number, ByteAddressSource>()
|
|
81
|
+
|
|
82
|
+
interface ByteAddressSource {
|
|
83
|
+
byteLength: number
|
|
84
|
+
getByte(offset: number): number
|
|
85
|
+
setByte(offset: number, value: number): void
|
|
86
|
+
}
|
|
79
87
|
|
|
80
88
|
/**
|
|
81
89
|
* SliceProxy is a proxy object for complex slices
|
|
@@ -129,9 +137,7 @@ function wrapSliceProxy<T>(proxy: SliceProxy<T>): SliceProxy<T> {
|
|
|
129
137
|
if (index < meta.length) {
|
|
130
138
|
return meta.backing[meta.offset + index]
|
|
131
139
|
}
|
|
132
|
-
throw new Error(
|
|
133
|
-
`Slice index out of range: ${index} >= ${meta.length}`,
|
|
134
|
-
)
|
|
140
|
+
throw new Error(`Slice index out of range: ${index} >= ${meta.length}`)
|
|
135
141
|
}
|
|
136
142
|
|
|
137
143
|
if (prop === 'length') {
|
|
@@ -153,9 +159,7 @@ function wrapSliceProxy<T>(proxy: SliceProxy<T>): SliceProxy<T> {
|
|
|
153
159
|
target[index] = value // Also update the proxy target for consistency
|
|
154
160
|
return true
|
|
155
161
|
}
|
|
156
|
-
throw new Error(
|
|
157
|
-
`Slice index out of range: ${index} >= ${meta.length}`,
|
|
158
|
-
)
|
|
162
|
+
throw new Error(`Slice index out of range: ${index} >= ${meta.length}`)
|
|
159
163
|
}
|
|
160
164
|
|
|
161
165
|
if (prop === 'length' || prop === '__meta__') {
|
|
@@ -329,7 +333,12 @@ export const makeSlice = <T>(
|
|
|
329
333
|
return new Uint8Array(length) as Slice<T>
|
|
330
334
|
}
|
|
331
335
|
|
|
332
|
-
return byteSliceView(
|
|
336
|
+
return byteSliceView(
|
|
337
|
+
new Uint8Array(actualCapacity),
|
|
338
|
+
0,
|
|
339
|
+
length,
|
|
340
|
+
actualCapacity,
|
|
341
|
+
) as Slice<T>
|
|
333
342
|
}
|
|
334
343
|
|
|
335
344
|
const actualCapacity = capacity === undefined ? length : capacity
|
|
@@ -535,8 +544,7 @@ export function goSlice<T>( // T can be number for Uint8Array case
|
|
|
535
544
|
if (s instanceof Uint8Array) {
|
|
536
545
|
const meta = byteSliceMeta(s)
|
|
537
546
|
const metaBacking = meta?.backing as unknown
|
|
538
|
-
const backing =
|
|
539
|
-
metaBacking instanceof Uint8Array ? metaBacking : s
|
|
547
|
+
const backing = metaBacking instanceof Uint8Array ? metaBacking : s
|
|
540
548
|
const baseOffset = meta?.offset ?? 0
|
|
541
549
|
const baseCapacity = meta?.capacity ?? s.length
|
|
542
550
|
const actualLow = low ?? 0
|
|
@@ -1003,8 +1011,7 @@ export function append<T>(
|
|
|
1003
1011
|
function appendByteSlice(slice: Uint8Array, elements: any[]): Uint8Array {
|
|
1004
1012
|
const meta = byteSliceMeta(slice)
|
|
1005
1013
|
const metaBacking = meta?.backing as unknown
|
|
1006
|
-
const backing =
|
|
1007
|
-
metaBacking instanceof Uint8Array ? metaBacking : slice
|
|
1014
|
+
const backing = metaBacking instanceof Uint8Array ? metaBacking : slice
|
|
1008
1015
|
const offset = meta?.offset ?? 0
|
|
1009
1016
|
const oldLength = slice.length
|
|
1010
1017
|
const oldCapacity = meta?.capacity ?? oldLength
|
|
@@ -1032,12 +1039,18 @@ function byteElementLength(item: any): number {
|
|
|
1032
1039
|
return len(item as Slice<any>)
|
|
1033
1040
|
}
|
|
1034
1041
|
if (typeof item !== 'number') {
|
|
1035
|
-
throw new Error(
|
|
1042
|
+
throw new Error(
|
|
1043
|
+
'Cannot produce Uint8Array: appended elements contain non-numbers.',
|
|
1044
|
+
)
|
|
1036
1045
|
}
|
|
1037
1046
|
return 1
|
|
1038
1047
|
}
|
|
1039
1048
|
|
|
1040
|
-
function writeByteElements(
|
|
1049
|
+
function writeByteElements(
|
|
1050
|
+
dst: Uint8Array,
|
|
1051
|
+
offset: number,
|
|
1052
|
+
elements: any[],
|
|
1053
|
+
): void {
|
|
1041
1054
|
let cursor = offset
|
|
1042
1055
|
for (const item of elements) {
|
|
1043
1056
|
if (item instanceof Uint8Array) {
|
|
@@ -1060,7 +1073,9 @@ function writeByteElements(dst: Uint8Array, offset: number, elements: any[]): vo
|
|
|
1060
1073
|
continue
|
|
1061
1074
|
}
|
|
1062
1075
|
if (typeof item !== 'number') {
|
|
1063
|
-
throw new Error(
|
|
1076
|
+
throw new Error(
|
|
1077
|
+
'Cannot produce Uint8Array: appended elements contain non-numbers.',
|
|
1078
|
+
)
|
|
1064
1079
|
}
|
|
1065
1080
|
dst[cursor] = item
|
|
1066
1081
|
cursor++
|
|
@@ -1394,6 +1409,8 @@ export function sliceFromOwnedPointer<T>(
|
|
|
1394
1409
|
export function arrayPointerFromIndexRef<T>(
|
|
1395
1410
|
ref: VarRef<T>,
|
|
1396
1411
|
length: number,
|
|
1412
|
+
sourceElementByteSize = 1,
|
|
1413
|
+
targetElementByteSize = sourceElementByteSize,
|
|
1397
1414
|
): VarRef<Slice<T> | T[] | Uint8Array> {
|
|
1398
1415
|
const collection = ref.__goCollection as
|
|
1399
1416
|
| Slice<T>
|
|
@@ -1406,12 +1423,25 @@ export function arrayPointerFromIndexRef<T>(
|
|
|
1406
1423
|
)
|
|
1407
1424
|
}
|
|
1408
1425
|
const index = ref.__goIndex ?? 0
|
|
1409
|
-
|
|
1410
|
-
|
|
1411
|
-
|
|
1412
|
-
|
|
1413
|
-
|
|
1414
|
-
|
|
1426
|
+
const view =
|
|
1427
|
+
targetElementByteSize === 1 && sourceElementByteSize > 1 ?
|
|
1428
|
+
byteArrayFromAddress(
|
|
1429
|
+
indexByteAddress(collection, index, sourceElementByteSize),
|
|
1430
|
+
length,
|
|
1431
|
+
)
|
|
1432
|
+
: (goSlice(collection as any, index, index + length) as
|
|
1433
|
+
| Slice<T>
|
|
1434
|
+
| T[]
|
|
1435
|
+
| Uint8Array)
|
|
1436
|
+
return {
|
|
1437
|
+
get value() {
|
|
1438
|
+
return view as Slice<T> | T[] | Uint8Array
|
|
1439
|
+
},
|
|
1440
|
+
set value(value: Slice<T> | T[] | Uint8Array) {
|
|
1441
|
+
copy(view as any, value as any)
|
|
1442
|
+
},
|
|
1443
|
+
__isVarRef: true,
|
|
1444
|
+
}
|
|
1415
1445
|
}
|
|
1416
1446
|
|
|
1417
1447
|
/**
|
|
@@ -1460,6 +1490,181 @@ export function indexAddress<T>(
|
|
|
1460
1490
|
return base + backingIndex
|
|
1461
1491
|
}
|
|
1462
1492
|
|
|
1493
|
+
function uintElementValue(value: unknown, byteSize: number): bigint {
|
|
1494
|
+
const bits = BigInt(byteSize * 8)
|
|
1495
|
+
if (typeof value === 'bigint') {
|
|
1496
|
+
return BigInt.asUintN(Number(bits), value)
|
|
1497
|
+
}
|
|
1498
|
+
if (typeof value === 'number' && Number.isFinite(value)) {
|
|
1499
|
+
return BigInt.asUintN(Number(bits), BigInt(Math.trunc(value)))
|
|
1500
|
+
}
|
|
1501
|
+
return 0n
|
|
1502
|
+
}
|
|
1503
|
+
|
|
1504
|
+
function uintElementResult(value: bigint, byteSize: number): number {
|
|
1505
|
+
const normalized = BigInt.asUintN(byteSize * 8, value)
|
|
1506
|
+
if (normalized <= BigInt(Number.MAX_SAFE_INTEGER)) {
|
|
1507
|
+
return Number(normalized)
|
|
1508
|
+
}
|
|
1509
|
+
return normalized as unknown as number
|
|
1510
|
+
}
|
|
1511
|
+
|
|
1512
|
+
function byteAddressBase(backing: object, source: ByteAddressSource): number {
|
|
1513
|
+
let base = byteAddressBases.get(backing)
|
|
1514
|
+
if (base === undefined) {
|
|
1515
|
+
base = nextAddressBase * addressStride
|
|
1516
|
+
nextAddressBase++
|
|
1517
|
+
byteAddressBases.set(backing, base)
|
|
1518
|
+
}
|
|
1519
|
+
byteAddressSources.set(base, source)
|
|
1520
|
+
return base
|
|
1521
|
+
}
|
|
1522
|
+
|
|
1523
|
+
function numericByteSource(
|
|
1524
|
+
backing: number[],
|
|
1525
|
+
elementByteSize: number,
|
|
1526
|
+
): ByteAddressSource {
|
|
1527
|
+
const byteSize = Math.max(1, Math.trunc(elementByteSize))
|
|
1528
|
+
return {
|
|
1529
|
+
byteLength: backing.length * byteSize,
|
|
1530
|
+
getByte(offset: number): number {
|
|
1531
|
+
const elementIndex = Math.trunc(offset / byteSize)
|
|
1532
|
+
const byteOffset = offset % byteSize
|
|
1533
|
+
const value = uintElementValue(backing[elementIndex], byteSize)
|
|
1534
|
+
return Number((value >> BigInt(byteOffset * 8)) & 0xffn)
|
|
1535
|
+
},
|
|
1536
|
+
setByte(offset: number, value: number): void {
|
|
1537
|
+
const elementIndex = Math.trunc(offset / byteSize)
|
|
1538
|
+
const byteOffset = offset % byteSize
|
|
1539
|
+
const shift = BigInt(byteOffset * 8)
|
|
1540
|
+
const mask = 0xffn << shift
|
|
1541
|
+
const current = uintElementValue(backing[elementIndex], byteSize)
|
|
1542
|
+
const next = (current & ~mask) | ((BigInt(value) & 0xffn) << shift)
|
|
1543
|
+
backing[elementIndex] = uintElementResult(next, byteSize)
|
|
1544
|
+
},
|
|
1545
|
+
}
|
|
1546
|
+
}
|
|
1547
|
+
|
|
1548
|
+
/**
|
|
1549
|
+
* indexByteAddress returns a byte-addressed synthetic address for unsafe
|
|
1550
|
+
* uintptr arithmetic rooted at a slice or array element.
|
|
1551
|
+
*/
|
|
1552
|
+
export function indexByteAddress<T>(
|
|
1553
|
+
collection: Slice<T> | T[] | Uint8Array,
|
|
1554
|
+
index: number,
|
|
1555
|
+
elementByteSize: number,
|
|
1556
|
+
): number {
|
|
1557
|
+
if (collection === null || collection === undefined) {
|
|
1558
|
+
throw new Error('runtime error: index on nil or undefined collection')
|
|
1559
|
+
}
|
|
1560
|
+
|
|
1561
|
+
if (collection instanceof Uint8Array) {
|
|
1562
|
+
if (index < 0 || index >= collection.length) {
|
|
1563
|
+
throw new Error(
|
|
1564
|
+
`runtime error: index out of range [${index}] with length ${collection.length}`,
|
|
1565
|
+
)
|
|
1566
|
+
}
|
|
1567
|
+
const view = new Uint8Array(collection.buffer)
|
|
1568
|
+
const base = byteAddressBase(collection.buffer, {
|
|
1569
|
+
byteLength: view.length,
|
|
1570
|
+
getByte(offset: number): number {
|
|
1571
|
+
return view[offset]
|
|
1572
|
+
},
|
|
1573
|
+
setByte(offset: number, value: number): void {
|
|
1574
|
+
view[offset] = value
|
|
1575
|
+
},
|
|
1576
|
+
})
|
|
1577
|
+
return base + collection.byteOffset + index
|
|
1578
|
+
}
|
|
1579
|
+
|
|
1580
|
+
if (isComplexSlice(collection)) {
|
|
1581
|
+
if (index < 0 || index >= collection.__meta__.length) {
|
|
1582
|
+
throw new Error(
|
|
1583
|
+
`runtime error: index out of range [${index}] with length ${collection.__meta__.length}`,
|
|
1584
|
+
)
|
|
1585
|
+
}
|
|
1586
|
+
const backing = collection.__meta__.backing as unknown as number[]
|
|
1587
|
+
const byteSize = Math.max(1, Math.trunc(elementByteSize))
|
|
1588
|
+
const base = byteAddressBase(backing, numericByteSource(backing, byteSize))
|
|
1589
|
+
return base + (collection.__meta__.offset + index) * byteSize
|
|
1590
|
+
}
|
|
1591
|
+
|
|
1592
|
+
if (Array.isArray(collection)) {
|
|
1593
|
+
if (index < 0 || index >= collection.length) {
|
|
1594
|
+
throw new Error(
|
|
1595
|
+
`runtime error: index out of range [${index}] with length ${collection.length}`,
|
|
1596
|
+
)
|
|
1597
|
+
}
|
|
1598
|
+
const byteSize = Math.max(1, Math.trunc(elementByteSize))
|
|
1599
|
+
const base = byteAddressBase(
|
|
1600
|
+
collection,
|
|
1601
|
+
numericByteSource(collection as unknown as number[], byteSize),
|
|
1602
|
+
)
|
|
1603
|
+
return base + index * byteSize
|
|
1604
|
+
}
|
|
1605
|
+
|
|
1606
|
+
throw new Error('runtime error: index on unsupported type')
|
|
1607
|
+
}
|
|
1608
|
+
|
|
1609
|
+
/**
|
|
1610
|
+
* unsafePointerRef resolves a byte-addressed synthetic unsafe pointer created
|
|
1611
|
+
* by indexByteAddress back to an addressable byte reference.
|
|
1612
|
+
*/
|
|
1613
|
+
export function unsafePointerRef<T>(address: number | bigint): VarRef<T> {
|
|
1614
|
+
const numericAddress = Number(address)
|
|
1615
|
+
const base = Math.floor(numericAddress / addressStride) * addressStride
|
|
1616
|
+
const source = byteAddressSources.get(base)
|
|
1617
|
+
if (source === undefined) {
|
|
1618
|
+
throw new Error(
|
|
1619
|
+
'unsafe pointer dereference is not supported in JavaScript/TypeScript',
|
|
1620
|
+
)
|
|
1621
|
+
}
|
|
1622
|
+
const offset = numericAddress - base
|
|
1623
|
+
if (offset < 0 || offset >= source.byteLength) {
|
|
1624
|
+
throw new Error('runtime error: unsafe pointer address out of range')
|
|
1625
|
+
}
|
|
1626
|
+
return {
|
|
1627
|
+
get value(): T {
|
|
1628
|
+
return source.getByte(offset) as T
|
|
1629
|
+
},
|
|
1630
|
+
set value(value: T) {
|
|
1631
|
+
source.setByte(offset, value as number)
|
|
1632
|
+
},
|
|
1633
|
+
__isVarRef: true,
|
|
1634
|
+
}
|
|
1635
|
+
}
|
|
1636
|
+
|
|
1637
|
+
function byteArrayFromAddress(
|
|
1638
|
+
address: number | bigint,
|
|
1639
|
+
length: number,
|
|
1640
|
+
): number[] {
|
|
1641
|
+
const start = Number(address)
|
|
1642
|
+
const target = new Array<number>(length)
|
|
1643
|
+
return new Proxy(target, {
|
|
1644
|
+
get(arrayTarget, prop, receiver) {
|
|
1645
|
+
const index = sliceIndexProperty(prop)
|
|
1646
|
+
if (index >= 0) {
|
|
1647
|
+
if (index >= length) {
|
|
1648
|
+
throw new Error(`Slice index out of range: ${index} >= ${length}`)
|
|
1649
|
+
}
|
|
1650
|
+
return unsafePointerRef<number>(start + index).value
|
|
1651
|
+
}
|
|
1652
|
+
return Reflect.get(arrayTarget, prop, receiver)
|
|
1653
|
+
},
|
|
1654
|
+
set(arrayTarget, prop, value, receiver) {
|
|
1655
|
+
const index = sliceIndexProperty(prop)
|
|
1656
|
+
if (index >= 0) {
|
|
1657
|
+
if (index >= length) {
|
|
1658
|
+
throw new Error(`Slice index out of range: ${index} >= ${length}`)
|
|
1659
|
+
}
|
|
1660
|
+
unsafePointerRef<number>(start + index).value = value
|
|
1661
|
+
return true
|
|
1662
|
+
}
|
|
1663
|
+
return Reflect.set(arrayTarget, prop, value, receiver)
|
|
1664
|
+
},
|
|
1665
|
+
})
|
|
1666
|
+
}
|
|
1667
|
+
|
|
1463
1668
|
/**
|
|
1464
1669
|
* Converts a string to an array of Unicode code points (runes).
|
|
1465
1670
|
* @param str The input string.
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import { describe, expect, test } from 'vitest'
|
|
2
|
+
|
|
3
|
+
import * as $ from '@goscript/builtin/index.js'
|
|
4
|
+
import * as cipher from '@goscript/crypto/cipher/index.js'
|
|
5
|
+
|
|
6
|
+
import { BlockSize, KeySizeError_Error, NewCipher } from './index.js'
|
|
7
|
+
|
|
8
|
+
describe('crypto/aes WebCrypto override', () => {
|
|
9
|
+
test.each([
|
|
10
|
+
[
|
|
11
|
+
'AES-128',
|
|
12
|
+
'000102030405060708090a0b0c0d0e0f',
|
|
13
|
+
'00112233445566778899aabbccddeeff',
|
|
14
|
+
'69c4e0d86a7b0430d8cdb78070b4c55a',
|
|
15
|
+
],
|
|
16
|
+
[
|
|
17
|
+
'AES-192',
|
|
18
|
+
'000102030405060708090a0b0c0d0e0f1011121314151617',
|
|
19
|
+
'00112233445566778899aabbccddeeff',
|
|
20
|
+
'dda97ca4864cdfe06eaf70a0ec0d7191',
|
|
21
|
+
],
|
|
22
|
+
[
|
|
23
|
+
'AES-256',
|
|
24
|
+
'000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f',
|
|
25
|
+
'00112233445566778899aabbccddeeff',
|
|
26
|
+
'8ea2b7ca516745bfeafc49904b496089',
|
|
27
|
+
],
|
|
28
|
+
])(
|
|
29
|
+
'encrypts and decrypts a raw %s block',
|
|
30
|
+
(_name, key, plaintext, ciphertext) => {
|
|
31
|
+
const [block, err] = NewCipher(hex(key))
|
|
32
|
+
expect(err).toBeNull()
|
|
33
|
+
|
|
34
|
+
const encrypted = new Uint8Array(BlockSize)
|
|
35
|
+
block!.Encrypt(encrypted, hex(plaintext))
|
|
36
|
+
expect(toHex(encrypted)).toBe(ciphertext)
|
|
37
|
+
|
|
38
|
+
const decrypted = new Uint8Array(BlockSize)
|
|
39
|
+
block!.Decrypt(decrypted, encrypted)
|
|
40
|
+
expect(toHex(decrypted)).toBe(plaintext)
|
|
41
|
+
},
|
|
42
|
+
)
|
|
43
|
+
|
|
44
|
+
test('seals and opens the NIST AES-256-GCM vector', async () => {
|
|
45
|
+
const [block, blockErr] = NewCipher(
|
|
46
|
+
hex('0000000000000000000000000000000000000000000000000000000000000000'),
|
|
47
|
+
)
|
|
48
|
+
expect(blockErr).toBeNull()
|
|
49
|
+
expect(block?.BlockSize()).toBe(BlockSize)
|
|
50
|
+
|
|
51
|
+
const [aead, aeadErr] = cipher.NewGCM(block)
|
|
52
|
+
expect(aeadErr).toBeNull()
|
|
53
|
+
expect(aead?.NonceSize()).toBe(12)
|
|
54
|
+
expect(aead?.Overhead()).toBe(16)
|
|
55
|
+
|
|
56
|
+
const sealed = await aead!.Seal(
|
|
57
|
+
null,
|
|
58
|
+
hex('000000000000000000000000'),
|
|
59
|
+
hex('00000000000000000000000000000000'),
|
|
60
|
+
null,
|
|
61
|
+
)
|
|
62
|
+
expect(toHex(sealed)).toBe(
|
|
63
|
+
'cea7403d4d606b6e074ec5d3baf39d18d0d1c8a799996bf0265b98b5d48ab919',
|
|
64
|
+
)
|
|
65
|
+
|
|
66
|
+
const [opened, openErr] = await aead!.Open(
|
|
67
|
+
$.stringToBytes('prefix:'),
|
|
68
|
+
hex('000000000000000000000000'),
|
|
69
|
+
sealed,
|
|
70
|
+
null,
|
|
71
|
+
)
|
|
72
|
+
expect(openErr).toBeNull()
|
|
73
|
+
expect(toHex(opened)).toBe(
|
|
74
|
+
toHex($.stringToBytes('prefix:')) + '00000000000000000000000000000000',
|
|
75
|
+
)
|
|
76
|
+
})
|
|
77
|
+
|
|
78
|
+
test('rejects tampered ciphertext', async () => {
|
|
79
|
+
const [block] = NewCipher(
|
|
80
|
+
hex('0000000000000000000000000000000000000000000000000000000000000000'),
|
|
81
|
+
)
|
|
82
|
+
const [aead] = cipher.NewGCM(block)
|
|
83
|
+
const sealed = await aead!.Seal(
|
|
84
|
+
null,
|
|
85
|
+
hex('000000000000000000000000'),
|
|
86
|
+
$.stringToBytes('hello'),
|
|
87
|
+
$.stringToBytes('aad'),
|
|
88
|
+
)
|
|
89
|
+
sealed![0] ^= 1
|
|
90
|
+
|
|
91
|
+
const [opened, openErr] = await aead!.Open(
|
|
92
|
+
null,
|
|
93
|
+
hex('000000000000000000000000'),
|
|
94
|
+
sealed,
|
|
95
|
+
$.stringToBytes('aad'),
|
|
96
|
+
)
|
|
97
|
+
expect(opened).toBeNull()
|
|
98
|
+
expect(openErr?.Error()).toBe('cipher: message authentication failed')
|
|
99
|
+
})
|
|
100
|
+
|
|
101
|
+
test('rejects invalid key lengths', () => {
|
|
102
|
+
const [block, err] = NewCipher(new Uint8Array(31))
|
|
103
|
+
expect(block).toBeNull()
|
|
104
|
+
expect(err?.Error()).toBe(KeySizeError_Error(31))
|
|
105
|
+
})
|
|
106
|
+
})
|
|
107
|
+
|
|
108
|
+
function hex(input: string): Uint8Array {
|
|
109
|
+
const out = new Uint8Array(input.length / 2)
|
|
110
|
+
for (let idx = 0; idx < out.length; idx++) {
|
|
111
|
+
out[idx] = Number.parseInt(input.slice(idx * 2, idx * 2 + 2), 16)
|
|
112
|
+
}
|
|
113
|
+
return out
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
function toHex(input: Uint8Array | number[] | null): string {
|
|
117
|
+
return Array.from(input ?? [])
|
|
118
|
+
.map((value) => value.toString(16).padStart(2, '0'))
|
|
119
|
+
.join('')
|
|
120
|
+
}
|