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

tinygo wasm externref support #2702

Open
wuhuizuo opened this issue Mar 11, 2022 · 3 comments
Open

tinygo wasm externref support #2702

wuhuizuo opened this issue Mar 11, 2022 · 3 comments
Labels
wasm WebAssembly

Comments

@wuhuizuo
Copy link

Because reflect support issue, such as #2660
I try to import host func into wasm module,my steps:

define go import function:

//go:wasm-module json
//export ExternrefTest
func Marshal(data interface{}) ([]byte, error) {
   return []byte{'1'}, nil
}

build with wasi target, but got import func type: func (param i32 i32 i32)

I import the func with host's golang encoding/json.Marshal:

// ...
linker := wasmtime.NewLinker(store.Engine)
linker.DefineFunc(store, "json", "Marshal", json.Marshal)
// ...

but i got incompatible import error:

panic: incompatible import type for `json`

Caused by:
    0: instance export "Marshal" incompatible
    1: function types incompatible: expected func of type `(i32, i32, i32) -> ()`, found func of type `(externref) -> (externref, externref)` [recovered]
	panic: incompatible import type for `json`

Are there any milestones to support compiled to externref?

@codefromthecrypt
Copy link
Contributor

I think main issue will be how to represent what should be an externref param unambiguously. Also, per spec they are supposed to be completely opaque from the perspective of the guest. So, if represented by any, it should be impossible to cast it to a real type.

@aykevl
Copy link
Member

aykevl commented Sep 15, 2022

Right now we don't support externref. Also I don't see how this is going to help you: TinyGo interface values are different from Go interface values. They cannot be interchangeable. If they were, implementing the missing reflect functions would be trivial.

I don't see anything to do here right now.

@codefromthecrypt
Copy link
Contributor

@aykevl externref is a way to opaquely pass a value the host has in a way that the wasm compiled by tinygo never uses. It is intentional that it isn't interchangeable in other words.

rust doesn't yet support externref either, hence a package like this: https://github.com/slowli/externref

The main thing you get with externref is for the host to be able to stash scoped objects either as globals or stack parameters. The example above is a host-defined sender. I've seen other things like host-defined memory as examples (sometimes string literals they don't want to actually wash through wasm).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
wasm WebAssembly
Projects
None yet
Development

No branches or pull requests

4 participants