goscript 0.0.60 → 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 (87) hide show
  1. package/README.md +9 -0
  2. package/compiler/analysis.go +974 -369
  3. package/compiler/assignment.go +72 -0
  4. package/compiler/compiler.go +74 -15
  5. package/compiler/composite-lit.go +29 -8
  6. package/compiler/decl.go +67 -98
  7. package/compiler/expr-call-async.go +26 -1
  8. package/compiler/expr-call-builtins.go +60 -4
  9. package/compiler/expr-call-helpers.go +182 -0
  10. package/compiler/expr-call-type-conversion.go +37 -5
  11. package/compiler/expr-call.go +25 -33
  12. package/compiler/expr-selector.go +71 -1
  13. package/compiler/expr-type.go +49 -3
  14. package/compiler/expr.go +37 -28
  15. package/compiler/index.test.ts +3 -1
  16. package/compiler/lit.go +13 -4
  17. package/compiler/spec-struct.go +42 -9
  18. package/compiler/spec-value.go +2 -2
  19. package/compiler/spec.go +42 -5
  20. package/compiler/stmt-assign.go +71 -0
  21. package/compiler/stmt-range.go +2 -2
  22. package/compiler/stmt.go +130 -10
  23. package/compiler/type-utils.go +40 -16
  24. package/compiler/type.go +50 -12
  25. package/dist/gs/builtin/builtin.d.ts +8 -1
  26. package/dist/gs/builtin/builtin.js +26 -1
  27. package/dist/gs/builtin/builtin.js.map +1 -1
  28. package/dist/gs/builtin/errors.d.ts +1 -0
  29. package/dist/gs/builtin/errors.js +8 -0
  30. package/dist/gs/builtin/errors.js.map +1 -1
  31. package/dist/gs/builtin/slice.d.ts +5 -4
  32. package/dist/gs/builtin/slice.js +51 -21
  33. package/dist/gs/builtin/slice.js.map +1 -1
  34. package/dist/gs/builtin/type.d.ts +28 -2
  35. package/dist/gs/builtin/type.js +132 -0
  36. package/dist/gs/builtin/type.js.map +1 -1
  37. package/dist/gs/bytes/reader.gs.d.ts +1 -1
  38. package/dist/gs/bytes/reader.gs.js +1 -1
  39. package/dist/gs/bytes/reader.gs.js.map +1 -1
  40. package/dist/gs/internal/byteorder/index.d.ts +6 -0
  41. package/dist/gs/internal/byteorder/index.js +34 -0
  42. package/dist/gs/internal/byteorder/index.js.map +1 -1
  43. package/dist/gs/reflect/index.d.ts +3 -3
  44. package/dist/gs/reflect/index.js +2 -2
  45. package/dist/gs/reflect/index.js.map +1 -1
  46. package/dist/gs/reflect/map.d.ts +3 -2
  47. package/dist/gs/reflect/map.js +37 -3
  48. package/dist/gs/reflect/map.js.map +1 -1
  49. package/dist/gs/reflect/type.d.ts +55 -8
  50. package/dist/gs/reflect/type.js +889 -23
  51. package/dist/gs/reflect/type.js.map +1 -1
  52. package/dist/gs/reflect/types.d.ts +11 -12
  53. package/dist/gs/reflect/types.js +26 -15
  54. package/dist/gs/reflect/types.js.map +1 -1
  55. package/dist/gs/reflect/value.d.ts +4 -4
  56. package/dist/gs/reflect/value.js +8 -2
  57. package/dist/gs/reflect/value.js.map +1 -1
  58. package/dist/gs/slices/slices.d.ts +32 -0
  59. package/dist/gs/slices/slices.js +81 -0
  60. package/dist/gs/slices/slices.js.map +1 -1
  61. package/dist/gs/sync/atomic/type.gs.d.ts +2 -2
  62. package/dist/gs/sync/atomic/type.gs.js +12 -2
  63. package/dist/gs/sync/atomic/type.gs.js.map +1 -1
  64. package/dist/gs/unicode/utf8/utf8.d.ts +2 -2
  65. package/dist/gs/unicode/utf8/utf8.js +10 -6
  66. package/dist/gs/unicode/utf8/utf8.js.map +1 -1
  67. package/go.mod +4 -4
  68. package/go.sum +8 -16
  69. package/gs/builtin/builtin.ts +27 -2
  70. package/gs/builtin/errors.ts +12 -0
  71. package/gs/builtin/slice.ts +77 -14
  72. package/gs/builtin/type.ts +167 -2
  73. package/gs/bytes/reader.gs.ts +2 -2
  74. package/gs/internal/byteorder/index.ts +40 -0
  75. package/gs/math/hypot.gs.test.ts +3 -1
  76. package/gs/math/pow10.gs.test.ts +5 -4
  77. package/gs/reflect/index.ts +6 -3
  78. package/gs/reflect/map.test.ts +7 -6
  79. package/gs/reflect/map.ts +49 -7
  80. package/gs/reflect/type.ts +1139 -43
  81. package/gs/reflect/types.ts +34 -21
  82. package/gs/reflect/value.ts +12 -6
  83. package/gs/slices/slices.ts +92 -0
  84. package/gs/sync/atomic/type.gs.ts +14 -5
  85. package/gs/sync/meta.json +1 -1
  86. package/gs/unicode/utf8/utf8.ts +12 -8
  87. package/package.json +13 -13
@@ -1,5 +1,16 @@
1
- import { ReflectValue, StructField } from './types.js'
1
+ import { ReflectValue, StructField, StructTag, ValueError } from './types.js'
2
+ export { StructField }
2
3
  import { MapIter } from './map.js'
4
+ import {
5
+ getTypeByName as builtinGetTypeByName,
6
+ TypeKind,
7
+ isStructTypeInfo,
8
+ isInterfaceTypeInfo,
9
+ isStructFieldInfo,
10
+ } from '../builtin/type.js'
11
+ import { Zero } from './value.js'
12
+ import { DeepEqual } from './deepequal.js'
13
+ import * as $ from '../builtin/index.js'
3
14
 
4
15
  // rtype is the common implementation of most values
5
16
  export class rtype {
@@ -173,7 +184,8 @@ export interface Type {
173
184
  Size(): number
174
185
 
175
186
  // Elem returns a type's element type.
176
- Elem(): Type | null
187
+ // Panics if the type's Kind is not Array, Chan, Map, Pointer, or Slice.
188
+ Elem(): Type
177
189
 
178
190
  // NumField returns a struct type's field count.
179
191
  NumField(): number
@@ -182,18 +194,113 @@ export interface Type {
182
194
  PkgPath?(): string
183
195
 
184
196
  // Field returns a struct type's i'th field.
185
- Field?(i: number): StructField | null
197
+ // Panics if the type's Kind is not Struct or i is out of range.
198
+ Field(i: number): StructField
199
+
200
+ // Key returns a map type's key type.
201
+ // Panics if the type's Kind is not Map.
202
+ Key(): Type
203
+
204
+ // Name returns the type's name within its package.
205
+ Name(): string
206
+
207
+ // Implements reports whether the type implements the interface type u.
208
+ Implements(u: Type | null): boolean
186
209
 
187
210
  // common returns the common type implementation.
188
211
  common?(): rtype
212
+
213
+ // OverflowInt reports whether the int64 x cannot be represented by the type
214
+ // Panics if the type's Kind is not Int, Int8, Int16, Int32, or Int64.
215
+ OverflowInt(x: number): boolean
216
+
217
+ // OverflowUint reports whether the uint64 x cannot be represented by the type
218
+ // Panics if the type's Kind is not Uint, Uint8, Uint16, Uint32, Uint64, or Uintptr.
219
+ OverflowUint(x: number): boolean
220
+
221
+ // OverflowFloat reports whether the float64 x cannot be represented by the type
222
+ // Panics if the type's Kind is not Float32 or Float64.
223
+ OverflowFloat(x: number): boolean
224
+
225
+ // NumMethod returns the number of methods in the type's method set
226
+ NumMethod(): number
227
+
228
+ // Bits returns the size of the type in bits
229
+ // Panics if the type's Kind is not a sized type.
230
+ Bits(): number
189
231
  }
190
232
 
233
+ // InvalidTypeInstance is a singleton type for invalid/zero reflect.Value
234
+ class InvalidTypeClass implements Type {
235
+ Kind(): Kind {
236
+ return Invalid
237
+ }
238
+ String(): string {
239
+ return '<invalid reflect.Value>'
240
+ }
241
+ Name(): string {
242
+ return ''
243
+ }
244
+ Size(): number {
245
+ return 0
246
+ }
247
+ Elem(): Type {
248
+ throw new Error('reflect: Elem of invalid type')
249
+ }
250
+ Key(): Type {
251
+ throw new Error('reflect: Key of invalid type')
252
+ }
253
+ NumField(): number {
254
+ return 0
255
+ }
256
+ Field(_i: number): StructField {
257
+ throw new Error('reflect: Field of invalid type')
258
+ }
259
+ Implements(_u: Type | null): boolean {
260
+ return false
261
+ }
262
+ OverflowInt(_x: number): boolean {
263
+ throw new Error('reflect: OverflowInt of invalid type')
264
+ }
265
+ OverflowUint(_x: number): boolean {
266
+ throw new Error('reflect: OverflowUint of invalid type')
267
+ }
268
+ OverflowFloat(_x: number): boolean {
269
+ throw new Error('reflect: OverflowFloat of invalid type')
270
+ }
271
+ NumMethod(): number {
272
+ return 0
273
+ }
274
+ Bits(): number {
275
+ throw new Error('reflect: Bits of invalid type')
276
+ }
277
+ }
278
+ const invalidTypeInstance = new InvalidTypeClass()
279
+
191
280
  // Value is the reflection interface to a Go value - consolidated from all implementations
192
281
  export class Value {
193
- constructor(
194
- private _value: ReflectValue,
195
- private _type: Type,
196
- ) {}
282
+ private _value: ReflectValue
283
+ private _type: Type
284
+
285
+ constructor(value?: ReflectValue | Record<string, never>, type?: Type | null) {
286
+ // Handle zero-value initialization: new Value({}) or new Value()
287
+ // This corresponds to reflect.Value{} in Go which is an invalid/zero value
288
+ if (
289
+ type === undefined ||
290
+ type === null ||
291
+ (typeof value === 'object' &&
292
+ value !== null &&
293
+ Object.keys(value).length === 0 &&
294
+ !(value instanceof globalThis.Array) &&
295
+ !(value instanceof globalThis.Map))
296
+ ) {
297
+ this._value = null
298
+ this._type = invalidTypeInstance
299
+ } else {
300
+ this._value = value as ReflectValue
301
+ this._type = type
302
+ }
303
+ }
197
304
 
198
305
  public clone(): Value {
199
306
  return new Value(this._value, this._type)
@@ -346,9 +453,21 @@ export class Value {
346
453
  return this._type.NumField()
347
454
  }
348
455
 
349
- public Field(_i: number): Value {
350
- // Simplified implementation for struct field access
351
- return new Value(null, this._type)
456
+ public Field(i: number): Value {
457
+ if (this.Kind() !== Struct) {
458
+ throw new ValueError({ Kind: this.Kind(), Method: 'Field' })
459
+ }
460
+
461
+ const field = this.Type().Field(i)
462
+ if (!field) {
463
+ throw new Error('reflect: struct field index out of range')
464
+ }
465
+
466
+ let fieldVal = (this._value as Record<string, any>)[field.Name]
467
+ if (fieldVal === undefined) {
468
+ fieldVal = null
469
+ }
470
+ return new Value(fieldVal, field.Type)
352
471
  }
353
472
 
354
473
  // Additional methods needed by various parts of the codebase
@@ -375,7 +494,42 @@ export class Value {
375
494
  return new Value(this._value, t)
376
495
  }
377
496
 
497
+ public CanAddr(): boolean {
498
+ return this.Kind() !== Ptr && this._value !== null // Simplified
499
+ }
500
+
501
+ public Addr(): Value {
502
+ if (!this.CanAddr()) {
503
+ throw new Error('reflect: call of reflect.Value.Addr on invalid Value')
504
+ }
505
+ const ptrType = PointerTo(this.Type())
506
+ return new Value(this, ptrType) // Simplified
507
+ }
508
+
509
+ public CanSet(): boolean {
510
+ return this.IsValid() && this.Kind() !== Ptr // Simplified
511
+ }
512
+
513
+ public Set(x: Value): void {
514
+ if (!this.CanSet()) {
515
+ throw new Error('reflect: assign to invalid value')
516
+ }
517
+ if (this.Type() !== x.Type()) {
518
+ throw new Error('reflect: assign to wrong type')
519
+ }
520
+ this._value = x.value
521
+ }
522
+
378
523
  // Additional methods from deleted reflect.gs.ts
524
+ public Interface(): any {
525
+ return this._value
526
+ }
527
+
528
+ public IsZero(): boolean {
529
+ const zeroVal = Zero(this.Type()).value
530
+ return DeepEqual(this._value, zeroVal)
531
+ }
532
+
379
533
  public typ(): rtype | null {
380
534
  return new rtype(this._type.Kind())
381
535
  }
@@ -426,6 +580,269 @@ export class Value {
426
580
  channelObj._sendQueue.push(valueToSend)
427
581
  }
428
582
  }
583
+
584
+ // SetString sets v's underlying value to x
585
+ public SetString(x: string): void {
586
+ if (!this.CanSet()) {
587
+ throw new Error(
588
+ 'reflect: call of reflect.Value.SetString on unaddressable value',
589
+ )
590
+ }
591
+ if (this.Kind() !== String) {
592
+ throw new Error(
593
+ 'reflect: call of reflect.Value.SetString on ' + this.Kind() + ' Value',
594
+ )
595
+ }
596
+ this._value = x
597
+ }
598
+
599
+ // SetInt sets v's underlying value to x
600
+ public SetInt(x: number): void {
601
+ if (!this.CanSet()) {
602
+ throw new Error(
603
+ 'reflect: call of reflect.Value.SetInt on unaddressable value',
604
+ )
605
+ }
606
+ const k = this.Kind()
607
+ if (k !== Int && k !== Int8 && k !== Int16 && k !== Int32 && k !== Int64) {
608
+ throw new Error(
609
+ 'reflect: call of reflect.Value.SetInt on ' + k + ' Value',
610
+ )
611
+ }
612
+ this._value = x
613
+ }
614
+
615
+ // SetUint sets v's underlying value to x
616
+ public SetUint(x: number): void {
617
+ if (!this.CanSet()) {
618
+ throw new Error(
619
+ 'reflect: call of reflect.Value.SetUint on unaddressable value',
620
+ )
621
+ }
622
+ const k = this.Kind()
623
+ if (
624
+ k !== Uint &&
625
+ k !== Uint8 &&
626
+ k !== Uint16 &&
627
+ k !== Uint32 &&
628
+ k !== Uint64 &&
629
+ k !== Uintptr
630
+ ) {
631
+ throw new Error(
632
+ 'reflect: call of reflect.Value.SetUint on ' + k + ' Value',
633
+ )
634
+ }
635
+ this._value = x
636
+ }
637
+
638
+ // SetBool sets v's underlying value to x
639
+ public SetBool(x: boolean): void {
640
+ if (!this.CanSet()) {
641
+ throw new Error(
642
+ 'reflect: call of reflect.Value.SetBool on unaddressable value',
643
+ )
644
+ }
645
+ if (this.Kind() !== Bool) {
646
+ throw new Error(
647
+ 'reflect: call of reflect.Value.SetBool on ' + this.Kind() + ' Value',
648
+ )
649
+ }
650
+ this._value = x
651
+ }
652
+
653
+ // SetFloat sets v's underlying value to x
654
+ public SetFloat(x: number): void {
655
+ if (!this.CanSet()) {
656
+ throw new Error(
657
+ 'reflect: call of reflect.Value.SetFloat on unaddressable value',
658
+ )
659
+ }
660
+ const k = this.Kind()
661
+ if (k !== Float32 && k !== Float64) {
662
+ throw new Error(
663
+ 'reflect: call of reflect.Value.SetFloat on ' + k + ' Value',
664
+ )
665
+ }
666
+ this._value = x
667
+ }
668
+
669
+ // SetBytes sets v's underlying value to x
670
+ public SetBytes(x: $.Slice<number>): void {
671
+ if (!this.CanSet()) {
672
+ throw new Error(
673
+ 'reflect: call of reflect.Value.SetBytes on unaddressable value',
674
+ )
675
+ }
676
+ if (this.Kind() !== Slice) {
677
+ throw new Error(
678
+ 'reflect: call of reflect.Value.SetBytes on ' + this.Kind() + ' Value',
679
+ )
680
+ }
681
+ // Convert Uint8Array or slice to array
682
+ if (x instanceof Uint8Array) {
683
+ this._value = globalThis.Array.from(x)
684
+ } else if (globalThis.Array.isArray(x)) {
685
+ this._value = x
686
+ } else {
687
+ this._value = x
688
+ }
689
+ }
690
+
691
+ // SetZero sets v to be the zero value of v's type
692
+ public SetZero(): void {
693
+ if (!this.CanSet()) {
694
+ throw new Error(
695
+ 'reflect: call of reflect.Value.SetZero on unaddressable value',
696
+ )
697
+ }
698
+ const zeroVal = Zero(this.Type())
699
+ this._value = (zeroVal as unknown as { value: ReflectValue }).value
700
+ }
701
+
702
+ // SetLen sets v's length to n
703
+ public SetLen(n: number): void {
704
+ if (!this.CanSet()) {
705
+ throw new Error(
706
+ 'reflect: call of reflect.Value.SetLen on unaddressable value',
707
+ )
708
+ }
709
+ if (this.Kind() !== Slice) {
710
+ throw new Error(
711
+ 'reflect: call of reflect.Value.SetLen on ' + this.Kind() + ' Value',
712
+ )
713
+ }
714
+ if (globalThis.Array.isArray(this._value)) {
715
+ this._value.length = n
716
+ }
717
+ }
718
+
719
+ // SetMapIndex sets the element associated with key in the map v to elem
720
+ public SetMapIndex(key: Value, elem: Value): void {
721
+ if (!this.CanSet()) {
722
+ throw new Error(
723
+ 'reflect: call of reflect.Value.SetMapIndex on unaddressable value',
724
+ )
725
+ }
726
+ if (this.Kind() !== Map) {
727
+ throw new Error(
728
+ 'reflect: call of reflect.Value.SetMapIndex on ' +
729
+ this.Kind() +
730
+ ' Value',
731
+ )
732
+ }
733
+ const mapObj = this._value as globalThis.Map<unknown, unknown>
734
+ const keyVal = (key as unknown as { value: ReflectValue }).value
735
+ const elemVal = (elem as unknown as { value: ReflectValue }).value
736
+ mapObj.set(keyVal, elemVal)
737
+ }
738
+
739
+ // Grow increases the slice's capacity, if necessary
740
+ public Grow(n: number): void {
741
+ if (this.Kind() !== Slice) {
742
+ throw new Error(
743
+ 'reflect: call of reflect.Value.Grow on ' + this.Kind() + ' Value',
744
+ )
745
+ }
746
+ if (!globalThis.Array.isArray(this._value)) {
747
+ return
748
+ }
749
+ // JavaScript arrays grow automatically, but we ensure capacity
750
+ const currentLen = this._value.length
751
+ const targetCap = currentLen + n
752
+ if (this._value.length < targetCap) {
753
+ this._value.length = targetCap
754
+ this._value.length = currentLen // Reset to original length
755
+ }
756
+ }
757
+
758
+ // Cap returns v's capacity
759
+ public Cap(): number {
760
+ const k = this.Kind()
761
+ if (k === Slice || k === Array) {
762
+ if (globalThis.Array.isArray(this._value)) {
763
+ return this._value.length
764
+ }
765
+ return 0
766
+ }
767
+ if (k === Chan) {
768
+ return 0 // Simplified
769
+ }
770
+ throw new Error('reflect: call of reflect.Value.Cap on ' + k + ' Value')
771
+ }
772
+
773
+ // NumMethod returns the number of methods in the value's method set
774
+ public NumMethod(): number {
775
+ return 0 // Simplified - methods not fully implemented
776
+ }
777
+
778
+ // Equal reports whether v is equal to u
779
+ public Equal(u: Value): boolean {
780
+ return DeepEqual(
781
+ this._value,
782
+ (u as unknown as { value: ReflectValue }).value,
783
+ )
784
+ }
785
+
786
+ // CanInterface reports whether Interface can be used without panicking
787
+ public CanInterface(): boolean {
788
+ return this.IsValid()
789
+ }
790
+
791
+ // OverflowInt reports whether the int64 x cannot be represented by v's type
792
+ public OverflowInt(x: number): boolean {
793
+ const k = this.Kind()
794
+ switch (k) {
795
+ case Int8:
796
+ return x < -128 || x > 127
797
+ case Int16:
798
+ return x < -32768 || x > 32767
799
+ case Int32:
800
+ return x < -2147483648 || x > 2147483647
801
+ case Int:
802
+ case Int64:
803
+ return x < Number.MIN_SAFE_INTEGER || x > Number.MAX_SAFE_INTEGER
804
+ default:
805
+ throw new Error(
806
+ 'reflect: call of reflect.Value.OverflowInt on ' + k + ' Value',
807
+ )
808
+ }
809
+ }
810
+
811
+ // OverflowUint reports whether the uint64 x cannot be represented by v's type
812
+ public OverflowUint(x: number): boolean {
813
+ const k = this.Kind()
814
+ switch (k) {
815
+ case Uint8:
816
+ return x < 0 || x > 255
817
+ case Uint16:
818
+ return x < 0 || x > 65535
819
+ case Uint32:
820
+ return x < 0 || x > 4294967295
821
+ case Uint:
822
+ case Uint64:
823
+ case Uintptr:
824
+ return x < 0 || x > Number.MAX_SAFE_INTEGER
825
+ default:
826
+ throw new Error(
827
+ 'reflect: call of reflect.Value.OverflowUint on ' + k + ' Value',
828
+ )
829
+ }
830
+ }
831
+
832
+ // OverflowFloat reports whether the float64 x cannot be represented by v's type
833
+ public OverflowFloat(x: number): boolean {
834
+ const k = this.Kind()
835
+ if (k === Float32) {
836
+ const f32max = 3.4028234663852886e38
837
+ return Math.abs(x) > f32max && !isNaN(x) && !!isFinite(x)
838
+ }
839
+ if (k === Float64) {
840
+ return false // float64 can represent any JavaScript number
841
+ }
842
+ throw new Error(
843
+ 'reflect: call of reflect.Value.OverflowFloat on ' + k + ' Value',
844
+ )
845
+ }
429
846
  }
430
847
 
431
848
  // Basic type implementation - exported for compatibility
@@ -448,8 +865,8 @@ export class BasicType implements Type {
448
865
  return this._size
449
866
  }
450
867
 
451
- public Elem(): Type | null {
452
- return null
868
+ public Elem(): Type {
869
+ throw new Error(`reflect: Elem of invalid type ${this._name}`)
453
870
  }
454
871
 
455
872
  public NumField(): number {
@@ -460,13 +877,125 @@ export class BasicType implements Type {
460
877
  return ''
461
878
  }
462
879
 
463
- public Field?(_i: number): StructField | null {
464
- return null
880
+ public Name(): string {
881
+ // Basic types have names like 'int', 'string', etc.
882
+ return this._name
883
+ }
884
+
885
+ public Field(_i: number): StructField {
886
+ throw new Error(`reflect: Field of non-struct type ${this._name}`)
887
+ }
888
+
889
+ public Key(): Type {
890
+ throw new Error(`reflect: Key of non-map type ${this._name}`)
891
+ }
892
+
893
+ public Implements(u: Type | null): boolean {
894
+ if (!u) {
895
+ return false
896
+ }
897
+ if (u.Kind() !== Interface) {
898
+ throw new Error('reflect: non-interface type passed to Type.Implements')
899
+ }
900
+ return false
465
901
  }
466
902
 
467
903
  public common?(): rtype {
468
904
  return new rtype(this._kind)
469
905
  }
906
+
907
+ public OverflowInt(x: number): boolean {
908
+ const k = this._kind
909
+ switch (k) {
910
+ case Int8:
911
+ return x < -128 || x > 127
912
+ case Int16:
913
+ return x < -32768 || x > 32767
914
+ case Int32:
915
+ return x < -2147483648 || x > 2147483647
916
+ case Int:
917
+ case Int64:
918
+ return x < Number.MIN_SAFE_INTEGER || x > Number.MAX_SAFE_INTEGER
919
+ default:
920
+ throw new Error(
921
+ 'reflect: call of reflect.Type.OverflowInt on ' +
922
+ Kind_String(k) +
923
+ ' Type',
924
+ )
925
+ }
926
+ }
927
+
928
+ public OverflowUint(x: number): boolean {
929
+ const k = this._kind
930
+ switch (k) {
931
+ case Uint8:
932
+ return x < 0 || x > 255
933
+ case Uint16:
934
+ return x < 0 || x > 65535
935
+ case Uint32:
936
+ return x < 0 || x > 4294967295
937
+ case Uint:
938
+ case Uint64:
939
+ case Uintptr:
940
+ return x < 0 || x > Number.MAX_SAFE_INTEGER
941
+ default:
942
+ throw new Error(
943
+ 'reflect: call of reflect.Type.OverflowUint on ' +
944
+ Kind_String(k) +
945
+ ' Type',
946
+ )
947
+ }
948
+ }
949
+
950
+ public OverflowFloat(x: number): boolean {
951
+ const k = this._kind
952
+ if (k === Float32) {
953
+ const f32max = 3.4028234663852886e38
954
+ return Math.abs(x) > f32max && !isNaN(x) && !!isFinite(x)
955
+ }
956
+ if (k === Float64) {
957
+ return false
958
+ }
959
+ throw new Error(
960
+ 'reflect: call of reflect.Type.OverflowFloat on ' +
961
+ Kind_String(k) +
962
+ ' Type',
963
+ )
964
+ }
965
+
966
+ public NumMethod(): number {
967
+ return 0
968
+ }
969
+
970
+ public Bits(): number {
971
+ const k = this._kind
972
+ switch (k) {
973
+ case Bool:
974
+ return 1
975
+ case Int8:
976
+ case Uint8:
977
+ return 8
978
+ case Int16:
979
+ case Uint16:
980
+ return 16
981
+ case Int32:
982
+ case Uint32:
983
+ case Float32:
984
+ return 32
985
+ case Int64:
986
+ case Uint64:
987
+ case Float64:
988
+ return 64
989
+ case Int:
990
+ case Uint:
991
+ case Uintptr:
992
+ return 64 // Assuming 64-bit architecture
993
+ default:
994
+ throw new Error(
995
+ 'reflect: call of reflect.Type.Bits on ' + Kind_String(k) + ' Type',
996
+ )
997
+ }
998
+ }
470
999
  }
471
1000
 
472
1001
  // Slice type implementation
@@ -485,7 +1014,7 @@ class SliceType implements Type {
485
1014
  return 24 // slice header size
486
1015
  }
487
1016
 
488
- public Elem(): Type | null {
1017
+ public Elem(): Type {
489
1018
  return this._elemType
490
1019
  }
491
1020
 
@@ -496,6 +1025,49 @@ class SliceType implements Type {
496
1025
  public PkgPath?(): string {
497
1026
  return ''
498
1027
  }
1028
+
1029
+ public Name(): string {
1030
+ // Slice types are unnamed composite types
1031
+ return ''
1032
+ }
1033
+
1034
+ public Field(_i: number): StructField {
1035
+ throw new Error('reflect: Field of non-struct type')
1036
+ }
1037
+
1038
+ public Key(): Type {
1039
+ throw new Error('reflect: Key of non-map type')
1040
+ }
1041
+
1042
+ public Implements(u: Type | null): boolean {
1043
+ if (!u) {
1044
+ return false
1045
+ }
1046
+ if (u.Kind() !== Interface) {
1047
+ throw new Error('reflect: non-interface type passed to Type.Implements')
1048
+ }
1049
+ return false
1050
+ }
1051
+
1052
+ public OverflowInt(_x: number): boolean {
1053
+ throw new Error('reflect: call of reflect.Type.OverflowInt on slice Type')
1054
+ }
1055
+
1056
+ public OverflowUint(_x: number): boolean {
1057
+ throw new Error('reflect: call of reflect.Type.OverflowUint on slice Type')
1058
+ }
1059
+
1060
+ public OverflowFloat(_x: number): boolean {
1061
+ throw new Error('reflect: call of reflect.Type.OverflowFloat on slice Type')
1062
+ }
1063
+
1064
+ public NumMethod(): number {
1065
+ return 0
1066
+ }
1067
+
1068
+ public Bits(): number {
1069
+ throw new Error('reflect: call of reflect.Type.Bits on slice Type')
1070
+ }
499
1071
  }
500
1072
 
501
1073
  // Array type implementation
@@ -517,7 +1089,7 @@ class ArrayType implements Type {
517
1089
  return this._elemType.Size() * this._len
518
1090
  }
519
1091
 
520
- public Elem(): Type | null {
1092
+ public Elem(): Type {
521
1093
  return this._elemType
522
1094
  }
523
1095
 
@@ -533,13 +1105,52 @@ class ArrayType implements Type {
533
1105
  return ''
534
1106
  }
535
1107
 
536
- public Field?(_i: number): StructField | null {
537
- return null
1108
+ public Name(): string {
1109
+ // Array types are unnamed composite types
1110
+ return ''
1111
+ }
1112
+
1113
+ public Field(_i: number): StructField {
1114
+ throw new Error('reflect: Field of non-struct type')
1115
+ }
1116
+
1117
+ public Key(): Type {
1118
+ throw new Error('reflect: Key of non-map type')
1119
+ }
1120
+
1121
+ public Implements(u: Type | null): boolean {
1122
+ if (!u) {
1123
+ return false
1124
+ }
1125
+ if (u.Kind() !== Interface) {
1126
+ throw new Error('reflect: non-interface type passed to Type.Implements')
1127
+ }
1128
+ return false
538
1129
  }
539
1130
 
540
1131
  public common?(): rtype {
541
1132
  return new rtype(this.Kind())
542
1133
  }
1134
+
1135
+ public OverflowInt(_x: number): boolean {
1136
+ throw new Error('reflect: call of reflect.Type.OverflowInt on array Type')
1137
+ }
1138
+
1139
+ public OverflowUint(_x: number): boolean {
1140
+ throw new Error('reflect: call of reflect.Type.OverflowUint on array Type')
1141
+ }
1142
+
1143
+ public OverflowFloat(_x: number): boolean {
1144
+ throw new Error('reflect: call of reflect.Type.OverflowFloat on array Type')
1145
+ }
1146
+
1147
+ public NumMethod(): number {
1148
+ return 0
1149
+ }
1150
+
1151
+ public Bits(): number {
1152
+ throw new Error('reflect: call of reflect.Type.Bits on array Type')
1153
+ }
543
1154
  }
544
1155
 
545
1156
  // Pointer type implementation
@@ -558,7 +1169,7 @@ class PointerType implements Type {
558
1169
  return 8 // pointer size
559
1170
  }
560
1171
 
561
- public Elem(): Type | null {
1172
+ public Elem(): Type {
562
1173
  return this._elemType
563
1174
  }
564
1175
 
@@ -570,13 +1181,58 @@ class PointerType implements Type {
570
1181
  return ''
571
1182
  }
572
1183
 
573
- public Field?(_i: number): StructField | null {
574
- return null
1184
+ public Name(): string {
1185
+ // Pointer types are unnamed composite types
1186
+ return ''
1187
+ }
1188
+
1189
+ public Field(_i: number): StructField {
1190
+ throw new Error('reflect: Field of non-struct type')
1191
+ }
1192
+
1193
+ public Key(): Type {
1194
+ throw new Error('reflect: Key of non-map type')
1195
+ }
1196
+
1197
+ public Implements(u: Type | null): boolean {
1198
+ if (!u) {
1199
+ return false
1200
+ }
1201
+ if (u.Kind() !== Interface) {
1202
+ throw new Error('reflect: non-interface type passed to Type.Implements')
1203
+ }
1204
+ // For pointer types, check if the element type implements the interface
1205
+ const elemTypeName = this._elemType.String()
1206
+ return typeImplementsInterface(elemTypeName, u)
575
1207
  }
576
1208
 
577
1209
  public common?(): rtype {
578
1210
  return new rtype(this.Kind())
579
1211
  }
1212
+
1213
+ public OverflowInt(_x: number): boolean {
1214
+ throw new Error('reflect: call of reflect.Type.OverflowInt on pointer Type')
1215
+ }
1216
+
1217
+ public OverflowUint(_x: number): boolean {
1218
+ throw new Error(
1219
+ 'reflect: call of reflect.Type.OverflowUint on pointer Type',
1220
+ )
1221
+ }
1222
+
1223
+ public OverflowFloat(_x: number): boolean {
1224
+ throw new Error(
1225
+ 'reflect: call of reflect.Type.OverflowFloat on pointer Type',
1226
+ )
1227
+ }
1228
+
1229
+ public NumMethod(): number {
1230
+ return 0
1231
+ }
1232
+
1233
+ public Bits(): number {
1234
+ throw new Error('reflect: call of reflect.Type.Bits on pointer Type')
1235
+ }
580
1236
  }
581
1237
 
582
1238
  // Function type implementation
@@ -595,8 +1251,8 @@ class FunctionType implements Type {
595
1251
  return 8 // function pointer size
596
1252
  }
597
1253
 
598
- public Elem(): Type | null {
599
- return null
1254
+ public Elem(): Type {
1255
+ throw new Error('reflect: Elem of invalid type')
600
1256
  }
601
1257
 
602
1258
  public NumField(): number {
@@ -607,13 +1263,52 @@ class FunctionType implements Type {
607
1263
  return ''
608
1264
  }
609
1265
 
610
- public Field?(_i: number): StructField | null {
611
- return null
1266
+ public Name(): string {
1267
+ // Function types are unnamed composite types
1268
+ return ''
1269
+ }
1270
+
1271
+ public Field(_i: number): StructField {
1272
+ throw new Error('reflect: Field of non-struct type')
1273
+ }
1274
+
1275
+ public Key(): Type {
1276
+ throw new Error('reflect: Key of non-map type')
1277
+ }
1278
+
1279
+ public Implements(u: Type | null): boolean {
1280
+ if (!u) {
1281
+ return false
1282
+ }
1283
+ if (u.Kind() !== Interface) {
1284
+ throw new Error('reflect: non-interface type passed to Type.Implements')
1285
+ }
1286
+ return false
612
1287
  }
613
1288
 
614
1289
  public common?(): rtype {
615
1290
  return new rtype(this.Kind())
616
1291
  }
1292
+
1293
+ public OverflowInt(_x: number): boolean {
1294
+ throw new Error('reflect: call of reflect.Type.OverflowInt on func Type')
1295
+ }
1296
+
1297
+ public OverflowUint(_x: number): boolean {
1298
+ throw new Error('reflect: call of reflect.Type.OverflowUint on func Type')
1299
+ }
1300
+
1301
+ public OverflowFloat(_x: number): boolean {
1302
+ throw new Error('reflect: call of reflect.Type.OverflowFloat on func Type')
1303
+ }
1304
+
1305
+ public NumMethod(): number {
1306
+ return 0
1307
+ }
1308
+
1309
+ public Bits(): number {
1310
+ throw new Error('reflect: call of reflect.Type.Bits on func Type')
1311
+ }
617
1312
  }
618
1313
 
619
1314
  // Map type implementation
@@ -635,7 +1330,7 @@ class MapType implements Type {
635
1330
  return 8 // map header size
636
1331
  }
637
1332
 
638
- public Elem(): Type | null {
1333
+ public Elem(): Type {
639
1334
  return this._elemType
640
1335
  }
641
1336
 
@@ -651,20 +1346,109 @@ class MapType implements Type {
651
1346
  return ''
652
1347
  }
653
1348
 
654
- public Field?(_i: number): StructField | null {
655
- return null
1349
+ public Name(): string {
1350
+ // Map types are unnamed composite types
1351
+ return ''
1352
+ }
1353
+
1354
+ public Field(_i: number): StructField {
1355
+ throw new Error('reflect: Field of non-struct type')
1356
+ }
1357
+
1358
+ public Implements(u: Type | null): boolean {
1359
+ if (!u) {
1360
+ return false
1361
+ }
1362
+ if (u.Kind() !== Interface) {
1363
+ throw new Error('reflect: non-interface type passed to Type.Implements')
1364
+ }
1365
+ return false
656
1366
  }
657
1367
 
658
1368
  public common?(): rtype {
659
1369
  return new rtype(this.Kind())
660
1370
  }
1371
+
1372
+ public OverflowInt(_x: number): boolean {
1373
+ throw new Error('reflect: call of reflect.Type.OverflowInt on map Type')
1374
+ }
1375
+
1376
+ public OverflowUint(_x: number): boolean {
1377
+ throw new Error('reflect: call of reflect.Type.OverflowUint on map Type')
1378
+ }
1379
+
1380
+ public OverflowFloat(_x: number): boolean {
1381
+ throw new Error('reflect: call of reflect.Type.OverflowFloat on map Type')
1382
+ }
1383
+
1384
+ public NumMethod(): number {
1385
+ return 0
1386
+ }
1387
+
1388
+ public Bits(): number {
1389
+ throw new Error('reflect: call of reflect.Type.Bits on map Type')
1390
+ }
661
1391
  }
662
1392
 
663
1393
  // Struct type implementation
1394
+ /**
1395
+ * Helper function to check if a type's method set contains all methods
1396
+ * required by an interface.
1397
+ *
1398
+ * @param typeName The name of the type to check (e.g., "main.MyType")
1399
+ * @param interfaceType The interface type that must be implemented
1400
+ * @returns True if the type implements the interface, false otherwise
1401
+ */
1402
+ function typeImplementsInterface(
1403
+ typeName: string,
1404
+ interfaceType: Type,
1405
+ ): boolean {
1406
+ // Get the interface name and look it up in the type registry
1407
+ const interfaceName = interfaceType.String()
1408
+ const interfaceTypeInfo = builtinGetTypeByName(interfaceName)
1409
+
1410
+ if (!interfaceTypeInfo || !isInterfaceTypeInfo(interfaceTypeInfo)) {
1411
+ return false
1412
+ }
1413
+
1414
+ // Get the type info for the struct/type
1415
+ const typeInfo = builtinGetTypeByName(typeName)
1416
+
1417
+ if (!typeInfo || !isStructTypeInfo(typeInfo)) {
1418
+ return false
1419
+ }
1420
+
1421
+ // Check if the type has all required methods
1422
+ const requiredMethods = interfaceTypeInfo.methods || []
1423
+ const typeMethods = typeInfo.methods || []
1424
+
1425
+ // For each required method, check if the type has a matching method
1426
+ for (const requiredMethod of requiredMethods) {
1427
+ const typeMethod = typeMethods.find((m) => m.name === requiredMethod.name)
1428
+
1429
+ if (!typeMethod) {
1430
+ return false
1431
+ }
1432
+
1433
+ // Check if method signatures match (simplified - just check counts)
1434
+ if (typeMethod.args.length !== requiredMethod.args.length) {
1435
+ return false
1436
+ }
1437
+
1438
+ if (typeMethod.returns.length !== requiredMethod.returns.length) {
1439
+ return false
1440
+ }
1441
+
1442
+ // Could add deeper type checking here, but for now this is sufficient
1443
+ }
1444
+
1445
+ return true
1446
+ }
1447
+
664
1448
  class StructType implements Type {
665
1449
  constructor(
666
1450
  private _name: string,
667
- private _fields: Array<{ name: string; type: Type }> = [],
1451
+ private _fields: Array<{ name: string; type: Type; tag?: string }> = [],
668
1452
  ) {}
669
1453
 
670
1454
  public String(): string {
@@ -680,8 +1464,8 @@ class StructType implements Type {
680
1464
  return this._fields.reduce((sum, field) => sum + field.type.Size(), 0)
681
1465
  }
682
1466
 
683
- public Elem(): Type | null {
684
- return null
1467
+ public Elem(): Type {
1468
+ throw new Error('reflect: Elem of invalid type')
685
1469
  }
686
1470
 
687
1471
  public NumField(): number {
@@ -689,17 +1473,139 @@ class StructType implements Type {
689
1473
  }
690
1474
 
691
1475
  public PkgPath?(): string {
1476
+ // Extract package path from full type name (e.g., "main.Person" -> "main")
1477
+ const dotIndex = this._name.lastIndexOf('.')
1478
+ if (dotIndex > 0) {
1479
+ return this._name.substring(0, dotIndex)
1480
+ }
692
1481
  return ''
693
1482
  }
694
1483
 
695
- public Field?(_i: number): any {
696
- // Stub implementation
697
- return null
1484
+ public Name(): string {
1485
+ // Extract type name from full type name (e.g., "main.Person" -> "Person")
1486
+ const dotIndex = this._name.lastIndexOf('.')
1487
+ if (dotIndex >= 0) {
1488
+ return this._name.substring(dotIndex + 1)
1489
+ }
1490
+ return this._name
1491
+ }
1492
+
1493
+ public Field(i: number): StructField {
1494
+ if (i < 0 || i >= this.NumField()) {
1495
+ throw new Error(`reflect: Field index out of range [${i}] with length ${this.NumField()}`)
1496
+ }
1497
+ const f = this._fields[i]
1498
+ return new StructField({
1499
+ Name: f.name,
1500
+ Type: f.type,
1501
+ Tag: f.tag ? new StructTag(f.tag) : undefined,
1502
+ })
1503
+ }
1504
+
1505
+ public Key(): Type {
1506
+ throw new Error('reflect: Key of non-map type')
1507
+ }
1508
+
1509
+ public Implements(u: Type | null): boolean {
1510
+ if (!u) {
1511
+ return false
1512
+ }
1513
+ if (u.Kind() !== Interface) {
1514
+ throw new Error('reflect: non-interface type passed to Type.Implements')
1515
+ }
1516
+ return typeImplementsInterface(this._name, u)
698
1517
  }
699
1518
 
700
1519
  public common?(): rtype {
701
1520
  return new rtype(this.Kind())
702
1521
  }
1522
+
1523
+ public OverflowInt(_x: number): boolean {
1524
+ throw new Error('reflect: call of reflect.Type.OverflowInt on struct Type')
1525
+ }
1526
+
1527
+ public OverflowUint(_x: number): boolean {
1528
+ throw new Error('reflect: call of reflect.Type.OverflowUint on struct Type')
1529
+ }
1530
+
1531
+ public OverflowFloat(_x: number): boolean {
1532
+ throw new Error(
1533
+ 'reflect: call of reflect.Type.OverflowFloat on struct Type',
1534
+ )
1535
+ }
1536
+
1537
+ public NumMethod(): number {
1538
+ return 0
1539
+ }
1540
+
1541
+ public Bits(): number {
1542
+ throw new Error('reflect: call of reflect.Type.Bits on struct Type')
1543
+ }
1544
+
1545
+ static createTypeFromFieldInfo(ti: any): Type {
1546
+ if (typeof ti === 'string') {
1547
+ switch (ti) {
1548
+ case 'string':
1549
+ return new BasicType(String, ti, 16)
1550
+ case 'int':
1551
+ case 'int32':
1552
+ case 'int64':
1553
+ case 'number':
1554
+ return new BasicType(Int, ti === 'number' ? 'int' : ti, 8)
1555
+ case 'bool':
1556
+ case 'boolean':
1557
+ return new BasicType(Bool, 'bool', 1)
1558
+ case 'float64':
1559
+ return new BasicType(Float64, ti, 8)
1560
+ case 'uint':
1561
+ case 'uint32':
1562
+ case 'uint64':
1563
+ return new BasicType(Uint, ti, 8)
1564
+ default:
1565
+ return new BasicType(Invalid, ti, 8)
1566
+ }
1567
+ } else if (ti && ti.kind) {
1568
+ // Handle TypeInfo objects from the builtin type system
1569
+ const name = ti.name || 'unknown'
1570
+ switch (ti.kind) {
1571
+ case 'basic':
1572
+ // Map TypeScript type names to Go type names
1573
+ switch (name) {
1574
+ case 'string':
1575
+ return new BasicType(String, 'string', 16)
1576
+ case 'number':
1577
+ case 'int':
1578
+ case 'int32':
1579
+ case 'int64':
1580
+ return new BasicType(Int, name === 'number' ? 'int' : name, 8)
1581
+ case 'boolean':
1582
+ case 'bool':
1583
+ return new BasicType(Bool, 'bool', 1)
1584
+ case 'float64':
1585
+ return new BasicType(Float64, 'float64', 8)
1586
+ default:
1587
+ return new BasicType(Invalid, name, 8)
1588
+ }
1589
+ case 'slice':
1590
+ if (ti.elemType) {
1591
+ return new SliceType(StructType.createTypeFromFieldInfo(ti.elemType))
1592
+ }
1593
+ return new SliceType(new BasicType(Invalid, 'unknown', 8))
1594
+ case 'pointer':
1595
+ if (ti.elemType) {
1596
+ return new PointerType(StructType.createTypeFromFieldInfo(ti.elemType))
1597
+ }
1598
+ return new PointerType(new BasicType(Invalid, 'unknown', 8))
1599
+ case 'interface':
1600
+ return new InterfaceType(name)
1601
+ case 'struct':
1602
+ return new StructType(name, [])
1603
+ default:
1604
+ return new BasicType(Invalid, name, 8)
1605
+ }
1606
+ }
1607
+ return new BasicType(Invalid, 'unknown', 8)
1608
+ }
703
1609
  }
704
1610
 
705
1611
  class ChannelType implements Type {
@@ -731,7 +1637,7 @@ class ChannelType implements Type {
731
1637
  return 8
732
1638
  }
733
1639
 
734
- public Elem(): Type | null {
1640
+ public Elem(): Type {
735
1641
  return this._elemType
736
1642
  }
737
1643
 
@@ -743,8 +1649,27 @@ class ChannelType implements Type {
743
1649
  return ''
744
1650
  }
745
1651
 
746
- public Field?(_: number): any {
747
- return null
1652
+ public Name(): string {
1653
+ // Channel types are unnamed composite types
1654
+ return ''
1655
+ }
1656
+
1657
+ public Field(_i: number): StructField {
1658
+ throw new Error('reflect: Field of non-struct type')
1659
+ }
1660
+
1661
+ public Key(): Type {
1662
+ throw new Error('reflect: Key of non-map type')
1663
+ }
1664
+
1665
+ public Implements(u: Type | null): boolean {
1666
+ if (!u) {
1667
+ return false
1668
+ }
1669
+ if (u.Kind() !== Interface) {
1670
+ throw new Error('reflect: non-interface type passed to Type.Implements')
1671
+ }
1672
+ return false
748
1673
  }
749
1674
 
750
1675
  public common?(): rtype {
@@ -754,9 +1679,120 @@ class ChannelType implements Type {
754
1679
  public ChanDir(): ChanDir {
755
1680
  return this._dir
756
1681
  }
1682
+
1683
+ public OverflowInt(_x: number): boolean {
1684
+ throw new Error('reflect: call of reflect.Type.OverflowInt on chan Type')
1685
+ }
1686
+
1687
+ public OverflowUint(_x: number): boolean {
1688
+ throw new Error('reflect: call of reflect.Type.OverflowUint on chan Type')
1689
+ }
1690
+
1691
+ public OverflowFloat(_x: number): boolean {
1692
+ throw new Error('reflect: call of reflect.Type.OverflowFloat on chan Type')
1693
+ }
1694
+
1695
+ public NumMethod(): number {
1696
+ return 0
1697
+ }
1698
+
1699
+ public Bits(): number {
1700
+ throw new Error('reflect: call of reflect.Type.Bits on chan Type')
1701
+ }
1702
+ }
1703
+
1704
+ // Interface type implementation
1705
+ class InterfaceType implements Type {
1706
+ constructor(private _name: string = 'interface{}') {}
1707
+
1708
+ public String(): string {
1709
+ return this._name
1710
+ }
1711
+
1712
+ public Kind(): Kind {
1713
+ return Interface
1714
+ }
1715
+
1716
+ public Size(): number {
1717
+ return 16
1718
+ }
1719
+
1720
+ public Elem(): Type {
1721
+ throw new Error('reflect: Elem of invalid type')
1722
+ }
1723
+
1724
+ public NumField(): number {
1725
+ return 0
1726
+ }
1727
+
1728
+ public PkgPath?(): string {
1729
+ return ''
1730
+ }
1731
+
1732
+ public Name(): string {
1733
+ return this._name
1734
+ }
1735
+
1736
+ public Field(_i: number): StructField {
1737
+ throw new Error('reflect: Field of non-struct type')
1738
+ }
1739
+
1740
+ public Key(): Type {
1741
+ throw new Error('reflect: Key of non-map type')
1742
+ }
1743
+
1744
+ public Implements(_u: Type | null): boolean {
1745
+ return false
1746
+ }
1747
+
1748
+ public common?(): rtype {
1749
+ return new rtype(this.Kind())
1750
+ }
1751
+
1752
+ public OverflowInt(_x: number): boolean {
1753
+ throw new Error(
1754
+ 'reflect: call of reflect.Type.OverflowInt on interface Type',
1755
+ )
1756
+ }
1757
+
1758
+ public OverflowUint(_x: number): boolean {
1759
+ throw new Error(
1760
+ 'reflect: call of reflect.Type.OverflowUint on interface Type',
1761
+ )
1762
+ }
1763
+
1764
+ public OverflowFloat(_x: number): boolean {
1765
+ throw new Error(
1766
+ 'reflect: call of reflect.Type.OverflowFloat on interface Type',
1767
+ )
1768
+ }
1769
+
1770
+ public NumMethod(): number {
1771
+ return 0
1772
+ }
1773
+
1774
+ public Bits(): number {
1775
+ throw new Error('reflect: call of reflect.Type.Bits on interface Type')
1776
+ }
757
1777
  }
758
1778
 
759
1779
  function getTypeOf(value: ReflectValue): Type {
1780
+ // Check for typed nil before checking for plain null
1781
+ // Typed nils are created by $.typedNil() and have __goType and __isTypedNil properties
1782
+ if (value && typeof value === 'object' && (value as any).__isTypedNil) {
1783
+ const typeName = (value as any).__goType
1784
+ if (typeName && typeof typeName === 'string') {
1785
+ // Parse the type name to construct the appropriate Type
1786
+ // For pointer types like "*main.Stringer", extract the element type
1787
+ if (typeName.startsWith('*')) {
1788
+ const elemTypeName = typeName.slice(1) // Remove the '*' prefix
1789
+ // Create an InterfaceType for the element (works for interfaces and other types)
1790
+ const elemType = new InterfaceType(elemTypeName)
1791
+ return new PointerType(elemType)
1792
+ }
1793
+ }
1794
+ }
1795
+
760
1796
  if (value === null || value === undefined) {
761
1797
  return new BasicType(Interface, 'interface{}', 16)
762
1798
  }
@@ -937,23 +1973,43 @@ function getTypeOf(value: ReflectValue): Type {
937
1973
  value.constructor as { __typeInfo?: { name?: string } }
938
1974
  ).__typeInfo
939
1975
  if (typeInfo && typeInfo.name) {
940
- // Add package prefix for struct types if not already present
941
1976
  const typeName =
942
1977
  typeInfo.name.includes('.') ?
943
1978
  typeInfo.name
944
1979
  : `main.${typeInfo.name}`
945
- return new StructType(typeName)
1980
+ const regTypeInfo = builtinGetTypeByName(typeName)
1981
+ let fields: Array<{ name: string; type: Type; tag?: string }> = []
1982
+ if (regTypeInfo && isStructTypeInfo(regTypeInfo)) {
1983
+ fields = Object.entries(regTypeInfo.fields || {}).map(
1984
+ ([name, fieldInfo]) => {
1985
+ // Check if fieldInfo is a StructFieldInfo with type and tag
1986
+ if (isStructFieldInfo(fieldInfo)) {
1987
+ return {
1988
+ name,
1989
+ type: StructType.createTypeFromFieldInfo(fieldInfo.type),
1990
+ tag: fieldInfo.tag,
1991
+ }
1992
+ }
1993
+ // Otherwise it's just the type info directly (backwards compatible)
1994
+ return {
1995
+ name,
1996
+ type: StructType.createTypeFromFieldInfo(fieldInfo),
1997
+ }
1998
+ },
1999
+ )
2000
+ }
2001
+ return new StructType(typeName, fields)
946
2002
  }
947
2003
  }
948
2004
 
949
2005
  // Check if it has a constructor name we can use (fallback)
950
2006
  const constructorName = (value as object).constructor?.name
951
2007
  if (constructorName && constructorName !== 'Object') {
952
- return new StructType(constructorName)
2008
+ return new StructType(constructorName, [])
953
2009
  }
954
2010
 
955
2011
  // Default to struct type for plain objects
956
- return new StructType('struct')
2012
+ return new StructType('struct', [])
957
2013
  }
958
2014
  default:
959
2015
  return new BasicType(Interface, 'interface{}', 16)
@@ -977,11 +2033,12 @@ export function SliceOf(t: Type): Type {
977
2033
  return new SliceType(t)
978
2034
  }
979
2035
 
980
- export function PointerTo(t: Type): Type {
2036
+ export function PointerTo(t: Type | null): Type | null {
2037
+ if (t === null) return null
981
2038
  return new PointerType(t)
982
2039
  }
983
2040
 
984
- export function PtrTo(t: Type): Type {
2041
+ export function PtrTo(t: Type | null): Type | null {
985
2042
  return PointerTo(t) // PtrTo is an alias for PointerTo
986
2043
  }
987
2044
 
@@ -993,6 +2050,45 @@ export function ChanOf(dir: ChanDir, t: Type): Type {
993
2050
  return new ChannelType(t, dir)
994
2051
  }
995
2052
 
2053
+ export function TypeFor(): Type {
2054
+ return new InterfaceType('interface{}')
2055
+ }
2056
+
2057
+ /**
2058
+ * getInterfaceTypeByName looks up a registered interface type by name
2059
+ * and returns a Type for it. Returns an interface{} type if not found.
2060
+ */
2061
+ export function getInterfaceTypeByName(name: string): Type {
2062
+ const typeInfo = builtinGetTypeByName(name)
2063
+ if (typeInfo && typeInfo.kind === TypeKind.Interface) {
2064
+ // InterfaceTypeInfo
2065
+ const methods = (typeInfo as any).methods || []
2066
+ if (methods.length > 0) {
2067
+ // Build interface signature with methods
2068
+ const methodSigs = methods
2069
+ .map((m: any) => {
2070
+ const args =
2071
+ m.args
2072
+ ?.map((a: any) => (typeof a === 'string' ? a : 'any'))
2073
+ .join(', ') || ''
2074
+ const returns = m.returns?.map((r: any) =>
2075
+ typeof r === 'string' ? r : 'any',
2076
+ )
2077
+ let returnSig = ''
2078
+ if (returns && returns.length === 1) {
2079
+ returnSig = ` ${returns[0]}`
2080
+ } else if (returns && returns.length > 1) {
2081
+ returnSig = ` (${returns.join(', ')})`
2082
+ }
2083
+ return `${m.name}(${args})${returnSig}`
2084
+ })
2085
+ .join('; ')
2086
+ return new InterfaceType(`interface { ${methodSigs} }`)
2087
+ }
2088
+ }
2089
+ return new InterfaceType('interface{}')
2090
+ }
2091
+
996
2092
  // Additional functions from merged files
997
2093
  export function canRangeFunc(t: Type): boolean {
998
2094
  const kind = t.Kind()