Skip to content

Commit fd1855c

Browse files
committed
Merge branch 'pr/9'
2 parents e187a67 + e5f1ad3 commit fd1855c

File tree

3 files changed

+35
-4
lines changed

3 files changed

+35
-4
lines changed

src/instance.rs

+23-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,11 @@ impl ExportedFunctions {
2727
Self { instance }
2828
}
2929

30+
/// Check that an exported function exists.
31+
pub fn respond_to_missing(&self, method_name: &str) -> bool {
32+
self.instance.dyn_func(method_name).is_ok()
33+
}
34+
3035
/// Call an exported function on the given WebAssembly instance.
3136
pub fn method_missing(
3237
&self,
@@ -164,6 +169,22 @@ wrappable_struct!(
164169

165170
class!(RubyExportedFunctions);
166171

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

src/lib.rs

+6
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ 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 `respond_to_missing?` method.
38+
itself.def(
39+
"respond_to_missing?",
40+
instance::ruby_exported_functions_method_exists,
41+
);
42+
3743
// Declare the `method_missing` method.
3844
itself.def(
3945
"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)