Class: Wasmtime::Linker

Inherits:
Object
  • Object
show all
Defined in:
ext/src/ruby_api/linker.rs

Overview

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.new(engine, wasi: false) ⇒ Linker

Parameters:

  • engine (Engine)
  • wasi (Boolean) (defaults to: false)

    Whether WASI should be defined in this Linker. Defaults to false.

Returns:



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'ext/src/ruby_api/linker.rs', line 49

pub fn new(args: &[Value]) -> Result<Self, Error> {
    let args = scan_args::scan_args::<(&Engine,), (), (), (), _, ()>(args)?;
    let kw = scan_args::get_kwargs::<_, (), (Option<bool>,), ()>(args.keywords, &[], &[*WASI])?;
    let (engine,) = args.required;
    let wasi = kw.optional.0.unwrap_or(false);

    let mut inner: LinkerImpl<StoreData> = LinkerImpl::new(engine.get());
    if wasi {
        wasi_common::sync::add_to_linker(&mut inner, |s| s.wasi_ctx_mut())
            .map_err(|e| error!("{}", e))?
    }
    Ok(Self {
        inner: RefCell::new(inner),
        refs: Default::default(),
        has_wasi: wasi,
    })
}

Instance Method Details

#alias(mod, as_mod) ⇒ void

This method returns an undefined value.

Aliases one module’s name as another.

Parameters:

  • mod (String)

    Source module name

  • as_mod (String)

    Destination module name



242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'ext/src/ruby_api/linker.rs', line 242

pub fn alias(
    &self,
    module: RString,
    name: RString,
    as_module: RString,
    as_name: RString,
) -> Result<(), Error> {
    self.inner
        .borrow_mut()
        .alias(
            unsafe { module.as_str() }?,
            unsafe { name.as_str() }?,
            unsafe { as_module.as_str() }?,
            unsafe { as_name.as_str() }?,
        )
        .map_err(|e| error!("{}", e))
        .map(|_| ())
}

#allow_shadowing=(val) ⇒ Object

Allow shadowing.

Parameters:

  • val (Boolean)


71
72
73
# File 'ext/src/ruby_api/linker.rs', line 71

pub fn set_allow_shadowing(&self, val: bool) {
    self.inner.borrow_mut().allow_shadowing(val);
}

#allow_unknown_exports=(val) ⇒ Object

Allow unknown exports.

Parameters:

  • val (Boolean)


79
80
81
# File 'ext/src/ruby_api/linker.rs', line 79

pub fn set_allow_unknown_exports(&self, val: bool) {
    self.inner.borrow_mut().allow_unknown_exports(val);
}

#define(store, mod, name, item) ⇒ void

This method returns an undefined value.

Define an item in this linker.

Parameters:

  • store (Store)
  • mod (String)

    Module name

  • name (String)

    Import name

  • item (Func, Memory)

    The item to define.



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'ext/src/ruby_api/linker.rs', line 103

pub fn define(
    ruby: &Ruby,
    rb_self: &Self,
    store: &Store,
    module: RString,
    name: RString,
    item: Value,
) -> Result<(), Error> {
    let item = item.to_extern(ruby)?;

    rb_self
        .inner
        .borrow_mut()
        .define(
            store.context(),
            unsafe { module.as_str()? },
            unsafe { name.as_str()? },
            item,
        )
        .map(|_| ())
        .map_err(|e| error!("{}", e))
}

#define_unknown_imports_as_traps(mod) ⇒ void

This method returns an undefined value.

Define unknown (unresolved) imports as functions which trap.

Parameters:



88
89
90
91
92
93
# File 'ext/src/ruby_api/linker.rs', line 88

pub fn define_unknown_imports_as_traps(&self, module: &Module) -> Result<(), Error> {
    self.inner
        .borrow_mut()
        .define_unknown_imports_as_traps(module.get())
        .map_err(|e| error!("{}", e))
}

#func_new(mod, name, params, results, &block) ⇒ void

This method returns an undefined value.

Define a function in this linker.

Parameters:

  • mod (String)

    Module name

  • name (String)

    Import name

  • params (Array<Symbol>)

    The function’s parameters.

  • results (Array<Symbol>)

    The function’s results.

  • block (Block)

    See Func.new for block argument details.

See Also:



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
# File 'ext/src/ruby_api/linker.rs', line 139

pub fn func_new(&self, args: &[Value]) -> Result<(), Error> {
    let args = scan_args::<(RString, RString, RArray, RArray), (), (), (), RHash, Proc>(args)?;
    let (module, name, params, results) = args.required;
    let callable = args.block;
    let mut inner_mut = self.inner.borrow_mut();
    let engine = inner_mut.engine();
    let ty = wasmtime::FuncType::new(
        engine,
        params.to_val_type_vec()?,
        results.to_val_type_vec()?,
    );
    let func_closure = func::make_func_closure(&ty, callable.into());

    self.refs.borrow_mut().push(callable.as_value());

    inner_mut
        .func_new(
            unsafe { module.as_str() }?,
            unsafe { name.as_str() }?,
            ty,
            func_closure,
        )
        .map_err(|e| error!("{}", e))
        .map(|_| ())
}

#get(store, mod, name) ⇒ Extern?

Looks up a previously defined item in this linker.

Parameters:

  • store (Store)
  • mod (String)

    Module name

  • name (String)

    Import name

Returns:

  • (Extern, nil)

    The item if it exists, nil otherwise.



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'ext/src/ruby_api/linker.rs', line 173

pub fn get(
    &self,
    store: Obj<Store>,
    module: RString,
    name: RString,
) -> Result<Option<Extern>, Error> {
    let ext =
        self.inner
            .borrow()
            .get(store.context_mut(), unsafe { module.as_str() }?, unsafe {
                name.as_str()?
            });

    match ext {
        None => Ok(None),
        Some(ext) => ext.wrap_wasmtime_type(store.into()).map(Some),
    }
}

#get_default(store, mod) ⇒ Func

Returns the “default export” of a module.

Parameters:

  • store (Store)
  • mod (String)

    Module name

Returns:



309
310
311
312
313
314
315
316
# File 'ext/src/ruby_api/linker.rs', line 309

pub fn get_default(&self, store: Obj<Store>, module: RString) -> Result<Func, Error> {
        self.inner
            .borrow()
            .get_default(store.context_mut(), unsafe { module.as_str() }?)
            .map(|func| Func::from_inner(store.into(), func))
            .map_err(|e| error!("{}", e))
    }
}

#instance(store, mod, instance) ⇒ void

This method returns an undefined value.

Defines an entire Instance in this linker.

Parameters:



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'ext/src/ruby_api/linker.rs', line 200

pub fn instance(
    &self,
    store: &Store,
    module: RString,
    instance: &Instance,
) -> Result<(), Error> {
    self.inner
        .borrow_mut()
        .instance(
            store.context_mut(),
            unsafe { module.as_str() }?,
            instance.get(),
        )
        .map_err(|e| error!("{}", e))
        .map(|_| ())
}

#instantiate(store, mod) ⇒ Instance

Instantiates a Module in a Store using the defined imports in the linker.

Parameters:

Returns:



282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
# File 'ext/src/ruby_api/linker.rs', line 282

pub fn instantiate(&self, store: Obj<Store>, module: &Module) -> Result<Instance, Error> {
    if self.has_wasi && !store.context().data().has_wasi_ctx() {
        return err!(
            "Store is missing WASI configuration.\n\n\
            When using `wasi: true`, the Store given to\n\
            `Linker#instantiate` must have a WASI configuration.\n\
            To fix this, provide the `wasi_ctx` when creating the Store:\n\
                Wasmtime::Store.new(engine, wasi_ctx: WasiCtxBuilder.new)"
        );
    }

    self.inner
        .borrow_mut()
        .instantiate(store.context_mut(), module.get())
        .map_err(|e| StoreContextValue::from(store).handle_wasm_error(e))
        .map(|instance| {
            self.refs.borrow().iter().for_each(|val| store.retain(*val));
            Instance::from_inner(store, instance)
        })
}

#module(store, name, mod) ⇒ void

This method returns an undefined value.

Defines automatic instantiation of a Module in this linker.

Parameters:

  • store (Store)
  • name (String)

    Module name

  • mod (Module)


225
226
227
228
229
230
231
# File 'ext/src/ruby_api/linker.rs', line 225

pub fn module(&self, store: &Store, name: RString, module: &Module) -> Result<(), Error> {
    self.inner
        .borrow_mut()
        .module(store.context_mut(), unsafe { name.as_str()? }, module.get())
        .map(|_| ())
        .map_err(|e| error!("{}", e))
}