tinygo icon indicating copy to clipboard operation
tinygo copied to clipboard

Support fuction reflect for wasm

Open TerryGuo opened this issue 6 years ago • 14 comments
trafficstars

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)
}

TerryGuo avatar Sep 20 '19 12:09 TerryGuo

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.

niaow avatar Sep 20 '19 12:09 niaow

Thanks Jaden. We will give your suggestion a try.

TerryGuo avatar Sep 21 '19 01:09 TerryGuo

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

kwunyeung avatar Sep 24 '19 03:09 kwunyeung

No that is a seperate issue - reflect is incomplete and anything relying on complex functionality is unlikely to work yet

niaow avatar Sep 24 '19 03:09 niaow

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 avatar Mar 11 '20 04:03 northka

@northka I've started work on that in https://github.com/tinygo-org/tinygo/pull/907.

aykevl avatar Mar 11 '20 14:03 aykevl

Since the issue from #907 was solved differently can this issue now be closed?

deadprogram avatar Apr 22 '21 09:04 deadprogram

WASI (and tinygo in general) now supports reflection.

dgryski avatar Mar 23 '23 05:03 dgryski

@dgryski I don't think this is actually fixed: the bug is about .Call() which is not yet supported.

aykevl avatar May 03 '23 21:05 aykevl

Oh good point. I was responding to the second issue about encoding/asn1.

dgryski avatar May 03 '23 22:05 dgryski

This is part of the v0.28 release so now closing this issue. Thanks!

deadprogram avatar Jun 14 '23 09:06 deadprogram

It's not actually fixed yet in 0.28, function calls in reflect are not yet supported.

aykevl avatar Jun 17 '23 12:06 aykevl

Any updated plans to support reflect.FuncOf? This is something we really need.

Sexual avatar Oct 20 '23 15:10 Sexual

Is there any movement on this? This is something I'd really love to use.

T-J-A avatar Jul 16 '24 00:07 T-J-A