goscript 0.0.33 → 0.0.35
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/compiler/analysis.go +30 -22
- package/compiler/analysis_test.go +14 -0
- package/compiler/assignment.go +45 -7
- package/compiler/builtin_test.go +2 -0
- package/compiler/compiler.go +15 -89
- package/compiler/compiler_test.go +0 -53
- package/compiler/composite-lit.go +60 -17
- package/compiler/decl.go +1 -1
- package/compiler/expr-call.go +347 -30
- package/compiler/expr-selector.go +28 -2
- package/compiler/expr.go +79 -38
- package/compiler/lit.go +112 -3
- package/compiler/primitive.go +6 -6
- package/compiler/protobuf.go +0 -5
- package/compiler/sanitize.go +101 -0
- package/compiler/spec-value.go +25 -18
- package/compiler/stmt-assign.go +128 -91
- package/compiler/stmt-for.go +78 -1
- package/compiler/stmt-range.go +333 -461
- package/compiler/stmt.go +46 -9
- package/compiler/type.go +14 -11
- package/dist/gs/builtin/builtin.d.ts +8 -0
- package/dist/gs/builtin/builtin.js +31 -0
- package/dist/gs/builtin/builtin.js.map +1 -1
- package/dist/gs/builtin/map.d.ts +4 -4
- package/dist/gs/builtin/map.js +12 -6
- package/dist/gs/builtin/map.js.map +1 -1
- package/dist/gs/builtin/slice.d.ts +14 -8
- package/dist/gs/builtin/slice.js +131 -31
- package/dist/gs/builtin/slice.js.map +1 -1
- package/dist/gs/github.com/pkg/errors/errors.d.ts +13 -0
- package/dist/gs/github.com/pkg/errors/errors.js +232 -0
- package/dist/gs/github.com/pkg/errors/errors.js.map +1 -0
- package/dist/gs/github.com/pkg/errors/go113.d.ts +4 -0
- package/dist/gs/github.com/pkg/errors/go113.js +34 -0
- package/dist/gs/github.com/pkg/errors/go113.js.map +1 -0
- package/dist/gs/github.com/pkg/errors/index.d.ts +3 -0
- package/dist/gs/github.com/pkg/errors/index.js +4 -0
- package/dist/gs/github.com/pkg/errors/index.js.map +1 -0
- package/dist/gs/github.com/pkg/errors/stack.d.ts +32 -0
- package/dist/gs/github.com/pkg/errors/stack.js +111 -0
- package/dist/gs/github.com/pkg/errors/stack.js.map +1 -0
- package/dist/gs/maps/index.d.ts +2 -0
- package/dist/gs/maps/index.js +3 -0
- package/dist/gs/maps/index.js.map +1 -0
- package/dist/gs/maps/iter.d.ts +7 -0
- package/dist/gs/maps/iter.gs.d.ts +7 -0
- package/dist/gs/maps/iter.gs.js +65 -0
- package/dist/gs/maps/iter.gs.js.map +1 -0
- package/dist/gs/maps/iter.js +57 -0
- package/dist/gs/maps/iter.js.map +1 -0
- package/dist/gs/maps/maps.d.ts +7 -0
- package/dist/gs/maps/maps.gs.d.ts +7 -0
- package/dist/gs/maps/maps.gs.js +79 -0
- package/dist/gs/maps/maps.gs.js.map +1 -0
- package/dist/gs/maps/maps.js +67 -0
- package/dist/gs/maps/maps.js.map +1 -0
- package/dist/gs/math/abs.gs.d.ts +1 -0
- package/dist/gs/math/abs.gs.js +10 -0
- package/dist/gs/math/abs.gs.js.map +1 -0
- package/dist/gs/math/acosh.gs.d.ts +2 -0
- package/dist/gs/math/acosh.gs.js +14 -0
- package/dist/gs/math/acosh.gs.js.map +1 -0
- package/dist/gs/math/asin.gs.d.ts +4 -0
- package/dist/gs/math/asin.gs.js +24 -0
- package/dist/gs/math/asin.gs.js.map +1 -0
- package/dist/gs/math/asinh.gs.d.ts +2 -0
- package/dist/gs/math/asinh.gs.js +14 -0
- package/dist/gs/math/asinh.gs.js.map +1 -0
- package/dist/gs/math/atan.gs.d.ts +4 -0
- package/dist/gs/math/atan.gs.js +22 -0
- package/dist/gs/math/atan.gs.js.map +1 -0
- package/dist/gs/math/atan2.gs.d.ts +2 -0
- package/dist/gs/math/atan2.gs.js +30 -0
- package/dist/gs/math/atan2.gs.js.map +1 -0
- package/dist/gs/math/atanh.gs.d.ts +2 -0
- package/dist/gs/math/atanh.gs.js +16 -0
- package/dist/gs/math/atanh.gs.js.map +1 -0
- package/dist/gs/math/bits.gs.d.ts +5 -0
- package/dist/gs/math/bits.gs.js +46 -0
- package/dist/gs/math/bits.gs.js.map +1 -0
- package/dist/gs/math/cbrt.gs.d.ts +2 -0
- package/dist/gs/math/cbrt.gs.js +14 -0
- package/dist/gs/math/cbrt.gs.js.map +1 -0
- package/dist/gs/math/const.gs.d.ts +30 -0
- package/dist/gs/math/const.gs.js +61 -0
- package/dist/gs/math/const.gs.js.map +1 -0
- package/dist/gs/math/copysign.gs.d.ts +1 -0
- package/dist/gs/math/copysign.gs.js +20 -0
- package/dist/gs/math/copysign.gs.js.map +1 -0
- package/dist/gs/math/dim.gs.d.ts +5 -0
- package/dist/gs/math/dim.gs.js +69 -0
- package/dist/gs/math/dim.gs.js.map +1 -0
- package/dist/gs/math/erf.gs.d.ts +4 -0
- package/dist/gs/math/erf.gs.js +336 -0
- package/dist/gs/math/erf.gs.js.map +1 -0
- package/dist/gs/math/erfinv.gs.d.ts +2 -0
- package/dist/gs/math/erfinv.gs.js +118 -0
- package/dist/gs/math/erfinv.gs.js.map +1 -0
- package/dist/gs/math/exp.gs.d.ts +5 -0
- package/dist/gs/math/exp.gs.js +30 -0
- package/dist/gs/math/exp.gs.js.map +1 -0
- package/dist/gs/math/expm1.gs.d.ts +2 -0
- package/dist/gs/math/expm1.gs.js +17 -0
- package/dist/gs/math/expm1.gs.js.map +1 -0
- package/dist/gs/math/floor.gs.d.ts +8 -0
- package/dist/gs/math/floor.gs.js +75 -0
- package/dist/gs/math/floor.gs.js.map +1 -0
- package/dist/gs/math/fma.gs.d.ts +1 -0
- package/dist/gs/math/fma.gs.js +8 -0
- package/dist/gs/math/fma.gs.js.map +1 -0
- package/dist/gs/math/frexp.gs.d.ts +2 -0
- package/dist/gs/math/frexp.gs.js +28 -0
- package/dist/gs/math/frexp.gs.js.map +1 -0
- package/dist/gs/math/gamma.gs.d.ts +3 -0
- package/dist/gs/math/gamma.gs.js +149 -0
- package/dist/gs/math/gamma.gs.js.map +1 -0
- package/dist/gs/math/hypot.gs.d.ts +2 -0
- package/dist/gs/math/hypot.gs.js +16 -0
- package/dist/gs/math/hypot.gs.js.map +1 -0
- package/dist/gs/math/index.d.ts +44 -0
- package/dist/gs/math/index.js +45 -0
- package/dist/gs/math/index.js.map +1 -0
- package/dist/gs/math/j0.gs.d.ts +4 -0
- package/dist/gs/math/j0.gs.js +228 -0
- package/dist/gs/math/j0.gs.js.map +1 -0
- package/dist/gs/math/j1.gs.d.ts +4 -0
- package/dist/gs/math/j1.gs.js +211 -0
- package/dist/gs/math/j1.gs.js.map +1 -0
- package/dist/gs/math/jn.gs.d.ts +2 -0
- package/dist/gs/math/jn.gs.js +412 -0
- package/dist/gs/math/jn.gs.js.map +1 -0
- package/dist/gs/math/ldexp.gs.d.ts +2 -0
- package/dist/gs/math/ldexp.gs.js +20 -0
- package/dist/gs/math/ldexp.gs.js.map +1 -0
- package/dist/gs/math/lgamma.gs.d.ts +2 -0
- package/dist/gs/math/lgamma.gs.js +243 -0
- package/dist/gs/math/lgamma.gs.js.map +1 -0
- package/dist/gs/math/log.gs.d.ts +2 -0
- package/dist/gs/math/log.gs.js +16 -0
- package/dist/gs/math/log.gs.js.map +1 -0
- package/dist/gs/math/log10.gs.d.ts +4 -0
- package/dist/gs/math/log10.gs.js +17 -0
- package/dist/gs/math/log10.gs.js.map +1 -0
- package/dist/gs/math/log1p.gs.d.ts +2 -0
- package/dist/gs/math/log1p.gs.js +17 -0
- package/dist/gs/math/log1p.gs.js.map +1 -0
- package/dist/gs/math/logb.gs.d.ts +3 -0
- package/dist/gs/math/logb.gs.js +43 -0
- package/dist/gs/math/logb.gs.js.map +1 -0
- package/dist/gs/math/mod.gs.d.ts +2 -0
- package/dist/gs/math/mod.gs.js +26 -0
- package/dist/gs/math/mod.gs.js.map +1 -0
- package/dist/gs/math/modf.gs.d.ts +2 -0
- package/dist/gs/math/modf.gs.js +24 -0
- package/dist/gs/math/modf.gs.js.map +1 -0
- package/dist/gs/math/nextafter.gs.d.ts +2 -0
- package/dist/gs/math/nextafter.gs.js +66 -0
- package/dist/gs/math/nextafter.gs.js.map +1 -0
- package/dist/gs/math/pow.gs.d.ts +3 -0
- package/dist/gs/math/pow.gs.js +40 -0
- package/dist/gs/math/pow.gs.js.map +1 -0
- package/dist/gs/math/pow10.gs.d.ts +1 -0
- package/dist/gs/math/pow10.gs.js +14 -0
- package/dist/gs/math/pow10.gs.js.map +1 -0
- package/dist/gs/math/remainder.gs.d.ts +2 -0
- package/dist/gs/math/remainder.gs.js +25 -0
- package/dist/gs/math/remainder.gs.js.map +1 -0
- package/dist/gs/math/signbit.gs.d.ts +1 -0
- package/dist/gs/math/signbit.gs.js +5 -0
- package/dist/gs/math/signbit.gs.js.map +1 -0
- package/dist/gs/math/sin.gs.d.ts +4 -0
- package/dist/gs/math/sin.gs.js +29 -0
- package/dist/gs/math/sin.gs.js.map +1 -0
- package/dist/gs/math/sincos.gs.d.ts +1 -0
- package/dist/gs/math/sincos.gs.js +11 -0
- package/dist/gs/math/sincos.gs.js.map +1 -0
- package/dist/gs/math/sinh.gs.d.ts +4 -0
- package/dist/gs/math/sinh.gs.js +27 -0
- package/dist/gs/math/sinh.gs.js.map +1 -0
- package/dist/gs/math/sqrt.gs.d.ts +2 -0
- package/dist/gs/math/sqrt.gs.js +15 -0
- package/dist/gs/math/sqrt.gs.js.map +1 -0
- package/dist/gs/math/tan.gs.d.ts +2 -0
- package/dist/gs/math/tan.gs.js +17 -0
- package/dist/gs/math/tan.gs.js.map +1 -0
- package/dist/gs/math/tanh.gs.d.ts +2 -0
- package/dist/gs/math/tanh.gs.js +17 -0
- package/dist/gs/math/tanh.gs.js.map +1 -0
- package/dist/gs/math/trig_reduce.gs.d.ts +1 -0
- package/dist/gs/math/trig_reduce.gs.js +62 -0
- package/dist/gs/math/trig_reduce.gs.js.map +1 -0
- package/dist/gs/math/unsafe.gs.d.ts +4 -0
- package/dist/gs/math/unsafe.gs.js +47 -0
- package/dist/gs/math/unsafe.gs.js.map +1 -0
- package/dist/gs/slices/slices.d.ts +6 -0
- package/dist/gs/slices/slices.js +8 -0
- package/dist/gs/slices/slices.js.map +1 -1
- package/dist/gs/strconv/atob.gs.d.ts +4 -0
- package/dist/gs/strconv/atob.gs.js +42 -0
- package/dist/gs/strconv/atob.gs.js.map +1 -0
- package/dist/gs/strconv/atof.gs.d.ts +2 -0
- package/dist/gs/strconv/atof.gs.js +51 -0
- package/dist/gs/strconv/atof.gs.js.map +1 -0
- package/dist/gs/strconv/atoi.gs.d.ts +33 -0
- package/dist/gs/strconv/atoi.gs.js +200 -0
- package/dist/gs/strconv/atoi.gs.js.map +1 -0
- package/dist/gs/strconv/doc.gs.d.ts +1 -0
- package/dist/gs/strconv/doc.gs.js +2 -0
- package/dist/gs/strconv/doc.gs.js.map +1 -0
- package/dist/gs/strconv/ftoa.gs.d.ts +3 -0
- package/dist/gs/strconv/ftoa.gs.js +58 -0
- package/dist/gs/strconv/ftoa.gs.js.map +1 -0
- package/dist/gs/strconv/index.d.ts +6 -0
- package/dist/gs/strconv/index.js +7 -0
- package/dist/gs/strconv/index.js.map +1 -0
- package/dist/gs/strconv/itoa.gs.d.ts +6 -0
- package/dist/gs/strconv/itoa.gs.js +37 -0
- package/dist/gs/strconv/itoa.gs.js.map +1 -0
- package/dist/gs/strconv/quote.gs.d.ts +19 -0
- package/dist/gs/strconv/quote.gs.js +217 -0
- package/dist/gs/strconv/quote.gs.js.map +1 -0
- package/dist/gs/strings/index.d.ts +3 -0
- package/dist/gs/strings/index.js +4 -0
- package/dist/gs/strings/index.js.map +1 -1
- package/dist/gs/strings/replace.d.ts +0 -74
- package/dist/gs/strings/replace.js +6 -204
- package/dist/gs/strings/replace.js.map +1 -1
- package/dist/gs/strings/search.d.ts +0 -1
- package/dist/gs/strings/search.js +0 -21
- package/dist/gs/strings/search.js.map +1 -1
- package/gs/builtin/builtin.ts +40 -0
- package/gs/builtin/map.ts +12 -7
- package/gs/builtin/slice.ts +174 -34
- package/gs/github.com/pkg/errors/errors.ts +307 -0
- package/gs/github.com/pkg/errors/go113.ts +39 -0
- package/gs/github.com/pkg/errors/index.ts +3 -0
- package/gs/github.com/pkg/errors/stack.ts +127 -0
- package/gs/maps/index.ts +2 -0
- package/gs/maps/iter.ts +67 -0
- package/gs/maps/maps.ts +89 -0
- package/gs/math/TODO.md +156 -0
- package/gs/math/abs.gs.test.ts +29 -0
- package/gs/math/abs.gs.ts +13 -0
- package/gs/math/acosh.gs.test.ts +39 -0
- package/gs/math/acosh.gs.ts +21 -0
- package/gs/math/asin.gs.test.ts +66 -0
- package/gs/math/asin.gs.ts +27 -0
- package/gs/math/asinh.gs.test.ts +37 -0
- package/gs/math/asinh.gs.ts +21 -0
- package/gs/math/atan.gs.test.ts +49 -0
- package/gs/math/atan.gs.ts +27 -0
- package/gs/math/atan2.gs.test.ts +55 -0
- package/gs/math/atan2.gs.ts +37 -0
- package/gs/math/atanh.gs.test.ts +47 -0
- package/gs/math/atanh.gs.ts +21 -0
- package/gs/math/bits.gs.test.ts +88 -0
- package/gs/math/bits.gs.ts +61 -0
- package/gs/math/cbrt.gs.test.ts +57 -0
- package/gs/math/cbrt.gs.ts +20 -0
- package/gs/math/const.gs.test.ts +54 -0
- package/gs/math/const.gs.ts +93 -0
- package/gs/math/copysign.gs.test.ts +44 -0
- package/gs/math/copysign.gs.ts +27 -0
- package/gs/math/dim.gs.test.ts +102 -0
- package/gs/math/dim.gs.ts +84 -0
- package/gs/math/erf.gs.test.ts +92 -0
- package/gs/math/erf.gs.ts +409 -0
- package/gs/math/erfinv.gs.test.ts +104 -0
- package/gs/math/erfinv.gs.ts +169 -0
- package/gs/math/exp.gs.test.ts +82 -0
- package/gs/math/exp.gs.ts +39 -0
- package/gs/math/expm1.gs.test.ts +48 -0
- package/gs/math/expm1.gs.ts +23 -0
- package/gs/math/floor.gs.test.ts +146 -0
- package/gs/math/floor.gs.ts +88 -0
- package/gs/math/fma.gs.test.ts +83 -0
- package/gs/math/fma.gs.ts +7 -0
- package/gs/math/frexp.gs.test.ts +146 -0
- package/gs/math/frexp.gs.ts +37 -0
- package/gs/math/gamma.gs.test.ts +66 -0
- package/gs/math/gamma.gs.ts +158 -0
- package/gs/math/hypot.gs.test.ts +73 -0
- package/gs/math/hypot.gs.ts +23 -0
- package/gs/math/index.ts +44 -0
- package/gs/math/j0.gs.test.ts +74 -0
- package/gs/math/j0.gs.ts +257 -0
- package/gs/math/j1.gs.test.ts +81 -0
- package/gs/math/j1.gs.ts +231 -0
- package/gs/math/jn.gs.test.ts +133 -0
- package/gs/math/jn.gs.ts +447 -0
- package/gs/math/ldexp.gs.test.ts +128 -0
- package/gs/math/ldexp.gs.ts +28 -0
- package/gs/math/lgamma.gs.test.ts +102 -0
- package/gs/math/lgamma.gs.ts +251 -0
- package/gs/math/log.gs.test.ts +40 -0
- package/gs/math/log.gs.ts +21 -0
- package/gs/math/log10.gs.test.ts +80 -0
- package/gs/math/log10.gs.ts +25 -0
- package/gs/math/log1p.gs.test.ts +55 -0
- package/gs/math/log1p.gs.ts +24 -0
- package/gs/math/logb.gs.test.ts +87 -0
- package/gs/math/logb.gs.ts +54 -0
- package/gs/math/mod.gs.test.ts +64 -0
- package/gs/math/mod.gs.ts +36 -0
- package/gs/math/modf.gs.test.ts +80 -0
- package/gs/math/modf.gs.ts +32 -0
- package/gs/math/nextafter.gs.test.ts +107 -0
- package/gs/math/nextafter.gs.ts +71 -0
- package/gs/math/pow.gs.test.ts +103 -0
- package/gs/math/pow.gs.ts +55 -0
- package/gs/math/pow10.gs.test.ts +58 -0
- package/gs/math/pow10.gs.ts +19 -0
- package/gs/math/remainder.gs.test.ts +70 -0
- package/gs/math/remainder.gs.ts +33 -0
- package/gs/math/signbit.gs.test.ts +33 -0
- package/gs/math/signbit.gs.ts +8 -0
- package/gs/math/sin.gs.test.ts +83 -0
- package/gs/math/sin.gs.ts +38 -0
- package/gs/math/sincos.gs.test.ts +91 -0
- package/gs/math/sincos.gs.ts +15 -0
- package/gs/math/sinh.gs.test.ts +66 -0
- package/gs/math/sinh.gs.ts +34 -0
- package/gs/math/sqrt.gs.test.ts +49 -0
- package/gs/math/sqrt.gs.ts +20 -0
- package/gs/math/tan.gs.test.ts +50 -0
- package/gs/math/tan.gs.ts +23 -0
- package/gs/math/tanh.gs.test.ts +52 -0
- package/gs/math/tanh.gs.ts +23 -0
- package/gs/math/trig_reduce.gs.ts +66 -0
- package/gs/math/unsafe.gs.ts +52 -0
- package/gs/slices/slices.ts +9 -0
- package/gs/strconv/atob.gs.ts +45 -0
- package/gs/strconv/atof.gs.ts +60 -0
- package/gs/strconv/atoi.gs.ts +243 -0
- package/gs/strconv/doc.gs.ts +2 -0
- package/gs/strconv/ftoa.gs.ts +66 -0
- package/gs/strconv/index.ts +6 -0
- package/gs/strconv/itoa.gs.ts +41 -0
- package/gs/strconv/quote.gs.ts +245 -0
- package/gs/strings/index.ts +4 -0
- package/gs/strings/replace.ts +9 -237
- package/gs/strings/search.ts +0 -28
- package/package.json +1 -1
- package/gs/stringslite/godoc.txt +0 -17
- package/gs/stringslite/index.ts +0 -1
- package/gs/stringslite/strings.ts +0 -82
package/gs/builtin/map.ts
CHANGED
|
@@ -6,18 +6,23 @@ export const makeMap = <K, V>(): Map<K, V> => {
|
|
|
6
6
|
return new Map<K, V>()
|
|
7
7
|
}
|
|
8
8
|
/**
|
|
9
|
-
* Gets a value from a map,
|
|
9
|
+
* Gets a value from a map, returning a tuple [value, exists].
|
|
10
10
|
* @param map The map to get from.
|
|
11
11
|
* @param key The key to get.
|
|
12
|
-
* @param defaultValue The default value to return if the key doesn't exist
|
|
13
|
-
* @returns
|
|
12
|
+
* @param defaultValue The default value to return if the key doesn't exist.
|
|
13
|
+
* @returns A tuple [value, exists] where value is the map value or defaultValue, and exists is whether the key was found.
|
|
14
14
|
*/
|
|
15
|
-
export
|
|
15
|
+
export function mapGet<K, V, D>(
|
|
16
16
|
map: Map<K, V>,
|
|
17
17
|
key: K,
|
|
18
|
-
defaultValue:
|
|
19
|
-
): V |
|
|
20
|
-
|
|
18
|
+
defaultValue: D,
|
|
19
|
+
): [V, true] | [D, false] {
|
|
20
|
+
const exists = map.has(key)
|
|
21
|
+
if (exists) {
|
|
22
|
+
return [map.get(key)!, true]
|
|
23
|
+
} else {
|
|
24
|
+
return [defaultValue, false]
|
|
25
|
+
}
|
|
21
26
|
}
|
|
22
27
|
|
|
23
28
|
/**
|
package/gs/builtin/slice.ts
CHANGED
|
@@ -65,6 +65,14 @@ function isComplexSlice<T>(slice: Slice<T>): slice is SliceProxy<T> {
|
|
|
65
65
|
)
|
|
66
66
|
}
|
|
67
67
|
|
|
68
|
+
/**
|
|
69
|
+
* isSliceProxy checks if a slice is a SliceProxy (has __meta__ property)
|
|
70
|
+
* This is an alias for isComplexSlice for better type hinting
|
|
71
|
+
*/
|
|
72
|
+
export function isSliceProxy<T>(slice: Slice<T>): slice is SliceProxy<T> {
|
|
73
|
+
return isComplexSlice(slice)
|
|
74
|
+
}
|
|
75
|
+
|
|
68
76
|
/**
|
|
69
77
|
* Creates a new slice with the specified length and capacity.
|
|
70
78
|
* @param length The length of the slice.
|
|
@@ -77,9 +85,81 @@ export const makeSlice = <T>(
|
|
|
77
85
|
typeHint?: string,
|
|
78
86
|
): Slice<T> => {
|
|
79
87
|
if (typeHint === 'byte') {
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
88
|
+
const actualCapacity = capacity === undefined ? length : capacity
|
|
89
|
+
if (length < 0 || actualCapacity < 0 || length > actualCapacity) {
|
|
90
|
+
throw new Error(
|
|
91
|
+
`Invalid slice length (${length}) or capacity (${actualCapacity})`,
|
|
92
|
+
)
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// If capacity equals length, use Uint8Array directly for efficiency
|
|
96
|
+
if (actualCapacity === length) {
|
|
97
|
+
return new Uint8Array(length) as Slice<T>
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// If capacity > length, create a SliceProxy backed by a Uint8Array
|
|
101
|
+
const backingUint8 = new Uint8Array(actualCapacity)
|
|
102
|
+
const backingNumbers = Array.from(backingUint8) as T[] // Convert to number[] for backing
|
|
103
|
+
|
|
104
|
+
const proxyTargetArray = new Array<T>(length)
|
|
105
|
+
for (let i = 0; i < length; i++) {
|
|
106
|
+
proxyTargetArray[i] = 0 as T // Initialize with zeros
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
const proxy = proxyTargetArray as SliceProxy<T>
|
|
110
|
+
proxy.__meta__ = {
|
|
111
|
+
backing: backingNumbers,
|
|
112
|
+
offset: 0,
|
|
113
|
+
length: length,
|
|
114
|
+
capacity: actualCapacity,
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// Create a proper Proxy with the handler for SliceProxy behavior
|
|
118
|
+
const handler = {
|
|
119
|
+
get(target: any, prop: string | symbol): any {
|
|
120
|
+
if (typeof prop === 'string' && /^\d+$/.test(prop)) {
|
|
121
|
+
const index = Number(prop)
|
|
122
|
+
if (index >= 0 && index < target.__meta__.length) {
|
|
123
|
+
return target.__meta__.backing[target.__meta__.offset + index]
|
|
124
|
+
}
|
|
125
|
+
throw new Error(
|
|
126
|
+
`Slice index out of range: ${index} >= ${target.__meta__.length}`,
|
|
127
|
+
)
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
if (prop === 'length') {
|
|
131
|
+
return target.__meta__.length
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
if (prop === '__meta__') {
|
|
135
|
+
return target.__meta__
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
return Reflect.get(target, prop)
|
|
139
|
+
},
|
|
140
|
+
|
|
141
|
+
set(target: any, prop: string | symbol, value: any): boolean {
|
|
142
|
+
if (typeof prop === 'string' && /^\d+$/.test(prop)) {
|
|
143
|
+
const index = Number(prop)
|
|
144
|
+
if (index >= 0 && index < target.__meta__.length) {
|
|
145
|
+
target.__meta__.backing[target.__meta__.offset + index] = value
|
|
146
|
+
target[index] = value // Also update the proxy target for consistency
|
|
147
|
+
return true
|
|
148
|
+
}
|
|
149
|
+
throw new Error(
|
|
150
|
+
`Slice index out of range: ${index} >= ${target.__meta__.length}`,
|
|
151
|
+
)
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
if (prop === 'length' || prop === '__meta__') {
|
|
155
|
+
return false
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
return Reflect.set(target, prop, value)
|
|
159
|
+
},
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
return new Proxy(proxy, handler) as Slice<T>
|
|
83
163
|
}
|
|
84
164
|
|
|
85
165
|
const actualCapacity = capacity === undefined ? length : capacity
|
|
@@ -127,7 +207,52 @@ export const makeSlice = <T>(
|
|
|
127
207
|
capacity: actualCapacity,
|
|
128
208
|
}
|
|
129
209
|
|
|
130
|
-
|
|
210
|
+
// Create a proper Proxy with the handler for SliceProxy behavior
|
|
211
|
+
const handler = {
|
|
212
|
+
get(target: any, prop: string | symbol): any {
|
|
213
|
+
if (typeof prop === 'string' && /^\d+$/.test(prop)) {
|
|
214
|
+
const index = Number(prop)
|
|
215
|
+
if (index >= 0 && index < target.__meta__.length) {
|
|
216
|
+
return target.__meta__.backing[target.__meta__.offset + index]
|
|
217
|
+
}
|
|
218
|
+
throw new Error(
|
|
219
|
+
`Slice index out of range: ${index} >= ${target.__meta__.length}`,
|
|
220
|
+
)
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
if (prop === 'length') {
|
|
224
|
+
return target.__meta__.length
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
if (prop === '__meta__') {
|
|
228
|
+
return target.__meta__
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
return Reflect.get(target, prop)
|
|
232
|
+
},
|
|
233
|
+
|
|
234
|
+
set(target: any, prop: string | symbol, value: any): boolean {
|
|
235
|
+
if (typeof prop === 'string' && /^\d+$/.test(prop)) {
|
|
236
|
+
const index = Number(prop)
|
|
237
|
+
if (index >= 0 && index < target.__meta__.length) {
|
|
238
|
+
target.__meta__.backing[target.__meta__.offset + index] = value
|
|
239
|
+
target[index] = value // Also update the proxy target for consistency
|
|
240
|
+
return true
|
|
241
|
+
}
|
|
242
|
+
throw new Error(
|
|
243
|
+
`Slice index out of range: ${index} >= ${target.__meta__.length}`,
|
|
244
|
+
)
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
if (prop === 'length' || prop === '__meta__') {
|
|
248
|
+
return false
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
return Reflect.set(target, prop, value)
|
|
252
|
+
},
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
return new Proxy(proxy, handler) as Slice<T>
|
|
131
256
|
}
|
|
132
257
|
|
|
133
258
|
/**
|
|
@@ -329,18 +454,22 @@ export const goSlice = <T>( // T can be number for Uint8Array case
|
|
|
329
454
|
const newLength = high - low
|
|
330
455
|
const newOffset = oldOffset + low
|
|
331
456
|
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
457
|
+
// Create an array-like target with the correct length
|
|
458
|
+
const proxyTargetArray = new Array<T>(newLength)
|
|
459
|
+
// Note: We don't need to initialize the values here since the proxy handler
|
|
460
|
+
// will fetch them from the backing array when accessed
|
|
461
|
+
|
|
462
|
+
const proxy = proxyTargetArray as SliceProxy<T>
|
|
463
|
+
proxy.__meta__ = {
|
|
464
|
+
backing: backing,
|
|
465
|
+
offset: newOffset,
|
|
466
|
+
length: newLength,
|
|
467
|
+
capacity: newCap,
|
|
339
468
|
}
|
|
340
469
|
|
|
341
470
|
// const handler = { ... } // Handler is now defined at the top
|
|
342
471
|
|
|
343
|
-
return new Proxy(
|
|
472
|
+
return new Proxy(proxy, handler) as unknown as SliceProxy<T>
|
|
344
473
|
}
|
|
345
474
|
|
|
346
475
|
/**
|
|
@@ -522,10 +651,12 @@ export const cap = <T>(obj: Slice<T> | Uint8Array): number => {
|
|
|
522
651
|
* @param elements The elements to append.
|
|
523
652
|
* @returns The modified or new slice.
|
|
524
653
|
*/
|
|
525
|
-
export
|
|
654
|
+
export function append(slice: Uint8Array, ...elements: any[]): Uint8Array
|
|
655
|
+
export function append<T>(slice: Slice<T>, ...elements: any[]): Slice<T>
|
|
656
|
+
export function append<T>(
|
|
526
657
|
slice: Slice<T> | Uint8Array,
|
|
527
658
|
...elements: any[]
|
|
528
|
-
): Slice<T>
|
|
659
|
+
): Slice<T> {
|
|
529
660
|
// 1. Flatten all elements from the varargs `...elements` into `varargsElements`.
|
|
530
661
|
// Determine if the result should be a Uint8Array.
|
|
531
662
|
const inputIsUint8Array = slice instanceof Uint8Array
|
|
@@ -581,7 +712,7 @@ export const append = <T>(
|
|
|
581
712
|
}
|
|
582
713
|
const newArr = new Uint8Array(combinedBytes.length)
|
|
583
714
|
newArr.set(combinedBytes)
|
|
584
|
-
return newArr as
|
|
715
|
+
return newArr as any
|
|
585
716
|
}
|
|
586
717
|
|
|
587
718
|
// Handle generic Slice<T> (non-Uint8Array result).
|
|
@@ -590,7 +721,7 @@ export const append = <T>(
|
|
|
590
721
|
const numAdded = elements.length
|
|
591
722
|
|
|
592
723
|
if (numAdded === 0) {
|
|
593
|
-
return slice
|
|
724
|
+
return slice as any
|
|
594
725
|
}
|
|
595
726
|
|
|
596
727
|
let originalElements: T[] = []
|
|
@@ -631,7 +762,7 @@ export const append = <T>(
|
|
|
631
762
|
length: newLength,
|
|
632
763
|
capacity: oldCapacity,
|
|
633
764
|
}
|
|
634
|
-
return resultProxy
|
|
765
|
+
return resultProxy as any
|
|
635
766
|
}
|
|
636
767
|
|
|
637
768
|
// Case 2: Reallocation is needed.
|
|
@@ -663,7 +794,7 @@ export const append = <T>(
|
|
|
663
794
|
length: newLength,
|
|
664
795
|
capacity: newCapacity,
|
|
665
796
|
}
|
|
666
|
-
return resultProxy
|
|
797
|
+
return resultProxy as any
|
|
667
798
|
}
|
|
668
799
|
|
|
669
800
|
/**
|
|
@@ -910,8 +1041,7 @@ export const sliceString = (
|
|
|
910
1041
|
// Attempt to decode with strict UTF-8 validation
|
|
911
1042
|
const result = new TextDecoder('utf-8', { fatal: true }).decode(slicedBytes)
|
|
912
1043
|
return result
|
|
913
|
-
} catch (_e) {
|
|
914
|
-
//eslint-disable-line @typescript-eslint/no-unused-vars
|
|
1044
|
+
} catch (_e) { //eslint-disable-line @typescript-eslint/no-unused-vars
|
|
915
1045
|
// If we get here, the slice would create invalid UTF-8
|
|
916
1046
|
// This is a fundamental limitation of JavaScript string handling
|
|
917
1047
|
throw new Error(
|
|
@@ -975,20 +1105,20 @@ export function genericBytesOrStringToString(
|
|
|
975
1105
|
}
|
|
976
1106
|
|
|
977
1107
|
/**
|
|
978
|
-
* Indexes into a value that could be either a string or
|
|
1108
|
+
* Indexes into a value that could be either a string or bytes.
|
|
979
1109
|
* Used for generic type parameters with constraint string | []byte.
|
|
980
1110
|
* Both cases return a byte value (number).
|
|
981
|
-
* @param value Value that is either a string or Uint8Array
|
|
1111
|
+
* @param value Value that is either a string or bytes (Uint8Array or Slice<number>)
|
|
982
1112
|
* @param index The index to access
|
|
983
1113
|
* @returns The byte value at the specified index
|
|
984
1114
|
*/
|
|
985
1115
|
export function indexStringOrBytes(
|
|
986
|
-
value: string |
|
|
1116
|
+
value: string | import('./builtin.js').Bytes,
|
|
987
1117
|
index: number,
|
|
988
1118
|
): number {
|
|
989
1119
|
if (typeof value === 'string') {
|
|
990
1120
|
return indexString(value, index)
|
|
991
|
-
} else {
|
|
1121
|
+
} else if (value instanceof Uint8Array) {
|
|
992
1122
|
// For Uint8Array, direct access returns the byte value
|
|
993
1123
|
if (index < 0 || index >= value.length) {
|
|
994
1124
|
throw new Error(
|
|
@@ -996,29 +1126,39 @@ export function indexStringOrBytes(
|
|
|
996
1126
|
)
|
|
997
1127
|
}
|
|
998
1128
|
return value[index]
|
|
1129
|
+
} else if (value === null) {
|
|
1130
|
+
throw new Error(
|
|
1131
|
+
`runtime error: index out of range [${index}] with length 0`,
|
|
1132
|
+
)
|
|
1133
|
+
} else {
|
|
1134
|
+
// For Slice<number> (including SliceProxy)
|
|
1135
|
+
const length = len(value)
|
|
1136
|
+
if (index < 0 || index >= length) {
|
|
1137
|
+
throw new Error(
|
|
1138
|
+
`runtime error: index out of range [${index}] with length ${length}`,
|
|
1139
|
+
)
|
|
1140
|
+
}
|
|
1141
|
+
return (value as any)[index] as number
|
|
999
1142
|
}
|
|
1000
1143
|
}
|
|
1001
1144
|
|
|
1002
1145
|
/**
|
|
1003
|
-
* Slices a value that could be either a string or
|
|
1146
|
+
* Slices a value that could be either a string or bytes.
|
|
1004
1147
|
* Used for generic type parameters with constraint string | []byte.
|
|
1005
|
-
* @param value Value that is either a string or Uint8Array
|
|
1148
|
+
* @param value Value that is either a string or bytes (Uint8Array or Slice<number>)
|
|
1006
1149
|
* @param low Starting index (inclusive). Defaults to 0.
|
|
1007
1150
|
* @param high Ending index (exclusive). Defaults to length.
|
|
1008
|
-
* @param max Capacity limit (only used for
|
|
1151
|
+
* @param max Capacity limit (only used for bytes, ignored for strings)
|
|
1009
1152
|
* @returns The sliced value of the same type as input
|
|
1010
1153
|
*/
|
|
1011
|
-
export function sliceStringOrBytes<
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
high?: number,
|
|
1015
|
-
max?: number,
|
|
1016
|
-
): T {
|
|
1154
|
+
export function sliceStringOrBytes<
|
|
1155
|
+
T extends string | import('./builtin.js').Bytes,
|
|
1156
|
+
>(value: T, low?: number, high?: number, max?: number): T {
|
|
1017
1157
|
if (typeof value === 'string') {
|
|
1018
1158
|
// For strings, use sliceString and ignore max parameter
|
|
1019
1159
|
return sliceString(value, low, high) as T
|
|
1020
1160
|
} else {
|
|
1021
|
-
// For Uint8Array, use goSlice
|
|
1161
|
+
// For bytes (Uint8Array or Slice<number>), use goSlice
|
|
1022
1162
|
return goSlice(value as Slice<number>, low, high, max) as T
|
|
1023
1163
|
}
|
|
1024
1164
|
}
|
|
@@ -0,0 +1,307 @@
|
|
|
1
|
+
import * as $ from "../../../builtin/builtin.js";
|
|
2
|
+
import { callers } from "./stack.js";
|
|
3
|
+
|
|
4
|
+
// Type definitions
|
|
5
|
+
export type Frame = any; // Simplified frame type
|
|
6
|
+
export type StackTrace = Frame[] | null;
|
|
7
|
+
export type stack = any; // Simplified stack type
|
|
8
|
+
export type uintptr = number;
|
|
9
|
+
|
|
10
|
+
// Simplified fmt functions for basic string formatting
|
|
11
|
+
const fmt = {
|
|
12
|
+
Sprintf: (format: string, ...args: any[]): string => {
|
|
13
|
+
// Basic sprintf implementation for the errors package
|
|
14
|
+
let result = format;
|
|
15
|
+
let argIndex = 0;
|
|
16
|
+
result = result.replace(/%[sdqv%]/g, (match) => {
|
|
17
|
+
if (match === '%%') return '%';
|
|
18
|
+
if (argIndex >= args.length) return match;
|
|
19
|
+
const arg = args[argIndex++];
|
|
20
|
+
switch (match) {
|
|
21
|
+
case '%s': return String(arg);
|
|
22
|
+
case '%d': return String(Number(arg));
|
|
23
|
+
case '%q': return JSON.stringify(String(arg));
|
|
24
|
+
case '%v': return String(arg);
|
|
25
|
+
default: return match;
|
|
26
|
+
}
|
|
27
|
+
});
|
|
28
|
+
return result;
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
// New returns an error with the supplied message.
|
|
33
|
+
// New also records the stack trace at the point it was called.
|
|
34
|
+
export function New(message: string): $.GoError {
|
|
35
|
+
return new fundamental({msg: message, stack: callers()})
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// Errorf formats according to a format specifier and returns the string
|
|
39
|
+
// as a value that satisfies error.
|
|
40
|
+
// Errorf also records the stack trace at the point it was called.
|
|
41
|
+
export function Errorf(format: string, ...args: any[]): $.GoError {
|
|
42
|
+
return new fundamental({msg: fmt.Sprintf(format, ...args), stack: callers()})
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
class fundamental {
|
|
46
|
+
public get msg(): string {
|
|
47
|
+
return this._fields.msg.value
|
|
48
|
+
}
|
|
49
|
+
public set msg(value: string) {
|
|
50
|
+
this._fields.msg.value = value
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
public get stack(): $.VarRef<stack> | null {
|
|
54
|
+
return this._fields.stack.value
|
|
55
|
+
}
|
|
56
|
+
public set stack(value: $.VarRef<stack> | null) {
|
|
57
|
+
this._fields.stack.value = value
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
public _fields: {
|
|
61
|
+
msg: $.VarRef<string>;
|
|
62
|
+
stack: $.VarRef<$.VarRef<stack> | null>;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
constructor(init?: Partial<{msg?: string, stack?: $.VarRef<stack> | null}>) {
|
|
66
|
+
this._fields = {
|
|
67
|
+
msg: $.varRef(init?.msg ?? ""),
|
|
68
|
+
stack: $.varRef(init?.stack ?? null)
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
public clone(): fundamental {
|
|
73
|
+
const cloned = new fundamental()
|
|
74
|
+
cloned._fields = {
|
|
75
|
+
msg: $.varRef(this._fields.msg.value),
|
|
76
|
+
stack: $.varRef(this._fields.stack.value)
|
|
77
|
+
}
|
|
78
|
+
return cloned
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
public Error(): string {
|
|
82
|
+
const f = this
|
|
83
|
+
return f!.msg
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
public StackTrace(): StackTrace {
|
|
87
|
+
return null; // Simplified - no stack trace for now
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// Register this type with the runtime type system
|
|
91
|
+
static __typeInfo = $.registerStructType(
|
|
92
|
+
'fundamental',
|
|
93
|
+
new fundamental(),
|
|
94
|
+
[{ name: "Error", args: [], returns: [{ type: { kind: $.TypeKind.Basic, name: "string" } }] }],
|
|
95
|
+
fundamental,
|
|
96
|
+
{"msg": { kind: $.TypeKind.Basic, name: "string" }, "stack": { kind: $.TypeKind.Pointer, elemType: "stack" }}
|
|
97
|
+
);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// WithStack annotates err with a stack trace at the point WithStack was called.
|
|
101
|
+
// If err is nil, WithStack returns nil.
|
|
102
|
+
export function WithStack(err: $.GoError): $.GoError {
|
|
103
|
+
if (err == null) {
|
|
104
|
+
return null
|
|
105
|
+
}
|
|
106
|
+
return new withStack({error: err, stack: callers()})
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
class withStack {
|
|
110
|
+
public get error(): $.GoError {
|
|
111
|
+
return this._fields.error.value
|
|
112
|
+
}
|
|
113
|
+
public set error(value: $.GoError) {
|
|
114
|
+
this._fields.error.value = value
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
public get stack(): $.VarRef<stack> | null {
|
|
118
|
+
return this._fields.stack.value
|
|
119
|
+
}
|
|
120
|
+
public set stack(value: $.VarRef<stack> | null) {
|
|
121
|
+
this._fields.stack.value = value
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
public _fields: {
|
|
125
|
+
error: $.VarRef<$.GoError>;
|
|
126
|
+
stack: $.VarRef<$.VarRef<stack> | null>;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
constructor(init?: Partial<{error?: $.GoError, stack?: $.VarRef<stack> | null}>) {
|
|
130
|
+
this._fields = {
|
|
131
|
+
error: $.varRef(init?.error ?? null),
|
|
132
|
+
stack: $.varRef(init?.stack ?? null)
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
public clone(): withStack {
|
|
137
|
+
const cloned = new withStack()
|
|
138
|
+
cloned._fields = {
|
|
139
|
+
error: $.varRef(this._fields.error.value),
|
|
140
|
+
stack: $.varRef(this._fields.stack.value)
|
|
141
|
+
}
|
|
142
|
+
return cloned
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
public Cause(): $.GoError {
|
|
146
|
+
const w = this
|
|
147
|
+
return w!.error
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// Unwrap provides compatibility for Go 1.13 error chains.
|
|
151
|
+
public Unwrap(): $.GoError {
|
|
152
|
+
const w = this
|
|
153
|
+
return w!.error
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
public Error(): string {
|
|
157
|
+
return this.error?.Error() ?? ""
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
public StackTrace(): StackTrace {
|
|
161
|
+
return null; // Simplified - no stack trace for now
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
// Register this type with the runtime type system
|
|
165
|
+
static __typeInfo = $.registerStructType(
|
|
166
|
+
'withStack',
|
|
167
|
+
new withStack(),
|
|
168
|
+
[{ name: "Cause", args: [], returns: [{ type: { kind: $.TypeKind.Interface, name: 'GoError', methods: [{ name: 'Error', args: [], returns: [{ type: { kind: $.TypeKind.Basic, name: 'string' } }] }] } }] }, { name: "Unwrap", args: [], returns: [{ type: { kind: $.TypeKind.Interface, name: 'GoError', methods: [{ name: 'Error', args: [], returns: [{ type: { kind: $.TypeKind.Basic, name: 'string' } }] }] } }] }],
|
|
169
|
+
withStack,
|
|
170
|
+
{"error": { kind: $.TypeKind.Interface, name: 'GoError', methods: [{ name: 'Error', args: [], returns: [{ type: { kind: $.TypeKind.Basic, name: 'string' } }] }] }, "stack": { kind: $.TypeKind.Pointer, elemType: "stack" }}
|
|
171
|
+
);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
// Wrap returns an error annotating err with a stack trace
|
|
175
|
+
// at the point Wrap is called, and the supplied message.
|
|
176
|
+
// If err is nil, Wrap returns nil.
|
|
177
|
+
export function Wrap(err: $.GoError, message: string): $.GoError {
|
|
178
|
+
if (err == null) {
|
|
179
|
+
return null
|
|
180
|
+
}
|
|
181
|
+
const wrappedErr = new withMessage({cause: err, msg: message})
|
|
182
|
+
return new withStack({error: wrappedErr, stack: callers()})
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
// Wrapf returns an error annotating err with a stack trace
|
|
186
|
+
// at the point Wrapf is called, and the format specifier.
|
|
187
|
+
// If err is nil, Wrapf returns nil.
|
|
188
|
+
export function Wrapf(err: $.GoError, format: string, ...args: any[]): $.GoError {
|
|
189
|
+
if (err == null) {
|
|
190
|
+
return null
|
|
191
|
+
}
|
|
192
|
+
const wrappedErr = new withMessage({cause: err, msg: fmt.Sprintf(format, ...args)})
|
|
193
|
+
return new withStack({error: wrappedErr, stack: callers()})
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
// WithMessage annotates err with a new message.
|
|
197
|
+
// If err is nil, WithMessage returns nil.
|
|
198
|
+
export function WithMessage(err: $.GoError, message: string): $.GoError {
|
|
199
|
+
if (err == null) {
|
|
200
|
+
return null
|
|
201
|
+
}
|
|
202
|
+
return new withMessage({cause: err, msg: message})
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
// WithMessagef annotates err with the format specifier.
|
|
206
|
+
// If err is nil, WithMessagef returns nil.
|
|
207
|
+
export function WithMessagef(err: $.GoError, format: string, ...args: any[]): $.GoError {
|
|
208
|
+
if (err == null) {
|
|
209
|
+
return null
|
|
210
|
+
}
|
|
211
|
+
return new withMessage({cause: err, msg: fmt.Sprintf(format, ...args)})
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
class withMessage {
|
|
215
|
+
public get cause(): $.GoError {
|
|
216
|
+
return this._fields.cause.value
|
|
217
|
+
}
|
|
218
|
+
public set cause(value: $.GoError) {
|
|
219
|
+
this._fields.cause.value = value
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
public get msg(): string {
|
|
223
|
+
return this._fields.msg.value
|
|
224
|
+
}
|
|
225
|
+
public set msg(value: string) {
|
|
226
|
+
this._fields.msg.value = value
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
public _fields: {
|
|
230
|
+
cause: $.VarRef<$.GoError>;
|
|
231
|
+
msg: $.VarRef<string>;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
constructor(init?: Partial<{cause?: $.GoError, msg?: string}>) {
|
|
235
|
+
this._fields = {
|
|
236
|
+
cause: $.varRef(init?.cause ?? null),
|
|
237
|
+
msg: $.varRef(init?.msg ?? "")
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
public clone(): withMessage {
|
|
242
|
+
const cloned = new withMessage()
|
|
243
|
+
cloned._fields = {
|
|
244
|
+
cause: $.varRef(this._fields.cause.value),
|
|
245
|
+
msg: $.varRef(this._fields.msg.value)
|
|
246
|
+
}
|
|
247
|
+
return cloned
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
public Error(): string {
|
|
251
|
+
const w = this
|
|
252
|
+
return w!.msg + ": " + w!.cause!.Error()
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
public Cause(): $.GoError {
|
|
256
|
+
const w = this
|
|
257
|
+
return w!.cause
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
// Unwrap provides compatibility for Go 1.13 error chains.
|
|
261
|
+
public Unwrap(): $.GoError {
|
|
262
|
+
const w = this
|
|
263
|
+
return w!.cause
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
// Register this type with the runtime type system
|
|
267
|
+
static __typeInfo = $.registerStructType(
|
|
268
|
+
'withMessage',
|
|
269
|
+
new withMessage(),
|
|
270
|
+
[{ name: "Error", args: [], returns: [{ type: { kind: $.TypeKind.Basic, name: "string" } }] }, { name: "Cause", args: [], returns: [{ type: { kind: $.TypeKind.Interface, name: 'GoError', methods: [{ name: 'Error', args: [], returns: [{ type: { kind: $.TypeKind.Basic, name: 'string' } }] }] } }] }, { name: "Unwrap", args: [], returns: [{ type: { kind: $.TypeKind.Interface, name: 'GoError', methods: [{ name: 'Error', args: [], returns: [{ type: { kind: $.TypeKind.Basic, name: 'string' } }] }] } }] }],
|
|
271
|
+
withMessage,
|
|
272
|
+
{"cause": { kind: $.TypeKind.Interface, name: 'GoError', methods: [{ name: 'Error', args: [], returns: [{ type: { kind: $.TypeKind.Basic, name: 'string' } }] }] }, "msg": { kind: $.TypeKind.Basic, name: "string" }}
|
|
273
|
+
);
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
// Cause returns the underlying cause of the error, if possible.
|
|
277
|
+
// An error value has a cause if it implements the following
|
|
278
|
+
// interface:
|
|
279
|
+
//
|
|
280
|
+
// type causer interface {
|
|
281
|
+
// Cause() error
|
|
282
|
+
// }
|
|
283
|
+
//
|
|
284
|
+
// If the error does not implement Cause, the original error will
|
|
285
|
+
// be returned. If the error is nil, nil will be returned without further
|
|
286
|
+
// investigation.
|
|
287
|
+
export function Cause(err: $.GoError): $.GoError {
|
|
288
|
+
type causer = null | {
|
|
289
|
+
Cause(): $.GoError
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
$.registerInterfaceType(
|
|
293
|
+
'causer',
|
|
294
|
+
null, // Zero value for interface is null
|
|
295
|
+
[{ name: "Cause", args: [], returns: [{ type: { kind: $.TypeKind.Interface, name: 'GoError', methods: [{ name: 'Error', args: [], returns: [{ type: { kind: $.TypeKind.Basic, name: 'string' } }] }] } }] }]
|
|
296
|
+
);
|
|
297
|
+
|
|
298
|
+
for (; err != null; ) {
|
|
299
|
+
let { value: cause, ok: ok } = $.typeAssert<causer>(err, 'causer')
|
|
300
|
+
if (!ok) {
|
|
301
|
+
break
|
|
302
|
+
}
|
|
303
|
+
err = cause!.Cause()
|
|
304
|
+
}
|
|
305
|
+
return err
|
|
306
|
+
}
|
|
307
|
+
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import * as $ from "../../../builtin/builtin.js";
|
|
2
|
+
|
|
3
|
+
import * as stderrors from "../../../errors/index.js"
|
|
4
|
+
|
|
5
|
+
// Is reports whether any error in err's chain matches target.
|
|
6
|
+
//
|
|
7
|
+
// The chain consists of err itself followed by the sequence of errors obtained by
|
|
8
|
+
// repeatedly calling Unwrap.
|
|
9
|
+
//
|
|
10
|
+
// An error is considered to match a target if it is equal to that target or if
|
|
11
|
+
// it implements a method Is(error) bool such that Is(target) returns true.
|
|
12
|
+
export function Is(err: $.GoError, target: $.GoError): boolean {
|
|
13
|
+
return stderrors.Is(err, target)
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
// As finds the first error in err's chain that matches target, and if so, sets
|
|
17
|
+
// target to that error value and returns true.
|
|
18
|
+
//
|
|
19
|
+
// The chain consists of err itself followed by the sequence of errors obtained by
|
|
20
|
+
// repeatedly calling Unwrap.
|
|
21
|
+
//
|
|
22
|
+
// An error matches target if the error's concrete value is assignable to the value
|
|
23
|
+
// pointed to by target, or if the error has a method As(interface{}) bool such that
|
|
24
|
+
// As(target) returns true. In the latter case, the As method is responsible for
|
|
25
|
+
// setting target.
|
|
26
|
+
//
|
|
27
|
+
// As will panic if target is not a non-nil pointer to either a type that implements
|
|
28
|
+
// error, or to any interface type. As returns false if err is nil.
|
|
29
|
+
export function As(err: $.GoError, target: null | any): boolean {
|
|
30
|
+
return stderrors.As(err, target)
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// Unwrap returns the result of calling the Unwrap method on err, if err's
|
|
34
|
+
// type contains an Unwrap method returning error.
|
|
35
|
+
// Otherwise, Unwrap returns nil.
|
|
36
|
+
export function Unwrap(err: $.GoError): $.GoError {
|
|
37
|
+
return stderrors.Unwrap(err)
|
|
38
|
+
}
|
|
39
|
+
|