diff --git a/ruby/example.rb b/ruby/example.rb index 6545ab7..8d880ce 100644 --- a/ruby/example.rb +++ b/ruby/example.rb @@ -4,7 +4,7 @@ require 'json' manifest = { :wasm => [{:path => "../wasm/code.wasm"}] } -plugin = Plugin.new(manifest) +plugin = Extism::Plugin.new(manifest) res = JSON.parse(plugin.call("count_vowels", ARGV[0] || "this is a test")) puts res['count'] diff --git a/ruby/lib/extism.rb b/ruby/lib/extism.rb index f0649be..33658cc 100644 --- a/ruby/lib/extism.rb +++ b/ruby/lib/extism.rb @@ -1,57 +1,59 @@ require 'ffi' require 'json' -module C - extend FFI::Library - ffi_lib "extism" - attach_function :extism_plugin_register, [:pointer, :uint64, :bool], :int32 - attach_function :extism_error, [:int32], :string - attach_function :extism_call, [:int32, :string, :pointer, :uint64], :int32 - attach_function :extism_output_length, [:int32], :uint64 - attach_function :extism_output_get, [:int32, :pointer, :uint64], :void - attach_function :extism_log_file, [:string, :pointer], :void -end - -class Error < StandardError -end - -def set_log_file(name, level=FFI::NIL) - if level != FFI::NIL then - level = FFI::MemoryPointer::from_string(level) - end - C.extism_log_file(name, level) -end - - -class Plugin - def initialize(wasm, wasi=false, config=nil) - if wasm.class == Hash then - wasm = JSON.generate(wasm) - end - code = FFI::MemoryPointer.new(:char, wasm.bytesize) - code.put_bytes(0, wasm) - @plugin = C.extism_plugin_register(code, wasm.bytesize, wasi) - - if config != nil and @plugin >= 0 then - s = JSON.generate(config) - ptr = FFI::MemoryPointer::from_string(s) - C.extism_plugin_config(@plugin, ptr, s.bytesize) - end +module Extism + module C + extend FFI::Library + ffi_lib "extism" + attach_function :extism_plugin_register, [:pointer, :uint64, :bool], :int32 + attach_function :extism_error, [:int32], :string + attach_function :extism_call, [:int32, :string, :pointer, :uint64], :int32 + attach_function :extism_output_length, [:int32], :uint64 + attach_function :extism_output_get, [:int32, :pointer, :uint64], :void + attach_function :extism_log_file, [:string, :pointer], :void end - def call(name, data) - input = FFI::MemoryPointer::from_string(data) - rc = C.extism_call(@plugin, name, input, data.bytesize) - if rc != 0 then - err = C.extism_error(@plugin) - if err&.empty? then - raise Error.new "extism_call failed" - else raise Error.new err + + class Error < StandardError + end + + def self.set_log_file(name, level=nil) + if level then + level = FFI::MemoryPointer::from_string(level) + end + C.extism_log_file(name, level) + end + + class Plugin + def initialize(wasm, wasi=false, config=nil) + if wasm.class == Hash then + wasm = JSON.generate(wasm) + end + code = FFI::MemoryPointer.new(:char, wasm.bytesize) + code.put_bytes(0, wasm) + @plugin = C.extism_plugin_register(code, wasm.bytesize, wasi) + + if config != nil and @plugin >= 0 then + s = JSON.generate(config) + ptr = FFI::MemoryPointer::from_string(s) + C.extism_plugin_config(@plugin, ptr, s.bytesize) end end - out_len = C.extism_output_length(@plugin) - buf = FFI::MemoryPointer.new(:char, out_len) - C.extism_output_get(@plugin, buf, out_len) - return buf.read_string() + + def call(name, data) + input = FFI::MemoryPointer::from_string(data) + rc = C.extism_call(@plugin, name, input, data.bytesize) + if rc != 0 then + err = C.extism_error(@plugin) + if err&.empty? then + raise Error.new "extism_call failed" + else raise Error.new err + end + end + out_len = C.extism_output_length(@plugin) + buf = FFI::MemoryPointer.new(:char, out_len) + C.extism_output_get(@plugin, buf, out_len) + return buf.read_string() + end end end