Skip to content

Commit

Permalink
fix: ensure extism functions are freed (#17)
Browse files Browse the repository at this point in the history
Fixes #12 using a similar approach to how plugins are cleaned up using
`ObjectSpace.define_finalizer`
  • Loading branch information
zshipko authored Nov 3, 2023
1 parent a45f5c6 commit afa5932
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lib/extism.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@ def self.set_log_file(name, level = nil)
end
}


$FUNCTIONS = {}
$FREE_FUNCTION = proc { |ptr|
x = $FUNCTIONS[ptr]
unless x.nil?
LibExtism.extism_function_free(x[:function])
$FUNCTIONS.delete(ptr)
end
}

# Represents a "block" of memory in Extism.
# This memory is in the communication buffer b/w the
# guest in the host and technically lives in host memory.
Expand Down
3 changes: 3 additions & 0 deletions lib/extism/wasm.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ def pointer
returns = LibExtism.from_int_array(@returns)
@_pointer = LibExtism.extism_function_new(@name, args, @params.length, returns, @returns.length, c_func, free,
nil)
$FUNCTIONS[object_id] = { function: @_pointer}
ObjectSpace.define_finalizer(self, $FREE_FUNCTION)
return @_pointer
end

def c_func
Expand Down

0 comments on commit afa5932

Please sign in to comment.