Class: Wasmtime::Module

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

Overview

Represents a WebAssembly module.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

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

Instantiates a serialized module coming from either #serialize or Engine#precompile_module.

The engine serializing and the engine deserializing must:

  • have the same configuration

  • be of the same gem version

Parameters:

Returns:



79
80
81
82
83
84
# File 'ext/src/ruby_api/module.rs', line 79

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

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

Instantiates a serialized module from a file.

Parameters:

Returns:

See Also:



94
95
96
97
98
# File 'ext/src/ruby_api/module.rs', line 94

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

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

Parameters:

Returns:



58
59
60
61
62
63
64
65
66
# File 'ext/src/ruby_api/module.rs', line 58

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 module = nogvl(|| ModuleImpl::from_file(eng, path))
        .map_err(|e| error!("Could not build module from file: {}", e))?;

    Ok(module.into())
}

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

Parameters:

Returns:



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

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 module = nogvl(|| ModuleImpl::new(eng, locked_slice))
        .map_err(|e| error!("Could not build module: {}", e))?;

    Ok(module.into())
}

Instance Method Details

#importsArray<Hash>

Returns the list of imports that this Module has and must be satisfied.

Returns:

  • (Array<Hash>)

    An array of hashes containing import information



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'ext/src/ruby_api/module.rs', line 120

pub fn imports(&self) -> Result<RArray, Error> {
        let module = self.get();
        let imports = module.imports();

        let result = RArray::with_capacity(imports.len());
        for import in imports {
            let hash = RHash::new();
            hash.aset("module", import.module())?;
            hash.aset("name", import.name())?;
            hash.aset("type", import.ty().wrap_wasmtime_type()?)?;
            result.push(hash)?;
        }
        Ok(result)
    }
}

#serializeString

Serialize the module.

Returns:

  • (String)

See Also:



104
105
106
107
108
109
110
111
# File 'ext/src/ruby_api/module.rs', line 104

pub fn serialize(&self) -> Result<RString, Error> {
    let module = self.get();
    let bytes = module.serialize();

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