From 668b12e7566282f6c0165252e2b2f5432964020a Mon Sep 17 00:00:00 2001 From: dearblue Date: Wed, 24 Nov 2021 23:34:31 +0900 Subject: Check more `MRB_ARGS_NONE()` The `__id__` method implemented in the C function has `MRB_ARGS_NONE()` specified, but it is also effective in the following cases. ```ruby p nil.__id__ opts: 1 rescue p :a p nil.method(:__id__).call 1 rescue p :b p nil.method(:__id__).call opts: 1 rescue p :c p nil.method(:__id__).to_proc.call 1 rescue p :d p nil.method(:__id__).to_proc.call opts: 1 rescue p :e p nil.method(:__id__).unbind.bind_call nil, 1 rescue p :f p nil.method(:__id__).unbind.bind_call nil, opts: 1 rescue p :g p nil.__send__ :__id__, 1 rescue p :h p nil.__send__ :__id__, opts: 1 rescue p :i ``` After applying this patch, all items will output symbols in the same way as CRuby. For this purpose, add `MRB_PROC_NOARG` to `struct RProc::flags`. --- mrbgems/mruby-method/src/method.c | 7 ++++++- mrbgems/mruby-method/test/method.rb | 9 +++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) (limited to 'mrbgems/mruby-method') diff --git a/mrbgems/mruby-method/src/method.c b/mrbgems/mruby-method/src/method.c index b8a55e618..18fcaa03a 100644 --- a/mrbgems/mruby-method/src/method.c +++ b/mrbgems/mruby-method/src/method.c @@ -300,7 +300,12 @@ method_search_vm(mrb_state *mrb, struct RClass **cp, mrb_sym mid) return NULL; if (MRB_METHOD_PROC_P(m)) return MRB_METHOD_PROC(m); - return mrb_proc_new_cfunc(mrb, MRB_METHOD_FUNC(m)); + + struct RProc *proc = mrb_proc_new_cfunc(mrb, MRB_METHOD_FUNC(m)); + if (MRB_METHOD_NOARG_P(m)) { + proc->flags |= MRB_PROC_NOARG; + } + return proc; } static mrb_value diff --git a/mrbgems/mruby-method/test/method.rb b/mrbgems/mruby-method/test/method.rb index ba4d05e3c..9089c96d6 100644 --- a/mrbgems/mruby-method/test/method.rb +++ b/mrbgems/mruby-method/test/method.rb @@ -98,6 +98,9 @@ assert 'Method#call' do }.new assert_raise(LocalJumpError) { i.method(:bar).call } assert_equal 3, i.method(:bar).call { |i| i } + + assert_raise(ArgumentError) { nil.method(:__id__).call nil, 1 } + assert_raise(ArgumentError) { nil.method(:__id__).call nil, opts: 1 } end assert 'Method#call for regression' do @@ -218,6 +221,9 @@ assert 'Method#to_proc' do assert_equal values, o.method(:baz).to_proc.call(1, 2, 3, 4, 5, 6, **{ u: 7, v: 8, s: 9, t: 10 }, &blk) assert_equal values, o.method(:baz).to_proc.call(*[1, 2, 3, 4, 5, 6], u: 7, v: 8, s: 9, t: 10, &blk) assert_equal values, o.method(:baz).to_proc.call(*[1, 2, 3, 4, 5, 6], **{ u: 7, v: 8, s: 9, t: 10 }, &blk) + + assert_raise(ArgumentError) { nil.method(:__id__).to_proc.call nil, 1 } + assert_raise(ArgumentError) { nil.method(:__id__).to_proc.call nil, opts: 1 } end assert 'to_s' do @@ -472,4 +478,7 @@ assert 'UnboundMethod#bind_call' do assert_equal values, m.bind_call(o, *[1, 2, 3, 4, 5, 6], u: 7, v: 8, s: 9, t: 10, &blk) assert_equal values, m.bind_call(o, *[1, 2, 3, 4, 5, 6], **{ u: 7, v: 8, s: 9, t: 10 }, &blk) assert_raise(ArgumentError) { m.bind_call } + + assert_raise(ArgumentError) { BasicObject.instance_method(:__id__).bind_call nil, 1 } + assert_raise(ArgumentError) { BasicObject.instance_method(:__id__).bind_call nil, opts: 1 } end -- cgit v1.2.3