Skip to content

Commit 4969bc6

Browse files
add JS::Object#method_missing as a shorthand
1 parent 048b863 commit 4969bc6

File tree

4 files changed

+22
-2
lines changed

4 files changed

+22
-2
lines changed

ext/js/js-core.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ static VALUE _rb_js_global_this(VALUE _) {
164164
* JS.global[:console][:log]
165165
*/
166166
static VALUE _rb_js_obj_aref(VALUE obj, VALUE key) {
167-
struct jsvalue *p = check_jsvalue(obj);
167+
struct jsvalue *p = check_jsvalue(_rb_js_try_convert(rb_mJS, obj));
168168
rb_js_abi_host_string_t key_abi_str;
169169
key = rb_obj_as_string(key);
170170
rstring_to_abi_string(key, &key_abi_str);

ext/js/lib/js.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
require "js.so"
2+
3+
class JS::Object
4+
def method_missing(name, *args)
5+
self.call(name, *args)
6+
end
7+
end

packages/npm-packages/ruby-wasm-wasi/test/init.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import path from "path";
33
import { DefaultRubyVM } from "../dist/node.cjs";
44

55
const rubyModule = (async () => {
6-
const binary = await fs.readFile(path.join(__dirname, "./../dist/ruby.wasm"));
6+
const binary = await fs.readFile(path.join(__dirname, "./../dist/ruby+stdlib.wasm"));
77
return await WebAssembly.compile(binary.buffer);
88
})();
99

packages/npm-packages/ruby-wasm-wasi/test/js_from_rb.test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,19 @@ describe("Manipulation of JS from Ruby", () => {
7474
expect(vm.eval(inspect_result).toString()).toBe(String(eval(value)));
7575
});
7676

77+
test.each([
78+
{ self: `24`, calee: "toString", args: [], result: "24" },
79+
{ self: `"hello"`, calee: "charAt", args: [4], result: "o" },
80+
])("JS::Object#method_missing (%s)", async (props) => {
81+
const vm = await initRubyVM();
82+
const result = `
83+
require "js"
84+
obj = JS.eval('return ${props.self}')
85+
obj.${props.calee}(${props.args.join(", ")})
86+
`;
87+
expect(vm.eval(result).toString()).toBe(props.result);
88+
});
89+
7790
test.each([
7891
{ expr: "JS.global[:Object]", result: Object },
7992
{ expr: "JS.global[:Object][:keys]", result: Object.keys },

0 commit comments

Comments
 (0)