-
Notifications
You must be signed in to change notification settings - Fork 185
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[GR-26849] Raise a RuntimeError when calling a method needing caller …
…data directly from a foreign language * Instead of an internal error later on.
- Loading branch information
Showing
4 changed files
with
86 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
* Copyright (c) 2021 Oracle and/or its affiliates. All rights reserved. This | ||
* code is released under a tri EPL/GPL/LGPL license. You can use it, | ||
* redistribute it and/or modify it under the terms of the: | ||
* | ||
* Eclipse Public License version 2.0, or | ||
* GNU General Public License version 2, or | ||
* GNU Lesser General Public License version 2.1. | ||
*/ | ||
#include "ruby.h" | ||
#include "ruby/thread.h" | ||
#include "rubyspec.h" | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
static VALUE call_binding(VALUE self) { | ||
return rb_tr_wrap(polyglot_invoke(rb_tr_unwrap(self), "binding")); | ||
} | ||
|
||
static VALUE call_binding_rb_funcall(VALUE self) { | ||
return rb_funcall(self, rb_intern("binding"), 0); | ||
} | ||
|
||
void Init_truffleruby_foreign_caller_spec(void) { | ||
VALUE cls = rb_define_class("CApiTruffleRubyForeignCallerSpecs", rb_cObject); | ||
rb_define_method(cls, "call_binding", call_binding, 0); | ||
rb_define_method(cls, "call_binding_rb_funcall", call_binding_rb_funcall, 0); | ||
} | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# Copyright (c) 2021 Oracle and/or its affiliates. All rights reserved. This | ||
# code is released under a tri EPL/GPL/LGPL license. You can use it, | ||
# redistribute it and/or modify it under the terms of the: | ||
# | ||
# Eclipse Public License version 2.0, or | ||
# GNU General Public License version 2, or | ||
# GNU Lesser General Public License version 2.1. | ||
|
||
require_relative '../../ruby/optional/capi/spec_helper' | ||
|
||
load_extension("truffleruby_foreign_caller") | ||
|
||
describe "Calling a method needing the caller frame" do | ||
before :each do | ||
@s = CApiTruffleRubyForeignCallerSpecs.new | ||
end | ||
|
||
it "directly from C code raises a RuntimeError" do | ||
-> { | ||
@s.call_binding | ||
}.should raise_error(RuntimeError, 'Cannot call Ruby method which needs caller data directly in a foreign language') | ||
end | ||
|
||
it "using rb_funcall() yields the Binding of rb_funcall()" do | ||
caller_variable = nil | ||
binding = @s.call_binding_rb_funcall | ||
binding.should be_kind_of(Binding) | ||
|
||
# On CRuby it would instead return the Binding of the caller Ruby frame | ||
binding.local_variables.should.include?(:args) | ||
binding.local_variables.should_not.include?(:caller_variable) | ||
|
||
caller_variable.should == nil | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters