Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion gs/builtin/builtin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -894,7 +894,8 @@ export function genericConvert<T>(
case 'rune':
return int(value, 32) as T
case 'int':
return int(value) as T
// int is 64 bits, so a uint64 beyond its range wraps as in Go.
return int(value, 64) as T
case 'uint8':
case 'byte':
return uint(value, 8) as T
Expand Down
2 changes: 2 additions & 0 deletions tests/tests/generic_typeparam_conversion/expected.log
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@
10 wide
16
7 44
-7 -7 -7
-1
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ func toWide[T, E EnumConstraint](e E) T {
return T(e)
}

// fromUint64 converts a uint64 to a type parameter, which wraps a value
// beyond the range of the type.
func fromUint64[T Integer](u uint64) T {
return T(u)
}

// fromConst converts a constant and a basic value to a type parameter.
func fromConst[T Integer](i int) (T, T) {
return T(7), T(i)
Expand All @@ -64,4 +70,7 @@ func main() {
println(int64(a) + int64(b))
c, d := fromConst[uint8](300)
println(c, d)
u := uint64(1<<64 - 7)
println(fromUint64[int](u), fromUint64[int64](u), fromUint64[int32](u))
println(fromUint64[int](u) % 3)
}
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ export function toWide(__typeArgs: $.GenericTypeArgs | undefined, e: any): any {
return $.genericConvert<any>(__typeArgs, "T", e)
}

export function fromUint64(__typeArgs: $.GenericTypeArgs | undefined, u: bigint): any {
return $.genericConvert<any>(__typeArgs, "T", u)
}

export function fromConst(__typeArgs: $.GenericTypeArgs | undefined, i: number): [any, any] {
return [$.genericConvert<any>(__typeArgs, "T", 7), $.genericConvert<any>(__typeArgs, "T", i)]
}
Expand All @@ -135,6 +139,9 @@ export async function main(): globalThis.Promise<void> {
let c: number = (__goscriptTuple1[0] as number)
let d: number = (__goscriptTuple1[1] as number)
await $.println(c, d)
let u = 18446744073709551609n
await $.println(fromUint64(__goscriptStatic4(), u), fromUint64(__goscriptStatic5(), u), fromUint64(__goscriptStatic6(), u))
await $.println(fromUint64(__goscriptStatic4(), u) % 3)
}

var __goscriptStatic0$value: any
Expand All @@ -157,6 +164,21 @@ function __goscriptStatic3(): any {
return __goscriptStatic3$value ??= ({[$.genericTypeArgsMarker]: $.genericTypeArgsBrand, T: { type: /* @__PURE__ */ $.basicType("uint8"), zero: () => 0 }})
}

var __goscriptStatic4$value: any
function __goscriptStatic4(): any {
return __goscriptStatic4$value ??= ({[$.genericTypeArgsMarker]: $.genericTypeArgsBrand, T: { type: /* @__PURE__ */ $.basicType("int"), zero: () => 0 }})
}

var __goscriptStatic5$value: any
function __goscriptStatic5(): any {
return __goscriptStatic5$value ??= ({[$.genericTypeArgsMarker]: $.genericTypeArgsBrand, T: { type: /* @__PURE__ */ $.basicType("int64"), zero: () => 0n }})
}

var __goscriptStatic6$value: any
function __goscriptStatic6(): any {
return __goscriptStatic6$value ??= ({[$.genericTypeArgsMarker]: $.genericTypeArgsBrand, T: { type: /* @__PURE__ */ $.basicType("int32"), zero: () => 0 }})
}

if ($.isMainScript(import.meta)) {
await main()
}
Loading