Skip to content

Commit cca558c

Browse files
authored
Remove HostRef<T> from the C API (bytecodealliance#1926)
This commit removes `HostRef<T>` from the C API which only served the purpose now of converting each type to a `wasm_ref_t*`. Our implementation, however, does not guarantee that you'll get the same `wasm_ref_t*` for each actual underlying item (e.g. if you put a func in a table and then get the func as an export and from the table then `same` will report `false`). Additionally the fate of `wasm_ref_t*` seems somewhat unclear at this point. The change here is to make the `same` and cast functions all abort saying they're unimplemented. (similar to the host info functions). If and when we get around to reimplementing these functions we can ensure they're implemented uniformly and work well for all intended use cases.
1 parent c3799c8 commit cca558c

File tree

14 files changed

+113
-332
lines changed

14 files changed

+113
-332
lines changed

crates/c-api/macros/src/lib.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,9 @@ pub fn declare_ref(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
7171
}
7272

7373
#[no_mangle]
74-
pub extern fn #same(a: &#ty, b: &#ty) -> bool {
75-
a.externref().ptr_eq(&b.externref())
74+
pub extern fn #same(_a: &#ty, _b: &#ty) -> bool {
75+
eprintln!("`{}` is not implemented", stringify!(#same));
76+
std::process::abort();
7677
}
7778

7879
#[no_mangle]
@@ -98,13 +99,14 @@ pub fn declare_ref(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
9899

99100
#[no_mangle]
100101
pub extern fn #as_ref(a: &#ty) -> Box<crate::wasm_ref_t> {
101-
let r = Some(a.externref());
102-
Box::new(crate::wasm_ref_t { r })
102+
eprintln!("`{}` is not implemented", stringify!(#as_ref));
103+
std::process::abort();
103104
}
104105

105106
#[no_mangle]
106107
pub extern fn #as_ref_const(a: &#ty) -> Box<crate::wasm_ref_t> {
107-
#as_ref(a)
108+
eprintln!("`{}` is not implemented", stringify!(#as_ref_const));
109+
std::process::abort();
108110
}
109111

110112
// TODO: implement `wasm_ref_as_#name#`

crates/c-api/src/extern.rs

Lines changed: 7 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,27 @@
1-
use crate::host_ref::HostRef;
21
use crate::wasm_externkind_t;
32
use crate::{wasm_externtype_t, wasm_func_t, wasm_global_t, wasm_memory_t, wasm_table_t};
4-
use wasmtime::{ExternType, Func, Global, Memory, Table};
3+
use wasmtime::Extern;
54

65
#[derive(Clone)]
76
pub struct wasm_extern_t {
8-
pub(crate) which: ExternHost,
7+
pub(crate) which: Extern,
98
}
109

1110
wasmtime_c_api_macros::declare_ref!(wasm_extern_t);
1211

13-
#[derive(Clone)]
14-
pub(crate) enum ExternHost {
15-
Func(HostRef<Func>),
16-
Global(HostRef<Global>),
17-
Memory(HostRef<Memory>),
18-
Table(HostRef<Table>),
19-
}
20-
21-
impl wasm_extern_t {
22-
pub(crate) fn externref(&self) -> wasmtime::ExternRef {
23-
match &self.which {
24-
ExternHost::Func(f) => f.clone().into(),
25-
ExternHost::Global(f) => f.clone().into(),
26-
ExternHost::Memory(f) => f.clone().into(),
27-
ExternHost::Table(f) => f.clone().into(),
28-
}
29-
}
30-
}
31-
3212
#[no_mangle]
3313
pub extern "C" fn wasm_extern_kind(e: &wasm_extern_t) -> wasm_externkind_t {
3414
match e.which {
35-
ExternHost::Func(_) => crate::WASM_EXTERN_FUNC,
36-
ExternHost::Global(_) => crate::WASM_EXTERN_GLOBAL,
37-
ExternHost::Table(_) => crate::WASM_EXTERN_TABLE,
38-
ExternHost::Memory(_) => crate::WASM_EXTERN_MEMORY,
15+
Extern::Func(_) => crate::WASM_EXTERN_FUNC,
16+
Extern::Global(_) => crate::WASM_EXTERN_GLOBAL,
17+
Extern::Table(_) => crate::WASM_EXTERN_TABLE,
18+
Extern::Memory(_) => crate::WASM_EXTERN_MEMORY,
3919
}
4020
}
4121

4222
#[no_mangle]
4323
pub extern "C" fn wasm_extern_type(e: &wasm_extern_t) -> Box<wasm_externtype_t> {
44-
let ty = match &e.which {
45-
ExternHost::Func(f) => ExternType::Func(f.borrow().ty()),
46-
ExternHost::Global(f) => ExternType::Global(f.borrow().ty()),
47-
ExternHost::Table(f) => ExternType::Table(f.borrow().ty()),
48-
ExternHost::Memory(f) => ExternType::Memory(f.borrow().ty()),
49-
};
50-
Box::new(wasm_externtype_t::new(ty))
24+
Box::new(wasm_externtype_t::new(e.which.ty()))
5125
}
5226

5327
#[no_mangle]

crates/c-api/src/func.rs

Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
use crate::host_ref::HostRef;
21
use crate::{wasm_extern_t, wasm_functype_t, wasm_store_t, wasm_val_t};
3-
use crate::{wasm_name_t, wasm_trap_t, wasmtime_error_t, ExternHost};
2+
use crate::{wasm_name_t, wasm_trap_t, wasmtime_error_t};
43
use anyhow::anyhow;
54
use std::ffi::c_void;
65
use std::panic::{self, AssertUnwindSafe};
@@ -59,29 +58,23 @@ impl Drop for Finalizer {
5958
impl wasm_func_t {
6059
pub(crate) fn try_from(e: &wasm_extern_t) -> Option<&wasm_func_t> {
6160
match &e.which {
62-
ExternHost::Func(_) => Some(unsafe { &*(e as *const _ as *const _) }),
61+
Extern::Func(_) => Some(unsafe { &*(e as *const _ as *const _) }),
6362
_ => None,
6463
}
6564
}
6665

67-
pub(crate) fn func(&self) -> &HostRef<Func> {
66+
pub(crate) fn func(&self) -> &Func {
6867
match &self.ext.which {
69-
ExternHost::Func(f) => f,
68+
Extern::Func(f) => f,
7069
_ => unsafe { std::hint::unreachable_unchecked() },
7170
}
7271
}
73-
74-
fn externref(&self) -> wasmtime::ExternRef {
75-
self.func().clone().into()
76-
}
7772
}
7873

79-
impl From<HostRef<Func>> for wasm_func_t {
80-
fn from(func: HostRef<Func>) -> wasm_func_t {
74+
impl From<Func> for wasm_func_t {
75+
fn from(func: Func) -> wasm_func_t {
8176
wasm_func_t {
82-
ext: wasm_extern_t {
83-
which: ExternHost::Func(func),
84-
},
77+
ext: wasm_extern_t { which: func.into() },
8578
}
8679
}
8780
}
@@ -101,14 +94,14 @@ fn create_function(
10194
let mut out_results = vec![wasm_val_t::default(); results.len()];
10295
let out = func(caller, params.as_ptr(), out_results.as_mut_ptr());
10396
if let Some(trap) = out {
104-
return Err(trap.trap.borrow().clone());
97+
return Err(trap.trap.clone());
10598
}
10699
for i in 0..results.len() {
107100
results[i] = out_results[i].val();
108101
}
109102
Ok(())
110103
});
111-
Box::new(HostRef::new(func).into())
104+
Box::new(func.into())
112105
}
113106

114107
#[no_mangle]
@@ -172,7 +165,7 @@ pub unsafe extern "C" fn wasm_func_call(
172165
args: *const wasm_val_t,
173166
results: *mut wasm_val_t,
174167
) -> *mut wasm_trap_t {
175-
let func = wasm_func.func().borrow();
168+
let func = wasm_func.func();
176169
let mut trap = ptr::null_mut();
177170
let error = wasmtime_func_call(
178171
wasm_func,
@@ -211,7 +204,7 @@ fn _wasmtime_func_call(
211204
results: &mut [wasm_val_t],
212205
trap_ptr: &mut *mut wasm_trap_t,
213206
) -> Option<Box<wasmtime_error_t>> {
214-
let func = func.func().borrow();
207+
let func = func.func();
215208
if results.len() != func.result_arity() {
216209
return Some(Box::new(anyhow!("wrong number of results provided").into()));
217210
}
@@ -253,17 +246,17 @@ fn _wasmtime_func_call(
253246

254247
#[no_mangle]
255248
pub extern "C" fn wasm_func_type(f: &wasm_func_t) -> Box<wasm_functype_t> {
256-
Box::new(wasm_functype_t::new(f.func().borrow().ty()))
249+
Box::new(wasm_functype_t::new(f.func().ty()))
257250
}
258251

259252
#[no_mangle]
260253
pub extern "C" fn wasm_func_param_arity(f: &wasm_func_t) -> usize {
261-
f.func().borrow().param_arity()
254+
f.func().param_arity()
262255
}
263256

264257
#[no_mangle]
265258
pub extern "C" fn wasm_func_result_arity(f: &wasm_func_t) -> usize {
266-
f.func().borrow().result_arity()
259+
f.func().result_arity()
267260
}
268261

269262
#[no_mangle]
@@ -277,12 +270,6 @@ pub extern "C" fn wasmtime_caller_export_get(
277270
name: &wasm_name_t,
278271
) -> Option<Box<wasm_extern_t>> {
279272
let name = str::from_utf8(name.as_slice()).ok()?;
280-
let export = caller.caller.get_export(name)?;
281-
let which = match export {
282-
Extern::Func(f) => ExternHost::Func(HostRef::new(f)),
283-
Extern::Global(g) => ExternHost::Global(HostRef::new(g)),
284-
Extern::Memory(m) => ExternHost::Memory(HostRef::new(m)),
285-
Extern::Table(t) => ExternHost::Table(HostRef::new(t)),
286-
};
273+
let which = caller.caller.get_export(name)?;
287274
Some(Box::new(wasm_extern_t { which }))
288275
}

crates/c-api/src/global.rs

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
use crate::host_ref::HostRef;
21
use crate::{handle_result, wasmtime_error_t};
3-
use crate::{wasm_extern_t, wasm_globaltype_t, wasm_store_t, wasm_val_t, ExternHost};
2+
use crate::{wasm_extern_t, wasm_globaltype_t, wasm_store_t, wasm_val_t};
43
use std::ptr;
5-
use wasmtime::Global;
4+
use wasmtime::{Extern, Global};
65

76
#[derive(Clone)]
87
#[repr(transparent)]
@@ -15,21 +14,17 @@ wasmtime_c_api_macros::declare_ref!(wasm_global_t);
1514
impl wasm_global_t {
1615
pub(crate) fn try_from(e: &wasm_extern_t) -> Option<&wasm_global_t> {
1716
match &e.which {
18-
ExternHost::Global(_) => Some(unsafe { &*(e as *const _ as *const _) }),
17+
Extern::Global(_) => Some(unsafe { &*(e as *const _ as *const _) }),
1918
_ => None,
2019
}
2120
}
2221

23-
fn global(&self) -> &HostRef<Global> {
22+
fn global(&self) -> &Global {
2423
match &self.ext.which {
25-
ExternHost::Global(g) => g,
24+
Extern::Global(g) => g,
2625
_ => unsafe { std::hint::unreachable_unchecked() },
2726
}
2827
}
29-
30-
fn externref(&self) -> wasmtime::ExternRef {
31-
self.global().clone().into()
32-
}
3328
}
3429

3530
#[no_mangle]
@@ -59,7 +54,7 @@ pub extern "C" fn wasmtime_global_new(
5954
handle_result(global, |global| {
6055
*ret = Box::into_raw(Box::new(wasm_global_t {
6156
ext: wasm_extern_t {
62-
which: ExternHost::Global(HostRef::new(global)),
57+
which: global.into(),
6358
},
6459
}));
6560
})
@@ -72,18 +67,18 @@ pub extern "C" fn wasm_global_as_extern(g: &wasm_global_t) -> &wasm_extern_t {
7267

7368
#[no_mangle]
7469
pub extern "C" fn wasm_global_type(g: &wasm_global_t) -> Box<wasm_globaltype_t> {
75-
let globaltype = g.global().borrow().ty();
70+
let globaltype = g.global().ty();
7671
Box::new(wasm_globaltype_t::new(globaltype))
7772
}
7873

7974
#[no_mangle]
8075
pub extern "C" fn wasm_global_get(g: &wasm_global_t, out: &mut wasm_val_t) {
81-
out.set(g.global().borrow().get());
76+
out.set(g.global().get());
8277
}
8378

8479
#[no_mangle]
8580
pub extern "C" fn wasm_global_set(g: &wasm_global_t, val: &wasm_val_t) {
86-
let result = g.global().borrow().set(val.val());
81+
let result = g.global().set(val.val());
8782
// FIXME(WebAssembly/wasm-c-api#131) should communicate the error here
8883
drop(result);
8984
}
@@ -93,5 +88,5 @@ pub extern "C" fn wasmtime_global_set(
9388
g: &wasm_global_t,
9489
val: &wasm_val_t,
9590
) -> Option<Box<wasmtime_error_t>> {
96-
handle_result(g.global().borrow().set(val.val()), |()| {})
91+
handle_result(g.global().set(val.val()), |()| {})
9792
}

crates/c-api/src/host_ref.rs

Lines changed: 0 additions & 101 deletions
This file was deleted.

0 commit comments

Comments
 (0)