Class: Wasmtime::Component::Component

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

Overview

Represents a WebAssembly component.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.deserialize(engine, compiled) ⇒ Wasmtime::Component::Component

Instantiates a serialized component coming from either #serialize or Engine#precompile_component.

The engine serializing and the engine deserializing must:

  • have the same configuration

  • be of the same gem version

Parameters:

Returns:



96
97
98
99
100
101
# File 'ext/src/ruby_api/component.rs', line 96

pub fn deserialize(engine: &Engine, compiled: RString) -> Result<Self, Error> {
    // SAFETY: this string is immediately copied and never moved off the stack
    unsafe { ComponentImpl::deserialize(engine.get(), compiled.as_slice()) }
        .map(Into::into)
        .map_err(|e| error!("Could not deserialize component: {}", e))
}

.deserialize_file(engine, path) ⇒ Wasmtime::Component::Component

Instantiates a serialized component from a file.

Parameters:

Returns:

See Also:



111
112
113
114
115
# File 'ext/src/ruby_api/component.rs', line 111

pub fn deserialize_file(engine: &Engine, path: RString) -> Result<Self, Error> {
    unsafe { ComponentImpl::deserialize_file(engine.get(), path.as_str()?) }
        .map(Into::into)
        .map_err(|e| error!("Could not deserialize component from file: {}", e))
}

.from_file(engine, path) ⇒ Wasmtime::Component::Component

Parameters:

Returns:



75
76
77
78
79
80
81
82
83
# File 'ext/src/ruby_api/component.rs', line 75

pub fn from_file(engine: &Engine, path: RString) -> Result<Self, Error> {
    let eng = engine.get();
    let (path, _locked_str_guard) = path.as_locked_str()?;
    // SAFETY: this string is immediately copied and never moved off the stack
    let component = nogvl(|| ComponentImpl::from_file(eng, path))
        .map_err(|e| error!("Could not build component from file: {}", e))?;

    Ok(component.into())
}

.new(engine, wat_or_wasm) ⇒ Wasmtime::Component::Component

Creates a new component from the given binary data.

Parameters:

Returns:



61
62
63
64
65
66
67
68
# File 'ext/src/ruby_api/component.rs', line 61

pub fn new(engine: &Engine, wat_or_wasm: RString) -> Result<Self, Error> {
    let eng = engine.get();
    let (locked_slice, _locked_slice_guard) = wat_or_wasm.as_locked_slice()?;
    let component = nogvl(|| ComponentImpl::new(eng, locked_slice))
        .map_err(|e| error!("Could not build component: {}", e))?;

    Ok(component.into())
}

Instance Method Details

#serializeString

Serialize the component.

Returns:

  • (String)

See Also:



121
122
123
124
125
126
127
# File 'ext/src/ruby_api/component.rs', line 121

pub fn serialize(ruby: &Ruby, rb_self: Obj<Self>) -> Result<RString, Error> {
    let bytes = rb_self.get().serialize();

    bytes
        .map(|bytes| ruby.str_from_slice(&bytes))
        .map_err(|e| error!("{:?}", e))
}