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:



55
56
57
58
59
60
61
62
63
# File 'ext/src/ruby_api/component/linker.rs', line 55

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:



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

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

    let instance = ruby.obj_wrap(LinkerInstance::from_inner(
        instance,
        name_str.to_string(),
        rb_self.as_value(),
    ));

    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:



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

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:



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'ext/src/ruby_api/component/linker.rs', line 80

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(),
        String::new(), // root path is empty
        rb_self.as_value(),
    ));
    let block_result: Result<Value, _> = ruby.yield_value(instance);

    instance.take_inner();

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