feat(ruby-sdk): create Extism module / namespace (#5)

I noticed when calling set_log_file that it's on the top-level namespace.
And so is the rest of the gem. It's not a good idea to do this as it
pollutes the user's application. Best practice is to wrap all code in a
module which by convention is the gem name.

I was also getting `Uninitialized constant FFI:NIL` and in the
set_log_file method and I could not tell why it was needed. I changed it
to a regular ruby nil. If it's needed for some reason I'll change it
back or move to another PR.
This commit is contained in:
Benjamin Eckel
2022-09-04 18:05:10 -05:00
committed by GitHub
parent cede8e179e
commit ffa01403a3
2 changed files with 51 additions and 49 deletions

View File

@@ -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']

View File

@@ -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