goscript 0.0.42 → 0.0.43

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 (108) hide show
  1. package/compiler/analysis.go +87 -0
  2. package/compiler/assignment.go +6 -0
  3. package/compiler/compiler.go +85 -5
  4. package/compiler/decl.go +42 -20
  5. package/compiler/expr-call.go +37 -1
  6. package/compiler/expr-type.go +20 -0
  7. package/compiler/lit.go +4 -20
  8. package/compiler/spec-struct.go +3 -4
  9. package/compiler/spec-value.go +18 -1
  10. package/compiler/spec.go +15 -8
  11. package/compiler/stmt-range.go +1 -1
  12. package/compiler/type-assert.go +4 -4
  13. package/compiler/type.go +63 -5
  14. package/dist/gs/builtin/map.d.ts +4 -4
  15. package/dist/gs/builtin/map.js +6 -3
  16. package/dist/gs/builtin/map.js.map +1 -1
  17. package/dist/gs/builtin/slice.d.ts +7 -0
  18. package/dist/gs/builtin/slice.js +12 -0
  19. package/dist/gs/builtin/slice.js.map +1 -1
  20. package/dist/gs/builtin/type.js +8 -70
  21. package/dist/gs/builtin/type.js.map +1 -1
  22. package/dist/gs/fmt/fmt.d.ts +22 -21
  23. package/dist/gs/fmt/fmt.js +12 -12
  24. package/dist/gs/fmt/fmt.js.map +1 -1
  25. package/dist/gs/internal/testlog/index.d.ts +1 -0
  26. package/dist/gs/internal/testlog/index.js +5 -0
  27. package/dist/gs/internal/testlog/index.js.map +1 -0
  28. package/dist/gs/io/io.d.ts +13 -13
  29. package/dist/gs/io/io.js +31 -21
  30. package/dist/gs/io/io.js.map +1 -1
  31. package/dist/gs/maps/iter.gs.d.ts +7 -0
  32. package/dist/gs/maps/iter.gs.js +65 -0
  33. package/dist/gs/maps/iter.gs.js.map +1 -0
  34. package/dist/gs/maps/maps.gs.d.ts +7 -0
  35. package/dist/gs/maps/maps.gs.js +79 -0
  36. package/dist/gs/maps/maps.gs.js.map +1 -0
  37. package/dist/gs/path/filepath/match.d.ts +4 -3
  38. package/dist/gs/path/filepath/match.js +2 -2
  39. package/dist/gs/path/filepath/match.js.map +1 -1
  40. package/dist/gs/path/filepath/path.d.ts +2 -2
  41. package/dist/gs/path/filepath/path.js +3 -3
  42. package/dist/gs/path/filepath/path.js.map +1 -1
  43. package/dist/gs/reflect/abi.d.ts +59 -0
  44. package/dist/gs/reflect/abi.gs.d.ts +59 -0
  45. package/dist/gs/reflect/abi.gs.js +79 -0
  46. package/dist/gs/reflect/abi.gs.js.map +1 -0
  47. package/dist/gs/reflect/abi.js +79 -0
  48. package/dist/gs/reflect/abi.js.map +1 -0
  49. package/dist/gs/reflect/badlinkname.d.ts +52 -0
  50. package/dist/gs/reflect/badlinkname.gs.d.ts +52 -0
  51. package/dist/gs/reflect/badlinkname.gs.js +72 -0
  52. package/dist/gs/reflect/badlinkname.gs.js.map +1 -0
  53. package/dist/gs/reflect/badlinkname.js +72 -0
  54. package/dist/gs/reflect/badlinkname.js.map +1 -0
  55. package/dist/gs/reflect/deepequal.gs.d.ts +25 -0
  56. package/dist/gs/reflect/deepequal.gs.js +308 -0
  57. package/dist/gs/reflect/deepequal.gs.js.map +1 -0
  58. package/dist/gs/reflect/float32reg_generic.gs.d.ts +2 -0
  59. package/dist/gs/reflect/float32reg_generic.gs.js +10 -0
  60. package/dist/gs/reflect/float32reg_generic.gs.js.map +1 -0
  61. package/dist/gs/reflect/index.gs.d.ts +1 -0
  62. package/dist/gs/reflect/index.gs.js +3 -0
  63. package/dist/gs/reflect/index.gs.js.map +1 -0
  64. package/dist/gs/reflect/iter.gs.d.ts +3 -0
  65. package/dist/gs/reflect/iter.gs.js +24 -0
  66. package/dist/gs/reflect/iter.gs.js.map +1 -0
  67. package/dist/gs/reflect/makefunc.gs.d.ts +34 -0
  68. package/dist/gs/reflect/makefunc.gs.js +288 -0
  69. package/dist/gs/reflect/makefunc.gs.js.map +1 -0
  70. package/dist/gs/reflect/map_swiss.gs.d.ts +14 -0
  71. package/dist/gs/reflect/map_swiss.gs.js +70 -0
  72. package/dist/gs/reflect/map_swiss.gs.js.map +1 -0
  73. package/dist/gs/reflect/reflect.gs.d.ts +132 -0
  74. package/dist/gs/reflect/reflect.gs.js +437 -0
  75. package/dist/gs/reflect/reflect.gs.js.map +1 -0
  76. package/dist/gs/reflect/swapper.gs.d.ts +1 -0
  77. package/dist/gs/reflect/swapper.gs.js +32 -0
  78. package/dist/gs/reflect/swapper.gs.js.map +1 -0
  79. package/dist/gs/reflect/type.gs.d.ts +4 -0
  80. package/dist/gs/reflect/type.gs.js +21 -0
  81. package/dist/gs/reflect/type.gs.js.map +1 -0
  82. package/dist/gs/reflect/value.gs.d.ts +4 -0
  83. package/dist/gs/reflect/value.gs.js +12 -0
  84. package/dist/gs/reflect/value.gs.js.map +1 -0
  85. package/dist/gs/reflect/visiblefields.gs.d.ts +3 -0
  86. package/dist/gs/reflect/visiblefields.gs.js +123 -0
  87. package/dist/gs/reflect/visiblefields.gs.js.map +1 -0
  88. package/dist/gs/strings/reader.d.ts +1 -1
  89. package/dist/gs/strings/reader.js.map +1 -1
  90. package/dist/gs/stringslite/index.d.ts +1 -0
  91. package/dist/gs/stringslite/index.js +2 -0
  92. package/dist/gs/stringslite/index.js.map +1 -0
  93. package/dist/gs/stringslite/strings.d.ts +11 -0
  94. package/dist/gs/stringslite/strings.js +67 -0
  95. package/dist/gs/stringslite/strings.js.map +1 -0
  96. package/dist/gs/time/time.d.ts +69 -0
  97. package/dist/gs/time/time.js +350 -0
  98. package/dist/gs/time/time.js.map +1 -1
  99. package/gs/builtin/map.ts +12 -8
  100. package/gs/builtin/slice.ts +13 -0
  101. package/gs/builtin/type.ts +8 -100
  102. package/gs/fmt/fmt.ts +33 -33
  103. package/gs/io/io.ts +47 -39
  104. package/gs/path/filepath/match.ts +4 -4
  105. package/gs/path/filepath/path.ts +3 -3
  106. package/gs/strings/reader.ts +1 -1
  107. package/gs/time/time.ts +403 -0
  108. package/package.json +1 -1
package/gs/io/io.ts CHANGED
@@ -41,12 +41,12 @@ export const SeekEnd = 2 // seek relative to the end
41
41
 
42
42
  // Reader is the interface that wraps the basic Read method
43
43
  export interface Reader {
44
- Read(p: Uint8Array): [number, $.GoError]
44
+ Read(p: $.Bytes): [number, $.GoError]
45
45
  }
46
46
 
47
47
  // Writer is the interface that wraps the basic Write method
48
48
  export interface Writer {
49
- Write(p: Uint8Array): [number, $.GoError]
49
+ Write(p: $.Bytes): [number, $.GoError]
50
50
  }
51
51
 
52
52
  // Closer is the interface that wraps the basic Close method
@@ -70,12 +70,12 @@ export interface ReadWriteSeeker extends Reader, Writer, Seeker {}
70
70
 
71
71
  // ReaderAt is the interface that wraps the basic ReadAt method
72
72
  export interface ReaderAt {
73
- ReadAt(p: Uint8Array, off: number): [number, $.GoError]
73
+ ReadAt(p: $.Bytes, off: number): [number, $.GoError]
74
74
  }
75
75
 
76
76
  // WriterAt is the interface that wraps the basic WriteAt method
77
77
  export interface WriterAt {
78
- WriteAt(p: Uint8Array, off: number): [number, $.GoError]
78
+ WriteAt(p: $.Bytes, off: number): [number, $.GoError]
79
79
  }
80
80
 
81
81
  // ByteReader is the interface that wraps the ReadByte method
@@ -120,8 +120,8 @@ export interface ReaderFrom {
120
120
 
121
121
  // Discard is a Writer on which all Write calls succeed without doing anything
122
122
  class DiscardWriter implements Writer {
123
- Write(p: Uint8Array): [number, $.GoError] {
124
- return [p.length, null]
123
+ Write(p: $.Bytes): [number, $.GoError] {
124
+ return [$.len(p), null]
125
125
  }
126
126
  }
127
127
 
@@ -149,14 +149,14 @@ export class LimitedReader implements Reader {
149
149
  this.N = n
150
150
  }
151
151
 
152
- Read(p: Uint8Array): [number, $.GoError] {
152
+ Read(p: $.Bytes): [number, $.GoError] {
153
153
  if (this.N <= 0) {
154
154
  return [0, EOF]
155
155
  }
156
156
 
157
157
  let readBuf = p
158
- if (p.length > this.N) {
159
- readBuf = p.subarray(0, this.N)
158
+ if ($.len(p) > this.N) {
159
+ readBuf = $.goSlice(p, 0, this.N)
160
160
  }
161
161
 
162
162
  const [n, err] = this.R.Read(readBuf)
@@ -184,14 +184,14 @@ export class SectionReader implements Reader, Seeker, ReaderAt {
184
184
  this.limit = off + n
185
185
  }
186
186
 
187
- Read(p: Uint8Array): [number, $.GoError] {
187
+ Read(p: $.Bytes): [number, $.GoError] {
188
188
  if (this.off >= this.limit) {
189
189
  return [0, EOF]
190
190
  }
191
191
 
192
192
  let max = this.limit - this.off
193
- if (p.length > max) {
194
- p = p.subarray(0, max)
193
+ if ($.len(p) > max) {
194
+ p = $.goSlice(p, 0, max)
195
195
  }
196
196
 
197
197
  const [n, err] = this.r.ReadAt(p, this.off)
@@ -223,14 +223,14 @@ export class SectionReader implements Reader, Seeker, ReaderAt {
223
223
  return [abs - this.base, null]
224
224
  }
225
225
 
226
- ReadAt(p: Uint8Array, off: number): [number, $.GoError] {
226
+ ReadAt(p: $.Bytes, off: number): [number, $.GoError] {
227
227
  if (off < 0 || off >= this.limit - this.base) {
228
228
  return [0, EOF]
229
229
  }
230
230
 
231
231
  off += this.base
232
- if (off + p.length > this.limit) {
233
- p = p.subarray(0, this.limit - off)
232
+ if (off + $.len(p) > this.limit) {
233
+ p = $.goSlice(p, 0, this.limit - off)
234
234
  const [n, err] = this.r.ReadAt(p, off)
235
235
  if (err === null) {
236
236
  return [n, EOF]
@@ -267,13 +267,13 @@ export class OffsetWriter implements Writer, WriterAt {
267
267
  this.off = 0
268
268
  }
269
269
 
270
- Write(p: Uint8Array): [number, $.GoError] {
270
+ Write(p: $.Bytes): [number, $.GoError] {
271
271
  const [n, err] = this.w.WriteAt(p, this.base + this.off)
272
272
  this.off += n
273
273
  return [n, err]
274
274
  }
275
275
 
276
- WriteAt(p: Uint8Array, off: number): [number, $.GoError] {
276
+ WriteAt(p: $.Bytes, off: number): [number, $.GoError] {
277
277
  if (off < 0) {
278
278
  return [0, newError('io.OffsetWriter.WriteAt: negative offset')]
279
279
  }
@@ -316,7 +316,7 @@ export function Copy(dst: Writer, src: Reader): [number, $.GoError] {
316
316
  export function CopyBuffer(
317
317
  dst: Writer,
318
318
  src: Reader,
319
- buf: Uint8Array | null,
319
+ buf: $.Bytes | null,
320
320
  ): [number, $.GoError] {
321
321
  // If src implements WriterTo, use it
322
322
  if ('WriteTo' in src && typeof (src as any).WriteTo === 'function') {
@@ -329,14 +329,14 @@ export function CopyBuffer(
329
329
  }
330
330
 
331
331
  if (buf === null) {
332
- buf = new Uint8Array(32 * 1024) // 32KB default buffer
332
+ buf = $.makeSlice<number>(32 * 1024, undefined, 'byte') // 32KB default buffer
333
333
  }
334
334
 
335
335
  let written = 0
336
336
  while (true) {
337
337
  const [nr, er] = src.Read(buf)
338
338
  if (nr > 0) {
339
- const [nw, ew] = dst.Write(buf.subarray(0, nr))
339
+ const [nw, ew] = dst.Write($.goSlice(buf, 0, nr))
340
340
  if (nw < 0 || nr < nw) {
341
341
  if (ew === null) {
342
342
  return [written, ErrShortWrite]
@@ -381,16 +381,16 @@ export function CopyN(
381
381
  // ReadAtLeast reads from r into buf until it has read at least min bytes
382
382
  export function ReadAtLeast(
383
383
  r: Reader,
384
- buf: Uint8Array,
384
+ buf: $.Bytes,
385
385
  min: number,
386
386
  ): [number, $.GoError] {
387
- if (buf.length < min) {
387
+ if ($.len(buf) < min) {
388
388
  return [0, ErrShortBuffer]
389
389
  }
390
390
 
391
391
  let n = 0
392
392
  while (n < min) {
393
- const [nn, err] = r.Read(buf.subarray(n))
393
+ const [nn, err] = r.Read($.goSlice(buf, n))
394
394
  n += nn
395
395
  if (err !== null) {
396
396
  if (err === EOF && n >= min) {
@@ -406,36 +406,44 @@ export function ReadAtLeast(
406
406
  }
407
407
 
408
408
  // ReadFull reads exactly len(buf) bytes from r into buf
409
- export function ReadFull(r: Reader, buf: Uint8Array): [number, $.GoError] {
410
- return ReadAtLeast(r, buf, buf.length)
409
+ export function ReadFull(r: Reader, buf: $.Bytes): [number, $.GoError] {
410
+ return ReadAtLeast(r, buf, $.len(buf))
411
411
  }
412
412
 
413
413
  // ReadAll reads from r until an error or EOF and returns the data it read
414
- export function ReadAll(r: Reader): [Uint8Array, $.GoError] {
415
- const chunks: Uint8Array[] = []
414
+ export function ReadAll(r: Reader): [$.Bytes, $.GoError] {
415
+ const chunks: $.Bytes[] = []
416
416
  let totalLength = 0
417
- const buf = new Uint8Array(512)
417
+ const buf = $.makeSlice<number>(512, undefined, 'byte')
418
418
 
419
419
  while (true) {
420
420
  const [n, err] = r.Read(buf)
421
421
  if (n > 0) {
422
- chunks.push(buf.subarray(0, n))
422
+ chunks.push($.goSlice(buf, 0, n))
423
423
  totalLength += n
424
424
  }
425
425
  if (err !== null) {
426
426
  if (err === EOF) {
427
427
  break
428
428
  }
429
- return [new Uint8Array(0), err]
429
+ return [$.makeSlice<number>(0, undefined, 'byte'), err]
430
430
  }
431
431
  }
432
432
 
433
433
  // Combine all chunks
434
- const result = new Uint8Array(totalLength)
434
+ const result = $.makeSlice<number>(totalLength, undefined, 'byte')
435
435
  let offset = 0
436
436
  for (const chunk of chunks) {
437
- result.set(chunk, offset)
438
- offset += chunk.length
437
+ if (chunk instanceof Uint8Array) {
438
+ // Handle Uint8Array chunks
439
+ const resultSlice = $.goSlice(result, offset, offset + chunk.length)
440
+ $.copy(resultSlice, chunk)
441
+ } else {
442
+ // Handle Slice<number> chunks
443
+ const resultSlice = $.goSlice(result, offset, offset + $.len(chunk))
444
+ $.copy(resultSlice, chunk)
445
+ }
446
+ offset += $.len(chunk)
439
447
  }
440
448
 
441
449
  return [result, null]
@@ -461,7 +469,7 @@ class multiReader implements Reader {
461
469
  this.readers = readers
462
470
  }
463
471
 
464
- Read(p: Uint8Array): [number, $.GoError] {
472
+ Read(p: $.Bytes): [number, $.GoError] {
465
473
  while (this.readers.length > 0) {
466
474
  if (this.readers.length === 1) {
467
475
  // Optimization for single reader
@@ -502,17 +510,17 @@ class multiWriter implements Writer {
502
510
  this.writers = writers
503
511
  }
504
512
 
505
- Write(p: Uint8Array): [number, $.GoError] {
513
+ Write(p: $.Bytes): [number, $.GoError] {
506
514
  for (const w of this.writers) {
507
515
  const [n, err] = w.Write(p)
508
516
  if (err !== null) {
509
517
  return [n, err]
510
518
  }
511
- if (n !== p.length) {
519
+ if (n !== $.len(p)) {
512
520
  return [n, ErrShortWrite]
513
521
  }
514
522
  }
515
- return [p.length, null]
523
+ return [$.len(p), null]
516
524
  }
517
525
  }
518
526
 
@@ -530,10 +538,10 @@ class teeReader implements Reader {
530
538
  this.w = w
531
539
  }
532
540
 
533
- Read(p: Uint8Array): [number, $.GoError] {
541
+ Read(p: $.Bytes): [number, $.GoError] {
534
542
  const [n, err] = this.r.Read(p)
535
543
  if (n > 0) {
536
- const [nw, ew] = this.w.Write(p.subarray(0, n))
544
+ const [nw, ew] = this.w.Write($.goSlice(p, 0, n))
537
545
  if (ew !== null) {
538
546
  return [n, ew]
539
547
  }
@@ -1,6 +1,6 @@
1
- // Pattern matching functions for filepath
1
+ import * as $ from '@goscript/builtin/index.js'
2
2
 
3
- export const ErrBadPattern = new Error('syntax error in pattern')
3
+ export const ErrBadPattern = $.newError('syntax error in pattern')
4
4
 
5
5
  // Match reports whether name matches the shell file name pattern.
6
6
  // The pattern syntax is:
@@ -23,7 +23,7 @@ export const ErrBadPattern = new Error('syntax error in pattern')
23
23
  // Match requires pattern to match all of name, not just a substring.
24
24
  // The only possible returned error is ErrBadPattern, when pattern
25
25
  // is malformed.
26
- export function Match(pattern: string, name: string): [boolean, Error | null] {
26
+ export function Match(pattern: string, name: string): [boolean, $.GoError] {
27
27
  try {
28
28
  // Validate pattern first
29
29
  validatePattern(pattern)
@@ -236,7 +236,7 @@ function matchCharClass(
236
236
  //
237
237
  // Glob ignores file system errors such as I/O errors reading directories.
238
238
  // The only possible returned error is ErrBadPattern, when pattern is malformed.
239
- export function Glob(pattern: string): [string[], Error | null] {
239
+ export function Glob(pattern: string): [string[], $.GoError] {
240
240
  try {
241
241
  // Validate the pattern using the same logic as Match
242
242
  validatePattern(pattern)
@@ -3,8 +3,8 @@
3
3
  import * as $ from "@goscript/builtin/index.js"
4
4
 
5
5
  // Path separator constants
6
- export const Separator = '/'
7
- export const ListSeparator = ':'
6
+ export const Separator = $.stringToRune('/')
7
+ export const ListSeparator = $.stringToRune(':')
8
8
 
9
9
  // Error constants
10
10
  export const SkipDir = $.newError('skip this directory')
@@ -234,7 +234,7 @@ export function SplitList(path: string): string[] {
234
234
  if (path === '') {
235
235
  return []
236
236
  }
237
- return path.split(ListSeparator)
237
+ return path.split(String.fromCharCode(ListSeparator))
238
238
  }
239
239
 
240
240
  // HasPrefix tests whether the path p begins with prefix.
@@ -72,7 +72,7 @@ export class Reader {
72
72
  }
73
73
 
74
74
  // Read implements the [io.Reader] interface.
75
- public Read(b: Uint8Array): [number, $.GoError] {
75
+ public Read(b: $.Bytes): [number, $.GoError] {
76
76
  const r = this
77
77
  if (r!.i >= ($.len(r!.s) as number)) {
78
78
  return [0, io.EOF]