goscript 0.0.37 → 0.0.39

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 (198) hide show
  1. package/compiler/analysis.go +129 -8
  2. package/compiler/compiler.go +4 -1
  3. package/compiler/composite-lit.go +2 -4
  4. package/compiler/expr-call.go +12 -0
  5. package/compiler/lit.go +100 -6
  6. package/compiler/protobuf.go +2 -2
  7. package/compiler/spec-value.go +3 -3
  8. package/compiler/spec.go +6 -3
  9. package/compiler/stmt-assign.go +1 -1
  10. package/dist/gs/builtin/builtin.d.ts +45 -0
  11. package/dist/gs/builtin/builtin.js +197 -0
  12. package/dist/gs/builtin/builtin.js.map +1 -1
  13. package/dist/gs/builtin/slice.js +2 -1
  14. package/dist/gs/builtin/slice.js.map +1 -1
  15. package/dist/gs/bytes/buffer.gs.d.ts +56 -0
  16. package/dist/gs/bytes/buffer.gs.js +611 -0
  17. package/dist/gs/bytes/buffer.gs.js.map +1 -0
  18. package/dist/gs/bytes/bytes.gs.d.ts +78 -0
  19. package/dist/gs/bytes/bytes.gs.js +1011 -0
  20. package/dist/gs/bytes/bytes.gs.js.map +1 -0
  21. package/dist/gs/bytes/index.d.ts +4 -0
  22. package/dist/gs/bytes/index.js +5 -0
  23. package/dist/gs/bytes/index.js.map +1 -0
  24. package/dist/gs/bytes/iter.gs.d.ts +9 -0
  25. package/dist/gs/bytes/iter.gs.js +143 -0
  26. package/dist/gs/bytes/iter.gs.js.map +1 -0
  27. package/dist/gs/bytes/reader.gs.d.ts +34 -0
  28. package/dist/gs/bytes/reader.gs.js +198 -0
  29. package/dist/gs/bytes/reader.gs.js.map +1 -0
  30. package/dist/gs/github.com/pkg/errors/errors.d.ts +1 -1
  31. package/dist/gs/github.com/pkg/errors/errors.js +182 -23
  32. package/dist/gs/github.com/pkg/errors/errors.js.map +1 -1
  33. package/dist/gs/github.com/pkg/errors/go113.d.ts +1 -1
  34. package/dist/gs/github.com/pkg/errors/go113.js +1 -1
  35. package/dist/gs/github.com/pkg/errors/go113.js.map +1 -1
  36. package/dist/gs/github.com/pkg/errors/index.d.ts +3 -3
  37. package/dist/gs/github.com/pkg/errors/index.js +3 -3
  38. package/dist/gs/github.com/pkg/errors/index.js.map +1 -1
  39. package/dist/gs/github.com/pkg/errors/stack.d.ts +1 -1
  40. package/dist/gs/github.com/pkg/errors/stack.js +8 -5
  41. package/dist/gs/github.com/pkg/errors/stack.js.map +1 -1
  42. package/dist/gs/internal/abi/index.d.ts +20 -0
  43. package/dist/gs/internal/abi/index.js +20 -0
  44. package/dist/gs/internal/abi/index.js.map +1 -0
  45. package/dist/gs/internal/bytealg/index.d.ts +14 -0
  46. package/dist/gs/internal/bytealg/index.js +139 -0
  47. package/dist/gs/internal/bytealg/index.js.map +1 -0
  48. package/dist/gs/internal/byteorder/index.d.ts +1 -1
  49. package/dist/gs/internal/byteorder/index.js +4 -4
  50. package/dist/gs/internal/byteorder/index.js.map +1 -1
  51. package/dist/gs/math/index.d.ts +44 -44
  52. package/dist/gs/math/index.js +44 -44
  53. package/dist/gs/math/index.js.map +1 -1
  54. package/dist/gs/os/index.d.ts +19 -19
  55. package/dist/gs/os/index.js +19 -19
  56. package/dist/gs/os/index.js.map +1 -1
  57. package/dist/gs/reflect/deepequal.d.ts +2 -0
  58. package/dist/gs/reflect/deepequal.js +91 -0
  59. package/dist/gs/reflect/deepequal.js.map +1 -0
  60. package/dist/gs/reflect/index.d.ts +8 -0
  61. package/dist/gs/reflect/index.js +10 -0
  62. package/dist/gs/reflect/index.js.map +1 -0
  63. package/dist/gs/reflect/iter.d.ts +4 -0
  64. package/dist/gs/reflect/iter.js +24 -0
  65. package/dist/gs/reflect/iter.js.map +1 -0
  66. package/dist/gs/reflect/map.d.ts +20 -0
  67. package/dist/gs/reflect/map.js +74 -0
  68. package/dist/gs/reflect/map.js.map +1 -0
  69. package/dist/gs/reflect/swapper.d.ts +2 -0
  70. package/dist/gs/reflect/swapper.js +46 -0
  71. package/dist/gs/reflect/swapper.js.map +1 -0
  72. package/dist/gs/reflect/type.d.ts +134 -0
  73. package/dist/gs/reflect/type.js +825 -0
  74. package/dist/gs/reflect/type.js.map +1 -0
  75. package/dist/gs/reflect/types.d.ts +90 -0
  76. package/dist/gs/reflect/types.js +119 -0
  77. package/dist/gs/reflect/types.js.map +1 -0
  78. package/dist/gs/reflect/value.d.ts +13 -0
  79. package/dist/gs/reflect/value.js +202 -0
  80. package/dist/gs/reflect/value.js.map +1 -0
  81. package/dist/gs/reflect/visiblefields.d.ts +4 -0
  82. package/dist/gs/reflect/visiblefields.js +149 -0
  83. package/dist/gs/reflect/visiblefields.js.map +1 -0
  84. package/dist/gs/strconv/index.d.ts +6 -6
  85. package/dist/gs/strconv/index.js +6 -6
  86. package/dist/gs/strconv/index.js.map +1 -1
  87. package/dist/gs/strings/index.d.ts +1 -1
  88. package/dist/gs/strings/index.js +1 -1
  89. package/dist/gs/strings/index.js.map +1 -1
  90. package/dist/gs/strings/replace.js.map +1 -1
  91. package/dist/gs/sync/atomic/index.d.ts +4 -4
  92. package/dist/gs/sync/atomic/index.js +4 -4
  93. package/dist/gs/sync/atomic/index.js.map +1 -1
  94. package/dist/gs/syscall/index.d.ts +6 -6
  95. package/dist/gs/syscall/index.js +34 -28
  96. package/dist/gs/syscall/index.js.map +1 -1
  97. package/dist/gs/unicode/utf8/utf8.d.ts +1 -1
  98. package/dist/gs/unicode/utf8/utf8.js +4 -2
  99. package/dist/gs/unicode/utf8/utf8.js.map +1 -1
  100. package/dist/gs/unsafe/unsafe.js.map +1 -1
  101. package/gs/builtin/builtin.ts +219 -0
  102. package/gs/builtin/slice.ts +2 -1
  103. package/gs/bytes/buffer.gs.ts +614 -0
  104. package/gs/bytes/bytes.gs.ts +1180 -0
  105. package/gs/bytes/godoc.txt +69 -0
  106. package/gs/bytes/index.ts +69 -0
  107. package/gs/bytes/iter.gs.ts +149 -0
  108. package/gs/bytes/reader.gs.ts +230 -0
  109. package/gs/github.com/pkg/errors/errors.ts +408 -238
  110. package/gs/github.com/pkg/errors/go113.ts +5 -6
  111. package/gs/github.com/pkg/errors/index.ts +12 -3
  112. package/gs/github.com/pkg/errors/stack.ts +107 -105
  113. package/gs/internal/abi/index.ts +37 -0
  114. package/gs/internal/bytealg/index.ts +149 -0
  115. package/gs/internal/byteorder/index.ts +5 -5
  116. package/gs/math/abs.gs.test.ts +1 -1
  117. package/gs/math/acosh.gs.test.ts +4 -2
  118. package/gs/math/asin.gs.test.ts +1 -1
  119. package/gs/math/asinh.gs.test.ts +7 -3
  120. package/gs/math/atan.gs.test.ts +1 -1
  121. package/gs/math/atan2.gs.test.ts +17 -9
  122. package/gs/math/atanh.gs.test.ts +1 -1
  123. package/gs/math/bits.gs.test.ts +1 -1
  124. package/gs/math/cbrt.gs.test.ts +1 -1
  125. package/gs/math/const.gs.test.ts +34 -8
  126. package/gs/math/copysign.gs.test.ts +7 -3
  127. package/gs/math/dim.gs.test.ts +19 -7
  128. package/gs/math/erf.gs.test.ts +1 -1
  129. package/gs/math/erfinv.gs.test.ts +4 -2
  130. package/gs/math/exp.gs.test.ts +1 -1
  131. package/gs/math/expm1.gs.test.ts +6 -4
  132. package/gs/math/floor.gs.test.ts +17 -4
  133. package/gs/math/fma.gs.test.ts +53 -53
  134. package/gs/math/frexp.gs.test.ts +112 -117
  135. package/gs/math/gamma.gs.test.ts +1 -1
  136. package/gs/math/hypot.gs.test.ts +53 -53
  137. package/gs/math/index.ts +80 -44
  138. package/gs/math/j0.gs.test.ts +6 -2
  139. package/gs/math/j1.gs.test.ts +6 -2
  140. package/gs/math/jn.gs.test.ts +9 -5
  141. package/gs/math/ldexp.gs.test.ts +103 -86
  142. package/gs/math/lgamma.gs.test.ts +10 -10
  143. package/gs/math/log.gs.test.ts +1 -1
  144. package/gs/math/log10.gs.test.ts +1 -1
  145. package/gs/math/log1p.gs.test.ts +2 -2
  146. package/gs/math/logb.gs.test.ts +1 -1
  147. package/gs/math/mod.gs.test.ts +2 -2
  148. package/gs/math/modf.gs.test.ts +7 -7
  149. package/gs/math/nextafter.gs.test.ts +9 -7
  150. package/gs/math/pow.gs.test.ts +6 -4
  151. package/gs/math/pow10.gs.test.ts +1 -1
  152. package/gs/math/remainder.gs.test.ts +1 -1
  153. package/gs/math/signbit.gs.test.ts +1 -1
  154. package/gs/math/sin.gs.test.ts +1 -1
  155. package/gs/math/sincos.gs.test.ts +33 -14
  156. package/gs/math/sinh.gs.test.ts +1 -1
  157. package/gs/math/sqrt.gs.test.ts +1 -1
  158. package/gs/math/tan.gs.test.ts +3 -3
  159. package/gs/math/tanh.gs.test.ts +1 -1
  160. package/gs/os/index.ts +128 -19
  161. package/gs/reflect/ANALYSIS.md +278 -0
  162. package/gs/reflect/deepequal.test.ts +41 -0
  163. package/gs/reflect/deepequal.ts +169 -0
  164. package/gs/reflect/function-types.test.ts +146 -0
  165. package/gs/reflect/godoc.txt +67 -0
  166. package/gs/reflect/index.ts +83 -0
  167. package/gs/reflect/iter.ts +44 -0
  168. package/gs/reflect/map.test.ts +30 -0
  169. package/gs/reflect/map.ts +85 -0
  170. package/gs/reflect/swapper.ts +52 -0
  171. package/gs/reflect/type.ts +1016 -0
  172. package/gs/reflect/types.ts +214 -0
  173. package/gs/reflect/value.ts +270 -0
  174. package/gs/reflect/visiblefields.ts +177 -0
  175. package/gs/strconv/index.ts +39 -6
  176. package/gs/strings/index.ts +7 -1
  177. package/gs/strings/replace.ts +1 -9
  178. package/gs/sync/atomic/index.ts +53 -4
  179. package/gs/syscall/index.ts +45 -37
  180. package/gs/unicode/utf8/utf8.ts +8 -5
  181. package/gs/unsafe/unsafe.ts +1 -1
  182. package/package.json +2 -1
  183. package/dist/gs/internal/testlog/index.d.ts +0 -1
  184. package/dist/gs/internal/testlog/index.js +0 -5
  185. package/dist/gs/internal/testlog/index.js.map +0 -1
  186. package/dist/gs/maps/iter.gs.d.ts +0 -7
  187. package/dist/gs/maps/iter.gs.js +0 -65
  188. package/dist/gs/maps/iter.gs.js.map +0 -1
  189. package/dist/gs/maps/maps.gs.d.ts +0 -7
  190. package/dist/gs/maps/maps.gs.js +0 -79
  191. package/dist/gs/maps/maps.gs.js.map +0 -1
  192. package/dist/gs/stringslite/index.d.ts +0 -1
  193. package/dist/gs/stringslite/index.js +0 -2
  194. package/dist/gs/stringslite/index.js.map +0 -1
  195. package/dist/gs/stringslite/strings.d.ts +0 -11
  196. package/dist/gs/stringslite/strings.js +0 -67
  197. package/dist/gs/stringslite/strings.js.map +0 -1
  198. package/gs/internal/testlog/index.ts +0 -7
@@ -0,0 +1,611 @@
1
+ var __addDisposableResource = (this && this.__addDisposableResource) || function (env, value, async) {
2
+ if (value !== null && value !== void 0) {
3
+ if (typeof value !== "object" && typeof value !== "function") throw new TypeError("Object expected.");
4
+ var dispose, inner;
5
+ if (async) {
6
+ if (!Symbol.asyncDispose) throw new TypeError("Symbol.asyncDispose is not defined.");
7
+ dispose = value[Symbol.asyncDispose];
8
+ }
9
+ if (dispose === void 0) {
10
+ if (!Symbol.dispose) throw new TypeError("Symbol.dispose is not defined.");
11
+ dispose = value[Symbol.dispose];
12
+ if (async) inner = dispose;
13
+ }
14
+ if (typeof dispose !== "function") throw new TypeError("Object not disposable.");
15
+ if (inner) dispose = function() { try { inner.call(this); } catch (e) { return Promise.reject(e); } };
16
+ env.stack.push({ value: value, dispose: dispose, async: async });
17
+ }
18
+ else if (async) {
19
+ env.stack.push({ async: true });
20
+ }
21
+ return value;
22
+ };
23
+ var __disposeResources = (this && this.__disposeResources) || (function (SuppressedError) {
24
+ return function (env) {
25
+ function fail(e) {
26
+ env.error = env.hasError ? new SuppressedError(e, env.error, "An error was suppressed during disposal.") : e;
27
+ env.hasError = true;
28
+ }
29
+ var r, s = 0;
30
+ function next() {
31
+ while (r = env.stack.pop()) {
32
+ try {
33
+ if (!r.async && s === 1) return s = 0, env.stack.push(r), Promise.resolve().then(next);
34
+ if (r.dispose) {
35
+ var result = r.dispose.call(r.value);
36
+ if (r.async) return s |= 2, Promise.resolve(result).then(next, function(e) { fail(e); return next(); });
37
+ }
38
+ else s |= 1;
39
+ }
40
+ catch (e) {
41
+ fail(e);
42
+ }
43
+ }
44
+ if (s === 1) return env.hasError ? Promise.reject(env.error) : Promise.resolve();
45
+ if (env.hasError) throw env.error;
46
+ }
47
+ return next();
48
+ };
49
+ })(typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
50
+ var e = new Error(message);
51
+ return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
52
+ });
53
+ import * as $ from "@goscript/builtin/builtin.js";
54
+ const { copy, recover } = $;
55
+ import { IndexByte } from "./bytes.gs.js";
56
+ import * as errors from "@goscript/errors/index.js";
57
+ import * as io from "@goscript/io/index.js";
58
+ import * as utf8 from "@goscript/unicode/utf8/index.js";
59
+ let smallBufferSize = 64;
60
+ export class Buffer {
61
+ // contents are the bytes buf[off : len(buf)]
62
+ get buf() {
63
+ return this._fields.buf.value;
64
+ }
65
+ set buf(value) {
66
+ this._fields.buf.value = value;
67
+ }
68
+ // read at &buf[off], write at &buf[len(buf)]
69
+ get off() {
70
+ return this._fields.off.value;
71
+ }
72
+ set off(value) {
73
+ this._fields.off.value = value;
74
+ }
75
+ // last read operation, so that Unread* can work correctly.
76
+ get lastRead() {
77
+ return this._fields.lastRead.value;
78
+ }
79
+ set lastRead(value) {
80
+ this._fields.lastRead.value = value;
81
+ }
82
+ _fields;
83
+ constructor(init) {
84
+ this._fields = {
85
+ buf: $.varRef(init?.buf ?? null),
86
+ off: $.varRef(init?.off ?? 0),
87
+ lastRead: $.varRef(init?.lastRead ?? 0)
88
+ };
89
+ }
90
+ clone() {
91
+ const cloned = new Buffer();
92
+ cloned._fields = {
93
+ buf: $.varRef(this._fields.buf.value),
94
+ off: $.varRef(this._fields.off.value),
95
+ lastRead: $.varRef(this._fields.lastRead.value)
96
+ };
97
+ return cloned;
98
+ }
99
+ // Bytes returns a slice of length b.Len() holding the unread portion of the buffer.
100
+ // The slice is valid for use only until the next buffer modification (that is,
101
+ // only until the next call to a method like [Buffer.Read], [Buffer.Write], [Buffer.Reset], or [Buffer.Truncate]).
102
+ // The slice aliases the buffer content at least until the next buffer modification,
103
+ // so immediate changes to the slice will affect the result of future reads.
104
+ Bytes() {
105
+ const b = this;
106
+ return $.goSlice(b.buf, b.off, undefined);
107
+ }
108
+ // AvailableBuffer returns an empty buffer with b.Available() capacity.
109
+ // This buffer is intended to be appended to and
110
+ // passed to an immediately succeeding [Buffer.Write] call.
111
+ // The buffer is only valid until the next write operation on b.
112
+ AvailableBuffer() {
113
+ const b = this;
114
+ return $.goSlice(b.buf, $.len(b.buf), undefined);
115
+ }
116
+ // String returns the contents of the unread portion of the buffer
117
+ // as a string. If the [Buffer] is a nil pointer, it returns "<nil>".
118
+ //
119
+ // To build strings more efficiently, see the [strings.Builder] type.
120
+ String() {
121
+ const b = this;
122
+ if (b == null) {
123
+ // Special case, useful in debugging.
124
+ return "<nil>";
125
+ }
126
+ if (b.buf === null) {
127
+ return "";
128
+ }
129
+ return $.bytesToString($.goSlice(b.buf, b.off, undefined));
130
+ }
131
+ // empty reports whether the unread portion of the buffer is empty.
132
+ empty() {
133
+ const b = this;
134
+ return $.len(b.buf) <= b.off;
135
+ }
136
+ // Len returns the number of bytes of the unread portion of the buffer;
137
+ // b.Len() == len(b.Bytes()).
138
+ Len() {
139
+ const b = this;
140
+ return $.len(b.buf) - b.off;
141
+ }
142
+ // Cap returns the capacity of the buffer's underlying byte slice, that is, the
143
+ // total space allocated for the buffer's data.
144
+ Cap() {
145
+ const b = this;
146
+ return $.cap(b.buf);
147
+ }
148
+ // Available returns how many bytes are unused in the buffer.
149
+ Available() {
150
+ const b = this;
151
+ return $.cap(b.buf) - $.len(b.buf);
152
+ }
153
+ // Truncate discards all but the first n unread bytes from the buffer
154
+ // but continues to use the same allocated storage.
155
+ // It panics if n is negative or greater than the length of the buffer.
156
+ Truncate(n) {
157
+ const b = this;
158
+ if (n == 0) {
159
+ b.Reset();
160
+ return;
161
+ }
162
+ b.lastRead = 0;
163
+ if (n < 0 || n > b.Len()) {
164
+ $.panic("bytes.Buffer: truncation out of range");
165
+ }
166
+ b.buf = $.goSlice(b.buf, undefined, b.off + n);
167
+ }
168
+ // Reset resets the buffer to be empty,
169
+ // but it retains the underlying storage for use by future writes.
170
+ // Reset is the same as [Buffer.Truncate](0).
171
+ Reset() {
172
+ const b = this;
173
+ b.buf = $.goSlice(b.buf, undefined, 0);
174
+ b.off = 0;
175
+ b.lastRead = 0;
176
+ }
177
+ // tryGrowByReslice is an inlineable version of grow for the fast-case where the
178
+ // internal buffer only needs to be resliced.
179
+ // It returns the index where bytes should be written and whether it succeeded.
180
+ tryGrowByReslice(n) {
181
+ const b = this;
182
+ if (b.buf === null) {
183
+ return [0, false];
184
+ }
185
+ {
186
+ let l = $.len(b.buf);
187
+ if (n <= $.cap(b.buf) - l) {
188
+ b.buf = $.goSlice(b.buf, undefined, l + n);
189
+ return [l, true];
190
+ }
191
+ }
192
+ return [0, false];
193
+ }
194
+ // grow grows the buffer to guarantee space for n more bytes.
195
+ // It returns the index where bytes should be written.
196
+ // If the buffer can't grow it will panic with ErrTooLarge.
197
+ grow(n) {
198
+ const b = this;
199
+ let m = b.Len();
200
+ if (m == 0 && b.off != 0) {
201
+ b.Reset();
202
+ }
203
+ {
204
+ let [i, ok] = b.tryGrowByReslice(n);
205
+ if (ok) {
206
+ return i;
207
+ }
208
+ }
209
+ if (b.buf == null) {
210
+ // Handle null buffer case - create new buffer
211
+ const capacity = n <= 64 ? 64 : n;
212
+ b.buf = $.makeSlice(n, capacity, 'byte');
213
+ return 0;
214
+ }
215
+ let c = $.cap(b.buf);
216
+ if (n <= c / 2 - m) {
217
+ // We can slide things down instead of allocating a new
218
+ // slice. We only need m+n <= c to slide, but
219
+ // we instead let capacity get twice as large so we
220
+ // don't spend all our time copying.
221
+ $.copy($.bytesToUint8Array(b.buf), $.bytesToUint8Array($.goSlice(b.buf, b.off, undefined)));
222
+ }
223
+ else if (c > 9223372036854775807 - c - n) {
224
+ $.panic(ErrTooLarge);
225
+ }
226
+ else {
227
+ // Add b.off to account for b.buf[:b.off] being sliced off the front.
228
+ b.buf = growSlice($.goSlice(b.buf, b.off, undefined), b.off + n);
229
+ }
230
+ b.off = 0;
231
+ b.buf = $.goSlice(b.buf, undefined, m + n);
232
+ return m;
233
+ }
234
+ // Grow grows the buffer's capacity, if necessary, to guarantee space for
235
+ // another n bytes. After Grow(n), at least n bytes can be written to the
236
+ // buffer without another allocation.
237
+ // If n is negative, Grow will panic.
238
+ // If the buffer can't grow it will panic with [ErrTooLarge].
239
+ Grow(n) {
240
+ const b = this;
241
+ if (n < 0) {
242
+ $.panic("bytes.Buffer.Grow: negative count");
243
+ }
244
+ let m = b.grow(n);
245
+ b.buf = $.goSlice(b.buf, undefined, m);
246
+ }
247
+ // Write appends the contents of p to the buffer, growing the buffer as
248
+ // needed. The return value n is the length of p; err is always nil. If the
249
+ // buffer becomes too large, Write will panic with [ErrTooLarge].
250
+ Write(p) {
251
+ const b = this;
252
+ b.lastRead = 0;
253
+ let [m, ok] = b.tryGrowByReslice($.len(p));
254
+ if (!ok) {
255
+ m = b.grow($.len(p));
256
+ }
257
+ // Copy directly to the buffer at position m
258
+ const targetSlice = $.goSlice(b.buf, m, m + $.len(p));
259
+ if (p instanceof Uint8Array) {
260
+ // Direct Uint8Array copy
261
+ for (let i = 0; i < $.len(p); i++) {
262
+ targetSlice[i] = p[i];
263
+ }
264
+ }
265
+ else {
266
+ // Copy from Slice<number>
267
+ for (let i = 0; i < $.len(p); i++) {
268
+ targetSlice[i] = p[i];
269
+ }
270
+ }
271
+ return [$.len(p), null];
272
+ }
273
+ // WriteString appends the contents of s to the buffer, growing the buffer as
274
+ // needed. The return value n is the length of s; err is always nil. If the
275
+ // buffer becomes too large, WriteString will panic with [ErrTooLarge].
276
+ WriteString(s) {
277
+ const b = this;
278
+ b.lastRead = 0;
279
+ let [m, ok] = b.tryGrowByReslice($.len(s));
280
+ if (!ok) {
281
+ m = b.grow($.len(s));
282
+ }
283
+ // Copy string directly to the buffer at position m
284
+ const targetSlice = $.goSlice(b.buf, m, m + $.len(s));
285
+ const encoder = new TextEncoder();
286
+ const encoded = encoder.encode(s);
287
+ for (let i = 0; i < encoded.length; i++) {
288
+ targetSlice[i] = encoded[i];
289
+ }
290
+ return [encoded.length, null];
291
+ }
292
+ // ReadFrom reads data from r until EOF and appends it to the buffer, growing
293
+ // the buffer as needed. The return value n is the number of bytes read. Any
294
+ // error except io.EOF encountered during the read is also returned. If the
295
+ // buffer becomes too large, ReadFrom will panic with [ErrTooLarge].
296
+ ReadFrom(r) {
297
+ const b = this;
298
+ b.lastRead = 0;
299
+ let n = 0;
300
+ for (;;) {
301
+ let i = b.grow(512);
302
+ b.buf = $.goSlice(b.buf, undefined, i);
303
+ let [m, e] = r.Read($.bytesToUint8Array($.goSlice(b.buf, i, $.cap(b.buf))));
304
+ if (m < 0) {
305
+ $.panic(errNegativeRead);
306
+ }
307
+ b.buf = $.goSlice(b.buf, undefined, i + m);
308
+ n += m;
309
+ // e is EOF, so return nil explicitly
310
+ if (e == io.EOF) {
311
+ return [n, null];
312
+ }
313
+ if (e != null) {
314
+ return [n, e];
315
+ }
316
+ }
317
+ }
318
+ // WriteTo writes data to w until the buffer is drained or an error occurs.
319
+ // The return value n is the number of bytes written; it always fits into an
320
+ // int, but it is int64 to match the [io.WriterTo] interface. Any error
321
+ // encountered during the write is also returned.
322
+ WriteTo(w) {
323
+ const b = this;
324
+ b.lastRead = 0;
325
+ let n = 0;
326
+ {
327
+ let nBytes = b.Len();
328
+ if (nBytes > 0) {
329
+ let [m, e] = w.Write($.bytesToUint8Array($.goSlice(b.buf, b.off, undefined)));
330
+ if (m > nBytes) {
331
+ $.panic("bytes.Buffer.WriteTo: invalid Write count");
332
+ }
333
+ b.off += m;
334
+ n = m;
335
+ if (e != null) {
336
+ return [n, e];
337
+ }
338
+ // all bytes should have been written, by definition of
339
+ // Write method in io.Writer
340
+ if (m != nBytes) {
341
+ return [n, io.ErrShortWrite];
342
+ }
343
+ }
344
+ }
345
+ b.Reset();
346
+ return [n, null];
347
+ }
348
+ // WriteByte appends the byte c to the buffer, growing the buffer as needed.
349
+ // The returned error is always nil, but is included to match [bufio.Writer]'s
350
+ // WriteByte. If the buffer becomes too large, WriteByte will panic with
351
+ // [ErrTooLarge].
352
+ WriteByte(c) {
353
+ const b = this;
354
+ b.lastRead = 0;
355
+ let [m, ok] = b.tryGrowByReslice(1);
356
+ if (!ok) {
357
+ m = b.grow(1);
358
+ }
359
+ b.buf[m] = c;
360
+ return null;
361
+ }
362
+ // WriteRune appends the UTF-8 encoding of Unicode code point r to the
363
+ // buffer, returning its length and an error, which is always nil but is
364
+ // included to match [bufio.Writer]'s WriteRune. The buffer is grown as needed;
365
+ // if it becomes too large, WriteRune will panic with [ErrTooLarge].
366
+ WriteRune(r) {
367
+ const b = this;
368
+ if (r < utf8.RuneSelf) {
369
+ b.WriteByte($.byte(r));
370
+ return [1, null];
371
+ }
372
+ b.lastRead = 0;
373
+ let [m, ok] = b.tryGrowByReslice(utf8.UTFMax);
374
+ if (!ok) {
375
+ m = b.grow(utf8.UTFMax);
376
+ }
377
+ b.buf = utf8.AppendRune($.bytesToUint8Array($.goSlice(b.buf, undefined, m)), r);
378
+ return [$.len(b.buf) - m, null];
379
+ }
380
+ // Read reads the next len(p) bytes from the buffer or until the buffer
381
+ // is drained. The return value n is the number of bytes read. If the
382
+ // buffer has no data to return, err is [io.EOF] (unless len(p) is zero);
383
+ // otherwise it is nil.
384
+ Read(p) {
385
+ const b = this;
386
+ b.lastRead = 0;
387
+ if (b.empty()) {
388
+ // Buffer is empty, reset to recover space.
389
+ b.Reset();
390
+ if ($.len(p) == 0) {
391
+ return [0, null];
392
+ }
393
+ return [0, io.EOF];
394
+ }
395
+ const n = $.copy($.bytesToUint8Array(p), $.bytesToUint8Array($.goSlice(b.buf, b.off, undefined)));
396
+ b.off += n;
397
+ if (n > 0) {
398
+ b.lastRead = -1;
399
+ }
400
+ return [n, null];
401
+ }
402
+ // Next returns a slice containing the next n bytes from the buffer,
403
+ // advancing the buffer as if the bytes had been returned by [Buffer.Read].
404
+ // If there are fewer than n bytes in the buffer, Next returns the entire buffer.
405
+ // The slice is only valid until the next call to a read or write method.
406
+ Next(n) {
407
+ const b = this;
408
+ b.lastRead = 0;
409
+ let m = b.Len();
410
+ if (n > m) {
411
+ n = m;
412
+ }
413
+ let data = $.goSlice(b.buf, b.off, b.off + n);
414
+ b.off += n;
415
+ if (n > 0) {
416
+ b.lastRead = -1;
417
+ }
418
+ return data;
419
+ }
420
+ // ReadByte reads and returns the next byte from the buffer.
421
+ // If no byte is available, it returns error [io.EOF].
422
+ ReadByte() {
423
+ const b = this;
424
+ if (b.empty()) {
425
+ // Buffer is empty, reset to recover space.
426
+ b.Reset();
427
+ return [0, io.EOF];
428
+ }
429
+ let c = b.buf[b.off];
430
+ b.off++;
431
+ b.lastRead = -1;
432
+ return [c, null];
433
+ }
434
+ // ReadRune reads and returns the next UTF-8-encoded
435
+ // Unicode code point from the buffer.
436
+ // If no bytes are available, the error returned is io.EOF.
437
+ // If the bytes are an erroneous UTF-8 encoding, it
438
+ // consumes one byte and returns U+FFFD, 1.
439
+ ReadRune() {
440
+ const b = this;
441
+ if (b.empty()) {
442
+ // Buffer is empty, reset to recover space.
443
+ b.Reset();
444
+ return [0, 0, io.EOF];
445
+ }
446
+ let c = b.buf[b.off];
447
+ if (c < utf8.RuneSelf) {
448
+ b.off++;
449
+ b.lastRead = 1;
450
+ return [c, 1, null];
451
+ }
452
+ let r;
453
+ let n;
454
+ [r, n] = utf8.DecodeRune($.goSlice(b.buf, b.off, undefined));
455
+ b.off += n;
456
+ b.lastRead = n;
457
+ return [r, n, null];
458
+ }
459
+ // UnreadRune unreads the last rune returned by [Buffer.ReadRune].
460
+ // If the most recent read or write operation on the buffer was
461
+ // not a successful [Buffer.ReadRune], UnreadRune returns an error. (In this regard
462
+ // it is stricter than [Buffer.UnreadByte], which will unread the last byte
463
+ // from any read operation.)
464
+ UnreadRune() {
465
+ const b = this;
466
+ if (b.lastRead <= 0) {
467
+ return errors.New("bytes.Buffer: UnreadRune: previous operation was not a successful ReadRune");
468
+ }
469
+ if (b.off >= $.int(b.lastRead)) {
470
+ b.off -= $.int(b.lastRead);
471
+ }
472
+ b.lastRead = 0;
473
+ return null;
474
+ }
475
+ // UnreadByte unreads the last byte returned by the most recent successful
476
+ // read operation that read at least one byte. If a write has happened since
477
+ // the last read, if the last read returned an error, or if the read read zero
478
+ // bytes, UnreadByte returns an error.
479
+ UnreadByte() {
480
+ const b = this;
481
+ if (b.lastRead == 0) {
482
+ return errUnreadByte;
483
+ }
484
+ b.lastRead = 0;
485
+ if (b.off > 0) {
486
+ b.off--;
487
+ }
488
+ return null;
489
+ }
490
+ // ReadBytes reads until the first occurrence of delim in the input,
491
+ // returning a slice containing the data up to and including the delimiter.
492
+ // If ReadBytes encounters an error before finding a delimiter,
493
+ // it returns the data read before the error and the error itself (often [io.EOF]).
494
+ // ReadBytes returns err != nil if and only if the returned data does not end in
495
+ // delim.
496
+ ReadBytes(delim) {
497
+ const b = this;
498
+ let [slice, err] = b.readSlice(delim);
499
+ let line = $.append(null, slice);
500
+ return [line, err];
501
+ }
502
+ // readSlice is like ReadBytes but returns a reference to internal buffer data.
503
+ readSlice(delim) {
504
+ const b = this;
505
+ let i = IndexByte($.goSlice(b.buf, b.off, undefined), delim);
506
+ let end = b.off + i + 1;
507
+ let err = null;
508
+ let line;
509
+ if (i < 0) {
510
+ end = $.len(b.buf);
511
+ err = io.EOF;
512
+ }
513
+ line = $.goSlice(b.buf, b.off, end);
514
+ b.off = end;
515
+ b.lastRead = -1;
516
+ return [line, err];
517
+ }
518
+ // ReadString reads until the first occurrence of delim in the input,
519
+ // returning a string containing the data up to and including the delimiter.
520
+ // If ReadString encounters an error before finding a delimiter,
521
+ // it returns the data read before the error and the error itself (often [io.EOF]).
522
+ // ReadString returns err != nil if and only if the returned data does not end
523
+ // in delim.
524
+ ReadString(delim) {
525
+ const b = this;
526
+ let slice;
527
+ let err;
528
+ [slice, err] = b.readSlice(delim);
529
+ return [$.bytesToString(slice), err];
530
+ }
531
+ // Register this type with the runtime type system
532
+ static __typeInfo = $.registerStructType('Buffer', new Buffer(), [{ name: "Bytes", args: [], returns: [{ type: { kind: $.TypeKind.Slice, elemType: { kind: $.TypeKind.Basic, name: "number" } } }] }, { name: "AvailableBuffer", args: [], returns: [{ type: { kind: $.TypeKind.Slice, elemType: { kind: $.TypeKind.Basic, name: "number" } } }] }, { name: "String", args: [], returns: [{ type: { kind: $.TypeKind.Basic, name: "string" } }] }, { name: "empty", args: [], returns: [{ type: { kind: $.TypeKind.Basic, name: "boolean" } }] }, { name: "Len", args: [], returns: [{ type: { kind: $.TypeKind.Basic, name: "number" } }] }, { name: "Cap", args: [], returns: [{ type: { kind: $.TypeKind.Basic, name: "number" } }] }, { name: "Available", args: [], returns: [{ type: { kind: $.TypeKind.Basic, name: "number" } }] }, { name: "Truncate", args: [{ name: "n", type: { kind: $.TypeKind.Basic, name: "number" } }], returns: [] }, { name: "Reset", args: [], returns: [] }, { name: "tryGrowByReslice", args: [{ name: "n", type: { kind: $.TypeKind.Basic, name: "number" } }], returns: [{ type: { kind: $.TypeKind.Basic, name: "number" } }, { type: { kind: $.TypeKind.Basic, name: "boolean" } }] }, { name: "grow", args: [{ name: "n", type: { kind: $.TypeKind.Basic, name: "number" } }], returns: [{ type: { kind: $.TypeKind.Basic, name: "number" } }] }, { name: "Grow", args: [{ name: "n", type: { kind: $.TypeKind.Basic, name: "number" } }], returns: [] }, { name: "Write", args: [{ name: "p", type: { kind: $.TypeKind.Slice, elemType: { kind: $.TypeKind.Basic, name: "number" } } }], returns: [{ type: { kind: $.TypeKind.Basic, name: "number" } }, { type: { kind: $.TypeKind.Interface, name: 'GoError', methods: [{ name: 'Error', args: [], returns: [{ type: { kind: $.TypeKind.Basic, name: 'string' } }] }] } }] }, { name: "WriteString", args: [{ name: "s", type: { kind: $.TypeKind.Basic, name: "string" } }], returns: [{ type: { kind: $.TypeKind.Basic, name: "number" } }, { type: { kind: $.TypeKind.Interface, name: 'GoError', methods: [{ name: 'Error', args: [], returns: [{ type: { kind: $.TypeKind.Basic, name: 'string' } }] }] } }] }, { name: "ReadFrom", args: [{ name: "r", type: "Reader" }], returns: [{ type: { kind: $.TypeKind.Basic, name: "number" } }, { type: { kind: $.TypeKind.Interface, name: 'GoError', methods: [{ name: 'Error', args: [], returns: [{ type: { kind: $.TypeKind.Basic, name: 'string' } }] }] } }] }, { name: "WriteTo", args: [{ name: "w", type: "Writer" }], returns: [{ type: { kind: $.TypeKind.Basic, name: "number" } }, { type: { kind: $.TypeKind.Interface, name: 'GoError', methods: [{ name: 'Error', args: [], returns: [{ type: { kind: $.TypeKind.Basic, name: 'string' } }] }] } }] }, { name: "WriteByte", args: [{ name: "c", type: { kind: $.TypeKind.Basic, name: "number" } }], returns: [{ type: { kind: $.TypeKind.Interface, name: 'GoError', methods: [{ name: 'Error', args: [], returns: [{ type: { kind: $.TypeKind.Basic, name: 'string' } }] }] } }] }, { name: "WriteRune", args: [{ name: "r", type: { kind: $.TypeKind.Basic, name: "number" } }], returns: [{ type: { kind: $.TypeKind.Basic, name: "number" } }, { type: { kind: $.TypeKind.Interface, name: 'GoError', methods: [{ name: 'Error', args: [], returns: [{ type: { kind: $.TypeKind.Basic, name: 'string' } }] }] } }] }, { name: "Read", args: [{ name: "p", type: { kind: $.TypeKind.Slice, elemType: { kind: $.TypeKind.Basic, name: "number" } } }], returns: [{ type: { kind: $.TypeKind.Basic, name: "number" } }, { type: { kind: $.TypeKind.Interface, name: 'GoError', methods: [{ name: 'Error', args: [], returns: [{ type: { kind: $.TypeKind.Basic, name: 'string' } }] }] } }] }, { name: "Next", args: [{ name: "n", type: { kind: $.TypeKind.Basic, name: "number" } }], returns: [{ type: { kind: $.TypeKind.Slice, elemType: { kind: $.TypeKind.Basic, name: "number" } } }] }, { name: "ReadByte", args: [], returns: [{ type: { kind: $.TypeKind.Basic, name: "number" } }, { type: { kind: $.TypeKind.Interface, name: 'GoError', methods: [{ name: 'Error', args: [], returns: [{ type: { kind: $.TypeKind.Basic, name: 'string' } }] }] } }] }, { name: "ReadRune", args: [], returns: [{ type: { kind: $.TypeKind.Basic, name: "number" } }, { type: { kind: $.TypeKind.Basic, name: "number" } }, { type: { kind: $.TypeKind.Interface, name: 'GoError', methods: [{ name: 'Error', args: [], returns: [{ type: { kind: $.TypeKind.Basic, name: 'string' } }] }] } }] }, { name: "UnreadRune", args: [], returns: [{ type: { kind: $.TypeKind.Interface, name: 'GoError', methods: [{ name: 'Error', args: [], returns: [{ type: { kind: $.TypeKind.Basic, name: 'string' } }] }] } }] }, { name: "UnreadByte", args: [], returns: [{ type: { kind: $.TypeKind.Interface, name: 'GoError', methods: [{ name: 'Error', args: [], returns: [{ type: { kind: $.TypeKind.Basic, name: 'string' } }] }] } }] }, { name: "ReadBytes", args: [{ name: "delim", type: { kind: $.TypeKind.Basic, name: "number" } }], returns: [{ type: { kind: $.TypeKind.Slice, elemType: { kind: $.TypeKind.Basic, name: "number" } } }, { type: { kind: $.TypeKind.Interface, name: 'GoError', methods: [{ name: 'Error', args: [], returns: [{ type: { kind: $.TypeKind.Basic, name: 'string' } }] }] } }] }, { name: "readSlice", args: [{ name: "delim", type: { kind: $.TypeKind.Basic, name: "number" } }], returns: [{ type: { kind: $.TypeKind.Slice, elemType: { kind: $.TypeKind.Basic, name: "number" } } }, { type: { kind: $.TypeKind.Interface, name: 'GoError', methods: [{ name: 'Error', args: [], returns: [{ type: { kind: $.TypeKind.Basic, name: 'string' } }] }] } }] }, { name: "ReadString", args: [{ name: "delim", type: { kind: $.TypeKind.Basic, name: "number" } }], returns: [{ type: { kind: $.TypeKind.Basic, name: "string" } }, { type: { kind: $.TypeKind.Interface, name: 'GoError', methods: [{ name: 'Error', args: [], returns: [{ type: { kind: $.TypeKind.Basic, name: 'string' } }] }] } }] }], Buffer, { "buf": { kind: $.TypeKind.Slice, elemType: { kind: $.TypeKind.Basic, name: "number" } }, "off": { kind: $.TypeKind.Basic, name: "number" }, "lastRead": "readOp" });
533
+ }
534
+ // Any other read operation.
535
+ let opRead = -1;
536
+ // Non-read operation.
537
+ let opInvalid = 0;
538
+ // Read rune of size 1.
539
+ let opReadRune1 = 1;
540
+ // Read rune of size 2.
541
+ let opReadRune2 = 2;
542
+ // Read rune of size 3.
543
+ let opReadRune3 = 3;
544
+ // Read rune of size 4.
545
+ let opReadRune4 = 4;
546
+ export let ErrTooLarge = errors.New("bytes.Buffer: too large");
547
+ let errNegativeRead = errors.New("bytes.Buffer: reader returned negative count from Read");
548
+ let maxInt = $.int((~0 >> 1));
549
+ export let MinRead = 512;
550
+ // growSlice grows b by n, preserving the original content of b.
551
+ // If the allocation fails, it panics with ErrTooLarge.
552
+ export function growSlice(b, n) {
553
+ const env_1 = { stack: [], error: void 0, hasError: false };
554
+ try {
555
+ const __defer = __addDisposableResource(env_1, new $.DisposableStack(), false);
556
+ __defer.defer(() => {
557
+ if (recover() != null) {
558
+ $.panic(ErrTooLarge);
559
+ }
560
+ });
561
+ // TODO(http://golang.org/issue/51462): We should rely on the append-make
562
+ // pattern so that the compiler can call runtime.growslice. For example:
563
+ // return append(b, make([]byte, n)...)
564
+ // This avoids unnecessary zero-ing of the first len(b) bytes of the
565
+ // allocated slice, but this pattern causes b to escape onto the heap.
566
+ //
567
+ // Instead use the append-make pattern with a nil slice to ensure that
568
+ // we allocate buffers rounded up to the closest size class.
569
+ let c = $.len(b) + n; // ensure enough space for n elements
570
+ // The growth rate has historically always been 2x. In the future,
571
+ // we could rely purely on append to determine the growth rate.
572
+ if (c < 2 * $.cap(b)) {
573
+ // The growth rate has historically always been 2x. In the future,
574
+ // we could rely purely on append to determine the growth rate.
575
+ c = 2 * $.cap(b);
576
+ }
577
+ let b2 = $.append(null, new Uint8Array(c));
578
+ let i = $.copy($.bytesToUint8Array(b2), $.bytesToUint8Array(b));
579
+ return $.goSlice(b2, undefined, i);
580
+ }
581
+ catch (e_1) {
582
+ env_1.error = e_1;
583
+ env_1.hasError = true;
584
+ }
585
+ finally {
586
+ __disposeResources(env_1);
587
+ }
588
+ }
589
+ let errUnreadByte = errors.New("bytes.Buffer: UnreadByte: previous operation was not a successful read");
590
+ // NewBuffer creates and initializes a new [Buffer] using buf as its
591
+ // initial contents. The new [Buffer] takes ownership of buf, and the
592
+ // caller should not use buf after this call. NewBuffer is intended to
593
+ // prepare a [Buffer] to read existing data. It can also be used to set
594
+ // the initial size of the internal buffer for writing. To do that,
595
+ // buf should have the desired capacity but a length of zero.
596
+ //
597
+ // In most cases, new([Buffer]) (or just declaring a [Buffer] variable) is
598
+ // sufficient to initialize a [Buffer].
599
+ export function NewBuffer(buf) {
600
+ return new Buffer({ buf: buf });
601
+ }
602
+ // NewBufferString creates and initializes a new [Buffer] using string s as its
603
+ // initial contents. It is intended to prepare a buffer to read an existing
604
+ // string.
605
+ //
606
+ // In most cases, new([Buffer]) (or just declaring a [Buffer] variable) is
607
+ // sufficient to initialize a [Buffer].
608
+ export function NewBufferString(s) {
609
+ return new Buffer({ buf: $.stringToBytes(s) });
610
+ }
611
+ //# sourceMappingURL=buffer.gs.js.map