Skip to content

Commit

Permalink
Ref-count the runtime from RubyLandProxy.
Browse files Browse the repository at this point in the history
By storing a reference to the runtime in a long-lived location while
there are live proxies, we can ensure the proxies will be collected
first. Otherwise, when the whole proxy+runtime chain becomes
unreachable, they could be collected in any order... and having the
runtime go away while we still have proxies around makes SpiderMonkey
quite upset.
  • Loading branch information
matthewd committed Sep 19, 2009
1 parent bfb7e4a commit 6074fee
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
6 changes: 5 additions & 1 deletion ext/spidermonkey/ruby_land_proxy.c
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,8 @@ static void finalize(RubyLandProxy* proxy)
{
// remove our GC handle on the JS value
JS_RemoveRootRT(proxy->runtime->js, &(proxy->key));

johnson_runtime_unref(proxy->runtime);
}

free(proxy);
Expand Down Expand Up @@ -519,7 +521,9 @@ VALUE make_ruby_land_proxy(JohnsonRuntime* runtime, jsval value, const char cons

// put the proxy OID in the id map
JCHECK(JS_HashTableAdd(runtime->jsids, (void *)value, (void *)proxy));


johnson_runtime_ref(runtime);

JRETURN_RUBY(proxy);
}
}
Expand Down
27 changes: 27 additions & 0 deletions ext/spidermonkey/runtime.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#include "jroot.h"
#include "ruby_land_proxy.h"

static VALUE live_runtimes;

/*
* call-seq:
* global
Expand Down Expand Up @@ -270,6 +272,8 @@ initialize_native(VALUE self, VALUE UNUSED(options))
JohnsonRuntime* runtime;
Data_Get_Struct(self, JohnsonRuntime, runtime);

runtime->refs = 0;

if ((runtime->js = JS_NewRuntime(0x100000))
&& (runtime->jsids = create_id_hash())
&& (runtime->rbids = create_id_hash()))
Expand Down Expand Up @@ -306,6 +310,26 @@ JSContext* johnson_get_current_context(JohnsonRuntime * runtime)
return context->js;
}

void johnson_runtime_ref(JohnsonRuntime* runtime)
{
runtime->refs++;
if (runtime->refs == 1)
{
VALUE self = (VALUE)JS_GetRuntimePrivate(runtime->js);
rb_hash_aset(live_runtimes, self, Qtrue);
}
}

void johnson_runtime_unref(JohnsonRuntime* runtime)
{
runtime->refs--;
if (runtime->refs == 0)
{
VALUE self = (VALUE)JS_GetRuntimePrivate(runtime->js);
rb_hash_delete(live_runtimes, self);
}
}

static void deallocate(JohnsonRuntime* runtime)
{
// our gc callback can create ruby objects, so disable it
Expand Down Expand Up @@ -334,6 +358,9 @@ void init_Johnson_SpiderMonkey_Runtime(VALUE spidermonkey)
{
VALUE klass = rb_define_class_under(spidermonkey, "Runtime", rb_cObject);

live_runtimes = rb_hash_new();
rb_iv_set(klass, "@live_runtimes", live_runtimes);

rb_define_alloc_func(klass, allocate);
rb_define_private_method(klass, "initialize_native", initialize_native, 1);

Expand Down
5 changes: 5 additions & 0 deletions ext/spidermonkey/runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,14 @@ typedef struct {

JSHashTable *jsids; // jsid -> rbid
JSHashTable *rbids; // rbid -> jsid

int refs;
} JohnsonRuntime;

JSContext* johnson_get_current_context(JohnsonRuntime* runtime);
void init_Johnson_SpiderMonkey_Runtime(VALUE spidermonkey);

void johnson_runtime_ref(JohnsonRuntime* runtime);
void johnson_runtime_unref(JohnsonRuntime* runtime);

#endif

0 comments on commit 6074fee

Please sign in to comment.