Skip to content

Commit

Permalink
Fix rubygem panic code, add test (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
tsmartt authored Aug 2, 2021
1 parent 695f6ac commit 8b5e38e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 18 deletions.
36 changes: 18 additions & 18 deletions wasm-thumbnail-rb/lib/wasm/thumbnail/rb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,6 @@ def self.register_panic(_msg_ptr = nil, _msg_len = nil, _file_ptr = nil, _file_l
puts("WASM panicked")
end

WASMStore = Wasmer::Store.new
WASMImportObject = Wasmer::ImportObject.new
WASMImportObject.register(
"env",
register_panic: Wasmer::Function.new(
WASMStore,
:register_panic,
Wasmer::FunctionType.new([Wasmer::Type::I32,
Wasmer::Type::I32,
Wasmer::Type::I32,
Wasmer::Type::I32,
Wasmer::Type::I32,
Wasmer::Type::I32], [])
)
)

def self.resize_and_pad_with_header(file_bytes:, width:, height:, size:)
# Let's compile the module to be able to execute it!
wasm_instance = Wasm::Thumbnail::Rb::GetWasmInstance.call
Expand Down Expand Up @@ -82,10 +66,26 @@ def self.resize_and_pad(file_bytes:, width:, height:, size:)
# Return an instance so you don't have to constantly compile
class GetWasmInstance
def self.call
store = Wasmer::Store.new
import_object = Wasmer::ImportObject.new
import_object.register(
"env",
register_panic: Wasmer::Function.new(
store,
->(*args) { Wasm::Thumbnail::Rb.register_panic(*args) },
Wasmer::FunctionType.new([Wasmer::Type::I32,
Wasmer::Type::I32,
Wasmer::Type::I32,
Wasmer::Type::I32,
Wasmer::Type::I32,
Wasmer::Type::I32], [])
)
)

# Let's compile the module to be able to execute it!
Wasmer::Instance.new(
Wasmer::Module.new(WASMStore, IO.read("#{__dir__}/rb/data/wasm_thumbnail.wasm", mode: "rb")),
WASMImportObject
Wasmer::Module.new(store, IO.read("#{__dir__}/rb/data/wasm_thumbnail.wasm", mode: "rb")),
import_object
)
end
end
Expand Down
11 changes: 11 additions & 0 deletions wasm-thumbnail-rb/test/wasm/thumbnail/rb_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,15 @@ def test_it_does_something_useful
size: 250_000)
puts "Image resized and padded to size #{image.length}"
end

def test_it_calls_panic_if_image_too_big
file_bytes = File.binread("#{__dir__}/brave.png").unpack("C*")
exception = assert_raises RuntimeError do
Wasm::Thumbnail::Rb.resize_and_pad(file_bytes: file_bytes,
width: 100,
height: 200,
size: 5)
end
assert_equal(exception.message, "Error processing the image.")
end
end

0 comments on commit 8b5e38e

Please sign in to comment.