Skip to content
Browse files

Use direct pointers in the hash tables etc, and just Root values we n…

…eed.
  • Loading branch information...
1 parent 400ecbd commit c6ea5997c382469f443bf51393d7ee3f6fb25d03 @matthewd matthewd committed Jun 4, 2008
View
17 ext/spidermonkey/js_land_proxy.c
@@ -114,7 +114,7 @@ static bool const_p(VALUE self, char* name)
static bool global_p(char* name)
{
- return rb_ary_includes(rb_f_global_variables(), rb_str_new2(name));
+ return *name == '$' && rb_ary_includes(rb_f_global_variables(), rb_str_new2(name));
}
static bool method_p(VALUE self, char* name)
@@ -549,7 +549,7 @@ static void finalize(JSContext* js_context, JSObject* obj)
JS_GET_CLASS(context->js, obj), NULL);
// remove the proxy OID from the id map
- JS_HashTableRemove(runtime->rbids, (void *)rb_obj_id(self));
+ JS_HashTableRemove(runtime->rbids, (void *)self);
// free up the ruby value for GC
call_ruby_from_js(runtime, NULL, ruby_context, rb_intern("remove_gcthing"), 1, self);
@@ -558,15 +558,15 @@ static void finalize(JSContext* js_context, JSObject* obj)
JSBool make_js_land_proxy(JohnsonRuntime* runtime, VALUE value, jsval* retval)
{
- JSContext * context = johnson_get_current_context(runtime);
- jsid id = (jsid)JS_HashTableLookup(runtime->rbids, (void *)rb_obj_id(value));
+ *retval = (jsval)JS_HashTableLookup(runtime->rbids, (void *)value);
- if (id)
+ if (*retval)
{
- return JS_IdToValue(context, id, retval);
+ return JS_TRUE;
}
else
{
+ JSContext * context = johnson_get_current_context(runtime);
PREPARE_JROOTS(context, 1);
JSObject *jsobj;
@@ -597,11 +597,8 @@ JSBool make_js_land_proxy(JohnsonRuntime* runtime, VALUE value, jsval* retval)
*retval = OBJECT_TO_JSVAL(jsobj);
- jsval newid;
- JCHECK(JS_ValueToId(context, *retval, &newid));
-
// put the proxy OID in the id map
- JCHECK(JS_HashTableAdd(runtime->rbids, (void *)rb_obj_id(value), (void *)newid));
+ JCHECK(JS_HashTableAdd(runtime->rbids, (void *)value, (void *)(*retval)));
// root the ruby value for GC
VALUE ruby_context = (VALUE)JS_GetContextPrivate(context);
View
54 ext/spidermonkey/ruby_land_proxy.c
@@ -9,23 +9,10 @@ DEFINE_RUBY_WRAPPER(rb_yield, rb_yield, ARGLIST1(v))
static VALUE proxy_class = Qnil;
-static JSBool get_jsval_for_proxy(RubyLandProxy* proxy, jsval* jv)
+static inline JSBool get_jsval_for_proxy(RubyLandProxy* proxy, jsval* jv)
{
- JSContext * context = johnson_get_current_context(proxy->runtime);
- PREPARE_JROOTS(context, 0);
-
- // FIXME: this is totally lame
- char global_key[10];
- sprintf(global_key, "%x", (int)proxy->runtime->global);
-
- if (0 == strcmp(global_key, proxy->key))
- {
- *jv = OBJECT_TO_JSVAL(proxy->runtime->global);
- JRETURN;
- }
-
- JCHECK(JS_GetProperty(context, proxy->runtime->gcthings, proxy->key, jv));
- JRETURN;
+ *jv = (jsval)(proxy->key);
+ return JS_TRUE;
}
static VALUE call_js_function_value(JohnsonRuntime* runtime, jsval target, jsval function, int argc, VALUE* argv)
@@ -454,23 +441,21 @@ static VALUE to_s(VALUE self)
static void finalize(RubyLandProxy* proxy)
{
- JSContext * context = johnson_get_current_context(proxy->runtime);
- PREPARE_RUBY_JROOTS(context, 0);
- jsval proxy_value;
- JCHECK_RUBY(get_jsval_for_proxy(proxy, &proxy_value));
-
// could get finalized after the context has been freed
if (proxy->runtime && proxy->runtime->jsids)
{
// remove this proxy from the OID map
+ jsval proxy_value;
+ get_jsval_for_proxy(proxy, &proxy_value);
JS_HashTableRemove(proxy->runtime->jsids, (void *)proxy_value);
-
+ }
+
+ if (proxy->runtime)
+ {
// remove our GC handle on the JS value
- JS_DeleteProperty(context, proxy->runtime->gcthings, proxy->key);
-
- proxy->runtime = 0;
+ JS_RemoveRootRT(proxy->runtime->js, &(proxy->key));
}
-
+
free(proxy);
}
@@ -496,32 +481,31 @@ JSBool unwrap_ruby_land_proxy(JohnsonRuntime* runtime, VALUE wrapped, jsval* ret
VALUE make_ruby_land_proxy(JohnsonRuntime* runtime, jsval value)
{
VALUE id = (VALUE)JS_HashTableLookup(runtime->jsids, (void *)value);
- JSContext * context = johnson_get_current_context(runtime);
if (id)
{
// if we already have a proxy, return it
- return rb_funcall(rb_const_get(rb_cObject,
- rb_intern("ObjectSpace")), rb_intern("_id2ref"), 1, id);
+ return id;
}
else
{
// otherwise make one and cache it
RubyLandProxy* our_proxy;
VALUE proxy = Data_Make_Struct(proxy_class, RubyLandProxy, 0, finalize, our_proxy);
+ JSContext * context = johnson_get_current_context(runtime);
+
PREPARE_RUBY_JROOTS(context, 1);
JROOT(value);
- // root the value for JS GC and lookups
- sprintf(our_proxy->key, "%x", (int)value);
-
- JCHECK(JS_SetProperty(context, runtime->gcthings, our_proxy->key, &value));
-
our_proxy->runtime = runtime;
+ our_proxy->key = (void *)value;
+
+ // root the value for JS GC and lookups
+ JCHECK(JS_AddNamedRootRT(runtime->js, &(our_proxy->key), "RubyLandProxy"));
// put the proxy OID in the id map
- JCHECK(JS_HashTableAdd(runtime->jsids, (void *)value, (void *)rb_obj_id(proxy)));
+ JCHECK(JS_HashTableAdd(runtime->jsids, (void *)value, (void *)proxy));
JRETURN_RUBY(proxy);
}
View
2 ext/spidermonkey/ruby_land_proxy.h
@@ -5,7 +5,7 @@
#include "runtime.h"
typedef struct {
- char key[10];
+ void* key;
JohnsonRuntime* runtime;
} RubyLandProxy;
View
13 ext/spidermonkey/runtime.c
@@ -134,9 +134,6 @@ initialize_native(VALUE self, VALUE UNUSED(options))
JohnsonRuntime* runtime;
Data_Get_Struct(self, JohnsonRuntime, runtime);
- bool global_rooted_p = false;
- bool gcthings_rooted_p = false;
-
if ((runtime->js = JS_NewRuntime(0x100000))
&& (runtime->jsids = create_id_hash())
&& (runtime->rbids = create_id_hash())
@@ -147,17 +144,11 @@ initialize_native(VALUE self, VALUE UNUSED(options))
JSContext* context = johnson_get_current_context(runtime);
if(
- (runtime->gcthings = JS_NewObject(context, NULL, 0, 0))
- &&(gcthings_rooted_p = JS_AddNamedRoot(context, &(runtime->gcthings), "runtime->gcthings"))
- &&(runtime->global = JS_GetGlobalObject(context))
- &&(global_rooted_p = JS_AddNamedRoot(context, &(runtime->global), "runtime->global"))
+ (runtime->global = JS_GetGlobalObject(context))
+ && (JS_AddNamedRoot(context, &(runtime->global), "runtime->global"))
) {
return self;
}
- if (global_rooted_p)
- JS_RemoveRoot(context, &(runtime->global));
- if (gcthings_rooted_p)
- JS_RemoveRoot(context, &(runtime->gcthings));
}
View
1 ext/spidermonkey/runtime.h
@@ -17,7 +17,6 @@ typedef struct {
JSHashTable *jsids; // jsid -> rbid
JSHashTable *rbids; // rbid -> jsid
- JSObject *gcthings;
} JohnsonRuntime;
JSContext* johnson_get_current_context(JohnsonRuntime* runtime);

0 comments on commit c6ea599

Please sign in to comment.
Something went wrong with that request. Please try again.