tinygo-org / tinygo

Go compiler for small places. Microcontrollers, WebAssembly (WASM/WASI), and command-line tools. Based on LLVM.
https://tinygo.org
Other
14.75k stars 862 forks source link

reflect: setting an interface value #4040

Closed radkomih closed 4 months ago

radkomih commented 7 months ago

I have encountered an issue and was able to create a small example to reproduce it. When running the code with standard Go, it prints out the value '13', however, with TinyGo, it prints out '35008' instead.

Env: tinygo version 0.30.0 darwin/amd64 (using go version go1.21.0 and LLVM version 16.0.1

tinygo run -target=wasi main.go

package main

import (
    "reflect"
)

func main() {
    var value interface{} = uint16(0)

    // get the pointer to the interface value
    inPtr := reflect.ValueOf(&value)

    // dereference to get the actual value (an interface)
    inElem := inPtr.Elem()

    // create a new value of the same concrete type
    uint16Type := reflect.TypeOf(uint16(0))
    newUint16Value := reflect.New(uint16Type).Elem()
    newUint16Value.Set(reflect.ValueOf(uint16(13)))

    // set the new value to the interface
    inElem.Set(newUint16Value)

    println(value.(uint16))
}
dgryski commented 7 months ago

Thanks for the bug report. I'll see what I can do.

dgryski commented 7 months ago

Can you please try https://github.com/tinygo-org/tinygo/pull/4041 and see if that fixes your problem? (It solves your test case, but I want to make sure it works on your real code base.)

radkomih commented 5 months ago

@dgryski it works for my case thank you ❤️