typenative 0.3.1 → 0.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (170) hide show
  1. package/.github/workflows/ci.yml +2 -0
  2. package/CHANGELOG.md +258 -0
  3. package/CONTRIBUTION.md +1 -1
  4. package/README.md +17 -1
  5. package/bin/cli.js +26 -11
  6. package/bin/go/bridge.js +74 -14
  7. package/bin/go/convert.js +59 -1
  8. package/bin/go/metadata.js +13 -4
  9. package/bin/go/overrides.js +4 -6
  10. package/bin/go/stdlib.generated.js +2 -0
  11. package/bin/index.js +338 -47
  12. package/bin/main.js +36 -14
  13. package/bin/node/assert.js +2 -2
  14. package/bin/node/buffer.js +9 -0
  15. package/bin/node/child_process.js +51 -3
  16. package/bin/node/cluster.js +41 -3
  17. package/bin/node/core.js +1 -1
  18. package/bin/node/crypto.js +205 -4
  19. package/bin/node/dgram.js +230 -6
  20. package/bin/node/dns.js +471 -8
  21. package/bin/node/errno_tables.js +445 -0
  22. package/bin/node/events.js +1 -1
  23. package/bin/node/fs.js +22 -15
  24. package/bin/node/fs_watch.js +407 -0
  25. package/bin/node/http.js +128 -30
  26. package/bin/node/http2.js +37 -7
  27. package/bin/node/module_v8.js +142 -0
  28. package/bin/node/net.js +9 -6
  29. package/bin/node/process.js +242 -0
  30. package/bin/node/readline.js +272 -1
  31. package/bin/node/registry.js +3 -1
  32. package/bin/node/sqlite.js +60 -12
  33. package/bin/node/stream.js +145 -10
  34. package/bin/node/test.js +311 -19
  35. package/bin/node/timers.js +1 -1
  36. package/bin/node/typed_arrays.js +608 -0
  37. package/bin/node/url.js +23 -5
  38. package/bin/node/util.js +21 -1
  39. package/bin/node/util_extra.js +6 -30
  40. package/bin/node/worker_threads.js +79 -19
  41. package/bin/node/zlib.js +131 -16
  42. package/bin/transpiler.js +4729 -616
  43. package/bin/typecheck-node.js +1 -0
  44. package/bin/version.js +1 -1
  45. package/conformance.md +1030 -877
  46. package/examples/README.md +54 -0
  47. package/examples/fibonacci/README.md +89 -0
  48. package/examples/fibonacci/bench.mjs +87 -0
  49. package/examples/fibonacci/main.ts +21 -0
  50. package/examples/hello-world/README.md +62 -0
  51. package/examples/hello-world/bench.mjs +66 -0
  52. package/examples/hello-world/main.ts +7 -0
  53. package/examples/json-server/README.md +127 -0
  54. package/examples/json-server/bench.mjs +263 -0
  55. package/examples/json-server/main.ts +67 -0
  56. package/govendor/github.com/andybalholm/brotli/LICENSE +19 -0
  57. package/govendor/github.com/andybalholm/brotli/README.md +19 -0
  58. package/govendor/github.com/andybalholm/brotli/backward_references.go +185 -0
  59. package/govendor/github.com/andybalholm/brotli/backward_references_hq.go +796 -0
  60. package/govendor/github.com/andybalholm/brotli/bit_cost.go +436 -0
  61. package/govendor/github.com/andybalholm/brotli/bit_reader.go +266 -0
  62. package/govendor/github.com/andybalholm/brotli/bitwriter.go +56 -0
  63. package/govendor/github.com/andybalholm/brotli/block_splitter.go +144 -0
  64. package/govendor/github.com/andybalholm/brotli/block_splitter_command.go +434 -0
  65. package/govendor/github.com/andybalholm/brotli/block_splitter_distance.go +433 -0
  66. package/govendor/github.com/andybalholm/brotli/block_splitter_literal.go +433 -0
  67. package/govendor/github.com/andybalholm/brotli/brotli_bit_stream.go +1435 -0
  68. package/govendor/github.com/andybalholm/brotli/cluster.go +30 -0
  69. package/govendor/github.com/andybalholm/brotli/cluster_command.go +164 -0
  70. package/govendor/github.com/andybalholm/brotli/cluster_distance.go +326 -0
  71. package/govendor/github.com/andybalholm/brotli/cluster_literal.go +326 -0
  72. package/govendor/github.com/andybalholm/brotli/command.go +254 -0
  73. package/govendor/github.com/andybalholm/brotli/compress_fragment.go +834 -0
  74. package/govendor/github.com/andybalholm/brotli/compress_fragment_two_pass.go +775 -0
  75. package/govendor/github.com/andybalholm/brotli/constants.go +77 -0
  76. package/govendor/github.com/andybalholm/brotli/context.go +2176 -0
  77. package/govendor/github.com/andybalholm/brotli/decode.go +2570 -0
  78. package/govendor/github.com/andybalholm/brotli/dictionary.go +122890 -0
  79. package/govendor/github.com/andybalholm/brotli/dictionary_hash.go +32779 -0
  80. package/govendor/github.com/andybalholm/brotli/encode.go +1220 -0
  81. package/govendor/github.com/andybalholm/brotli/encoder.go +177 -0
  82. package/govendor/github.com/andybalholm/brotli/encoder_dict.go +22 -0
  83. package/govendor/github.com/andybalholm/brotli/encoder_fast.go +201 -0
  84. package/govendor/github.com/andybalholm/brotli/entropy_encode.go +592 -0
  85. package/govendor/github.com/andybalholm/brotli/entropy_encode_static.go +4399 -0
  86. package/govendor/github.com/andybalholm/brotli/fast_log.go +290 -0
  87. package/govendor/github.com/andybalholm/brotli/find_match_length.go +45 -0
  88. package/govendor/github.com/andybalholm/brotli/go.mod +3 -0
  89. package/govendor/github.com/andybalholm/brotli/h10.go +287 -0
  90. package/govendor/github.com/andybalholm/brotli/h5.go +214 -0
  91. package/govendor/github.com/andybalholm/brotli/h6.go +216 -0
  92. package/govendor/github.com/andybalholm/brotli/hash.go +342 -0
  93. package/govendor/github.com/andybalholm/brotli/hash_composite.go +93 -0
  94. package/govendor/github.com/andybalholm/brotli/hash_forgetful_chain.go +252 -0
  95. package/govendor/github.com/andybalholm/brotli/hash_longest_match_quickly.go +214 -0
  96. package/govendor/github.com/andybalholm/brotli/hash_rolling.go +168 -0
  97. package/govendor/github.com/andybalholm/brotli/histogram.go +226 -0
  98. package/govendor/github.com/andybalholm/brotli/http.go +201 -0
  99. package/govendor/github.com/andybalholm/brotli/huffman.go +653 -0
  100. package/govendor/github.com/andybalholm/brotli/literal_cost.go +182 -0
  101. package/govendor/github.com/andybalholm/brotli/matchfinder/bargain1.go +212 -0
  102. package/govendor/github.com/andybalholm/brotli/matchfinder/bargain2.go +248 -0
  103. package/govendor/github.com/andybalholm/brotli/matchfinder/bargain3.go +278 -0
  104. package/govendor/github.com/andybalholm/brotli/matchfinder/emitter.go +34 -0
  105. package/govendor/github.com/andybalholm/brotli/matchfinder/m0.go +175 -0
  106. package/govendor/github.com/andybalholm/brotli/matchfinder/m4.go +339 -0
  107. package/govendor/github.com/andybalholm/brotli/matchfinder/matchfinder.go +103 -0
  108. package/govendor/github.com/andybalholm/brotli/matchfinder/pathfinder.go +328 -0
  109. package/govendor/github.com/andybalholm/brotli/matchfinder/textencoder.go +53 -0
  110. package/govendor/github.com/andybalholm/brotli/matchfinder/trio.go +327 -0
  111. package/govendor/github.com/andybalholm/brotli/matchfinder/zdfast.go +258 -0
  112. package/govendor/github.com/andybalholm/brotli/matchfinder/zfast.go +206 -0
  113. package/govendor/github.com/andybalholm/brotli/matchfinder/zm.go +302 -0
  114. package/govendor/github.com/andybalholm/brotli/memory.go +66 -0
  115. package/govendor/github.com/andybalholm/brotli/metablock.go +574 -0
  116. package/govendor/github.com/andybalholm/brotli/metablock_command.go +165 -0
  117. package/govendor/github.com/andybalholm/brotli/metablock_distance.go +165 -0
  118. package/govendor/github.com/andybalholm/brotli/metablock_literal.go +165 -0
  119. package/govendor/github.com/andybalholm/brotli/params.go +37 -0
  120. package/govendor/github.com/andybalholm/brotli/platform.go +103 -0
  121. package/govendor/github.com/andybalholm/brotli/prefix.go +30 -0
  122. package/govendor/github.com/andybalholm/brotli/prefix_dec.go +723 -0
  123. package/govendor/github.com/andybalholm/brotli/quality.go +196 -0
  124. package/govendor/github.com/andybalholm/brotli/reader.go +127 -0
  125. package/govendor/github.com/andybalholm/brotli/ringbuffer.go +134 -0
  126. package/govendor/github.com/andybalholm/brotli/state.go +313 -0
  127. package/govendor/github.com/andybalholm/brotli/static_dict.go +662 -0
  128. package/govendor/github.com/andybalholm/brotli/static_dict_lut.go +75094 -0
  129. package/govendor/github.com/andybalholm/brotli/symbol_list.go +22 -0
  130. package/govendor/github.com/andybalholm/brotli/transform.go +641 -0
  131. package/govendor/github.com/andybalholm/brotli/utf8_util.go +70 -0
  132. package/govendor/github.com/andybalholm/brotli/util.go +7 -0
  133. package/govendor/github.com/andybalholm/brotli/write_bits.go +52 -0
  134. package/govendor/github.com/andybalholm/brotli/writer.go +162 -0
  135. package/govendor/github.com/fsnotify/fsnotify/.mailmap +2 -0
  136. package/govendor/github.com/fsnotify/fsnotify/CHANGELOG.md +651 -0
  137. package/govendor/github.com/fsnotify/fsnotify/CONTRIBUTING.md +147 -0
  138. package/govendor/github.com/fsnotify/fsnotify/LICENSE +25 -0
  139. package/govendor/github.com/fsnotify/fsnotify/README.md +214 -0
  140. package/govendor/github.com/fsnotify/fsnotify/backend_fen.go +469 -0
  141. package/govendor/github.com/fsnotify/fsnotify/backend_inotify.go +597 -0
  142. package/govendor/github.com/fsnotify/fsnotify/backend_kqueue.go +735 -0
  143. package/govendor/github.com/fsnotify/fsnotify/backend_other.go +22 -0
  144. package/govendor/github.com/fsnotify/fsnotify/backend_windows.go +702 -0
  145. package/govendor/github.com/fsnotify/fsnotify/fsnotify.go +526 -0
  146. package/govendor/github.com/fsnotify/fsnotify/go.mod +3 -0
  147. package/govendor/github.com/fsnotify/fsnotify/internal/darwin.go +20 -0
  148. package/govendor/github.com/fsnotify/fsnotify/internal/debug_darwin.go +15 -0
  149. package/govendor/github.com/fsnotify/fsnotify/internal/debug_dragonfly.go +15 -0
  150. package/govendor/github.com/fsnotify/fsnotify/internal/debug_freebsd.go +20 -0
  151. package/govendor/github.com/fsnotify/fsnotify/internal/debug_kqueue.go +32 -0
  152. package/govendor/github.com/fsnotify/fsnotify/internal/debug_linux.go +56 -0
  153. package/govendor/github.com/fsnotify/fsnotify/internal/debug_netbsd.go +15 -0
  154. package/govendor/github.com/fsnotify/fsnotify/internal/debug_openbsd.go +16 -0
  155. package/govendor/github.com/fsnotify/fsnotify/internal/debug_solaris.go +45 -0
  156. package/govendor/github.com/fsnotify/fsnotify/internal/debug_windows.go +40 -0
  157. package/govendor/github.com/fsnotify/fsnotify/internal/freebsd.go +20 -0
  158. package/govendor/github.com/fsnotify/fsnotify/internal/internal.go +2 -0
  159. package/govendor/github.com/fsnotify/fsnotify/internal/unix.go +20 -0
  160. package/govendor/github.com/fsnotify/fsnotify/internal/unix2.go +25 -0
  161. package/govendor/github.com/fsnotify/fsnotify/internal/windows.go +40 -0
  162. package/govendor/github.com/fsnotify/fsnotify/shared.go +64 -0
  163. package/govendor/github.com/fsnotify/fsnotify/staticcheck.conf +3 -0
  164. package/govendor/github.com/fsnotify/fsnotify/system_bsd.go +7 -0
  165. package/govendor/github.com/fsnotify/fsnotify/system_darwin.go +8 -0
  166. package/govendor/manifest.txt +3 -1
  167. package/package.json +1 -1
  168. package/types/typenative-go.d.ts +23 -0
  169. package/types/typenative-npm.d.ts +241 -5
  170. package/types/typenative.d.ts +302 -12
@@ -0,0 +1,608 @@
1
+ // Typed arrays other than Uint8Array (which is Go []byte, shared with Buffer),
2
+ // DataView and ArrayBuffer.
3
+ //
4
+ // Int8Array … Float64Array are one generic Go type, TnTypedArray[T], over a
5
+ // slice of the element type (TnInt32Array = TnTypedArray[int32], …). A view
6
+ // built on an ArrayBuffer (a Go []byte) shares its memory in the platform's
7
+ // byte order, as in JS. Elements read as numbers and writes convert like JS
8
+ // (ToInt32 wrapping, Uint8ClampedArray rounding / clamping). Methods taking a
9
+ // callback (map, filter, reduce …) are inlined by the transpiler over
10
+ // tnFloats(); the rest are Go methods here.
11
+ import { defineClass, defineHelper, setResults } from './registry.js';
12
+ // Typed array constructors and their Go element types (BYTES_PER_ELEMENT is the size)
13
+ export const TYPED_ARRAY_KINDS = {
14
+ Int8Array: 'int8',
15
+ Uint8ClampedArray: 'uint8',
16
+ Int16Array: 'int16',
17
+ Uint16Array: 'uint16',
18
+ Int32Array: 'int32',
19
+ Uint32Array: 'uint32',
20
+ Float32Array: 'float32',
21
+ Float64Array: 'float64'
22
+ };
23
+ const ELEMENT_SIZES = {
24
+ int8: 1,
25
+ uint8: 1,
26
+ int16: 2,
27
+ uint16: 2,
28
+ int32: 4,
29
+ uint32: 4,
30
+ float32: 4,
31
+ float64: 8
32
+ };
33
+ // Whether a Go type is one of the typed arrays above
34
+ export function isTypedArrayGoType(goType) {
35
+ if (!goType || !goType.startsWith('*Tn'))
36
+ return false;
37
+ return Object.keys(TYPED_ARRAY_KINDS).includes(goType.slice(3));
38
+ }
39
+ const typedArrayHelper = `// Typed arrays (Int8Array … Float64Array): a slice of the element type
40
+ type tnTypedElem interface {
41
+ int8 | uint8 | int16 | uint16 | int32 | uint32 | float32 | float64
42
+ }
43
+
44
+ type TnTypedArray[T tnTypedElem] struct {
45
+ data []T
46
+ kind string
47
+ clamped bool
48
+ }
49
+
50
+ type TnInt8Array = TnTypedArray[int8]
51
+ type TnUint8ClampedArray = TnTypedArray[uint8]
52
+ type TnInt16Array = TnTypedArray[int16]
53
+ type TnUint16Array = TnTypedArray[uint16]
54
+ type TnInt32Array = TnTypedArray[int32]
55
+ type TnUint32Array = TnTypedArray[uint32]
56
+ type TnFloat32Array = TnTypedArray[float32]
57
+ type TnFloat64Array = TnTypedArray[float64]
58
+
59
+ func tnTypedSize[T tnTypedElem]() int {
60
+ var zero T
61
+ return int(unsafe.Sizeof(zero))
62
+ }
63
+
64
+ // A number converted to the element type the way JS stores it: floats round,
65
+ // integers wrap modulo 2^n (NaN and ±Infinity store 0), clamped bytes round
66
+ // half to even into 0..255
67
+ func tnTypedConv[T tnTypedElem](v float64, clamped bool) T {
68
+ var zero T
69
+ switch any(zero).(type) {
70
+ case float32, float64:
71
+ return T(v)
72
+ }
73
+ if clamped {
74
+ if !(v > 0) {
75
+ return 0
76
+ }
77
+ if v >= 255 {
78
+ top := uint8(255)
79
+ return T(top)
80
+ }
81
+ return T(math.RoundToEven(v))
82
+ }
83
+ if math.IsNaN(v) || math.IsInf(v, 0) {
84
+ return 0
85
+ }
86
+ m := math.Mod(math.Trunc(v), 4294967296)
87
+ if m < 0 {
88
+ m += 4294967296
89
+ }
90
+ return T(uint32(m))
91
+ }
92
+
93
+ func tnTypedRangeError(message string) *TnError {
94
+ return TnNodeError("RangeError", "", message)
95
+ }
96
+
97
+ // The numbers of an array-like source (arrays, Uint8Arrays, typed arrays)
98
+ func tnTypedFloats(src interface{}) []float64 {
99
+ switch s := src.(type) {
100
+ case []float64:
101
+ return s
102
+ case *[]float64:
103
+ return *s
104
+ case []byte:
105
+ out := make([]float64, len(s))
106
+ for i, b := range s {
107
+ out[i] = float64(b)
108
+ }
109
+ return out
110
+ case []interface{}:
111
+ out := make([]float64, len(s))
112
+ for i, v := range s {
113
+ out[i] = TnNumber(v)
114
+ }
115
+ return out
116
+ case interface{ tnFloats() []float64 }:
117
+ return s.tnFloats()
118
+ }
119
+ panic(TnNodeError("TypeError", "", "Cannot convert argument to a typed array"))
120
+ }
121
+
122
+ func tnTypedOf[T tnTypedElem](kind string, clamped bool, values []float64) *TnTypedArray[T] {
123
+ data := make([]T, len(values))
124
+ for i, v := range values {
125
+ data[i] = tnTypedConv[T](v, clamped)
126
+ }
127
+ return &TnTypedArray[T]{data: data, kind: kind, clamped: clamped}
128
+ }
129
+
130
+ // new XArray(), new XArray(length), new XArray(array-like or typed array): a copy
131
+ func TnNewTypedArray[T tnTypedElem](kind string, clamped bool, src ...interface{}) *TnTypedArray[T] {
132
+ if len(src) == 0 || src[0] == nil {
133
+ return &TnTypedArray[T]{data: []T{}, kind: kind, clamped: clamped}
134
+ }
135
+ if n, ok := src[0].(float64); ok {
136
+ if !(n >= 0) || n != math.Trunc(n) || n > 4294967295 {
137
+ panic(tnTypedRangeError("Invalid typed array length: " + TnNumStr(n)))
138
+ }
139
+ return &TnTypedArray[T]{data: make([]T, int(n)), kind: kind, clamped: clamped}
140
+ }
141
+ return tnTypedOf[T](kind, clamped, tnTypedFloats(src[0]))
142
+ }
143
+
144
+ // new XArray(buffer[, byteOffset[, length]]): a view sharing the buffer's memory
145
+ func TnTypedArrayView[T tnTypedElem](kind string, clamped bool, buf []byte, args ...float64) *TnTypedArray[T] {
146
+ size := tnTypedSize[T]()
147
+ offset := 0
148
+ if len(args) > 0 {
149
+ offset = int(args[0])
150
+ }
151
+ if offset%size != 0 {
152
+ panic(tnTypedRangeError("start offset of " + kind + " should be a multiple of " + strconv.Itoa(size)))
153
+ }
154
+ if offset > len(buf) {
155
+ panic(tnTypedRangeError("Start offset " + strconv.Itoa(offset) + " is outside the bounds of the buffer"))
156
+ }
157
+ length := 0
158
+ if len(args) > 1 {
159
+ length = int(args[1])
160
+ if length < 0 || offset+length*size > len(buf) {
161
+ panic(tnTypedRangeError("Invalid typed array length: " + strconv.Itoa(length)))
162
+ }
163
+ } else {
164
+ if (len(buf)-offset)%size != 0 {
165
+ panic(tnTypedRangeError("byte length of " + kind + " should be a multiple of " + strconv.Itoa(size)))
166
+ }
167
+ length = (len(buf) - offset) / size
168
+ }
169
+ if length == 0 {
170
+ return &TnTypedArray[T]{data: []T{}, kind: kind, clamped: clamped}
171
+ }
172
+ data := unsafe.Slice((*T)(unsafe.Pointer(&buf[offset])), length)
173
+ return &TnTypedArray[T]{data: data, kind: kind, clamped: clamped}
174
+ }
175
+
176
+ // XArray.from(source[, mapFn]) / XArray.of(...items)
177
+ func TnTypedArrayFrom[T tnTypedElem](kind string, clamped bool, src interface{}, mapFn ...interface{}) *TnTypedArray[T] {
178
+ values := append([]float64{}, tnTypedFloats(src)...)
179
+ if len(mapFn) > 0 && mapFn[0] != nil {
180
+ for i, v := range values {
181
+ values[i] = TnNumber(TnInvoke(mapFn[0], v, float64(i)))
182
+ }
183
+ }
184
+ return tnTypedOf[T](kind, clamped, values)
185
+ }
186
+
187
+ func TnTypedArrayOf[T tnTypedElem](kind string, clamped bool, items ...float64) *TnTypedArray[T] {
188
+ return tnTypedOf[T](kind, clamped, items)
189
+ }
190
+
191
+ // Element reads: out-of-range and non-integer indices read 0 (undefined in JS)
192
+ func (a *TnTypedArray[T]) Get(i float64) float64 {
193
+ if !(i >= 0) || i >= float64(len(a.data)) || i != math.Trunc(i) {
194
+ return 0
195
+ }
196
+ return float64(a.data[int(i)])
197
+ }
198
+
199
+ // Element writes: out-of-range indices are ignored, as in JS
200
+ func (a *TnTypedArray[T]) Set(i float64, v float64) float64 {
201
+ if i >= 0 && i < float64(len(a.data)) && i == math.Trunc(i) {
202
+ a.data[int(i)] = tnTypedConv[T](v, a.clamped)
203
+ }
204
+ return v
205
+ }
206
+
207
+ func (a *TnTypedArray[T]) tnFloats() []float64 {
208
+ out := make([]float64, len(a.data))
209
+ for i, v := range a.data {
210
+ out[i] = float64(v)
211
+ }
212
+ return out
213
+ }
214
+
215
+ // A new typed array of the same kind (map / filter results)
216
+ func (a *TnTypedArray[T]) tnLike(values []float64) *TnTypedArray[T] {
217
+ return tnTypedOf[T](a.kind, a.clamped, values)
218
+ }
219
+
220
+ func (a *TnTypedArray[T]) Get_length() float64 { return float64(len(a.data)) }
221
+ func (a *TnTypedArray[T]) Get_byteLength() float64 { return float64(len(a.data) * tnTypedSize[T]()) }
222
+ func (a *TnTypedArray[T]) Get_byteOffset() float64 { return 0 }
223
+ func (a *TnTypedArray[T]) Get_BYTES_PER_ELEMENT() float64 { return float64(tnTypedSize[T]()) }
224
+
225
+ // The view's bytes (its byteOffset is 0: .buffer starts where the view does)
226
+ func (a *TnTypedArray[T]) Get_buffer() []byte {
227
+ if len(a.data) == 0 {
228
+ return []byte{}
229
+ }
230
+ return unsafe.Slice((*byte)(unsafe.Pointer(&a.data[0])), len(a.data)*tnTypedSize[T]())
231
+ }
232
+
233
+ // Relative start / end arguments (negative counts from the end), clamped
234
+ func tnTypedBounds(n int, args []float64) (int, int) {
235
+ rel := func(v float64) int {
236
+ if math.IsNaN(v) {
237
+ return 0
238
+ }
239
+ if v < 0 {
240
+ return max(n+int(math.Max(v, -1e15)), 0)
241
+ }
242
+ return min(int(math.Min(v, 1e15)), n)
243
+ }
244
+ start, end := 0, n
245
+ if len(args) > 0 {
246
+ start = rel(args[0])
247
+ }
248
+ if len(args) > 1 {
249
+ end = rel(args[1])
250
+ }
251
+ if end < start {
252
+ end = start
253
+ }
254
+ return start, end
255
+ }
256
+
257
+ func (a *TnTypedArray[T]) fill(v float64, args ...float64) *TnTypedArray[T] {
258
+ start, end := tnTypedBounds(len(a.data), args)
259
+ value := tnTypedConv[T](v, a.clamped)
260
+ for i := start; i < end; i++ {
261
+ a.data[i] = value
262
+ }
263
+ return a
264
+ }
265
+
266
+ // set(source[, offset]): copies an array-like in (overlapping views work)
267
+ func (a *TnTypedArray[T]) set(src interface{}, offset ...float64) {
268
+ at := 0
269
+ if len(offset) > 0 {
270
+ at = int(offset[0])
271
+ }
272
+ values := tnTypedFloats(src)
273
+ if at < 0 || at+len(values) > len(a.data) {
274
+ panic(tnTypedRangeError("offset is out of bounds"))
275
+ }
276
+ for i, v := range values {
277
+ a.data[at+i] = tnTypedConv[T](v, a.clamped)
278
+ }
279
+ }
280
+
281
+ // subarray shares memory; slice copies
282
+ func (a *TnTypedArray[T]) subarray(args ...float64) *TnTypedArray[T] {
283
+ start, end := tnTypedBounds(len(a.data), args)
284
+ return &TnTypedArray[T]{data: a.data[start:end:end], kind: a.kind, clamped: a.clamped}
285
+ }
286
+
287
+ func (a *TnTypedArray[T]) slice(args ...float64) *TnTypedArray[T] {
288
+ start, end := tnTypedBounds(len(a.data), args)
289
+ return &TnTypedArray[T]{data: append([]T{}, a.data[start:end]...), kind: a.kind, clamped: a.clamped}
290
+ }
291
+
292
+ func (a *TnTypedArray[T]) at(i float64) float64 {
293
+ if i < 0 {
294
+ i += float64(len(a.data))
295
+ }
296
+ return a.Get(math.Trunc(i))
297
+ }
298
+
299
+ func (a *TnTypedArray[T]) indexOf(v float64, from ...float64) float64 {
300
+ start, _ := tnTypedBounds(len(a.data), from)
301
+ for i := start; i < len(a.data); i++ {
302
+ if float64(a.data[i]) == v {
303
+ return float64(i)
304
+ }
305
+ }
306
+ return -1
307
+ }
308
+
309
+ func (a *TnTypedArray[T]) lastIndexOf(v float64, from ...float64) float64 {
310
+ end := len(a.data) - 1
311
+ if len(from) > 0 {
312
+ f := math.Trunc(from[0])
313
+ if f < 0 {
314
+ f += float64(len(a.data))
315
+ }
316
+ end = min(int(f), end)
317
+ }
318
+ for i := end; i >= 0; i-- {
319
+ if float64(a.data[i]) == v {
320
+ return float64(i)
321
+ }
322
+ }
323
+ return -1
324
+ }
325
+
326
+ // includes uses SameValueZero: NaN finds NaN
327
+ func (a *TnTypedArray[T]) includes(v float64, from ...float64) bool {
328
+ start, _ := tnTypedBounds(len(a.data), from)
329
+ for i := start; i < len(a.data); i++ {
330
+ x := float64(a.data[i])
331
+ if x == v || (x != x && v != v) {
332
+ return true
333
+ }
334
+ }
335
+ return false
336
+ }
337
+
338
+ func (a *TnTypedArray[T]) join(sep ...string) string {
339
+ separator := ","
340
+ if len(sep) > 0 {
341
+ separator = sep[0]
342
+ }
343
+ parts := make([]string, len(a.data))
344
+ for i, v := range a.data {
345
+ parts[i] = TnNumStr(float64(v))
346
+ }
347
+ return strings.Join(parts, separator)
348
+ }
349
+
350
+ func (a *TnTypedArray[T]) toString() string { return a.join(",") }
351
+ func (a *TnTypedArray[T]) toLocaleString() string { return a.join(",") }
352
+ func (a *TnTypedArray[T]) String() string { return a.join(",") }
353
+
354
+ func (a *TnTypedArray[T]) reverse() *TnTypedArray[T] {
355
+ for i, j := 0, len(a.data)-1; i < j; i, j = i+1, j-1 {
356
+ a.data[i], a.data[j] = a.data[j], a.data[i]
357
+ }
358
+ return a
359
+ }
360
+
361
+ func (a *TnTypedArray[T]) toReversed() *TnTypedArray[T] {
362
+ return a.slice().reverse()
363
+ }
364
+
365
+ // sort([compareFn]): numeric order by default (NaN last)
366
+ func (a *TnTypedArray[T]) sort(compare ...interface{}) *TnTypedArray[T] {
367
+ if len(compare) > 0 && compare[0] != nil {
368
+ cmp := compare[0]
369
+ sort.SliceStable(a.data, func(i, j int) bool {
370
+ x, y := float64(a.data[i]), float64(a.data[j])
371
+ if f, ok := cmp.(func(float64, float64) float64); ok {
372
+ return f(x, y) < 0
373
+ }
374
+ return TnNumber(TnInvoke(cmp, x, y)) < 0
375
+ })
376
+ return a
377
+ }
378
+ sort.SliceStable(a.data, func(i, j int) bool {
379
+ x, y := float64(a.data[i]), float64(a.data[j])
380
+ if x != x {
381
+ return false
382
+ }
383
+ if y != y {
384
+ return true
385
+ }
386
+ if x == 0 && y == 0 {
387
+ return math.Signbit(x) && !math.Signbit(y)
388
+ }
389
+ return x < y
390
+ })
391
+ return a
392
+ }
393
+
394
+ func (a *TnTypedArray[T]) toSorted(compare ...interface{}) *TnTypedArray[T] {
395
+ return a.slice().sort(compare...)
396
+ }
397
+
398
+ func (a *TnTypedArray[T]) with(i float64, v float64) *TnTypedArray[T] {
399
+ index := math.Trunc(i)
400
+ if index < 0 {
401
+ index += float64(len(a.data))
402
+ }
403
+ if !(index >= 0) || index >= float64(len(a.data)) {
404
+ panic(tnTypedRangeError("Invalid typed array index"))
405
+ }
406
+ out := a.slice()
407
+ out.data[int(index)] = tnTypedConv[T](v, a.clamped)
408
+ return out
409
+ }
410
+
411
+ func (a *TnTypedArray[T]) copyWithin(target float64, args ...float64) *TnTypedArray[T] {
412
+ n := len(a.data)
413
+ to, _ := tnTypedBounds(n, []float64{target})
414
+ start, end := tnTypedBounds(n, args)
415
+ copy(a.data[to:], a.data[start:end])
416
+ return a
417
+ }
418
+
419
+ func (a *TnTypedArray[T]) keys() []float64 {
420
+ out := make([]float64, len(a.data))
421
+ for i := range a.data {
422
+ out[i] = float64(i)
423
+ }
424
+ return out
425
+ }
426
+
427
+ func (a *TnTypedArray[T]) values() []float64 { return a.tnFloats() }
428
+
429
+ // util.inspect / console.log: Int32Array(3) [ 1, 2, 3 ]
430
+ func (a *TnTypedArray[T]) TnTypedInspect() (string, []float64) {
431
+ return a.kind + "(" + strconv.Itoa(len(a.data)) + ")", a.tnFloats()
432
+ }
433
+
434
+ // ArrayBuffer.isView: typed arrays, DataViews and Uint8Arrays
435
+ func TnIsView(v interface{}) bool {
436
+ switch v.(type) {
437
+ case []byte, *TnDataView:
438
+ return true
439
+ case interface{ tnFloats() []float64 }:
440
+ return true
441
+ }
442
+ return false
443
+ }
444
+
445
+ // DataView: typed reads and writes at byte offsets of an ArrayBuffer,
446
+ // big-endian unless littleEndian is true
447
+ type TnDataView struct {
448
+ buf []byte
449
+ offset int
450
+ length int
451
+ }
452
+
453
+ func TnNewDataView(buf []byte, args ...float64) *TnDataView {
454
+ offset := 0
455
+ if len(args) > 0 {
456
+ offset = int(args[0])
457
+ }
458
+ if offset < 0 || offset > len(buf) {
459
+ panic(tnTypedRangeError("Start offset " + strconv.Itoa(offset) + " is outside the bounds of the buffer"))
460
+ }
461
+ length := len(buf) - offset
462
+ if len(args) > 1 {
463
+ length = int(args[1])
464
+ if length < 0 || offset+length > len(buf) {
465
+ panic(tnTypedRangeError("Invalid DataView length " + strconv.Itoa(length)))
466
+ }
467
+ }
468
+ return &TnDataView{buf: buf, offset: offset, length: length}
469
+ }
470
+
471
+ func (d *TnDataView) Get_buffer() []byte { return d.buf }
472
+ func (d *TnDataView) Get_byteOffset() float64 { return float64(d.offset) }
473
+ func (d *TnDataView) Get_byteLength() float64 { return float64(d.length) }
474
+
475
+ func (d *TnDataView) at(offset float64, size int) []byte {
476
+ if !(offset >= 0) || offset != math.Trunc(offset) || int(offset)+size > d.length {
477
+ panic(tnTypedRangeError("Offset is outside the bounds of the DataView"))
478
+ }
479
+ start := d.offset + int(offset)
480
+ return d.buf[start : start+size]
481
+ }
482
+
483
+ func tnDataViewOrder(little []bool) binary.ByteOrder {
484
+ if len(little) > 0 && little[0] {
485
+ return binary.LittleEndian
486
+ }
487
+ return binary.BigEndian
488
+ }
489
+
490
+ func (d *TnDataView) getInt8(o float64) float64 { return float64(int8(d.at(o, 1)[0])) }
491
+ func (d *TnDataView) getUint8(o float64) float64 { return float64(d.at(o, 1)[0]) }
492
+ func (d *TnDataView) getInt16(o float64, little ...bool) float64 {
493
+ return float64(int16(tnDataViewOrder(little).Uint16(d.at(o, 2))))
494
+ }
495
+ func (d *TnDataView) getUint16(o float64, little ...bool) float64 {
496
+ return float64(tnDataViewOrder(little).Uint16(d.at(o, 2)))
497
+ }
498
+ func (d *TnDataView) getInt32(o float64, little ...bool) float64 {
499
+ return float64(int32(tnDataViewOrder(little).Uint32(d.at(o, 4))))
500
+ }
501
+ func (d *TnDataView) getUint32(o float64, little ...bool) float64 {
502
+ return float64(tnDataViewOrder(little).Uint32(d.at(o, 4)))
503
+ }
504
+ func (d *TnDataView) getFloat32(o float64, little ...bool) float64 {
505
+ return float64(math.Float32frombits(tnDataViewOrder(little).Uint32(d.at(o, 4))))
506
+ }
507
+ func (d *TnDataView) getFloat64(o float64, little ...bool) float64 {
508
+ return math.Float64frombits(tnDataViewOrder(little).Uint64(d.at(o, 8)))
509
+ }
510
+ func (d *TnDataView) getBigInt64(o float64, little ...bool) *tnbig.Int {
511
+ return tnbig.NewInt(int64(tnDataViewOrder(little).Uint64(d.at(o, 8))))
512
+ }
513
+ func (d *TnDataView) getBigUint64(o float64, little ...bool) *tnbig.Int {
514
+ return new(tnbig.Int).SetUint64(tnDataViewOrder(little).Uint64(d.at(o, 8)))
515
+ }
516
+
517
+ func (d *TnDataView) setInt8(o float64, v float64) { d.at(o, 1)[0] = byte(tnTypedConv[uint8](v, false)) }
518
+ func (d *TnDataView) setUint8(o float64, v float64) { d.at(o, 1)[0] = tnTypedConv[uint8](v, false) }
519
+ func (d *TnDataView) setInt16(o float64, v float64, little ...bool) {
520
+ tnDataViewOrder(little).PutUint16(d.at(o, 2), tnTypedConv[uint16](v, false))
521
+ }
522
+ func (d *TnDataView) setUint16(o float64, v float64, little ...bool) {
523
+ tnDataViewOrder(little).PutUint16(d.at(o, 2), tnTypedConv[uint16](v, false))
524
+ }
525
+ func (d *TnDataView) setInt32(o float64, v float64, little ...bool) {
526
+ tnDataViewOrder(little).PutUint32(d.at(o, 4), tnTypedConv[uint32](v, false))
527
+ }
528
+ func (d *TnDataView) setUint32(o float64, v float64, little ...bool) {
529
+ tnDataViewOrder(little).PutUint32(d.at(o, 4), tnTypedConv[uint32](v, false))
530
+ }
531
+ func (d *TnDataView) setFloat32(o float64, v float64, little ...bool) {
532
+ tnDataViewOrder(little).PutUint32(d.at(o, 4), math.Float32bits(float32(v)))
533
+ }
534
+ func (d *TnDataView) setFloat64(o float64, v float64, little ...bool) {
535
+ tnDataViewOrder(little).PutUint64(d.at(o, 8), math.Float64bits(v))
536
+ }
537
+
538
+ // BigInt writes wrap modulo 2^64 (BigInt.asUintN(64, v))
539
+ func tnDataViewUint64(v *tnbig.Int) uint64 {
540
+ m := new(tnbig.Int).And(v, new(tnbig.Int).SetUint64(math.MaxUint64))
541
+ return m.Uint64()
542
+ }
543
+ func (d *TnDataView) setBigInt64(o float64, v *tnbig.Int, little ...bool) {
544
+ tnDataViewOrder(little).PutUint64(d.at(o, 8), tnDataViewUint64(v))
545
+ }
546
+ func (d *TnDataView) setBigUint64(o float64, v *tnbig.Int, little ...bool) {
547
+ tnDataViewOrder(little).PutUint64(d.at(o, 8), tnDataViewUint64(v))
548
+ }
549
+
550
+ // ArrayBuffer#slice copies (a Uint8Array's subarray / Buffer's slice share)
551
+ func TnArrayBufferSlice(b []byte, args ...float64) []byte {
552
+ start, end := tnTypedBounds(len(b), args)
553
+ return append([]byte{}, b[start:end]...)
554
+ }`;
555
+ export function registerTypedArrays(r) {
556
+ defineHelper(r, 'typedArrays', ['encoding/binary', 'math', 'math/big', 'sort', 'strconv', 'strings', 'unsafe'], ['error', 'dynamic', 'nodeInvoke'], typedArrayHelper);
557
+ const use = () => r.helper('typedArrays');
558
+ for (const name of Object.keys(TYPED_ARRAY_KINDS)) {
559
+ const elem = TYPED_ARRAY_KINDS[name];
560
+ const goType = `*Tn${name}`;
561
+ const key = `globals.${name}`;
562
+ const clamped = name === 'Uint8ClampedArray' ? 'true' : 'false';
563
+ const generic = `[${elem}](${JSON.stringify(name)}, ${clamped}`;
564
+ defineClass(r, key, goType, 'class', `fill=${goType}; set=; subarray=${goType}; slice=${goType}; at=float64; indexOf=float64; lastIndexOf=float64; includes=bool; join=string; toString=string; toLocaleString=string; reverse=${goType}; toReversed=${goType}; sort=${goType}; toSorted=${goType}; with=${goType}; copyWithin=${goType}; keys=[]float64; values=[]float64; tnFloats=[]float64`, '', 'length=float64; byteLength=float64; byteOffset=float64; BYTES_PER_ELEMENT=float64; buffer=[]byte', '');
565
+ r.globals[name] = key;
566
+ r.iterators[goType] = 'tnFloats';
567
+ r.paramTypers[`${key}#sort`] = (_n) => ['float64', 'float64'];
568
+ r.paramTypers[`${key}#toSorted`] = r.paramTypers[`${key}#sort`];
569
+ // new Int32Array(length | array-like) copies; new Int32Array(buffer, …) views
570
+ r.constructors[key] = (call) => {
571
+ use();
572
+ if (call.args.length > 0 && r.isArrayBuffer(call.nodes[0])) {
573
+ return `TnTypedArrayView${generic}, ${call.args.join(', ')})`;
574
+ }
575
+ return `TnNewTypedArray${generic}${call.args.length > 0 ? ', ' + call.args[0] : ''})`;
576
+ };
577
+ r.functions[`${key}.from`] = (call) => {
578
+ use();
579
+ return `TnTypedArrayFrom${generic}, ${call.args.join(', ')})`;
580
+ };
581
+ r.paramTypers[`${key}.from`] = (_n) => ['float64', 'float64'];
582
+ r.functions[`${key}.of`] = (call) => {
583
+ use();
584
+ return `TnTypedArrayOf${generic}${call.args.length > 0 ? ', ' + call.args.join(', ') : ''})`;
585
+ };
586
+ setResults(r, key, `from=${goType}; of=${goType}`);
587
+ r.values[`${key}.BYTES_PER_ELEMENT`] = (_c) => `float64(${ELEMENT_SIZES[elem]})`;
588
+ r.valueTypes[`${key}.BYTES_PER_ELEMENT`] = 'float64';
589
+ }
590
+ defineClass(r, 'globals.DataView', '*TnDataView', 'class', 'getInt8=float64; getUint8=float64; getInt16=float64; getUint16=float64; getInt32=float64; getUint32=float64; getFloat32=float64; getFloat64=float64; getBigInt64=*tnbig.Int; getBigUint64=*tnbig.Int; setInt8=; setUint8=; setInt16=; setUint16=; setInt32=; setUint32=; setFloat32=; setFloat64=; setBigInt64=; setBigUint64=', '', 'buffer=[]byte; byteOffset=float64; byteLength=float64', '');
591
+ r.globals['DataView'] = 'globals.DataView';
592
+ r.constructors['globals.DataView'] = (call) => {
593
+ use();
594
+ return `TnNewDataView(${call.args.join(', ')})`;
595
+ };
596
+ // ArrayBuffer is Go []byte too: new ArrayBuffer(n), ArrayBuffer.isView
597
+ defineClass(r, 'globals.ArrayBuffer', '[]byte', 'Buffer', '', '', '', '');
598
+ r.classByGoType['[]byte'] = 'buffer.Buffer';
599
+ r.globals['ArrayBuffer'] = 'globals.ArrayBuffer';
600
+ r.constructors['globals.ArrayBuffer'] = (call) => call.args.length > 0 ? `make([]byte, int(${call.args[0]}))` : '[]byte{}';
601
+ r.functions['globals.ArrayBuffer.isView'] = (call) => {
602
+ use();
603
+ if (r.isArrayBuffer(call.nodes[0]))
604
+ return 'false';
605
+ return `TnIsView(${call.args[0]})`;
606
+ };
607
+ r.results['globals.ArrayBuffer.isView'] = 'bool';
608
+ }
package/bin/node/url.js CHANGED
@@ -18,7 +18,23 @@ type TnURL struct {
18
18
 
19
19
  var tnSpecialSchemes = map[string]string{"http:": "80", "https:": "443", "ws:": "80", "wss:": "443", "ftp:": "21", "file:": ""}
20
20
 
21
- var tnSchemePattern = regexp.MustCompile("^[A-Za-z][A-Za-z0-9+.-]*:")
21
+ // The leading "scheme:" of s (/^[A-Za-z][A-Za-z0-9+.-]*:/), "" when there is none
22
+ func tnURLScheme(s string) string {
23
+ for i := 0; i < len(s); i++ {
24
+ c := s[i]
25
+ switch {
26
+ case c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z':
27
+ case i > 0 && (c >= '0' && c <= '9' || c == '+' || c == '.' || c == '-'):
28
+ case i > 0 && c == ':':
29
+ return s[:i+1]
30
+ default:
31
+ return ""
32
+ }
33
+ }
34
+ return ""
35
+ }
36
+
37
+ var tnURLStrip = strings.NewReplacer("\\t", "", "\\n", "", "\\r", "")
22
38
 
23
39
  func tnInvalidURL(input string) *TnError {
24
40
  e := TnNodeError("TypeError", "ERR_INVALID_URL", "Invalid URL")
@@ -213,7 +229,9 @@ func tnCopyURL(b *TnURL) *TnURL {
213
229
 
214
230
  func tnParseURLString(input string, base *TnURL) (*TnURL, bool) {
215
231
  input = strings.TrimFunc(input, func(r rune) bool { return r <= 0x20 })
216
- input = strings.NewReplacer("\\t", "", "\\n", "", "\\r", "").Replace(input)
232
+ if strings.ContainsAny(input, "\\t\\n\\r") {
233
+ input = tnURLStrip.Replace(input)
234
+ }
217
235
  fragment, hasFragment := "", false
218
236
  if i := strings.Index(input, "#"); i >= 0 {
219
237
  fragment, hasFragment = input[i+1:], true
@@ -225,7 +243,7 @@ func tnParseURLString(input string, base *TnURL) (*TnURL, bool) {
225
243
  input = input[:i]
226
244
  }
227
245
  u := &TnURL{}
228
- if scheme := tnSchemePattern.FindString(input); scheme != "" {
246
+ if scheme := tnURLScheme(input); scheme != "" {
229
247
  u.protocol = strings.ToLower(scheme)
230
248
  rest := input[len(scheme):]
231
249
  if u.special() {
@@ -451,7 +469,7 @@ func (u *TnURL) Set_href(v string) {
451
469
 
452
470
  func (u *TnURL) Set_protocol(v string) {
453
471
  v = strings.ToLower(strings.TrimSuffix(v, ":")) + ":"
454
- if !tnSchemePattern.MatchString(v) {
472
+ if tnURLScheme(v) == "" {
455
473
  return
456
474
  }
457
475
  _, toSpecial := tnSpecialSchemes[v]
@@ -809,7 +827,7 @@ func TnDomainToUnicode(domain string) string {
809
827
  }`;
810
828
  export function registerUrl(r) {
811
829
  defineModule(r, 'url');
812
- defineHelper(r, 'nodeUrl', ['encoding/hex', 'fmt', 'net/url', 'path/filepath', 'reflect', 'regexp', 'runtime', 'sort', 'strconv', 'strings', 'unicode/utf16', 'unsafe'], ['error', 'dynamic', 'punycode', 'pathToFileURL', 'nodeInvoke'], urlHelper);
830
+ defineHelper(r, 'nodeUrl', ['encoding/hex', 'fmt', 'net/url', 'path/filepath', 'reflect', 'runtime', 'sort', 'strconv', 'strings', 'unicode/utf16', 'unsafe'], ['error', 'dynamic', 'punycode', 'pathToFileURL', 'nodeInvoke'], urlHelper);
813
831
  defineClass(r, 'url.URL', '*TnURL', 'class', 'toString=string; toJSON=string', '', 'href=string; protocol=string; username=string; password=string; host=string; hostname=string; port=string; pathname=string; search=string; hash=string; origin=string; searchParams=*TnURLSearchParams', 'href; protocol; username; password; host; hostname; port; pathname; search; hash');
814
832
  defineClass(r, 'url.URLSearchParams', '*TnURLSearchParams', 'class', 'append=; set=; delete=; get=*string; getAll=[]string; has=bool; sort=; toString=string; keys=[]string; values=[]string; entries=[][]string; forEach=', '', 'size=float64', '');
815
833
  r.iterators['*TnURLSearchParams'] = 'entries';