Skip to content
This repository has been archived by the owner on Aug 18, 2018. It is now read-only.

Commit

Permalink
Protect rb_yield calls so we can clean up our roots.
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewd committed Apr 28, 2008
1 parent a6dac52 commit 1d2b61f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
16 changes: 14 additions & 2 deletions ext/spidermonkey/ruby_land_proxy.c
Expand Up @@ -163,8 +163,14 @@ each(VALUE self)
for (i = 0; i < length; ++i)
{
jsval element;
int state;
JCHECK(JS_GetElement(proxy->context->js, value, (signed) i, &element));
rb_yield(convert_to_ruby(proxy->context, element));
rb_protect(rb_yield, convert_to_ruby(proxy->context, element), &state);
if (state)
{
REMOVE_JROOTS;
rb_jump_tag(state);
}
}
}
else
Expand Down Expand Up @@ -200,7 +206,13 @@ each(VALUE self)
VALUE key = convert_to_ruby(proxy->context, js_key);
VALUE value = convert_to_ruby(proxy->context, js_value);

rb_yield(rb_ary_new3(2, key, value));
int state;
rb_protect(rb_yield, rb_ary_new3(2, key, value), &state);
if (state)
{
REMOVE_JROOTS;
rb_jump_tag(state);
}

JUNROOT(js_value);
JUNROOT(js_key);
Expand Down
13 changes: 13 additions & 0 deletions test/johnson/spidermonkey/ruby_land_proxy_test.rb
Expand Up @@ -147,6 +147,19 @@ def test_supports_each_on_things_that_arent_arrays
assert_equal({ 'foo' => 'fooval', 'bar' => 'barval', 0 => 42 }, values)
end

def test_each_passes_an_exception
proxy = @context.evaluate("x = { foo: 'fooval', bar: 'barval' }; x[0] = 42; x")
values = {}

assert_raise(RuntimeError) do
proxy.each do |k, v|
values[k] = v
raise "splat" if values.keys.size == 2
end
end
assert_equal({ 'foo' => 'fooval', 'bar' => 'barval' }, values)
end

def test_is_enumerable
proxy = @context.evaluate("[1, 2, 3]")
assert_kind_of(Enumerable, proxy)
Expand Down

0 comments on commit 1d2b61f

Please sign in to comment.