bytecodealliance / bytecodealliance/wasmtime

Add `Func::wrap_with_type` API to specify exact function type, rather than inferring it

Open
#9,848 10 comments 0 reactions 1 assignee Claimed by @fitzgen View on GitHub
bug wasm-proposal:gc wasmtime:ref-types
Dominant language
Rust
Stars
18.6k
Forks
1.8k
Avg merge
1d 18h
Merged PRs (30d)
126

Description

**Edit by @fitzgen:** The OP described behavior is subtle but expected. To allow `Func::wrap`-style APIs to be used with specific function type subtypes, we should add a `Func::wrap_with_type` constructor. See [this comment](https://github.com/bytecodealliance/wasmtime/issues/9848#issuecomment-2613572142) for some more details.

-------------------------

original issue report

I'm getting this error:
```
types incompatible: expected type `(func (param (ref array (engine 5))))`, found type `(func (param (ref array)))`
```
The wasm looks like this:
```
(type $String (;3;) (array i8))
(type $debug.type (;4;) (func (param (ref $String))))
(import "host" "debug" (func $debug (;0;) (type $debug.type)))
```
And I'm registering the function like this:
```rust
let engine = wasmtime::Engine::new(&config).unwrap();
let store = wasmtime::Store::::new(&engine, ());
let mut linker = wasmtime::Linker::new(&engine);
linker
.func_wrap(
"host",
"debug",
|caller: Caller<'_, StoreData>, param: Rooted| {
// ...
},
)
.unwrap();
```

### Test Case

```
(module $hello.saga
(type $hello.type (;0;) (func (result i32)))
(type $add.type (;1;) (func (param i32) (result i32)))
(type $name.type (;2;) (func))
(type $String (;3;) (array (mut i8)))
(type $debug.type (;4;) (func (param (ref $String))))
(import "host" "debug" (func $debug (;0;) (type $debug.type)))
(export "hello" (func $hello))
(func $hello (;1;) (type $hello.type) (result i32)
i32.const 1
call $add
i32.const 2
i32.add
f32.const 0x1.8p+1 (;=3;)
i32.trunc_f32_s
i32.add
return
)
(func $add (;2;) (type $add.type) (param i32) (result i32)
(local i32)
i32.const 1
local.set 1
local.get 0
local.get 1
i32.add
return
)
(func $name (;3;) (type $name.type)
i32.const 0
i32.const 15
array.new_data $String 0
call $debug
)
(data (;0;) "Hello from SAGA")
)
```

### Steps to Reproduce

Create an Engine and Linker.
Bind a function which accepts an ArrayRef as a parameter.
Attempt to load the script.

### Expected Results

Script should load without error (it loads if I delete the code that calls the host func).

### Actual Results

Error when I attempt to instantiate the script.

### Versions and Environment

Wasmtime version or commit: `{ version = "27.0.0", features = ["gc", "gc-drc"] }`

Operating system: OS X

Architecture: M2

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.