Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for reflect package in TinyGo #2660

Closed
srikarrao opened this issue Mar 2, 2022 · 9 comments
Closed

Support for reflect package in TinyGo #2660

srikarrao opened this issue Mar 2, 2022 · 9 comments
Labels
duplicate This issue or pull request already exists enhancement New feature or request

Comments

@srikarrao
Copy link

Is the reflect package implemented in the latest version of Tinygo? I'm trying to decode the JSON in Go WASM and I have been getting the below error that reflect.Type.NumMethod() is unimplemented. I found a couple of issues reported earlier, I'm not sure if this is implemented. However, the TinyGo's supported libs document says it does allow importing encoding/json package.

Previous Issues:
#907
#575

Versions used:

> go version 
	go1.17.6 darwin/amd64
> tinygo version
	tinygo version 0.22.0 darwin/amd64 (using go version go1.17.6 and LLVM version 13.0.0)
> proxy-wasm-go-sdk version
	github.com/tetratelabs/proxy-wasm-go-sdk v0.16.0

Error logs:

proxy_1        | [2022-03-02 02:08:03.668][1][info][wasm] [source/extensions/common/wasm/context.cc:1170] wasm log: panic: unimplemented: (reflect.Type).NumMethod()
proxy_1        | [2022-03-02 02:08:03.668][1][error][wasm] [source/extensions/common/wasm/wasm_vm.cc:38] Function: proxy_on_tick failed: Uncaught RuntimeError: unreachable
proxy_1        | Proxy-Wasm plugin in-VM backtrace:
proxy_1        |   0:  0x170f - runtime._panic
proxy_1        |   1:  0x326d - (reflect.rawType).NumMethod
proxy_1        |   2:  0x6c23a - (*gopkg.in/square/go-jose.v2/json.decodeState).indirect
proxy_1        |   3:  0x696e5 - (*gopkg.in/square/go-jose.v2/json.decodeState).object
proxy_1        |   4:  0x687dc - (*gopkg.in/square/go-jose.v2/json.decodeState).value
proxy_1        |   5:  0x2af8b - gopkg.in/square/go-jose.v2/json.Unmarshal
proxy_1        |   6:  0x65be4 - (*gopkg.in/square/go-jose.v2.JSONWebKey).UnmarshalJSON
proxy_1        |   7:  0x64cef - proxy_on_tick

@dkegel-fastly
Copy link
Contributor

Only a subset of reflect is implemented. The supported package page is overdue to have a "passes tests" column, see
tinygo-org/tinygo-site#110

Work is ongoing to improve reflect, see #2640.

@joeblew99
Copy link

joeblew99 commented Apr 6, 2022

Hey @srikarrao

I know that https://github.com/buger/jsonparser is used with tinygo where you need to do JSON serialisation.

You can find the entry point here at https://github.com/genjidb/genji.js/blob/master/scripts/buildgo.sh#L27, which compiles the golang code using tinygo to wasm

@deadprogram deadprogram added duplicate This issue or pull request already exists enhancement New feature or request labels Apr 23, 2022
@johnkeates
Copy link

johnkeates commented May 17, 2022

I ran into the same issue with golang-jwt in a WASI target, it tries to do a (reflect.rawType).NumMethod during JSON decoding and that panics because it's simply not there (yet).

Edit: I tried to see if this is something I can implement but it seems much more complicated than expected, especially when referring to to 'full' go reflect package and all the additional functions that it needs to get this method in rtype.

@dgryski
Copy link
Member

dgryski commented Mar 23, 2023

encoding/json "should" be working once my last few reflect fixes are in. Please let me know if you're still encountering issues. I will try to test with golang-jwt.

@dgryski dgryski added the next-release Will be part of next release label Mar 23, 2023
@dgryski
Copy link
Member

dgryski commented Mar 23, 2023

The next release of tinygo will support validating jwt: (I had to disable the http listener example)

~/go/src/github.com/golang-jwt/jwt $ tinygo test -tags="purego noasm" -target=wasi
ok  	github.com/golang-jwt/jwt/v5	1.766s

@ricardbejarano
Copy link

Thank you so much! When can we expect that release to happen?

@deadprogram
Copy link
Member

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

@deadprogram deadprogram removed the next-release Will be part of next release label Jun 14, 2023
@HarikrishnanBalagopal
Copy link

HarikrishnanBalagopal commented Oct 6, 2023

Is it possible to implement this?

panic("unimplemented: (reflect.Type).NumOut()")

Related to cobra library support #2704 (comment)

Using Go version 1.21.0 I am able to both compile and run the program that uses cobra CLI library.

$ go version
go version go1.21.0 darwin/arm64
$ GOOS=wasip1 GOARCH=wasm go build -o ./bin/foo.wasm
$ wasmer ./bin/foo.wasm
...........

With TinyGo I am able to compile but at runtime I get a panic

$ tinygo version
tinygo version 0.30.0 darwin/amd64 (using go version go1.21.0 and LLVM version 16.0.1)
$ CGO_ENABLED=0 tinygo build -o ./bin/foo.wasm -target=wasi .
$ wasmer ./bin/foo.wasm
panic: unimplemented: (reflect.Type).NumOut()
error: RuntimeError: unreachable
╰─▶ 1: RuntimeError: unreachable
make: *** [run] Error 1

@dkegel-fastly
Copy link
Contributor

no necromancy, please :-). Filed #3943 for the cobra problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
duplicate This issue or pull request already exists enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

8 participants