Module: Extism::HostEnvironment

Defined in:
lib/extism/host_environment.rb

Overview

Represents an “environment” that can be imported to a plug-in

Examples:

class MyEnvironment
  include Extism::HostEnvironment
  # we need to register each import that the plug-in expects and match the Wasm signature
  # register_import takes the name, the param types, and the return types
  register_import :reflect, [Extism::ValType::I64], [Extism::ValType::I64]

  # reflect just takes a string from the plug-in and reflects it back in return
  def reflect(plugin, inputs, outputs, _user_data)
    msg = plugin.input_as_string(inputs.first)
    plugin.output_string(outputs.first, msg)
  end
end

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



19
20
21
22
# File 'lib/extism/host_environment.rb', line 19

def self.included(base)
  base.extend ClassMethods
  base.class_variable_set(:@@import_funcs, [])
end

Instance Method Details

#host_functionsArray<Extism::Function>

Creates the host functions to pass to the plug-in on intialization. Used internally by the Plugin initializer

Returns:

See Also:

  • Plugin::new


30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/extism/host_environment.rb', line 30

def host_functions
  import_funcs = self.class.class_variable_get(:@@import_funcs)
  import_funcs.map do |f|
    name, params, returns = f
    Extism::Function.new(
      name.to_s,
      params,
      returns,
      method(name).to_proc
    )
  end
end