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) ⇒ Linker

Parameters:

Returns:



44
45
46
47
48
49
50
51
# File 'ext/src/ruby_api/linker.rs', line 44

pub fn new(engine: &Engine) -> Result<Self, Error> {
    let inner: LinkerImpl<StoreData> = LinkerImpl::new(engine.get());
    Ok(Self {
        inner: RefCell::new(inner),
        refs: Default::default(),
        has_wasi: RefCell::new(false),
    })
}

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



229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'ext/src/ruby_api/linker.rs', line 229

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)


57
58
59
# File 'ext/src/ruby_api/linker.rs', line 57

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)


65
66
67
# File 'ext/src/ruby_api/linker.rs', line 65

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.



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'ext/src/ruby_api/linker.rs', line 89

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:



74
75
76
77
78
79
# File 'ext/src/ruby_api/linker.rs', line 74

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:



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

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:

Raises:

  • (Wasmtime::Error)

    if the item is not defined in this linker, or if looking it up fails for another reason (e.g. out of memory).



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'ext/src/ruby_api/linker.rs', line 161

pub fn get(
    &self,
    store: Obj<Store>,
    module: RString,
    name: RString,
) -> Result<Extern<'_>, Error> {
    let ext = self
        .inner
        .borrow()
        .get(store.context_mut(), unsafe { module.as_str() }?, unsafe {
            name.as_str()?
        })
        .map_err(|e| error!("{}", e))?;

    let ruby = Ruby::get_with(store);
    ext.wrap_wasmtime_type(&ruby, store.into())
}

#get_default(store, mod) ⇒ Func

Returns the “default export” of a module.

Parameters:

  • store (Store)
  • mod (String)

    Module name

Returns:



300
301
302
303
304
305
306
# File 'ext/src/ruby_api/linker.rs', line 300

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:



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'ext/src/ruby_api/linker.rs', line 187

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:



269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# File 'ext/src/ruby_api/linker.rs', line 269

pub fn instantiate(
    ruby: &Ruby,
    rb_self: Obj<Self>,
    store: Obj<Store>,
    module: &Module,
) -> Result<Instance, Error> {
    if *rb_self.has_wasi.borrow() && !store.context().data().has_wasi_p1_ctx() {
        return err!("{}", errors::missing_wasi_p1_ctx_error());
    }

    rb_self
        .inner
        .borrow_mut()
        .instantiate(store.context_mut(), module.get())
        .map_err(|e| StoreContextValue::from(store).handle_wasm_error(ruby, e))
        .map(|instance| {
            rb_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)


212
213
214
215
216
217
218
# File 'ext/src/ruby_api/linker.rs', line 212

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

#use_deterministic_scheduling_functionsvoid

This method returns an undefined value.

Replaces the poll_oneoff and sched_yield function implementations with deterministic ones.



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

pub fn use_deterministic_scheduling_functions(&self) -> Result<(), Error> {
    let mut inner = self.inner.borrow_mut();
    deterministic_wasi_ctx::replace_scheduling_functions(&mut inner).map_err(|e| error!("{e}"))
}