goscript 0.1.1 → 0.1.3
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/cmd-test.go +104 -11
- package/cmd/goscript/cmd-test_test.go +1 -1
- package/cmd/goscript/cmd_compile.go +9 -0
- package/compiler/compile-request.go +31 -0
- package/compiler/compiler.go +1 -1
- package/compiler/compliance_test.go +0 -2
- package/compiler/config.go +2 -0
- package/compiler/gotest/package-result.go +2 -0
- package/compiler/gotest/request.go +85 -20
- package/compiler/gotest/runner.go +733 -96
- package/compiler/gotest/runner_test.go +647 -3
- package/compiler/lowered-program.go +10 -2
- package/compiler/lowering.go +2676 -349
- package/compiler/override-facts.go +77 -27
- package/compiler/override-registry.go +5 -4
- package/compiler/override-registry_test.go +178 -0
- package/compiler/package-graph_test.go +62 -7
- package/compiler/package-test-graph-variant.go +40 -16
- package/compiler/package-test-graph.go +0 -5
- package/compiler/package-test-graph_test.go +61 -3
- package/compiler/runtime-contract.go +40 -0
- package/compiler/semantic-model-types.go +16 -0
- package/compiler/semantic-model.go +336 -91
- package/compiler/semantic-model_test.go +50 -1
- package/compiler/service.go +9 -3
- package/compiler/skeleton_test.go +2371 -296
- package/compiler/tsworkspace/owner-process-unix_test.go +72 -0
- package/compiler/tsworkspace/owner.go +8 -0
- package/compiler/tsworkspace/tool-process-other.go +14 -0
- package/compiler/tsworkspace/tool-process-unix.go +19 -0
- package/compiler/typescript-emitter.go +149 -10
- package/dist/gs/builtin/builtin.d.ts +20 -1
- package/dist/gs/builtin/builtin.js +246 -26
- package/dist/gs/builtin/builtin.js.map +1 -1
- package/dist/gs/builtin/channel.d.ts +24 -10
- package/dist/gs/builtin/channel.js +143 -34
- package/dist/gs/builtin/channel.js.map +1 -1
- package/dist/gs/builtin/defer.d.ts +1 -0
- package/dist/gs/builtin/defer.js +12 -2
- package/dist/gs/builtin/defer.js.map +1 -1
- package/dist/gs/builtin/hostio.d.ts +9 -0
- package/dist/gs/builtin/hostio.js +25 -0
- package/dist/gs/builtin/hostio.js.map +1 -1
- package/dist/gs/builtin/map.js +40 -6
- package/dist/gs/builtin/map.js.map +1 -1
- package/dist/gs/builtin/print.js.map +1 -1
- package/dist/gs/builtin/slice.d.ts +43 -9
- package/dist/gs/builtin/slice.js +437 -234
- package/dist/gs/builtin/slice.js.map +1 -1
- package/dist/gs/builtin/type.d.ts +2 -0
- package/dist/gs/builtin/type.js +55 -10
- package/dist/gs/builtin/type.js.map +1 -1
- package/dist/gs/builtin/varRef.d.ts +2 -0
- package/dist/gs/builtin/varRef.js.map +1 -1
- package/dist/gs/bytes/buffer.gs.js +28 -28
- package/dist/gs/bytes/buffer.gs.js.map +1 -1
- package/dist/gs/bytes/bytes.gs.d.ts +7 -5
- package/dist/gs/bytes/bytes.gs.js +10 -4
- package/dist/gs/bytes/bytes.gs.js.map +1 -1
- package/dist/gs/bytes/iter.gs.js +13 -13
- package/dist/gs/bytes/iter.gs.js.map +1 -1
- package/dist/gs/compress/zlib/index.d.ts +26 -0
- package/dist/gs/compress/zlib/index.js +168 -0
- package/dist/gs/compress/zlib/index.js.map +1 -0
- package/dist/gs/context/context.d.ts +1 -1
- package/dist/gs/context/context.js +8 -3
- package/dist/gs/context/context.js.map +1 -1
- package/dist/gs/crypto/ecdh/index.d.ts +52 -0
- package/dist/gs/crypto/ecdh/index.js +226 -0
- package/dist/gs/crypto/ecdh/index.js.map +1 -0
- package/dist/gs/crypto/ed25519/index.d.ts +34 -0
- package/dist/gs/crypto/ed25519/index.js +160 -0
- package/dist/gs/crypto/ed25519/index.js.map +1 -0
- package/dist/gs/crypto/internal/constanttime/index.d.ts +4 -0
- package/dist/gs/crypto/internal/constanttime/index.js +18 -0
- package/dist/gs/crypto/internal/constanttime/index.js.map +1 -0
- package/dist/gs/crypto/rand/index.d.ts +2 -0
- package/dist/gs/crypto/rand/index.js +85 -0
- package/dist/gs/crypto/rand/index.js.map +1 -1
- package/dist/gs/crypto/sha1/index.d.ts +5 -0
- package/dist/gs/crypto/sha1/index.js +106 -0
- package/dist/gs/crypto/sha1/index.js.map +1 -0
- package/dist/gs/crypto/sha256/index.d.ts +8 -0
- package/dist/gs/crypto/sha256/index.js +118 -0
- package/dist/gs/crypto/sha256/index.js.map +1 -0
- package/dist/gs/crypto/sha512/index.d.ts +14 -0
- package/dist/gs/crypto/sha512/index.js +129 -0
- package/dist/gs/crypto/sha512/index.js.map +1 -0
- package/dist/gs/encoding/json/index.d.ts +3 -0
- package/dist/gs/encoding/json/index.js +15 -0
- package/dist/gs/encoding/json/index.js.map +1 -1
- package/dist/gs/errors/errors.js +29 -6
- package/dist/gs/errors/errors.js.map +1 -1
- package/dist/gs/fmt/fmt.d.ts +1 -1
- package/dist/gs/fmt/fmt.js +64 -3
- package/dist/gs/fmt/fmt.js.map +1 -1
- package/dist/gs/github.com/aperturerobotics/protobuf-go-lite/index.d.ts +7 -7
- package/dist/gs/github.com/aperturerobotics/protobuf-go-lite/index.js +52 -18
- 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.js +56 -20
- package/dist/gs/github.com/aperturerobotics/protobuf-go-lite/json/index.js.map +1 -1
- package/dist/gs/github.com/aperturerobotics/starpc/srpc/index.d.ts +57 -3
- package/dist/gs/github.com/aperturerobotics/starpc/srpc/index.js +366 -1
- package/dist/gs/github.com/aperturerobotics/starpc/srpc/index.js.map +1 -1
- package/dist/gs/github.com/aperturerobotics/util/conc/index.d.ts +20 -0
- package/dist/gs/github.com/aperturerobotics/util/conc/index.js +134 -0
- package/dist/gs/github.com/aperturerobotics/util/conc/index.js.map +1 -0
- package/dist/gs/github.com/aperturerobotics/wasivm/wazero/kernel/runtime/browser/browser.js.map +1 -1
- package/dist/gs/github.com/hack-pad/safejs/internal/catch/index.d.ts +3 -0
- package/dist/gs/github.com/hack-pad/safejs/internal/catch/index.js +50 -0
- package/dist/gs/github.com/hack-pad/safejs/internal/catch/index.js.map +1 -0
- package/dist/gs/github.com/klauspost/compress/internal/le/index.js +3 -2
- package/dist/gs/github.com/klauspost/compress/internal/le/index.js.map +1 -1
- package/dist/gs/github.com/mr-tron/base58/base58/index.d.ts +27 -0
- package/dist/gs/github.com/mr-tron/base58/base58/index.js +172 -0
- package/dist/gs/github.com/mr-tron/base58/base58/index.js.map +1 -0
- package/dist/gs/github.com/zeebo/blake3/internal/consts/index.d.ts +21 -0
- package/dist/gs/github.com/zeebo/blake3/internal/consts/index.js +22 -0
- package/dist/gs/github.com/zeebo/blake3/internal/consts/index.js.map +1 -0
- package/dist/gs/go/token/index.js +11 -4
- package/dist/gs/go/token/index.js.map +1 -1
- package/dist/gs/hash/fnv/index.d.ts +57 -0
- package/dist/gs/hash/fnv/index.js +299 -0
- package/dist/gs/hash/fnv/index.js.map +1 -0
- package/dist/gs/hash/index.d.ts +17 -0
- package/dist/gs/hash/index.js +94 -0
- package/dist/gs/hash/index.js.map +1 -0
- package/dist/gs/io/fs/readlink.js +2 -6
- package/dist/gs/io/fs/readlink.js.map +1 -1
- package/dist/gs/io/fs/walk.js.map +1 -1
- package/dist/gs/io/io.d.ts +8 -5
- package/dist/gs/io/io.js +20 -2
- package/dist/gs/io/io.js.map +1 -1
- package/dist/gs/iter/iter.d.ts +3 -2
- package/dist/gs/iter/iter.js.map +1 -1
- package/dist/gs/maps/iter.d.ts +5 -5
- package/dist/gs/maps/iter.js +48 -21
- package/dist/gs/maps/iter.js.map +1 -1
- package/dist/gs/maps/maps.d.ts +6 -6
- package/dist/gs/math/bits/index.js +14 -24
- package/dist/gs/math/bits/index.js.map +1 -1
- package/dist/gs/mime/index.js +3 -1
- package/dist/gs/mime/index.js.map +1 -1
- package/dist/gs/net/http/httptest/index.d.ts +20 -1
- package/dist/gs/net/http/httptest/index.js +85 -3
- package/dist/gs/net/http/httptest/index.js.map +1 -1
- package/dist/gs/net/http/index.d.ts +118 -6
- package/dist/gs/net/http/index.js +389 -14
- package/dist/gs/net/http/index.js.map +1 -1
- package/dist/gs/net/http/pprof/index.d.ts +8 -0
- package/dist/gs/net/http/pprof/index.js +59 -0
- package/dist/gs/net/http/pprof/index.js.map +1 -0
- package/dist/gs/os/error.gs.js +9 -7
- package/dist/gs/os/error.gs.js.map +1 -1
- package/dist/gs/os/types_js.gs.js +95 -15
- package/dist/gs/os/types_js.gs.js.map +1 -1
- package/dist/gs/os/zero_copy_posix.gs.js +1 -1
- package/dist/gs/os/zero_copy_posix.gs.js.map +1 -1
- package/dist/gs/path/filepath/match.js.map +1 -1
- package/dist/gs/path/filepath/path.d.ts +5 -3
- package/dist/gs/path/filepath/path.js +65 -10
- package/dist/gs/path/filepath/path.js.map +1 -1
- package/dist/gs/reflect/index.d.ts +3 -2
- package/dist/gs/reflect/index.js +2 -1
- package/dist/gs/reflect/index.js.map +1 -1
- package/dist/gs/reflect/iter.js +2 -2
- package/dist/gs/reflect/iter.js.map +1 -1
- package/dist/gs/reflect/map.js +26 -0
- package/dist/gs/reflect/map.js.map +1 -1
- package/dist/gs/reflect/type.d.ts +24 -5
- package/dist/gs/reflect/type.js +390 -38
- package/dist/gs/reflect/type.js.map +1 -1
- package/dist/gs/reflect/types.d.ts +1 -0
- package/dist/gs/reflect/types.js +3 -1
- package/dist/gs/reflect/types.js.map +1 -1
- package/dist/gs/reflect/value.d.ts +4 -1
- package/dist/gs/reflect/value.js +39 -1
- package/dist/gs/reflect/value.js.map +1 -1
- package/dist/gs/reflect/visiblefields.js +1 -1
- package/dist/gs/reflect/visiblefields.js.map +1 -1
- package/dist/gs/runtime/debug/index.d.ts +39 -0
- package/dist/gs/runtime/debug/index.js +58 -0
- package/dist/gs/runtime/debug/index.js.map +1 -1
- package/dist/gs/runtime/pprof/index.d.ts +20 -0
- package/dist/gs/runtime/pprof/index.js +85 -0
- package/dist/gs/runtime/pprof/index.js.map +1 -0
- package/dist/gs/runtime/trace/index.d.ts +19 -0
- package/dist/gs/runtime/trace/index.js +64 -0
- package/dist/gs/runtime/trace/index.js.map +1 -0
- package/dist/gs/slices/slices.d.ts +24 -9
- package/dist/gs/slices/slices.js +229 -24
- package/dist/gs/slices/slices.js.map +1 -1
- package/dist/gs/sort/slice.gs.d.ts +5 -3
- package/dist/gs/sort/slice.gs.js +55 -17
- package/dist/gs/sort/slice.gs.js.map +1 -1
- package/dist/gs/strings/builder.js +26 -17
- package/dist/gs/strings/builder.js.map +1 -1
- package/dist/gs/strings/iter.js +140 -75
- package/dist/gs/strings/iter.js.map +1 -1
- package/dist/gs/strings/replace.js +2 -2
- package/dist/gs/strings/replace.js.map +1 -1
- package/dist/gs/strings/strings.js +52 -6
- package/dist/gs/strings/strings.js.map +1 -1
- package/dist/gs/sync/sync.d.ts +6 -3
- package/dist/gs/sync/sync.js +39 -11
- package/dist/gs/sync/sync.js.map +1 -1
- package/dist/gs/syscall/errors.d.ts +116 -112
- package/dist/gs/syscall/errors.js +38 -1
- package/dist/gs/syscall/errors.js.map +1 -1
- package/dist/gs/syscall/fs.d.ts +2 -8
- package/dist/gs/syscall/fs.js.map +1 -1
- package/dist/gs/syscall/js/index.js +20 -12
- package/dist/gs/syscall/js/index.js.map +1 -1
- package/dist/gs/syscall/types.d.ts +4 -1
- package/dist/gs/syscall/types.js.map +1 -1
- package/dist/gs/testing/testing.d.ts +4 -3
- package/dist/gs/testing/testing.js +21 -4
- package/dist/gs/testing/testing.js.map +1 -1
- package/dist/gs/time/time.js +22 -0
- package/dist/gs/time/time.js.map +1 -1
- package/dist/gs/unicode/unicode.js.map +1 -1
- package/dist/gs/unique/index.js +7 -2
- package/dist/gs/unique/index.js.map +1 -1
- package/go.mod +8 -8
- package/go.sum +14 -23
- package/gs/builtin/builtin.ts +364 -37
- package/gs/builtin/channel.ts +208 -38
- package/gs/builtin/defer.ts +13 -2
- package/gs/builtin/hostio.test.ts +1 -0
- package/gs/builtin/hostio.ts +38 -0
- package/gs/builtin/map.ts +46 -6
- package/gs/builtin/print.ts +12 -3
- package/gs/builtin/runtime-contract.test.ts +290 -10
- package/gs/builtin/slice.test.ts +70 -0
- package/gs/builtin/slice.ts +566 -255
- package/gs/builtin/type.ts +63 -10
- package/gs/builtin/varRef.ts +2 -0
- package/gs/bytes/buffer.gs.ts +28 -28
- package/gs/bytes/bytes.gs.ts +19 -10
- package/gs/bytes/bytes.test.ts +17 -0
- package/gs/bytes/iter.gs.ts +13 -14
- package/gs/compress/zlib/index.test.ts +28 -0
- package/gs/compress/zlib/index.ts +200 -0
- package/gs/compress/zlib/meta.json +3 -0
- package/gs/context/context.test.ts +36 -2
- package/gs/context/context.ts +9 -4
- package/gs/crypto/ecdh/index.test.ts +43 -0
- package/gs/crypto/ecdh/index.ts +274 -0
- package/gs/crypto/ed25519/index.test.ts +41 -0
- package/gs/crypto/ed25519/index.ts +238 -0
- package/gs/crypto/ed25519/meta.json +13 -0
- package/gs/crypto/internal/constanttime/index.test.ts +25 -0
- package/gs/crypto/internal/constanttime/index.ts +22 -0
- package/gs/crypto/rand/index.test.ts +89 -1
- package/gs/crypto/rand/index.ts +103 -1
- package/gs/crypto/rand/meta.json +4 -1
- package/gs/crypto/sha1/index.test.ts +28 -0
- package/gs/crypto/sha1/index.ts +130 -0
- package/gs/crypto/sha1/meta.json +8 -0
- package/gs/crypto/sha256/index.test.ts +78 -0
- package/gs/crypto/sha256/index.ts +150 -0
- package/gs/crypto/sha256/meta.json +9 -0
- package/gs/crypto/sha512/index.test.ts +31 -0
- package/gs/crypto/sha512/index.ts +161 -0
- package/gs/crypto/sha512/meta.json +11 -0
- package/gs/encoding/json/index.test.ts +25 -3
- package/gs/encoding/json/index.ts +21 -3
- package/gs/errors/errors.test.ts +4 -1
- package/gs/errors/errors.ts +32 -8
- package/gs/fmt/fmt.test.ts +23 -1
- package/gs/fmt/fmt.ts +76 -10
- package/gs/github.com/aperturerobotics/protobuf-go-lite/index.test.ts +62 -7
- package/gs/github.com/aperturerobotics/protobuf-go-lite/index.ts +78 -36
- package/gs/github.com/aperturerobotics/protobuf-go-lite/json/index.test.ts +32 -11
- package/gs/github.com/aperturerobotics/protobuf-go-lite/json/index.ts +122 -43
- package/gs/github.com/aperturerobotics/starpc/srpc/index.test.ts +31 -0
- package/gs/github.com/aperturerobotics/starpc/srpc/index.ts +518 -4
- package/gs/github.com/aperturerobotics/starpc/srpc/meta.json +6 -0
- package/gs/github.com/aperturerobotics/util/conc/index.test.ts +30 -0
- package/gs/github.com/aperturerobotics/util/conc/index.ts +172 -0
- package/gs/github.com/aperturerobotics/util/conc/meta.json +9 -0
- package/gs/github.com/aperturerobotics/wasivm/wazero/kernel/runtime/browser/browser.ts +1 -4
- package/gs/github.com/hack-pad/safejs/internal/catch/index.test.ts +35 -0
- package/gs/github.com/hack-pad/safejs/internal/catch/index.ts +65 -0
- package/gs/github.com/hack-pad/safejs/internal/catch/meta.json +9 -0
- package/gs/github.com/klauspost/compress/internal/le/index.test.ts +2 -1
- package/gs/github.com/klauspost/compress/internal/le/index.ts +6 -5
- package/gs/github.com/mr-tron/base58/base58/index.test.ts +70 -0
- package/gs/github.com/mr-tron/base58/base58/index.ts +231 -0
- package/gs/github.com/mr-tron/base58/base58/meta.json +3 -0
- package/gs/github.com/zeebo/blake3/internal/consts/index.test.ts +46 -0
- package/gs/github.com/zeebo/blake3/internal/consts/index.ts +26 -0
- package/gs/go/token/index.ts +17 -4
- package/gs/hash/fnv/index.test.ts +67 -0
- package/gs/hash/fnv/index.ts +351 -0
- package/gs/hash/fnv/meta.json +3 -0
- package/gs/hash/index.test.ts +37 -0
- package/gs/hash/index.ts +118 -0
- package/gs/hash/meta.json +5 -0
- package/gs/internal/byteorder/index.test.ts +6 -6
- package/gs/io/fs/readlink.ts +40 -48
- package/gs/io/fs/walk.ts +10 -2
- package/gs/io/io.test.ts +64 -0
- package/gs/io/io.ts +34 -13
- package/gs/iter/iter.ts +8 -2
- package/gs/maps/iter.ts +69 -26
- package/gs/maps/maps.test.ts +23 -0
- package/gs/maps/maps.ts +6 -6
- package/gs/math/bits/index.test.ts +20 -0
- package/gs/math/bits/index.ts +15 -28
- package/gs/mime/index.ts +8 -2
- package/gs/net/http/httptest/index.test.ts +85 -0
- package/gs/net/http/httptest/index.ts +113 -3
- package/gs/net/http/index.test.ts +159 -1
- package/gs/net/http/index.ts +515 -15
- package/gs/net/http/meta.json +6 -0
- package/gs/net/http/pprof/index.test.ts +47 -0
- package/gs/net/http/pprof/index.ts +65 -0
- package/gs/os/error.gs.ts +9 -10
- package/gs/os/error.test.ts +41 -0
- package/gs/os/file_unix_js.test.ts +55 -0
- package/gs/os/tempfile.gs.test.ts +37 -10
- package/gs/os/types_js.gs.ts +94 -15
- package/gs/os/zero_copy_posix.gs.ts +1 -2
- package/gs/path/filepath/match.ts +4 -1
- package/gs/path/filepath/meta.json +6 -0
- package/gs/path/filepath/path.test.ts +57 -2
- package/gs/path/filepath/path.ts +91 -12
- package/gs/reflect/field.test.ts +63 -0
- package/gs/reflect/index.ts +4 -1
- package/gs/reflect/iter.ts +2 -2
- package/gs/reflect/map.test.ts +24 -2
- package/gs/reflect/map.ts +35 -0
- package/gs/reflect/type.ts +543 -60
- package/gs/reflect/typefor.test.ts +100 -0
- package/gs/reflect/types.ts +3 -1
- package/gs/reflect/value.ts +50 -1
- package/gs/reflect/visiblefields.ts +1 -1
- package/gs/runtime/debug/index.test.ts +22 -1
- package/gs/runtime/debug/index.ts +88 -0
- package/gs/runtime/pprof/index.test.ts +36 -0
- package/gs/runtime/pprof/index.ts +104 -0
- package/gs/runtime/pprof/meta.json +6 -0
- package/gs/runtime/trace/index.test.ts +45 -0
- package/gs/runtime/trace/index.ts +97 -0
- package/gs/runtime/trace/meta.json +7 -0
- package/gs/slices/meta.json +2 -1
- package/gs/slices/slices.test.ts +86 -0
- package/gs/slices/slices.ts +284 -37
- package/gs/sort/slice.gs.ts +73 -23
- package/gs/sort/slice.test.ts +40 -0
- package/gs/strings/builder.test.ts +8 -0
- package/gs/strings/builder.ts +29 -17
- package/gs/strings/iter.test.ts +5 -7
- package/gs/strings/iter.ts +146 -71
- package/gs/strings/replace.test.ts +1 -4
- package/gs/strings/replace.ts +6 -6
- package/gs/strings/strings.test.ts +4 -0
- package/gs/strings/strings.ts +54 -6
- package/gs/sync/meta.json +1 -0
- package/gs/sync/sync.test.ts +57 -1
- package/gs/sync/sync.ts +45 -13
- package/gs/syscall/errors.ts +158 -115
- package/gs/syscall/fs.ts +8 -8
- package/gs/syscall/js/index.ts +49 -22
- package/gs/syscall/net.test.ts +26 -0
- package/gs/syscall/types.ts +7 -2
- package/gs/testing/testing.test.ts +56 -0
- package/gs/testing/testing.ts +27 -10
- package/gs/time/meta.json +2 -2
- package/gs/time/time.test.ts +4 -0
- package/gs/time/time.ts +33 -2
- package/gs/unicode/unicode.test.ts +14 -3
- package/gs/unicode/unicode.ts +1 -5
- package/gs/unique/index.ts +9 -2
- package/package.json +3 -3
package/gs/builtin/type.ts
CHANGED
|
@@ -18,6 +18,7 @@ export enum TypeKind {
|
|
|
18
18
|
*/
|
|
19
19
|
export interface BaseTypeInfo {
|
|
20
20
|
name?: string
|
|
21
|
+
typeName?: string
|
|
21
22
|
kind: TypeKind
|
|
22
23
|
zeroValue?: any
|
|
23
24
|
}
|
|
@@ -204,6 +205,19 @@ export interface Comparable {
|
|
|
204
205
|
|
|
205
206
|
// Registry to store runtime type information
|
|
206
207
|
const typeRegistry = new Map<string, TypeInfo>()
|
|
208
|
+
const duplicateTypeRegistry = new Map<string, TypeInfo[]>()
|
|
209
|
+
|
|
210
|
+
function registerTypeInfo(name: string, typeInfo: TypeInfo): void {
|
|
211
|
+
const existing = typeRegistry.get(name)
|
|
212
|
+
if (existing && existing !== typeInfo) {
|
|
213
|
+
const candidates = duplicateTypeRegistry.get(name) ?? [existing]
|
|
214
|
+
if (!candidates.includes(typeInfo)) {
|
|
215
|
+
candidates.push(typeInfo)
|
|
216
|
+
}
|
|
217
|
+
duplicateTypeRegistry.set(name, candidates)
|
|
218
|
+
}
|
|
219
|
+
typeRegistry.set(name, typeInfo)
|
|
220
|
+
}
|
|
207
221
|
|
|
208
222
|
/**
|
|
209
223
|
* Registers a struct type with the runtime type system.
|
|
@@ -230,7 +244,7 @@ export const registerStructType = (
|
|
|
230
244
|
ctor,
|
|
231
245
|
fields,
|
|
232
246
|
}
|
|
233
|
-
|
|
247
|
+
registerTypeInfo(name, typeInfo)
|
|
234
248
|
return typeInfo
|
|
235
249
|
}
|
|
236
250
|
|
|
@@ -260,7 +274,7 @@ export const registerInterfaceType = (
|
|
|
260
274
|
zeroValue,
|
|
261
275
|
methods,
|
|
262
276
|
}
|
|
263
|
-
|
|
277
|
+
registerTypeInfo(name, typeInfo)
|
|
264
278
|
return typeInfo
|
|
265
279
|
}
|
|
266
280
|
|
|
@@ -301,6 +315,14 @@ function normalizeTypeInfo(info: string | TypeInfo): TypeInfo {
|
|
|
301
315
|
return info
|
|
302
316
|
}
|
|
303
317
|
|
|
318
|
+
function typeInfoRuntimeName(info: TypeInfo): string | undefined {
|
|
319
|
+
return info.typeName || info.name
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
function goTypeMatchesTypeInfo(goType: string, info: TypeInfo): boolean {
|
|
323
|
+
return goType === typeInfoRuntimeName(info)
|
|
324
|
+
}
|
|
325
|
+
|
|
304
326
|
function compareOptionalTypeInfo(
|
|
305
327
|
type1?: string | TypeInfo,
|
|
306
328
|
type2?: string | TypeInfo,
|
|
@@ -530,17 +552,28 @@ function matchesStructType(value: any, info: TypeInfo): boolean {
|
|
|
530
552
|
*/
|
|
531
553
|
function matchesInterfaceType(value: any, info: TypeInfo): boolean {
|
|
532
554
|
// Check basic conditions first
|
|
555
|
+
if (!isInterfaceTypeInfo(info) || value === null || value === undefined) {
|
|
556
|
+
return false
|
|
557
|
+
}
|
|
558
|
+
if (info.methods.length === 0) {
|
|
559
|
+
return true
|
|
560
|
+
}
|
|
561
|
+
if (typeof value !== 'object') {
|
|
562
|
+
return false
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
const methodTarget =
|
|
566
|
+
value.__isVarRef === true && 'value' in value ? value.value : value
|
|
533
567
|
if (
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
value === null
|
|
568
|
+
(typeof methodTarget !== 'object' && typeof methodTarget !== 'function') ||
|
|
569
|
+
methodTarget === null
|
|
537
570
|
) {
|
|
538
571
|
return false
|
|
539
572
|
}
|
|
540
573
|
|
|
541
574
|
// For interfaces, check if the value has all the required methods with compatible signatures
|
|
542
575
|
return info.methods.every((requiredMethodSig) => {
|
|
543
|
-
const actualMethod = (
|
|
576
|
+
const actualMethod = (methodTarget as any)[requiredMethodSig.name]
|
|
544
577
|
|
|
545
578
|
// Method must exist and be a function
|
|
546
579
|
if (typeof actualMethod !== 'function') {
|
|
@@ -563,8 +596,8 @@ function matchesInterfaceType(value: any, info: TypeInfo): boolean {
|
|
|
563
596
|
|
|
564
597
|
// If the value has a __goTypeName property, it might be a registered type
|
|
565
598
|
// with more type information available
|
|
566
|
-
if (
|
|
567
|
-
const valueTypeInfo = typeRegistry.get(
|
|
599
|
+
if (methodTarget.__goTypeName) {
|
|
600
|
+
const valueTypeInfo = typeRegistry.get(methodTarget.__goTypeName)
|
|
568
601
|
if (valueTypeInfo && isStructTypeInfo(valueTypeInfo)) {
|
|
569
602
|
// Find the matching method in the value's type info
|
|
570
603
|
const valueMethodSig = valueTypeInfo.methods.find(
|
|
@@ -740,6 +773,18 @@ export function cloneStructValue<T>(value: T): T {
|
|
|
740
773
|
throw new Error('runtime error: value is not cloneable')
|
|
741
774
|
}
|
|
742
775
|
|
|
776
|
+
export function cloneArrayValue<T>(value: T): T {
|
|
777
|
+
if (value instanceof Uint8Array) {
|
|
778
|
+
const out = new Uint8Array(value.length)
|
|
779
|
+
out.set(value)
|
|
780
|
+
return out as T
|
|
781
|
+
}
|
|
782
|
+
if (Array.isArray(value)) {
|
|
783
|
+
return value.map((item) => cloneArrayValue(item)) as T
|
|
784
|
+
}
|
|
785
|
+
return value
|
|
786
|
+
}
|
|
787
|
+
|
|
743
788
|
// Check if a struct instance is marked as a value
|
|
744
789
|
function isMarkedAsStructValue(value: any): boolean {
|
|
745
790
|
return (
|
|
@@ -900,7 +945,7 @@ function matchesType(value: any, info: TypeInfo): boolean {
|
|
|
900
945
|
if (
|
|
901
946
|
typeof value === 'object' &&
|
|
902
947
|
typeof value.__goType === 'string' &&
|
|
903
|
-
value.__goType
|
|
948
|
+
goTypeMatchesTypeInfo(value.__goType, info)
|
|
904
949
|
) {
|
|
905
950
|
return true
|
|
906
951
|
}
|
|
@@ -1086,7 +1131,7 @@ export function typeAssert<T>(
|
|
|
1086
1131
|
typeof value === 'object' &&
|
|
1087
1132
|
value !== null &&
|
|
1088
1133
|
typeof value.__goType === 'string' &&
|
|
1089
|
-
value.__goType
|
|
1134
|
+
goTypeMatchesTypeInfo(value.__goType, normalizedType)
|
|
1090
1135
|
) {
|
|
1091
1136
|
if ('__goValue' in value) {
|
|
1092
1137
|
return { value: value.__goValue as T, ok: true }
|
|
@@ -1147,6 +1192,14 @@ export function typeAssert<T>(
|
|
|
1147
1192
|
return { value: value as T, ok: true }
|
|
1148
1193
|
}
|
|
1149
1194
|
|
|
1195
|
+
if (typeof typeInfo === 'string') {
|
|
1196
|
+
for (const candidate of duplicateTypeRegistry.get(typeInfo) ?? []) {
|
|
1197
|
+
if (candidate !== normalizedType && matchesType(value, candidate)) {
|
|
1198
|
+
return { value: value as T, ok: true }
|
|
1199
|
+
}
|
|
1200
|
+
}
|
|
1201
|
+
}
|
|
1202
|
+
|
|
1150
1203
|
// If we get here, the assertion failed
|
|
1151
1204
|
// For registered types, use the zero value from the registry
|
|
1152
1205
|
if (typeof typeInfo === 'string') {
|
package/gs/builtin/varRef.ts
CHANGED
package/gs/bytes/buffer.gs.ts
CHANGED
|
@@ -66,7 +66,7 @@ export class Buffer {
|
|
|
66
66
|
// The slice aliases the buffer content at least until the next buffer modification,
|
|
67
67
|
// so immediate changes to the slice will affect the result of future reads.
|
|
68
68
|
public Bytes(): $.Bytes {
|
|
69
|
-
const b = this
|
|
69
|
+
const b = $.pointerValue<Buffer>(this)
|
|
70
70
|
return $.goSlice(b.buf, b.off, undefined)
|
|
71
71
|
}
|
|
72
72
|
|
|
@@ -75,7 +75,7 @@ export class Buffer {
|
|
|
75
75
|
// passed to an immediately succeeding [Buffer.Write] call.
|
|
76
76
|
// The buffer is only valid until the next write operation on b.
|
|
77
77
|
public AvailableBuffer(): $.Bytes {
|
|
78
|
-
const b = this
|
|
78
|
+
const b = $.pointerValue<Buffer>(this)
|
|
79
79
|
return $.goSlice(b.buf, $.len(b.buf), undefined)
|
|
80
80
|
}
|
|
81
81
|
|
|
@@ -84,7 +84,7 @@ export class Buffer {
|
|
|
84
84
|
//
|
|
85
85
|
// To build strings more efficiently, see the [strings.Builder] type.
|
|
86
86
|
public String(): string {
|
|
87
|
-
const b = this
|
|
87
|
+
const b = $.pointerValueOrNil<Buffer>(this)
|
|
88
88
|
if (b == null) {
|
|
89
89
|
// Special case, useful in debugging.
|
|
90
90
|
return "<nil>"
|
|
@@ -97,27 +97,27 @@ export class Buffer {
|
|
|
97
97
|
|
|
98
98
|
// empty reports whether the unread portion of the buffer is empty.
|
|
99
99
|
public empty(): boolean {
|
|
100
|
-
const b = this
|
|
100
|
+
const b = $.pointerValue<Buffer>(this)
|
|
101
101
|
return $.len(b.buf) <= b.off
|
|
102
102
|
}
|
|
103
103
|
|
|
104
104
|
// Len returns the number of bytes of the unread portion of the buffer;
|
|
105
105
|
// b.Len() == len(b.Bytes()).
|
|
106
106
|
public Len(): number {
|
|
107
|
-
const b = this
|
|
107
|
+
const b = $.pointerValue<Buffer>(this)
|
|
108
108
|
return $.len(b.buf) - b.off
|
|
109
109
|
}
|
|
110
110
|
|
|
111
111
|
// Cap returns the capacity of the buffer's underlying byte slice, that is, the
|
|
112
112
|
// total space allocated for the buffer's data.
|
|
113
113
|
public Cap(): number {
|
|
114
|
-
const b = this
|
|
114
|
+
const b = $.pointerValue<Buffer>(this)
|
|
115
115
|
return $.cap(b.buf)
|
|
116
116
|
}
|
|
117
117
|
|
|
118
118
|
// Available returns how many bytes are unused in the buffer.
|
|
119
119
|
public Available(): number {
|
|
120
|
-
const b = this
|
|
120
|
+
const b = $.pointerValue<Buffer>(this)
|
|
121
121
|
return $.cap(b.buf) - $.len(b.buf)
|
|
122
122
|
}
|
|
123
123
|
|
|
@@ -125,7 +125,7 @@ export class Buffer {
|
|
|
125
125
|
// but continues to use the same allocated storage.
|
|
126
126
|
// It panics if n is negative or greater than the length of the buffer.
|
|
127
127
|
public Truncate(n: number): void {
|
|
128
|
-
const b = this
|
|
128
|
+
const b = $.pointerValue<Buffer>(this)
|
|
129
129
|
if (n == 0) {
|
|
130
130
|
b.Reset()
|
|
131
131
|
return
|
|
@@ -141,7 +141,7 @@ export class Buffer {
|
|
|
141
141
|
// but it retains the underlying storage for use by future writes.
|
|
142
142
|
// Reset is the same as [Buffer.Truncate](0).
|
|
143
143
|
public Reset(): void {
|
|
144
|
-
const b = this
|
|
144
|
+
const b = $.pointerValue<Buffer>(this)
|
|
145
145
|
b.buf = $.goSlice(b.buf, undefined, 0)
|
|
146
146
|
b.off = 0
|
|
147
147
|
b.lastRead = 0
|
|
@@ -151,7 +151,7 @@ export class Buffer {
|
|
|
151
151
|
// internal buffer only needs to be resliced.
|
|
152
152
|
// It returns the index where bytes should be written and whether it succeeded.
|
|
153
153
|
public tryGrowByReslice(n: number): [number, boolean] {
|
|
154
|
-
const b = this
|
|
154
|
+
const b = $.pointerValue<Buffer>(this)
|
|
155
155
|
if (b.buf === null) {
|
|
156
156
|
return [0, false]
|
|
157
157
|
}
|
|
@@ -169,7 +169,7 @@ export class Buffer {
|
|
|
169
169
|
// It returns the index where bytes should be written.
|
|
170
170
|
// If the buffer can't grow it will panic with ErrTooLarge.
|
|
171
171
|
public grow(n: number): number {
|
|
172
|
-
const b = this
|
|
172
|
+
const b = $.pointerValue<Buffer>(this)
|
|
173
173
|
let m = b.Len()
|
|
174
174
|
if (m == 0 && b.off != 0) {
|
|
175
175
|
b.Reset()
|
|
@@ -210,7 +210,7 @@ export class Buffer {
|
|
|
210
210
|
// If n is negative, Grow will panic.
|
|
211
211
|
// If the buffer can't grow it will panic with [ErrTooLarge].
|
|
212
212
|
public Grow(n: number): void {
|
|
213
|
-
const b = this
|
|
213
|
+
const b = $.pointerValue<Buffer>(this)
|
|
214
214
|
if (n < 0) {
|
|
215
215
|
$.panic("bytes.Buffer.Grow: negative count")
|
|
216
216
|
}
|
|
@@ -222,7 +222,7 @@ export class Buffer {
|
|
|
222
222
|
// needed. The return value n is the length of p; err is always nil. If the
|
|
223
223
|
// buffer becomes too large, Write will panic with [ErrTooLarge].
|
|
224
224
|
public Write(p: $.Bytes): [number, $.GoError] {
|
|
225
|
-
const b = this
|
|
225
|
+
const b = $.pointerValue<Buffer>(this)
|
|
226
226
|
b.lastRead = 0
|
|
227
227
|
let [m, ok] = b.tryGrowByReslice($.len(p))
|
|
228
228
|
if (!ok) {
|
|
@@ -248,7 +248,7 @@ export class Buffer {
|
|
|
248
248
|
// needed. The return value n is the length of s; err is always nil. If the
|
|
249
249
|
// buffer becomes too large, WriteString will panic with [ErrTooLarge].
|
|
250
250
|
public WriteString(s: string): [number, $.GoError] {
|
|
251
|
-
const b = this
|
|
251
|
+
const b = $.pointerValue<Buffer>(this)
|
|
252
252
|
b.lastRead = 0
|
|
253
253
|
let [m, ok] = b.tryGrowByReslice($.len(s))
|
|
254
254
|
if (!ok) {
|
|
@@ -270,7 +270,7 @@ export class Buffer {
|
|
|
270
270
|
// buffer becomes too large, ReadFrom will panic with [ErrTooLarge].
|
|
271
271
|
public ReadFrom(r: io.Reader): [number, $.GoError] {
|
|
272
272
|
return (async (): Promise<[number, $.GoError]> => {
|
|
273
|
-
const b = this
|
|
273
|
+
const b = $.pointerValue<Buffer>(this)
|
|
274
274
|
b.lastRead = 0
|
|
275
275
|
let n = 0
|
|
276
276
|
for (; ; ) {
|
|
@@ -301,7 +301,7 @@ export class Buffer {
|
|
|
301
301
|
// encountered during the write is also returned.
|
|
302
302
|
public WriteTo(w: io.Writer): [number, $.GoError] {
|
|
303
303
|
return (async (): Promise<[number, $.GoError]> => {
|
|
304
|
-
const b = this
|
|
304
|
+
const b = $.pointerValue<Buffer>(this)
|
|
305
305
|
b.lastRead = 0
|
|
306
306
|
let n = 0
|
|
307
307
|
{
|
|
@@ -333,7 +333,7 @@ export class Buffer {
|
|
|
333
333
|
// WriteByte. If the buffer becomes too large, WriteByte will panic with
|
|
334
334
|
// [ErrTooLarge].
|
|
335
335
|
public WriteByte(c: number): $.GoError {
|
|
336
|
-
const b = this
|
|
336
|
+
const b = $.pointerValue<Buffer>(this)
|
|
337
337
|
b.lastRead = 0
|
|
338
338
|
let [m, ok] = b.tryGrowByReslice(1)
|
|
339
339
|
if (!ok) {
|
|
@@ -348,7 +348,7 @@ export class Buffer {
|
|
|
348
348
|
// included to match [bufio.Writer]'s WriteRune. The buffer is grown as needed;
|
|
349
349
|
// if it becomes too large, WriteRune will panic with [ErrTooLarge].
|
|
350
350
|
public WriteRune(r: number): [number, $.GoError] {
|
|
351
|
-
const b = this
|
|
351
|
+
const b = $.pointerValue<Buffer>(this)
|
|
352
352
|
if ((r as number) < utf8.RuneSelf) {
|
|
353
353
|
b.WriteByte($.byte(r))
|
|
354
354
|
return [1, null]
|
|
@@ -367,7 +367,7 @@ export class Buffer {
|
|
|
367
367
|
// buffer has no data to return, err is [io.EOF] (unless len(p) is zero);
|
|
368
368
|
// otherwise it is nil.
|
|
369
369
|
public Read(p: $.Bytes): [number, $.GoError] {
|
|
370
|
-
const b = this
|
|
370
|
+
const b = $.pointerValue<Buffer>(this)
|
|
371
371
|
b.lastRead = 0
|
|
372
372
|
if (b.empty()) {
|
|
373
373
|
// Buffer is empty, reset to recover space.
|
|
@@ -390,7 +390,7 @@ export class Buffer {
|
|
|
390
390
|
// If there are fewer than n bytes in the buffer, Next returns the entire buffer.
|
|
391
391
|
// The slice is only valid until the next call to a read or write method.
|
|
392
392
|
public Next(n: number): $.Bytes {
|
|
393
|
-
const b = this
|
|
393
|
+
const b = $.pointerValue<Buffer>(this)
|
|
394
394
|
b.lastRead = 0
|
|
395
395
|
let m = b.Len()
|
|
396
396
|
if (n > m) {
|
|
@@ -407,7 +407,7 @@ export class Buffer {
|
|
|
407
407
|
// Peek returns the next n bytes without advancing the buffer.
|
|
408
408
|
// If fewer than n bytes are available, it returns the unread bytes and io.EOF.
|
|
409
409
|
public Peek(n: number): [$.Bytes, $.GoError] {
|
|
410
|
-
const b = this
|
|
410
|
+
const b = $.pointerValue<Buffer>(this)
|
|
411
411
|
if (n < 0) {
|
|
412
412
|
return [null, errors.New("bytes.Buffer.Peek: negative count")]
|
|
413
413
|
}
|
|
@@ -428,7 +428,7 @@ export class Buffer {
|
|
|
428
428
|
// ReadByte reads and returns the next byte from the buffer.
|
|
429
429
|
// If no byte is available, it returns error [io.EOF].
|
|
430
430
|
public ReadByte(): [number, $.GoError] {
|
|
431
|
-
const b = this
|
|
431
|
+
const b = $.pointerValue<Buffer>(this)
|
|
432
432
|
if (b.empty()) {
|
|
433
433
|
// Buffer is empty, reset to recover space.
|
|
434
434
|
b.Reset()
|
|
@@ -446,7 +446,7 @@ export class Buffer {
|
|
|
446
446
|
// If the bytes are an erroneous UTF-8 encoding, it
|
|
447
447
|
// consumes one byte and returns U+FFFD, 1.
|
|
448
448
|
public ReadRune(): [number, number, $.GoError] {
|
|
449
|
-
const b = this
|
|
449
|
+
const b = $.pointerValue<Buffer>(this)
|
|
450
450
|
if (b.empty()) {
|
|
451
451
|
// Buffer is empty, reset to recover space.
|
|
452
452
|
b.Reset()
|
|
@@ -472,7 +472,7 @@ export class Buffer {
|
|
|
472
472
|
// it is stricter than [Buffer.UnreadByte], which will unread the last byte
|
|
473
473
|
// from any read operation.)
|
|
474
474
|
public UnreadRune(): $.GoError {
|
|
475
|
-
const b = this
|
|
475
|
+
const b = $.pointerValue<Buffer>(this)
|
|
476
476
|
if (b.lastRead <= 0) {
|
|
477
477
|
return errors.New("bytes.Buffer: UnreadRune: previous operation was not a successful ReadRune")
|
|
478
478
|
}
|
|
@@ -488,7 +488,7 @@ export class Buffer {
|
|
|
488
488
|
// the last read, if the last read returned an error, or if the read read zero
|
|
489
489
|
// bytes, UnreadByte returns an error.
|
|
490
490
|
public UnreadByte(): $.GoError {
|
|
491
|
-
const b = this
|
|
491
|
+
const b = $.pointerValue<Buffer>(this)
|
|
492
492
|
if (b.lastRead == 0) {
|
|
493
493
|
return errUnreadByte
|
|
494
494
|
}
|
|
@@ -506,7 +506,7 @@ export class Buffer {
|
|
|
506
506
|
// ReadBytes returns err != nil if and only if the returned data does not end in
|
|
507
507
|
// delim.
|
|
508
508
|
public ReadBytes(delim: number): [$.Bytes, $.GoError] {
|
|
509
|
-
const b = this
|
|
509
|
+
const b = $.pointerValue<Buffer>(this)
|
|
510
510
|
let [slice, err] = b.readSlice(delim)
|
|
511
511
|
let line = $.append<number>(null, slice)
|
|
512
512
|
return [line, err]
|
|
@@ -514,7 +514,7 @@ export class Buffer {
|
|
|
514
514
|
|
|
515
515
|
// readSlice is like ReadBytes but returns a reference to internal buffer data.
|
|
516
516
|
public readSlice(delim: number): [$.Bytes, $.GoError] {
|
|
517
|
-
const b = this
|
|
517
|
+
const b = $.pointerValue<Buffer>(this)
|
|
518
518
|
let i = IndexByte($.goSlice(b.buf, b.off, undefined), delim)
|
|
519
519
|
let end = b.off + i + 1
|
|
520
520
|
let err: $.GoError = null
|
|
@@ -536,7 +536,7 @@ export class Buffer {
|
|
|
536
536
|
// ReadString returns err != nil if and only if the returned data does not end
|
|
537
537
|
// in delim.
|
|
538
538
|
public ReadString(delim: number): [string, $.GoError] {
|
|
539
|
-
const b = this
|
|
539
|
+
const b = $.pointerValue<Buffer>(this)
|
|
540
540
|
let slice: $.Bytes
|
|
541
541
|
let err: $.GoError
|
|
542
542
|
[slice, err] = b.readSlice(delim)
|
package/gs/bytes/bytes.gs.ts
CHANGED
|
@@ -8,6 +8,9 @@ import * as utf8 from "@goscript/unicode/utf8/index.js"
|
|
|
8
8
|
// for linkname
|
|
9
9
|
import * as _ from "@goscript/unsafe/index.js"
|
|
10
10
|
|
|
11
|
+
type SyncCallbackResult<T> = T | globalThis.Promise<T>
|
|
12
|
+
type PredicateCallback = ((r: number) => SyncCallbackResult<boolean>) | null
|
|
13
|
+
|
|
11
14
|
// Equal reports whether a and b
|
|
12
15
|
// are the same length and contain the same bytes.
|
|
13
16
|
// A nil argument is equivalent to an empty slice.
|
|
@@ -90,7 +93,7 @@ export function ContainsRune(b: $.Bytes, r: number): boolean {
|
|
|
90
93
|
}
|
|
91
94
|
|
|
92
95
|
// ContainsFunc reports whether any of the UTF-8-encoded code points r within b satisfy f(r).
|
|
93
|
-
export function ContainsFunc(b: $.Bytes, f:
|
|
96
|
+
export function ContainsFunc(b: $.Bytes, f: PredicateCallback): boolean {
|
|
94
97
|
return IndexFunc(b, f) >= 0
|
|
95
98
|
}
|
|
96
99
|
|
|
@@ -794,21 +797,21 @@ export function TrimSuffix(s: $.Bytes, suffix: $.Bytes): $.Bytes {
|
|
|
794
797
|
// IndexFunc interprets s as a sequence of UTF-8-encoded code points.
|
|
795
798
|
// It returns the byte index in s of the first Unicode
|
|
796
799
|
// code point satisfying f(c), or -1 if none do.
|
|
797
|
-
export function IndexFunc(s: $.Bytes, f:
|
|
800
|
+
export function IndexFunc(s: $.Bytes, f: PredicateCallback): number {
|
|
798
801
|
return indexFunc(s, f, true)
|
|
799
802
|
}
|
|
800
803
|
|
|
801
804
|
// LastIndexFunc interprets s as a sequence of UTF-8-encoded code points.
|
|
802
805
|
// It returns the byte index in s of the last Unicode
|
|
803
806
|
// code point satisfying f(c), or -1 if none do.
|
|
804
|
-
export function LastIndexFunc(s: $.Bytes, f:
|
|
807
|
+
export function LastIndexFunc(s: $.Bytes, f: PredicateCallback): number {
|
|
805
808
|
return lastIndexFunc(s, f, true)
|
|
806
809
|
}
|
|
807
810
|
|
|
808
811
|
// indexFunc is the same as IndexFunc except that if
|
|
809
812
|
// truth==false, the sense of the predicate function is
|
|
810
813
|
// inverted.
|
|
811
|
-
export function indexFunc(s: $.Bytes, f:
|
|
814
|
+
export function indexFunc(s: $.Bytes, f: PredicateCallback, truth: boolean): number {
|
|
812
815
|
if (s === null || f === null) {
|
|
813
816
|
return -1
|
|
814
817
|
}
|
|
@@ -817,12 +820,12 @@ export function indexFunc(s: $.Bytes, f: ((r: number) => boolean) | null, truth:
|
|
|
817
820
|
const [r, size] = utf8.DecodeRune($.goSlice(s, i, undefined))
|
|
818
821
|
if (size <= 0) {
|
|
819
822
|
// Invalid UTF-8
|
|
820
|
-
if (f(utf8.RuneError) === truth) {
|
|
823
|
+
if (syncBoolean(f(utf8.RuneError)) === truth) {
|
|
821
824
|
return i
|
|
822
825
|
}
|
|
823
826
|
i++
|
|
824
827
|
} else {
|
|
825
|
-
if (f(r) === truth) {
|
|
828
|
+
if (syncBoolean(f(r)) === truth) {
|
|
826
829
|
return i
|
|
827
830
|
}
|
|
828
831
|
i += size
|
|
@@ -835,7 +838,7 @@ export function indexFunc(s: $.Bytes, f: ((r: number) => boolean) | null, truth:
|
|
|
835
838
|
// lastIndexFunc is the same as LastIndexFunc except that if
|
|
836
839
|
// truth==false, the sense of the predicate function is
|
|
837
840
|
// inverted.
|
|
838
|
-
export function lastIndexFunc(s: $.Bytes, f:
|
|
841
|
+
export function lastIndexFunc(s: $.Bytes, f: PredicateCallback, truth: boolean): number {
|
|
839
842
|
if (s === null || f === null) {
|
|
840
843
|
return -1
|
|
841
844
|
}
|
|
@@ -846,12 +849,12 @@ export function lastIndexFunc(s: $.Bytes, f: ((r: number) => boolean) | null, tr
|
|
|
846
849
|
const [r, size] = utf8.DecodeRune($.goSlice(s, i, undefined))
|
|
847
850
|
if (size <= 0) {
|
|
848
851
|
// Invalid UTF-8
|
|
849
|
-
if (f(utf8.RuneError) === truth) {
|
|
852
|
+
if (syncBoolean(f(utf8.RuneError)) === truth) {
|
|
850
853
|
lastIndex = i
|
|
851
854
|
}
|
|
852
855
|
i++
|
|
853
856
|
} else {
|
|
854
|
-
if (f(r) === truth) {
|
|
857
|
+
if (syncBoolean(f(r)) === truth) {
|
|
855
858
|
lastIndex = i
|
|
856
859
|
}
|
|
857
860
|
i += size
|
|
@@ -861,6 +864,13 @@ export function lastIndexFunc(s: $.Bytes, f: ((r: number) => boolean) | null, tr
|
|
|
861
864
|
return lastIndex
|
|
862
865
|
}
|
|
863
866
|
|
|
867
|
+
function syncBoolean(value: SyncCallbackResult<boolean>): boolean {
|
|
868
|
+
if (value instanceof Promise) {
|
|
869
|
+
throw new Error("bytes: asynchronous callback result is not supported")
|
|
870
|
+
}
|
|
871
|
+
return value
|
|
872
|
+
}
|
|
873
|
+
|
|
864
874
|
class asciiSet {
|
|
865
875
|
constructor(public _value: number[]) {}
|
|
866
876
|
|
|
@@ -1285,4 +1295,3 @@ export function CutSuffix(s: $.Bytes, suffix: $.Bytes): [$.Bytes, boolean] {
|
|
|
1285
1295
|
}
|
|
1286
1296
|
return [$.goSlice(s, undefined, $.len(s) - $.len(suffix)), true]
|
|
1287
1297
|
}
|
|
1288
|
-
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import * as $ from "@goscript/builtin/index.js"
|
|
2
|
+
import { IndexFunc } from "./index.js"
|
|
3
|
+
import { describe, expect, test } from "vitest"
|
|
4
|
+
|
|
5
|
+
describe("bytes", () => {
|
|
6
|
+
test("IndexFunc accepts generated async-shaped callbacks", () => {
|
|
7
|
+
const predicate: (r: number) => boolean | Promise<boolean> = (r) => r === 0x62
|
|
8
|
+
|
|
9
|
+
expect(IndexFunc($.stringToBytes("abc"), predicate)).toBe(1)
|
|
10
|
+
})
|
|
11
|
+
|
|
12
|
+
test("IndexFunc rejects actual async callback results", () => {
|
|
13
|
+
expect(() => IndexFunc($.stringToBytes("abc"), async (r) => r === 0x62)).toThrow(
|
|
14
|
+
"bytes: asynchronous callback result is not supported",
|
|
15
|
+
)
|
|
16
|
+
})
|
|
17
|
+
})
|
package/gs/bytes/iter.gs.ts
CHANGED
|
@@ -13,7 +13,7 @@ import * as utf8 from "@goscript/unicode/utf8/index.js"
|
|
|
13
13
|
// If s does not end in a newline, the final yielded line will not end in a newline.
|
|
14
14
|
// It returns a single-use iterator.
|
|
15
15
|
export function Lines(s: $.Bytes): iter.Seq<$.Bytes> {
|
|
16
|
-
return (_yield: ((p0: $.Bytes) =>
|
|
16
|
+
return async (_yield: ((p0: $.Bytes) => iter.YieldResult) | null): Promise<void> => {
|
|
17
17
|
for (; $.len(s) > 0; ) {
|
|
18
18
|
let line: $.Bytes = new Uint8Array(0)
|
|
19
19
|
{
|
|
@@ -24,7 +24,7 @@ export function Lines(s: $.Bytes): iter.Seq<$.Bytes> {
|
|
|
24
24
|
[line, s] = [s, null]
|
|
25
25
|
}
|
|
26
26
|
}
|
|
27
|
-
if (!_yield!($.goSlice(line, undefined, $.len(line), $.len(line)))) {
|
|
27
|
+
if (!await _yield!($.goSlice(line, undefined, $.len(line), $.len(line)))) {
|
|
28
28
|
return
|
|
29
29
|
}
|
|
30
30
|
}
|
|
@@ -34,10 +34,10 @@ export function Lines(s: $.Bytes): iter.Seq<$.Bytes> {
|
|
|
34
34
|
|
|
35
35
|
// explodeSeq returns an iterator over the runes in s.
|
|
36
36
|
export function explodeSeq(s: $.Bytes): iter.Seq<$.Bytes> {
|
|
37
|
-
return (_yield: ((p0: $.Bytes) =>
|
|
37
|
+
return async (_yield: ((p0: $.Bytes) => iter.YieldResult) | null): Promise<void> => {
|
|
38
38
|
for (; $.len(s) > 0; ) {
|
|
39
39
|
let [, size] = utf8.DecodeRune(s)
|
|
40
|
-
if (!_yield!($.goSlice(s, undefined, size, size))) {
|
|
40
|
+
if (!await _yield!($.goSlice(s, undefined, size, size))) {
|
|
41
41
|
return
|
|
42
42
|
}
|
|
43
43
|
s = $.goSlice(s, size, undefined)
|
|
@@ -51,19 +51,19 @@ export function splitSeq(s: $.Bytes, sep: $.Bytes, sepSave: number): iter.Seq<$.
|
|
|
51
51
|
if ($.len(sep) == 0) {
|
|
52
52
|
return explodeSeq(s)
|
|
53
53
|
}
|
|
54
|
-
return (_yield: ((p0: $.Bytes) =>
|
|
54
|
+
return async (_yield: ((p0: $.Bytes) => iter.YieldResult) | null): Promise<void> => {
|
|
55
55
|
for (; ; ) {
|
|
56
56
|
let i = Index(s, sep)
|
|
57
57
|
if (i < 0) {
|
|
58
58
|
break
|
|
59
59
|
}
|
|
60
60
|
let frag = $.goSlice(s, undefined, i + sepSave)
|
|
61
|
-
if (!_yield!($.goSlice(frag, undefined, $.len(frag), $.len(frag)))) {
|
|
61
|
+
if (!await _yield!($.goSlice(frag, undefined, $.len(frag), $.len(frag)))) {
|
|
62
62
|
return
|
|
63
63
|
}
|
|
64
64
|
s = $.goSlice(s, i + $.len(sep), undefined)
|
|
65
65
|
}
|
|
66
|
-
_yield!($.goSlice(s, undefined, $.len(s), $.len(s)))
|
|
66
|
+
await _yield!($.goSlice(s, undefined, $.len(s), $.len(s)))
|
|
67
67
|
}
|
|
68
68
|
}
|
|
69
69
|
|
|
@@ -88,7 +88,7 @@ export function SplitAfterSeq(s: $.Bytes, sep: $.Bytes): iter.Seq<$.Bytes> {
|
|
|
88
88
|
// The iterator yields the same subslices that would be returned by [Fields](s),
|
|
89
89
|
// but without constructing a new slice containing the subslices.
|
|
90
90
|
export function FieldsSeq(s: $.Bytes): iter.Seq<$.Bytes> {
|
|
91
|
-
return (_yield: ((p0: $.Bytes) =>
|
|
91
|
+
return async (_yield: ((p0: $.Bytes) => iter.YieldResult) | null): Promise<void> => {
|
|
92
92
|
let start = -1
|
|
93
93
|
for (let i = 0; i < $.len(s); ) {
|
|
94
94
|
let size = 1
|
|
@@ -100,7 +100,7 @@ export function FieldsSeq(s: $.Bytes): iter.Seq<$.Bytes> {
|
|
|
100
100
|
}
|
|
101
101
|
if (isSpace) {
|
|
102
102
|
if (start >= 0) {
|
|
103
|
-
if (!_yield!($.goSlice(s, start, i, i))) {
|
|
103
|
+
if (!await _yield!($.goSlice(s, start, i, i))) {
|
|
104
104
|
return
|
|
105
105
|
}
|
|
106
106
|
start = -1
|
|
@@ -111,7 +111,7 @@ export function FieldsSeq(s: $.Bytes): iter.Seq<$.Bytes> {
|
|
|
111
111
|
i += size
|
|
112
112
|
}
|
|
113
113
|
if (start >= 0) {
|
|
114
|
-
_yield!($.goSlice(s, start, $.len(s), $.len(s)))
|
|
114
|
+
await _yield!($.goSlice(s, start, $.len(s), $.len(s)))
|
|
115
115
|
}
|
|
116
116
|
}
|
|
117
117
|
}
|
|
@@ -121,7 +121,7 @@ export function FieldsSeq(s: $.Bytes): iter.Seq<$.Bytes> {
|
|
|
121
121
|
// The iterator yields the same subslices that would be returned by [FieldsFunc](s),
|
|
122
122
|
// but without constructing a new slice containing the subslices.
|
|
123
123
|
export function FieldsFuncSeq(s: $.Bytes, f: ((p0: number) => boolean) | null): iter.Seq<$.Bytes> {
|
|
124
|
-
return (_yield: ((p0: $.Bytes) =>
|
|
124
|
+
return async (_yield: ((p0: $.Bytes) => iter.YieldResult) | null): Promise<void> => {
|
|
125
125
|
let start = -1
|
|
126
126
|
for (let i = 0; i < $.len(s); ) {
|
|
127
127
|
let size = 1
|
|
@@ -131,7 +131,7 @@ export function FieldsFuncSeq(s: $.Bytes, f: ((p0: number) => boolean) | null):
|
|
|
131
131
|
}
|
|
132
132
|
if (f!(r)) {
|
|
133
133
|
if (start >= 0) {
|
|
134
|
-
if (!_yield!($.goSlice(s, start, i, i))) {
|
|
134
|
+
if (!await _yield!($.goSlice(s, start, i, i))) {
|
|
135
135
|
return
|
|
136
136
|
}
|
|
137
137
|
start = -1
|
|
@@ -142,8 +142,7 @@ export function FieldsFuncSeq(s: $.Bytes, f: ((p0: number) => boolean) | null):
|
|
|
142
142
|
i += size
|
|
143
143
|
}
|
|
144
144
|
if (start >= 0) {
|
|
145
|
-
_yield!($.goSlice(s, start, $.len(s), $.len(s)))
|
|
145
|
+
await _yield!($.goSlice(s, start, $.len(s), $.len(s)))
|
|
146
146
|
}
|
|
147
147
|
}
|
|
148
148
|
}
|
|
149
|
-
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { describe, expect, test } from 'vitest'
|
|
2
|
+
|
|
3
|
+
import * as $ from '@goscript/builtin/index.js'
|
|
4
|
+
import * as bytes from '@goscript/bytes/index.js'
|
|
5
|
+
import * as io from '@goscript/io/index.js'
|
|
6
|
+
|
|
7
|
+
import { NewReader, NewWriter } from './index.js'
|
|
8
|
+
|
|
9
|
+
describe('compress/zlib override', () => {
|
|
10
|
+
test('round trips bytes through writer and reader', async () => {
|
|
11
|
+
const input = $.stringToBytes('hello compressed world')
|
|
12
|
+
const buf = $.markAsStructValue(new bytes.Buffer())
|
|
13
|
+
const writer = NewWriter(buf)
|
|
14
|
+
|
|
15
|
+
const [written, writeErr] = writer.Write(input)
|
|
16
|
+
expect(writeErr).toBeNull()
|
|
17
|
+
expect(written).toBe(input.length)
|
|
18
|
+
expect(await writer.Close()).toBeNull()
|
|
19
|
+
|
|
20
|
+
const [reader, readerErr] = await NewReader(bytes.NewReader(buf.Bytes()))
|
|
21
|
+
expect(readerErr).toBeNull()
|
|
22
|
+
expect(reader).not.toBeNull()
|
|
23
|
+
|
|
24
|
+
const [out, readErr] = await io.ReadAll(reader!)
|
|
25
|
+
expect(readErr).toBeNull()
|
|
26
|
+
expect($.bytesToString(out)).toBe('hello compressed world')
|
|
27
|
+
})
|
|
28
|
+
})
|