tinygo
tinygo copied to clipboard
Support fuction reflect for wasm
Hi there,
We are trying to implement function reflect for wasm target. It seems wasm has some limitations stopping us to do so. Do you guys have any thoughts on this topic? Or is this an impossible mission?
BR, Terry
By fucntion reflect, I mean code like below:
package main
import "reflect"
func foo(x int) int {
return x
}
func bar(f reflect.Value) {
in := []reflect.Value{reflect.ValueOf(12345)}
out := f.Call(in)
println(out[0].Int())
}
func main() {
f := reflect.ValueOf(foo)
bar(f)
}
We do not currently support using reflection for function calls. This should eventually be supported, but this is one of the more complex pieces of functionality in reflect and nobody has quite gotten around to it. It is possible that this could be implemented by having the compiler generate wrappers.
Thanks Jaden. We will give your suggestion a try.
I got the following errors while compiling. Does it relate to this issue?
# encoding/asn1
../../../../usr/local/Cellar/[email protected]/1.12.9/libexec/src/encoding/asn1/marshal.go:537:47: v.Type().NumMethod undefined (type reflect.Type has no field or method NumMethod)
../../../../usr/local/Cellar/[email protected]/1.12.9/libexec/src/encoding/asn1/marshal.go:549:14: DeepEqual not declared by package reflect
../../../../usr/local/Cellar/[email protected]/1.12.9/libexec/src/encoding/asn1/marshal.go:558:14: DeepEqual not declared by package reflect
../../../../usr/local/Cellar/[email protected]/1.12.9/libexec/src/encoding/asn1/marshal.go:479:93: t.Field(startingField).Tag.Get undefined (type string has no field or method Get)
../../../../usr/local/Cellar/[email protected]/1.12.9/libexec/src/encoding/asn1/marshal.go:483:103: t.Field(i + startingField).Tag.Get undefined (type string has no field or method Get)
../../../../usr/local/Cellar/[email protected]/1.12.9/libexec/src/encoding/asn1/common.go:174:26: t.Name undefined (type reflect.Type has no field or method Name)
../../../../usr/local/Cellar/[email protected]/1.12.9/libexec/src/encoding/asn1/asn1.go:658:80: ifaceType.NumMethod undefined (type reflect.Type has no field or method NumMethod)
../../../../usr/local/Cellar/[email protected]/1.12.9/libexec/src/encoding/asn1/asn1.go:810:115: fieldType.Name undefined (type reflect.Type has no field or method Name)
../../../../usr/local/Cellar/[email protected]/1.12.9/libexec/src/encoding/asn1/asn1.go:831:12: Copy not declared by package reflect
../../../../usr/local/Cellar/[email protected]/1.12.9/libexec/src/encoding/asn1/asn1.go:919:104: field.Tag.Get undefined (type string has no field or method Get)
../../../../usr/local/Cellar/[email protected]/1.12.9/libexec/src/encoding/asn1/asn1.go:932:12: Copy not declared by package reflect
No that is a seperate issue - reflect is incomplete and anything relying on complex functionality is unlikely to work yet
I got the following errors while compiling. Does it relate to this issue?
# encoding/asn1 ../../../../usr/local/Cellar/[email protected]/1.12.9/libexec/src/encoding/asn1/marshal.go:537:47: v.Type().NumMethod undefined (type reflect.Type has no field or method NumMethod) ../../../../usr/local/Cellar/[email protected]/1.12.9/libexec/src/encoding/asn1/marshal.go:549:14: DeepEqual not declared by package reflect ../../../../usr/local/Cellar/[email protected]/1.12.9/libexec/src/encoding/asn1/marshal.go:558:14: DeepEqual not declared by package reflect ../../../../usr/local/Cellar/[email protected]/1.12.9/libexec/src/encoding/asn1/marshal.go:479:93: t.Field(startingField).Tag.Get undefined (type string has no field or method Get) ../../../../usr/local/Cellar/[email protected]/1.12.9/libexec/src/encoding/asn1/marshal.go:483:103: t.Field(i + startingField).Tag.Get undefined (type string has no field or method Get) ../../../../usr/local/Cellar/[email protected]/1.12.9/libexec/src/encoding/asn1/common.go:174:26: t.Name undefined (type reflect.Type has no field or method Name) ../../../../usr/local/Cellar/[email protected]/1.12.9/libexec/src/encoding/asn1/asn1.go:658:80: ifaceType.NumMethod undefined (type reflect.Type has no field or method NumMethod) ../../../../usr/local/Cellar/[email protected]/1.12.9/libexec/src/encoding/asn1/asn1.go:810:115: fieldType.Name undefined (type reflect.Type has no field or method Name) ../../../../usr/local/Cellar/[email protected]/1.12.9/libexec/src/encoding/asn1/asn1.go:831:12: Copy not declared by package reflect ../../../../usr/local/Cellar/[email protected]/1.12.9/libexec/src/encoding/asn1/asn1.go:919:104: field.Tag.Get undefined (type string has no field or method Get) ../../../../usr/local/Cellar/[email protected]/1.12.9/libexec/src/encoding/asn1/asn1.go:932:12: Copy not declared by package reflect
I met the same problem hope to fixed the problem faster
@northka I've started work on that in https://github.com/tinygo-org/tinygo/pull/907.
Since the issue from #907 was solved differently can this issue now be closed?
WASI (and tinygo in general) now supports reflection.
@dgryski I don't think this is actually fixed: the bug is about .Call() which is not yet supported.
Oh good point. I was responding to the second issue about encoding/asn1.
This is part of the v0.28 release so now closing this issue. Thanks!
It's not actually fixed yet in 0.28, function calls in reflect are not yet supported.
Any updated plans to support reflect.FuncOf? This is something we really need.
Is there any movement on this? This is something I'd really love to use.