Skip to content

Commit 93526bc

Browse files
committed
Try #9:
2 parents e187a67 + 30f8bbb commit 93526bc

File tree

3 files changed

+30
-5
lines changed

3 files changed

+30
-5
lines changed

src/instance.rs

+20-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ use rutie::{
88
rubysys::{class, value::ValueType},
99
types::{Argc, Value},
1010
util::str_to_cstring,
11-
wrappable_struct, AnyException, AnyObject, Array, Exception, Fixnum, Float, Module, NilClass,
12-
Object, RString, Symbol,
11+
wrappable_struct, AnyException, AnyObject, Array, Boolean, Exception, Fixnum, Float, Module,
12+
NilClass, Object, RString, Symbol,
1313
};
1414
use std::{mem, rc::Rc};
1515
use wasmer_runtime::{self as runtime, imports, Export};
@@ -27,6 +27,10 @@ impl ExportedFunctions {
2727
Self { instance }
2828
}
2929

30+
pub fn respond_to_missing(&self, method_name: &str) -> bool {
31+
self.instance.dyn_func(method_name).is_ok()
32+
}
33+
3034
/// Call an exported function on the given WebAssembly instance.
3135
pub fn method_missing(
3236
&self,
@@ -164,6 +168,20 @@ wrappable_struct!(
164168

165169
class!(RubyExportedFunctions);
166170

171+
#[rustfmt::skip]
172+
methods!(
173+
RubyExportedFunctions,
174+
itself,
175+
176+
fn ruby_exported_functions_method_exists(symbol: Symbol, _include_private: Boolean) -> Boolean {
177+
unwrap_or_raise(|| {
178+
let symbol = symbol?;
179+
let instance = itself.get_data(&*EXPORTED_FUNCTIONS_WRAPPER);
180+
Ok(Boolean::new(instance.respond_to_missing(symbol.to_str())))
181+
})
182+
}
183+
);
184+
167185
/// Glue code to call the `ExportedFunctions.method_missing` method.
168186
pub extern "C" fn ruby_exported_functions_method_missing(
169187
argc: Argc,

src/lib.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@ pub extern "C" fn Init_wasmer() {
3434
wasmer_module
3535
.define_nested_class("ExportedFunctions", Some(&exported_functions_data_class))
3636
.define(|itself| {
37-
// Declare the `method_missing` method.
37+
itself.def(
38+
"respond_to_missing?",
39+
instance::ruby_exported_functions_method_exists,
40+
);
3841
itself.def(
3942
"method_missing",
4043
instance::ruby_exported_functions_method_missing,

tests/instance_test.rb

+6-2
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,16 @@ def test_invalid_module
3636
end
3737

3838
def test_basic_sum
39-
assert_equal 3, Wasmer::Instance.new(self.bytes).exports.sum(1, 2)
39+
exports = Wasmer::Instance.new(self.bytes).exports
40+
assert exports.respond_to?(:sum)
41+
assert_equal 3, exports.sum(1, 2)
4042
end
4143

4244
def test_call_unknown_function
45+
exports = Wasmer::Instance.new(self.bytes).exports
46+
assert !exports.respond_to?(:foo)
4347
error = assert_raises(RuntimeError) {
44-
Wasmer::Instance.new(self.bytes).exports.foo
48+
exports.foo
4549
}
4650
assert_equal "Function `foo` does not exist.", error.message
4751
end

0 commit comments

Comments
 (0)