public
Rubygem
Description: Johnson wraps JavaScript in a loving Ruby embrace.
Homepage: http://github.com/jbarnette/johnson/wikis
Clone URL: git://github.com/jbarnette/johnson.git
Search Repo:
Use direct pointers in the hash tables etc, and just Root values we need.
matthewd (author)
Tue Jun 03 21:48:50 -0700 2008
commit  c6ea5997c382469f443bf51393d7ee3f6fb25d03
tree    f0e012e55efce4d758c5ca2ddc9fcd48fc105abc
parent  400ecbd97a3eb341ee02d84092c872294535be7f
...
114
115
116
117
 
118
119
120
...
549
550
551
552
 
553
554
555
...
558
559
560
561
562
 
563
564
 
565
566
 
567
568
569
 
570
571
572
...
597
598
599
600
601
602
603
604
 
605
606
607
...
114
115
116
 
117
118
119
120
...
549
550
551
 
552
553
554
555
...
558
559
560
 
 
561
562
 
563
564
 
565
566
567
568
569
570
571
572
...
597
598
599
 
 
 
600
 
601
602
603
604
0
@@ -114,7 +114,7 @@ static bool const_p(VALUE self, char* name)
0
 
0
 static bool global_p(char* name)
0
 {
0
- return rb_ary_includes(rb_f_global_variables(), rb_str_new2(name));
0
+ return *name == '$' && rb_ary_includes(rb_f_global_variables(), rb_str_new2(name));
0
 }
0
 
0
 static bool method_p(VALUE self, char* name)
0
@@ -549,7 +549,7 @@ static void finalize(JSContext* js_context, JSObject* obj)
0
             JS_GET_CLASS(context->js, obj), NULL);
0
     
0
     // remove the proxy OID from the id map
0
- JS_HashTableRemove(runtime->rbids, (void *)rb_obj_id(self));
0
+ JS_HashTableRemove(runtime->rbids, (void *)self);
0
     
0
     // free up the ruby value for GC
0
     call_ruby_from_js(runtime, NULL, ruby_context, rb_intern("remove_gcthing"), 1, self);
0
@@ -558,15 +558,15 @@ static void finalize(JSContext* js_context, JSObject* obj)
0
 
0
 JSBool make_js_land_proxy(JohnsonRuntime* runtime, VALUE value, jsval* retval)
0
 {
0
- JSContext * context = johnson_get_current_context(runtime);
0
- jsid id = (jsid)JS_HashTableLookup(runtime->rbids, (void *)rb_obj_id(value));
0
+ *retval = (jsval)JS_HashTableLookup(runtime->rbids, (void *)value);
0
   
0
- if (id)
0
+ if (*retval)
0
   {
0
- return JS_IdToValue(context, id, retval);
0
+ return JS_TRUE;
0
   }
0
   else
0
   {
0
+ JSContext * context = johnson_get_current_context(runtime);
0
     PREPARE_JROOTS(context, 1);
0
 
0
     JSObject *jsobj;
0
@@ -597,11 +597,8 @@ JSBool make_js_land_proxy(JohnsonRuntime* runtime, VALUE value, jsval* retval)
0
 
0
     *retval = OBJECT_TO_JSVAL(jsobj);
0
 
0
- jsval newid;
0
- JCHECK(JS_ValueToId(context, *retval, &newid));
0
-
0
     // put the proxy OID in the id map
0
- JCHECK(JS_HashTableAdd(runtime->rbids, (void *)rb_obj_id(value), (void *)newid));
0
+ JCHECK(JS_HashTableAdd(runtime->rbids, (void *)value, (void *)(*retval)));
0
     
0
     // root the ruby value for GC
0
     VALUE ruby_context = (VALUE)JS_GetContextPrivate(context);
...
9
10
11
12
 
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
 
 
29
30
31
...
454
455
456
457
458
459
460
461
462
463
464
465
 
 
466
467
 
 
 
 
468
469
470
471
 
472
473
 
474
475
476
...
496
497
498
499
500
501
502
503
504
505
 
506
507
508
...
510
511
512
 
 
513
514
515
516
517
518
519
520
521
 
 
 
 
522
523
524
 
525
526
527
...
9
10
11
 
12
13
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
15
16
17
18
...
441
442
443
 
 
 
 
 
444
445
446
447
448
449
450
 
451
452
453
454
455
 
 
 
456
457
 
458
459
460
461
...
481
482
483
 
484
485
486
487
 
 
488
489
490
491
...
493
494
495
496
497
498
499
500
 
 
 
 
 
501
502
503
504
505
506
507
 
508
509
510
511
0
@@ -9,23 +9,10 @@ DEFINE_RUBY_WRAPPER(rb_yield, rb_yield, ARGLIST1(v))
0
 
0
 static VALUE proxy_class = Qnil;
0
 
0
-static JSBool get_jsval_for_proxy(RubyLandProxy* proxy, jsval* jv)
0
+static inline JSBool get_jsval_for_proxy(RubyLandProxy* proxy, jsval* jv)
0
 {
0
- JSContext * context = johnson_get_current_context(proxy->runtime);
0
- PREPARE_JROOTS(context, 0);
0
-
0
- // FIXME: this is totally lame
0
- char global_key[10];
0
- sprintf(global_key, "%x", (int)proxy->runtime->global);
0
-
0
- if (0 == strcmp(global_key, proxy->key))
0
- {
0
- *jv = OBJECT_TO_JSVAL(proxy->runtime->global);
0
- JRETURN;
0
- }
0
-
0
- JCHECK(JS_GetProperty(context, proxy->runtime->gcthings, proxy->key, jv));
0
- JRETURN;
0
+ *jv = (jsval)(proxy->key);
0
+ return JS_TRUE;
0
 }
0
 
0
 static VALUE call_js_function_value(JohnsonRuntime* runtime, jsval target, jsval function, int argc, VALUE* argv)
0
@@ -454,23 +441,21 @@ static VALUE to_s(VALUE self)
0
 
0
 static void finalize(RubyLandProxy* proxy)
0
 {
0
- JSContext * context = johnson_get_current_context(proxy->runtime);
0
- PREPARE_RUBY_JROOTS(context, 0);
0
- jsval proxy_value;
0
- JCHECK_RUBY(get_jsval_for_proxy(proxy, &proxy_value));
0
-
0
   // could get finalized after the context has been freed
0
   if (proxy->runtime && proxy->runtime->jsids)
0
   {
0
     // remove this proxy from the OID map
0
+ jsval proxy_value;
0
+ get_jsval_for_proxy(proxy, &proxy_value);
0
     JS_HashTableRemove(proxy->runtime->jsids, (void *)proxy_value);
0
-
0
+ }
0
+
0
+ if (proxy->runtime)
0
+ {
0
     // remove our GC handle on the JS value
0
- JS_DeleteProperty(context, proxy->runtime->gcthings, proxy->key);
0
-
0
- proxy->runtime = 0;
0
+ JS_RemoveRootRT(proxy->runtime->js, &(proxy->key));
0
   }
0
-
0
+
0
   free(proxy);
0
 }
0
 
0
@@ -496,13 +481,11 @@ JSBool unwrap_ruby_land_proxy(JohnsonRuntime* runtime, VALUE wrapped, jsval* ret
0
 VALUE make_ruby_land_proxy(JohnsonRuntime* runtime, jsval value)
0
 {
0
   VALUE id = (VALUE)JS_HashTableLookup(runtime->jsids, (void *)value);
0
- JSContext * context = johnson_get_current_context(runtime);
0
   
0
   if (id)
0
   {
0
     // if we already have a proxy, return it
0
- return rb_funcall(rb_const_get(rb_cObject,
0
- rb_intern("ObjectSpace")), rb_intern("_id2ref"), 1, id);
0
+ return id;
0
   }
0
   else
0
   {
0
@@ -510,18 +493,19 @@ VALUE make_ruby_land_proxy(JohnsonRuntime* runtime, jsval value)
0
     RubyLandProxy* our_proxy;
0
     VALUE proxy = Data_Make_Struct(proxy_class, RubyLandProxy, 0, finalize, our_proxy);
0
 
0
+ JSContext * context = johnson_get_current_context(runtime);
0
+
0
     PREPARE_RUBY_JROOTS(context, 1);
0
     JROOT(value);
0
 
0
- // root the value for JS GC and lookups
0
- sprintf(our_proxy->key, "%x", (int)value);
0
-
0
- JCHECK(JS_SetProperty(context, runtime->gcthings, our_proxy->key, &value));
0
-
0
     our_proxy->runtime = runtime;
0
+ our_proxy->key = (void *)value;
0
+
0
+ // root the value for JS GC and lookups
0
+ JCHECK(JS_AddNamedRootRT(runtime->js, &(our_proxy->key), "RubyLandProxy"));
0
 
0
     // put the proxy OID in the id map
0
- JCHECK(JS_HashTableAdd(runtime->jsids, (void *)value, (void *)rb_obj_id(proxy)));
0
+ JCHECK(JS_HashTableAdd(runtime->jsids, (void *)value, (void *)proxy));
0
     
0
     JRETURN_RUBY(proxy);
0
   }
...
5
6
7
8
 
9
10
11
...
5
6
7
 
8
9
10
11
0
@@ -5,7 +5,7 @@
0
 #include "runtime.h"
0
 
0
 typedef struct {
0
- char key[10];
0
+ void* key;
0
   JohnsonRuntime* runtime;
0
 } RubyLandProxy;
0
 
...
134
135
136
137
138
139
140
141
142
...
147
148
149
150
151
152
153
 
 
154
155
156
157
158
159
160
161
162
163
...
134
135
136
 
 
 
137
138
139
...
144
145
146
 
 
 
 
147
148
149
150
151
 
 
 
 
152
153
154
0
@@ -134,9 +134,6 @@ initialize_native(VALUE self, VALUE UNUSED(options))
0
   JohnsonRuntime* runtime;
0
   Data_Get_Struct(self, JohnsonRuntime, runtime);
0
   
0
- bool global_rooted_p = false;
0
- bool gcthings_rooted_p = false;
0
-
0
   if ((runtime->js = JS_NewRuntime(0x100000))
0
     && (runtime->jsids = create_id_hash())
0
     && (runtime->rbids = create_id_hash())
0
@@ -147,17 +144,11 @@ initialize_native(VALUE self, VALUE UNUSED(options))
0
 
0
     JSContext* context = johnson_get_current_context(runtime);
0
     if(
0
- (runtime->gcthings = JS_NewObject(context, NULL, 0, 0))
0
- &&(gcthings_rooted_p = JS_AddNamedRoot(context, &(runtime->gcthings), "runtime->gcthings"))
0
- &&(runtime->global = JS_GetGlobalObject(context))
0
- &&(global_rooted_p = JS_AddNamedRoot(context, &(runtime->global), "runtime->global"))
0
+ (runtime->global = JS_GetGlobalObject(context))
0
+ && (JS_AddNamedRoot(context, &(runtime->global), "runtime->global"))
0
     ) {
0
       return self;
0
     }
0
- if (global_rooted_p)
0
- JS_RemoveRoot(context, &(runtime->global));
0
- if (gcthings_rooted_p)
0
- JS_RemoveRoot(context, &(runtime->gcthings));
0
   }
0
 
0
 
...
17
18
19
20
21
22
23
...
17
18
19
 
20
21
22
0
@@ -17,7 +17,6 @@ typedef struct {
0
 
0
   JSHashTable *jsids; // jsid -> rbid
0
   JSHashTable *rbids; // rbid -> jsid
0
- JSObject *gcthings;
0
 } JohnsonRuntime;
0
 
0
 JSContext* johnson_get_current_context(JohnsonRuntime* runtime);

Comments

    No one has commented yet.