Class: Extism::CurrentPlugin
- Inherits:
- 
      Object
      
        - Object
- Extism::CurrentPlugin
 
- Defined in:
- lib/extism/current_plugin.rb
Overview
Represents a reference to a plugin in a host function Use this class to read and write to the memory of the plugin These methods allow you to get data in and out of the plugin in a host function
Instance Method Summary collapse
- 
  
    
      #alloc(num_bytes)  ⇒ Extism::Memory 
    
    
  
  
  
  
  
  
  
  
  
    Allocates a memory block in the plugin. 
- 
  
    
      #free(memory)  ⇒ Extism::Memory 
    
    
  
  
  
  
  
  
  
  
  
    Frees the memory block. 
- 
  
    
      #initialize(ptr)  ⇒ CurrentPlugin 
    
    
  
  
  
    constructor
  
  
  
  
  
  
  
    Initialize a CurrentPlugin given an pointer. 
- 
  
    
      #input_as_json(input)  ⇒ Hash 
    
    
  
  
  
  
  
  
  
  
  
    Gets the input as a JSON parsed Hash. 
- 
  
    
      #input_as_string(input)  ⇒ String 
    
    
  
  
  
  
  
  
  
  
  
    Gets the input as a string. 
- 
  
    
      #memory_at_offset(offset)  ⇒ Extism::Memory 
    
    
  
  
  
  
  
  
  
  
  
    Gets the memory block at a given offset. 
- 
  
    
      #output_json(output, obj)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Sets json to the return of the host function. 
- 
  
    
      #output_string(output, bytes)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Sets string to the return of the host function. 
- 
  
    
      #set_return(output, value)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Sets the return value parameter. 
Constructor Details
#initialize(ptr) ⇒ CurrentPlugin
Initialize a CurrentPlugin given an pointer
| 13 14 15 | # File 'lib/extism/current_plugin.rb', line 13 def initialize(ptr) @ptr = ptr end | 
Instance Method Details
#alloc(num_bytes) ⇒ Extism::Memory
Allocates a memory block in the plugin
| 25 26 27 28 | # File 'lib/extism/current_plugin.rb', line 25 def alloc(num_bytes) offset = LibExtism.extism_current_plugin_memory_alloc(@ptr, num_bytes) Memory.new(offset, num_bytes) end | 
#free(memory) ⇒ Extism::Memory
Frees the memory block
| 38 39 40 | # File 'lib/extism/current_plugin.rb', line 38 def free(memory) LibExtism.extism_current_plugin_memory_free(@ptr, memory.offset) end | 
#input_as_json(input) ⇒ Hash
Gets the input as a JSON parsed Hash
| 86 87 88 89 90 91 92 | # File 'lib/extism/current_plugin.rb', line 86 def input_as_json(input) raise ArgumentError, 'input is not an Extism::Val' unless input.instance_of? Extism::Val mem = memory_at_offset(input.value) str = memory_ptr(mem).read_bytes(mem.len) JSON.parse(str) end | 
#input_as_string(input) ⇒ String
Gets the input as a string
| 69 70 71 72 73 74 | # File 'lib/extism/current_plugin.rb', line 69 def input_as_string(input) raise ArgumentError, 'input is not an Extism::Val' unless input.instance_of? Extism::Val mem = memory_at_offset(input.value) memory_ptr(mem).read_bytes(mem.len) end | 
#memory_at_offset(offset) ⇒ Extism::Memory
Gets the memory block at a given offset. Note: try to use input_* and output_* methods where possible.
| 52 53 54 55 56 57 | # File 'lib/extism/current_plugin.rb', line 52 def memory_at_offset(offset) len = LibExtism.extism_current_plugin_memory_length(@ptr, offset) raise Extism::Error, "Could not find memory block at offset #{offset}" if len.zero? Memory.new(offset, len) end | 
#output_json(output, obj) ⇒ Object
Sets json to the return of the host function
| 120 121 122 123 124 125 | # File 'lib/extism/current_plugin.rb', line 120 def output_json(output, obj) bytes = JSON.generate(obj) mem = alloc(bytes.length) memory_ptr(mem).put_bytes(0, bytes) set_return(output, mem.offset) end | 
#output_string(output, bytes) ⇒ Object
Sets string to the return of the host function
| 104 105 106 107 108 | # File 'lib/extism/current_plugin.rb', line 104 def output_string(output, bytes) mem = alloc(bytes.length) memory_ptr(mem).put_bytes(0, bytes) set_return(output, mem.offset) end | 
#set_return(output, value) ⇒ Object
Sets the return value parameter
| 133 134 135 136 137 138 139 140 | # File 'lib/extism/current_plugin.rb', line 133 def set_return(output, value) case output.type when :i32, :i64, :f32, :f64 output.value = value else raise ArgumentError, "Don't know how to set output type #{output.type}" end end |