wasmtime_c_api/
linker.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
use crate::{
    bad_utf8, handle_result, wasm_engine_t, wasm_functype_t, wasm_trap_t, wasmtime_error_t,
    wasmtime_extern_t, wasmtime_instance_pre_t, wasmtime_module_t, WasmtimeStoreContext,
    WasmtimeStoreContextMut,
};
use std::ffi::c_void;
use std::mem::MaybeUninit;
use std::str;
use wasmtime::{Func, Instance, Linker};

#[repr(C)]
pub struct wasmtime_linker_t {
    pub(crate) linker: Linker<crate::WasmtimeStoreData>,
}

wasmtime_c_api_macros::declare_own!(wasmtime_linker_t);

#[no_mangle]
pub extern "C" fn wasmtime_linker_new(engine: &wasm_engine_t) -> Box<wasmtime_linker_t> {
    Box::new(wasmtime_linker_t {
        linker: Linker::new(&engine.engine),
    })
}

#[no_mangle]
pub extern "C" fn wasmtime_linker_clone(linker: &wasmtime_linker_t) -> Box<wasmtime_linker_t> {
    Box::new(wasmtime_linker_t {
        linker: linker.linker.clone(),
    })
}

#[no_mangle]
pub extern "C" fn wasmtime_linker_allow_shadowing(
    linker: &mut wasmtime_linker_t,
    allow_shadowing: bool,
) {
    linker.linker.allow_shadowing(allow_shadowing);
}

macro_rules! to_str {
    ($ptr:expr, $len:expr) => {
        match str::from_utf8(crate::slice_from_raw_parts($ptr, $len)) {
            Ok(s) => s,
            Err(_) => return bad_utf8(),
        }
    };
}

pub(crate) use to_str;

#[no_mangle]
pub unsafe extern "C" fn wasmtime_linker_define(
    linker: &mut wasmtime_linker_t,
    store: WasmtimeStoreContext<'_>,
    module: *const u8,
    module_len: usize,
    name: *const u8,
    name_len: usize,
    item: &wasmtime_extern_t,
) -> Option<Box<wasmtime_error_t>> {
    let linker = &mut linker.linker;
    let module = to_str!(module, module_len);
    let name = to_str!(name, name_len);
    let item = item.to_extern();
    handle_result(linker.define(&store, module, name, item), |_linker| ())
}

#[no_mangle]
pub unsafe extern "C" fn wasmtime_linker_define_func(
    linker: &mut wasmtime_linker_t,
    module: *const u8,
    module_len: usize,
    name: *const u8,
    name_len: usize,
    ty: &wasm_functype_t,
    callback: crate::wasmtime_func_callback_t,
    data: *mut c_void,
    finalizer: Option<extern "C" fn(*mut std::ffi::c_void)>,
) -> Option<Box<wasmtime_error_t>> {
    let ty = ty.ty().ty(linker.linker.engine());
    let module = to_str!(module, module_len);
    let name = to_str!(name, name_len);
    let cb = crate::func::c_callback_to_rust_fn(callback, data, finalizer);
    handle_result(linker.linker.func_new(module, name, ty, cb), |_linker| ())
}

#[no_mangle]
pub unsafe extern "C" fn wasmtime_linker_define_func_unchecked(
    linker: &mut wasmtime_linker_t,
    module: *const u8,
    module_len: usize,
    name: *const u8,
    name_len: usize,
    ty: &wasm_functype_t,
    callback: crate::wasmtime_func_unchecked_callback_t,
    data: *mut c_void,
    finalizer: Option<extern "C" fn(*mut std::ffi::c_void)>,
) -> Option<Box<wasmtime_error_t>> {
    let ty = ty.ty().ty(linker.linker.engine());
    let module = to_str!(module, module_len);
    let name = to_str!(name, name_len);
    let cb = crate::func::c_unchecked_callback_to_rust_fn(callback, data, finalizer);
    handle_result(
        linker.linker.func_new_unchecked(module, name, ty, cb),
        |_linker| (),
    )
}

#[cfg(feature = "wasi")]
#[no_mangle]
pub extern "C" fn wasmtime_linker_define_wasi(
    linker: &mut wasmtime_linker_t,
) -> Option<Box<wasmtime_error_t>> {
    handle_result(
        wasmtime_wasi::preview1::add_to_linker_sync(&mut linker.linker, |ctx| {
            ctx.wasi.as_mut().expect("wasi context must be populated")
        }),
        |_linker| (),
    )
}

#[no_mangle]
pub unsafe extern "C" fn wasmtime_linker_define_instance(
    linker: &mut wasmtime_linker_t,
    store: WasmtimeStoreContextMut<'_>,
    name: *const u8,
    name_len: usize,
    instance: &Instance,
) -> Option<Box<wasmtime_error_t>> {
    let linker = &mut linker.linker;
    let name = to_str!(name, name_len);
    handle_result(linker.instance(store, name, *instance), |_linker| ())
}

#[no_mangle]
pub extern "C" fn wasmtime_linker_instantiate(
    linker: &wasmtime_linker_t,
    store: WasmtimeStoreContextMut<'_>,
    module: &wasmtime_module_t,
    instance_ptr: &mut Instance,
    trap_ptr: &mut *mut wasm_trap_t,
) -> Option<Box<wasmtime_error_t>> {
    let result = linker.linker.instantiate(store, &module.module);
    super::instance::handle_instantiate(result, instance_ptr, trap_ptr)
}

#[no_mangle]
pub unsafe extern "C" fn wasmtime_linker_instantiate_pre(
    linker: &wasmtime_linker_t,
    module: &wasmtime_module_t,
    instance_ptr: &mut *mut wasmtime_instance_pre_t,
) -> Option<Box<wasmtime_error_t>> {
    let linker = &linker.linker;
    handle_result(linker.instantiate_pre(&module.module), |i| {
        let instance_pre = Box::new(wasmtime_instance_pre_t { underlying: i });
        *instance_ptr = Box::into_raw(instance_pre)
    })
}

#[no_mangle]
pub unsafe extern "C" fn wasmtime_linker_module(
    linker: &mut wasmtime_linker_t,
    store: WasmtimeStoreContextMut<'_>,
    name: *const u8,
    name_len: usize,
    module: &wasmtime_module_t,
) -> Option<Box<wasmtime_error_t>> {
    let linker = &mut linker.linker;
    let name = to_str!(name, name_len);
    handle_result(linker.module(store, name, &module.module), |_linker| ())
}

#[no_mangle]
pub unsafe extern "C" fn wasmtime_linker_get_default(
    linker: &wasmtime_linker_t,
    store: WasmtimeStoreContextMut<'_>,
    name: *const u8,
    name_len: usize,
    func: &mut Func,
) -> Option<Box<wasmtime_error_t>> {
    let linker = &linker.linker;
    let name = to_str!(name, name_len);
    handle_result(linker.get_default(store, name), |f| *func = f)
}

#[no_mangle]
pub unsafe extern "C" fn wasmtime_linker_get(
    linker: &wasmtime_linker_t,
    store: WasmtimeStoreContextMut<'_>,
    module: *const u8,
    module_len: usize,
    name: *const u8,
    name_len: usize,
    item_ptr: &mut MaybeUninit<wasmtime_extern_t>,
) -> bool {
    let linker = &linker.linker;
    let module = match str::from_utf8(crate::slice_from_raw_parts(module, module_len)) {
        Ok(s) => s,
        Err(_) => return false,
    };
    let name = match str::from_utf8(crate::slice_from_raw_parts(name, name_len)) {
        Ok(s) => s,
        Err(_) => return false,
    };
    match linker.get(store, module, name) {
        Some(which) => {
            crate::initialize(item_ptr, which.into());
            true
        }
        None => false,
    }
}