Class: Wasmtime::Component::Linker

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

Overview

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.new(engine) ⇒ Linker

Parameters:

Returns:



46
47
48
49
50
51
52
53
54
# File 'ext/src/ruby_api/component/linker.rs', line 46

pub fn new(engine: &Engine) -> Result<Self, Error> {
    let linker: LinkerImpl<StoreData> = LinkerImpl::new(engine.get());

    Ok(Linker {
        inner: RefCell::new(linker),
        refs: RefCell::new(Vec::new()),
        has_wasi: RefCell::new(false),
    })
}

Instance Method Details

#instance(name) {|instance| ... } ⇒ Linker

Define items at the provided namespace in this Wasmtime::Component::Linker.

Parameters:

  • name (String)

Yields:

Yield Parameters:

Returns:



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

pub fn instance(ruby: &Ruby, rb_self: Obj<Self>, name: RString) -> Result<Obj<Self>, Error> {
    let mut inner = rb_self.inner.borrow_mut();
    let instance = inner
        .instance(unsafe { name.as_str() }?)
        .map_err(|e| error!("{}", e))?;

    let instance = ruby.obj_wrap(LinkerInstance::from_inner(instance));

    let block_result: Result<Value, _> = ruby.yield_value(instance);

    instance.take_inner();

    match block_result {
        Ok(_) => Ok(rb_self),
        Err(e) => Err(e),
    }
}

#instantiate(store, component) ⇒ Instance

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

Parameters:

Returns:



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'ext/src/ruby_api/component/linker.rs', line 118

fn instantiate(
    _ruby: &Ruby,
    rb_self: Obj<Self>,
    store: Obj<Store>,
    component: &Component,
) -> Result<Instance, Error> {
    if *rb_self.has_wasi.borrow() && !store.context().data().has_wasi_ctx() {
        return err!("{}", errors::missing_wasi_ctx_error("linker.instantiate"));
    }

    let inner = rb_self.inner.borrow();
    inner
        .instantiate(store.context_mut(), component.get())
        .map(|instance| {
            rb_self
                .refs
                .borrow()
                .iter()
                .for_each(|value| store.retain(*value));

            Instance::from_inner(store, instance)
        })
        .map_err(|e| error!("{}", e))
}

#root {|instance| ... } ⇒ Linker

Define items in the root of this Wasmtime::Component::Linker.

Yields:

Yield Parameters:

Returns:



71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'ext/src/ruby_api/component/linker.rs', line 71

pub fn root(ruby: &Ruby, rb_self: Obj<Self>) -> Result<Obj<Self>, Error> {
    let Ok(mut inner) = rb_self.inner.try_borrow_mut() else {
        return err!("Linker is not reentrant");
    };
    let instance = ruby.obj_wrap(LinkerInstance::from_inner(inner.root()));
    let block_result: Result<Value, _> = ruby.yield_value(instance);

    instance.take_inner();

    match block_result {
        Ok(_) => Ok(rb_self),
        Err(e) => Err(e),
    }
}