goscript 0.0.61 → 0.0.62

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 (81) hide show
  1. package/README.md +9 -0
  2. package/compiler/analysis.go +536 -15
  3. package/compiler/assignment.go +72 -0
  4. package/compiler/compiler.go +64 -11
  5. package/compiler/composite-lit.go +29 -8
  6. package/compiler/decl.go +20 -11
  7. package/compiler/expr-call-async.go +26 -1
  8. package/compiler/expr-call-builtins.go +60 -4
  9. package/compiler/expr-call-type-conversion.go +37 -5
  10. package/compiler/expr-call.go +16 -3
  11. package/compiler/expr-selector.go +35 -2
  12. package/compiler/expr-type.go +12 -2
  13. package/compiler/expr.go +37 -0
  14. package/compiler/index.test.ts +3 -1
  15. package/compiler/lit.go +13 -4
  16. package/compiler/spec-struct.go +30 -8
  17. package/compiler/spec-value.go +2 -2
  18. package/compiler/spec.go +21 -4
  19. package/compiler/stmt-assign.go +71 -0
  20. package/compiler/stmt-range.go +2 -2
  21. package/compiler/stmt.go +128 -0
  22. package/compiler/type-utils.go +40 -1
  23. package/compiler/type.go +50 -12
  24. package/dist/gs/builtin/builtin.d.ts +8 -1
  25. package/dist/gs/builtin/builtin.js +26 -1
  26. package/dist/gs/builtin/builtin.js.map +1 -1
  27. package/dist/gs/builtin/errors.d.ts +1 -0
  28. package/dist/gs/builtin/errors.js +8 -0
  29. package/dist/gs/builtin/errors.js.map +1 -1
  30. package/dist/gs/builtin/slice.d.ts +5 -4
  31. package/dist/gs/builtin/slice.js +45 -14
  32. package/dist/gs/builtin/slice.js.map +1 -1
  33. package/dist/gs/builtin/type.d.ts +23 -2
  34. package/dist/gs/builtin/type.js +125 -0
  35. package/dist/gs/builtin/type.js.map +1 -1
  36. package/dist/gs/bytes/reader.gs.d.ts +1 -1
  37. package/dist/gs/bytes/reader.gs.js +1 -1
  38. package/dist/gs/bytes/reader.gs.js.map +1 -1
  39. package/dist/gs/reflect/index.d.ts +2 -2
  40. package/dist/gs/reflect/index.js +1 -1
  41. package/dist/gs/reflect/index.js.map +1 -1
  42. package/dist/gs/reflect/map.d.ts +3 -2
  43. package/dist/gs/reflect/map.js +37 -3
  44. package/dist/gs/reflect/map.js.map +1 -1
  45. package/dist/gs/reflect/type.d.ts +50 -12
  46. package/dist/gs/reflect/type.js +820 -27
  47. package/dist/gs/reflect/type.js.map +1 -1
  48. package/dist/gs/reflect/types.d.ts +11 -12
  49. package/dist/gs/reflect/types.js +26 -15
  50. package/dist/gs/reflect/types.js.map +1 -1
  51. package/dist/gs/reflect/value.d.ts +4 -4
  52. package/dist/gs/reflect/value.js +8 -2
  53. package/dist/gs/reflect/value.js.map +1 -1
  54. package/dist/gs/slices/slices.d.ts +21 -0
  55. package/dist/gs/slices/slices.js +48 -0
  56. package/dist/gs/slices/slices.js.map +1 -1
  57. package/dist/gs/sync/atomic/type.gs.d.ts +2 -2
  58. package/dist/gs/sync/atomic/type.gs.js +12 -2
  59. package/dist/gs/sync/atomic/type.gs.js.map +1 -1
  60. package/dist/gs/unicode/utf8/utf8.d.ts +2 -2
  61. package/dist/gs/unicode/utf8/utf8.js +10 -6
  62. package/dist/gs/unicode/utf8/utf8.js.map +1 -1
  63. package/go.mod +4 -4
  64. package/go.sum +8 -8
  65. package/gs/builtin/builtin.ts +27 -2
  66. package/gs/builtin/errors.ts +12 -0
  67. package/gs/builtin/slice.ts +71 -7
  68. package/gs/builtin/type.ts +159 -2
  69. package/gs/bytes/reader.gs.ts +2 -2
  70. package/gs/math/hypot.gs.test.ts +3 -1
  71. package/gs/math/pow10.gs.test.ts +5 -4
  72. package/gs/reflect/index.ts +3 -2
  73. package/gs/reflect/map.test.ts +7 -6
  74. package/gs/reflect/map.ts +49 -7
  75. package/gs/reflect/type.ts +1053 -54
  76. package/gs/reflect/types.ts +34 -21
  77. package/gs/reflect/value.ts +12 -6
  78. package/gs/slices/slices.ts +55 -0
  79. package/gs/sync/atomic/type.gs.ts +14 -5
  80. package/gs/unicode/utf8/utf8.ts +12 -8
  81. package/package.json +13 -13
@@ -43,7 +43,9 @@ describe('Hypot', () => {
43
43
  it('should handle very large values without overflow', () => {
44
44
  const large = 1e150
45
45
  const result = Hypot(large, large)
46
- expect(result).toBeCloseTo(large * Math.sqrt(2), 5)
46
+ const expected = large * Math.sqrt(2)
47
+ // Use relative tolerance for very large numbers
48
+ expect(Math.abs(result - expected) / expected).toBeLessThan(1e-10)
47
49
  expect(IsInf(result, 0)).toBe(false)
48
50
  })
49
51
 
@@ -26,10 +26,11 @@ describe('Pow10', () => {
26
26
  })
27
27
 
28
28
  it('should handle large positive exponents', () => {
29
- expect(Pow10(100)).toBe(1e100)
30
- expect(Pow10(200)).toBe(1e200)
31
- expect(Pow10(300)).toBe(1e300)
32
- expect(Pow10(308)).toBe(1e308)
29
+ // Use relative tolerance for very large numbers
30
+ expect(Math.abs(Pow10(100) - 1e100) / 1e100).toBeLessThan(1e-10)
31
+ expect(Math.abs(Pow10(200) - 1e200) / 1e200).toBeLessThan(1e-10)
32
+ expect(Math.abs(Pow10(300) - 1e300) / 1e300).toBeLessThan(1e-10)
33
+ expect(Math.abs(Pow10(308) - 1e308) / 1e308).toBeLessThan(1e-10)
33
34
  expect(Pow10(309)).toBe(Number.POSITIVE_INFINITY)
34
35
  expect(Pow10(400)).toBe(Number.POSITIVE_INFINITY)
35
36
  })
@@ -35,19 +35,20 @@ export { Swapper } from './swapper.js'
35
35
  // Export new types and constants
36
36
  export {
37
37
  StructTag,
38
+ StructTag_Get,
38
39
  StructField,
39
40
  ValueError,
40
- SelectDir,
41
41
  SelectSend,
42
42
  SelectRecv,
43
43
  SelectDefault,
44
+ SelectCase,
44
45
  bitVector,
45
46
  } from './types.js'
46
47
  export type {
47
48
  uintptr,
48
49
  Pointer,
49
50
  Method,
50
- SelectCase,
51
+ SelectDir,
51
52
  SliceHeader,
52
53
  StringHeader,
53
54
  MapIter,
@@ -11,20 +11,21 @@ describe('MapIter', () => {
11
11
  const iter = new MapIter<string, number>(map)
12
12
 
13
13
  expect(iter.current?.done === false).toBe(true)
14
- expect(iter.Key()).toBe('one')
15
- expect(iter.Value()).toBe(1)
14
+ // Key() and Value() return reflect.Value objects now
15
+ expect(iter.Key().Interface()).toBe('one')
16
+ expect(iter.Value().Interface()).toBe(1)
16
17
 
17
18
  expect(iter.Next()).toBe(true)
18
19
  expect(iter.current?.done === false).toBe(true)
19
- expect(typeof iter.Key()).toBe('string')
20
- expect(typeof iter.Value()).toBe('number')
20
+ expect(iter.Key().Kind()).toBe(24) // String kind
21
+ expect(iter.Value().Kind()).toBe(2) // Int kind
21
22
 
22
23
  const newMap = new Map<string, number>()
23
24
  newMap.set('reset', 100)
24
25
  iter.Reset(newMap)
25
26
 
26
27
  expect(iter.current?.done === false).toBe(true)
27
- expect(iter.Key()).toBe('reset')
28
- expect(iter.Value()).toBe(100)
28
+ expect(iter.Key().Interface()).toBe('reset')
29
+ expect(iter.Value().Interface()).toBe(100)
29
30
  })
30
31
  })
package/gs/reflect/map.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { Type, Kind, Value, Map as MapKind } from './type.js'
1
+ import { Type, Kind, Value, Map as MapKind, StructField, TypeOf } from './type.js'
2
2
 
3
3
  // Simple MapOf implementation using JavaScript Map
4
4
  export function MapOf(key: Type, elem: Type): Type {
@@ -20,26 +20,66 @@ class MapType implements Type {
20
20
  return MapKind // Map kind
21
21
  }
22
22
 
23
+ public Name(): string {
24
+ return '' // Map types are unnamed composite types
25
+ }
26
+
23
27
  public Size(): number {
24
28
  return 8 // pointer size
25
29
  }
26
30
 
27
- public Elem(): Type | null {
31
+ public Elem(): Type {
28
32
  return this._elemType
29
33
  }
30
34
 
31
- public Key(): Type | null {
35
+ public Key(): Type {
32
36
  return this._keyType
33
37
  }
34
38
 
35
39
  public NumField(): number {
36
40
  return 0
37
41
  }
42
+
43
+ public Field(_i: number): StructField {
44
+ throw new Error('reflect: Field of non-struct type map')
45
+ }
46
+
47
+ public Implements(u: Type | null): boolean {
48
+ if (!u) {
49
+ return false
50
+ }
51
+ if (u.Kind() !== 20) {
52
+ // Interface kind
53
+ throw new Error('reflect: non-interface type passed to Type.Implements')
54
+ }
55
+ return false
56
+ }
57
+
58
+ public OverflowInt(_x: number): boolean {
59
+ throw new Error('reflect: OverflowInt of non-integer type map')
60
+ }
61
+
62
+ public OverflowUint(_x: number): boolean {
63
+ throw new Error('reflect: OverflowUint of non-integer type map')
64
+ }
65
+
66
+ public OverflowFloat(_x: number): boolean {
67
+ throw new Error('reflect: OverflowFloat of non-float type map')
68
+ }
69
+
70
+ public NumMethod(): number {
71
+ return 0
72
+ }
73
+
74
+ public Bits(): number {
75
+ throw new Error('reflect: Bits of non-sized type map')
76
+ }
38
77
  }
39
78
 
40
79
  /**
41
80
  * MapIter provides an iterator interface for Go maps.
42
81
  * It wraps a JavaScript Map iterator and provides methods to iterate over key-value pairs.
82
+ * Returns reflect.Value for Key() and Value() to match Go's reflect.MapIter.
43
83
  * @template K - The type of keys in the map
44
84
  * @template V - The type of values in the map
45
85
  */
@@ -57,12 +97,14 @@ export class MapIter<K = unknown, V = unknown> {
57
97
  return !this.current.done
58
98
  }
59
99
 
60
- public Key(): K | null {
61
- return this.current?.value?.[0] ?? null
100
+ public Key(): Value {
101
+ const rawKey = this.current?.value?.[0] ?? null
102
+ return new Value(rawKey, TypeOf(rawKey))
62
103
  }
63
104
 
64
- public Value(): V | null {
65
- return this.current?.value?.[1] ?? null
105
+ public Value(): Value {
106
+ const rawVal = this.current?.value?.[1] ?? null
107
+ return new Value(rawVal, TypeOf(rawVal))
66
108
  }
67
109
 
68
110
  public Reset(m: Map<K, V>): void {