Skip to content

Commit 5d058af

Browse files
committed
Implemented rb_eval_string_protect and added specs.
1 parent 6d3a365 commit 5d058af

File tree

4 files changed

+30
-1
lines changed

4 files changed

+30
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
Bug fixes:
44

55
* Fixed `BigDecimal#dup` so it now just returns the receiver, per Ruby 2.5+ semantics (#1680).
6+
* Implemented `rb_eval_string_protect`.
67

78
# 1.0 RC 17
89

spec/ruby/optional/capi/ext/kernel_spec.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,17 @@ static VALUE kernel_spec_rb_protect_yield(VALUE self, VALUE obj, VALUE ary) {
175175
return res;
176176
}
177177

178+
static VALUE kernel_spec_rb_eval_string_protect(VALUE self, VALUE str, VALUE ary) {
179+
int status = 0;
180+
VALUE res = rb_eval_string_protect(RSTRING_PTR(str), &status);
181+
rb_ary_store(ary, 0, INT2NUM(23));
182+
rb_ary_store(ary, 1, res);
183+
if (status) {
184+
rb_jump_tag(status);
185+
}
186+
return res;
187+
}
188+
178189
VALUE kernel_spec_rb_sys_fail(VALUE self, VALUE msg) {
179190
errno = 1;
180191
if(msg == Qnil) {
@@ -301,6 +312,7 @@ void Init_kernel_spec(void) {
301312
rb_define_method(cls, "rb_rescue", kernel_spec_rb_rescue, 4);
302313
rb_define_method(cls, "rb_rescue2", kernel_spec_rb_rescue2, -1);
303314
rb_define_method(cls, "rb_protect_yield", kernel_spec_rb_protect_yield, 2);
315+
rb_define_method(cls, "rb_eval_string_protect", kernel_spec_rb_eval_string_protect, 2);
304316
rb_define_method(cls, "rb_catch", kernel_spec_rb_catch, 2);
305317
rb_define_method(cls, "rb_catch_obj", kernel_spec_rb_catch_obj, 2);
306318
rb_define_method(cls, "rb_sys_fail", kernel_spec_rb_sys_fail, 1);

spec/ruby/optional/capi/kernel_spec.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,22 @@
292292
end
293293
end
294294

295+
describe "rb_eval_string_protect" do
296+
it "will evaluate the given string" do
297+
proof = []
298+
res = @s.rb_eval_string_protect('1 + 7', proof)
299+
proof.should == [23, 8]
300+
end
301+
302+
it "will allow cleanup code to be run when an exception is raised" do
303+
proof = []
304+
lambda do
305+
@s.rb_eval_string_protect('raise RuntimeError', proof)
306+
end.should raise_error(RuntimeError)
307+
proof.should == [23, nil]
308+
end
309+
end
310+
295311
describe "rb_rescue" do
296312
before :each do
297313
@proc = lambda { |x| x }

src/main/c/cext/ruby.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4663,7 +4663,7 @@ VALUE rb_check_symbol(volatile VALUE *namep) {
46634663
}
46644664

46654665
VALUE rb_eval_string_protect(const char *str, int *state) {
4666-
rb_tr_error("rb_eval_string_protect not implemented");
4666+
return rb_protect((VALUE (*)(VALUE))rb_eval_string, (VALUE)str, state);
46674667
}
46684668

46694669
VALUE rb_eval_string_wrap(const char *str, int *state) {

0 commit comments

Comments
 (0)