Skip to content

Commit 63d5814

Browse files
committed
fix: handle string(byte) conversion
Related: #87 Signed-off-by: Christian Stewart <[email protected]>
1 parent 3bd4203 commit 63d5814

File tree

4 files changed

+70
-1
lines changed

4 files changed

+70
-1
lines changed

compiler/expr-call-type-conversion.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,16 @@ func (c *GoToTSCompiler) writeStringConversion(exp *ast.CallExpr) error {
160160
return nil
161161
}
162162

163+
if basic, isBasic := tv.Type.Underlying().(*types.Basic); isBasic && basic.Kind() == types.Uint8 {
164+
// Translate string(byte_val) to $.runeOrStringToString(byte_val)
165+
c.tsw.WriteLiterally("$.runeOrStringToString(")
166+
if err := c.WriteValueExpr(arg); err != nil {
167+
return fmt.Errorf("failed to write argument for string(byte) conversion: %w", err)
168+
}
169+
c.tsw.WriteLiterally(")")
170+
return nil
171+
}
172+
163173
// Case 3: Argument is a slice of runes or bytes string([]rune{...}) or string([]byte{...})
164174
if c.isByteSliceType(tv.Type) {
165175
c.tsw.WriteLiterally("$.bytesToString(")

compliance/tests/string_conversion/expected.log

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,9 @@ GoScript
77
你好世界
88
你好世界
99
true
10-
mutable string
10+
mutable string
11+
Hello
12+
B
13+
interface test
14+
variable test
15+
A

compliance/tests/string_conversion/string_conversion.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,31 @@ func main() {
4141
mutableRunes[8] = 's'
4242
modifiedString := string(mutableRunes)
4343
println(modifiedString)
44+
45+
// === Test cases that might trigger "unhandled string conversion" ===
46+
47+
// string([]byte) conversion
48+
bytes := []byte{72, 101, 108, 108, 111}
49+
bytesString := string(bytes)
50+
println(bytesString)
51+
52+
// string(int32) conversion
53+
i32 := int32(66)
54+
i32String := string(i32)
55+
println(i32String)
56+
57+
// Test with interface{} type assertion
58+
var v interface{} = "interface test"
59+
interfaceString := string(v.(string))
60+
println(interfaceString)
61+
62+
// Test with type conversion through variable
63+
var myString string = "variable test"
64+
convertedString := string(myString)
65+
println(convertedString)
66+
67+
// === Test string(byte) conversion ===
68+
var b byte = 65
69+
byteString := string(b)
70+
println(byteString)
4471
}

compliance/tests/string_conversion/string_conversion.gs.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,5 +46,32 @@ export async function main(): Promise<void> {
4646
mutableRunes![8] = 115
4747
let modifiedString = $.runesToString(mutableRunes)
4848
console.log(modifiedString)
49+
50+
// === Test cases that might trigger "unhandled string conversion" ===
51+
52+
// string([]byte) conversion
53+
let bytes = new Uint8Array([72, 101, 108, 108, 111])
54+
let bytesString = $.bytesToString(bytes)
55+
console.log(bytesString)
56+
57+
// string(int32) conversion
58+
let i32 = (66 as number)
59+
let i32String = $.runeOrStringToString(i32)
60+
console.log(i32String)
61+
62+
// Test with interface{} type assertion
63+
let v: null | any = "interface test"
64+
let interfaceString = $.mustTypeAssert<string>(v, {kind: $.TypeKind.Basic, name: 'string'})
65+
console.log(interfaceString)
66+
67+
// Test with type conversion through variable
68+
let myString: string = "variable test"
69+
let convertedString = myString
70+
console.log(convertedString)
71+
72+
// === Test string(byte) conversion ===
73+
let b: number = 65
74+
let byteString = $.runeOrStringToString(b)
75+
console.log(byteString)
4976
}
5077

0 commit comments

Comments
 (0)