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:



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

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 {
        wasmtime_wasi::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



235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'ext/src/ruby_api/linker.rs', line 235

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)


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

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)


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

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.



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

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

    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:



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

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:



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'ext/src/ruby_api/linker.rs', line 136

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 ty = wasmtime::FuncType::new(params.to_val_type_vec()?, results.to_val_type_vec()?);
    let func_closure = func::make_func_closure(&ty, callable);

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

    self.inner
        .borrow_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.



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'ext/src/ruby_api/linker.rs', line 165

pub fn get(
    &self,
    s: Obj<Store>,
    module: RString,
    name: RString,
) -> Result<Option<Extern>, Error> {
    let store = s.get();
    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(s.into()).map(Some),
    }
}

#get_default(store, mod) ⇒ Func

Returns the “default export” of a module.

Parameters:

  • store (Store)
  • mod (String)

    Module name

Returns:



304
305
306
307
308
309
310
311
312
313
# File 'ext/src/ruby_api/linker.rs', line 304

pub fn get_default(&self, s: Obj<Store>, module: RString) -> Result<Func, Error> {
        let store = s.get();

        self.inner
            .borrow()
            .get_default(store.context_mut(), unsafe { module.as_str() }?)
            .map(|func| Func::from_inner(s.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:



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'ext/src/ruby_api/linker.rs', line 193

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:



275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# File 'ext/src/ruby_api/linker.rs', line 275

pub fn instantiate(&self, s: Obj<Store>, module: &Module) -> Result<Instance, Error> {
    let store = s.get();

    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(s).handle_wasm_error(e))
        .map(|instance| {
            self.refs.borrow().iter().for_each(|val| store.retain(*val));
            Instance::from_inner(s, 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)


218
219
220
221
222
223
224
# File 'ext/src/ruby_api/linker.rs', line 218

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))
}