Files
extism/ruby/bin/rdbg
Benjamin Eckel 2bf5ac75c0 feat(ruby): Host functions and clean up FFI code (#442)
Adds support for host functions and cleans up some of the FFI code.


## API

To make a host function, you can pass a proc to `Function::new`:

```ruby
func = proc do |current_plugin, inputs, outputs, user_data|
  input = current_plugin.input_as_bytes(inputs.first)
  current_plugin.return_string(outputs.first, "#{input} #{user_data}")
end
f = Extism::Function.new('transform_string', [Extism::ValType::I64], [Extism::ValType::I64], func, 'My User Data')
plugin = Extism::Plugin.new(host_manifest, [f], true)
result = plugin.call('reflect_string', 'Hello, World!')
assert_equal result, 'Hello, World! My User Data'
```

If your function is in a module or a class, you can use
`method(name).to_proc`. Example:

```ruby
module Test
  def self.my_function(current_plugin, inputs, outputs, user_data)
    input = current_plugin.input_as_bytes(inputs.first)
    current_plugin.return_string(outputs.first, "#{input} #{user_data}")
  end
end

func = Test.method(:my_function).to_proc
f = Extism::Function.new('my_function', [Extism::ValType::I64], [Extism::ValType::I64], func, 'My User Data')
```


`current_plugin` is of the type CurrentPlugin which has some helpful
methods:

* `CurrentPlugin#memory_at_offset(int)` returns a `Memory` object given
a memory pointer
* `CurrentPlugin#free(Memory)` frees the memory
* `CurrentPlugin#alloc(int)` allocates new memory and returns a `Memory`
* `CurrentPlugin#input_as_bytes(Value)` returns the bytes for the given
input param
* `CurrentPlugin#return_bytes(Value, Array)` Sets the array of bytes to
the return for the given output value
* `CurrentPlugin#input_as_bytes(Value, String)` Sets the string to the
return for the given output value
2023-09-11 18:21:11 -05:00

28 lines
736 B
Ruby
Executable File

#!/usr/bin/env ruby
# frozen_string_literal: true
#
# This file was generated by Bundler.
#
# The application 'rdbg' is installed as part of a gem, and
# this file is here to facilitate running it.
#
ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../Gemfile", __dir__)
bundle_binstub = File.expand_path("bundle", __dir__)
if File.file?(bundle_binstub)
if File.read(bundle_binstub, 300) =~ /This file was generated by Bundler/
load(bundle_binstub)
else
abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run.
Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.")
end
end
require "rubygems"
require "bundler/setup"
load Gem.bin_path("debug", "rdbg")