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:
Simplified the PREPARE_JROOTS API back to just one number.
matthewd (author)
Sun May 04 22:02:07 -0700 2008
commit  2b17b4dd7a2189d3fa27e64e05fc7200cae8cc8c
tree    a340764fcfbda69c6bd39ce7554c310db80af52c
parent  8567c74036da8477deabb1b613b8b3492fd2e67e
...
18
19
20
21
 
22
23
24
...
121
122
123
124
 
125
126
127
...
130
131
132
133
 
134
135
136
...
141
142
143
144
 
145
146
147
...
151
152
153
154
 
155
156
157
...
174
175
176
177
 
178
179
180
...
18
19
20
 
21
22
23
24
...
121
122
123
 
124
125
126
127
...
130
131
132
 
133
134
135
136
...
141
142
143
 
144
145
146
147
...
151
152
153
 
154
155
156
157
...
174
175
176
 
177
178
179
180
0
@@ -18,7 +18,7 @@ static JSBool convert_float_or_bignum_to_js(OurContext* context, VALUE float_or_
0
 
0
 static JSBool convert_symbol_to_js(OurContext* context, VALUE symbol, jsval* retval)
0
 {
0
- PREPARE_JROOTS(context, 2, 0);
0
+ PREPARE_JROOTS(context, 2);
0
 
0
   VALUE to_s = CALL_RUBY_WRAPPER(rb_funcall_0, symbol, rb_intern("to_s"), 0);
0
   jsval name = STRING_TO_JSVAL(JS_NewStringCopyN(context->js, StringValuePtr(to_s), (unsigned) StringValueLen(to_s)));
0
@@ -121,7 +121,7 @@ JSBool convert_to_js(OurContext* context, VALUE ruby, jsval* retval)
0
 
0
 VALUE convert_jsstring_to_ruby(OurContext* context, JSString* str)
0
 {
0
- PREPARE_RUBY_JROOTS(context, 1, 0);
0
+ PREPARE_RUBY_JROOTS(context, 1);
0
   JROOT(str);
0
   char* bytes = JS_GetStringBytes(str);
0
   JCHECK(bytes);
0
@@ -130,7 +130,7 @@ VALUE convert_jsstring_to_ruby(OurContext* context, JSString* str)
0
 
0
 static VALUE convert_regexp_to_ruby(OurContext* context, jsval regexp)
0
 {
0
- PREPARE_RUBY_JROOTS(context, 1, 0);
0
+ PREPARE_RUBY_JROOTS(context, 1);
0
   JROOT(regexp);
0
   JSRegExp* re = (JSRegExp*)JS_GetPrivate(context->js, JSVAL_TO_OBJECT(regexp));
0
 
0
@@ -141,7 +141,7 @@ static VALUE convert_regexp_to_ruby(OurContext* context, jsval regexp)
0
 
0
 static bool js_value_is_regexp(OurContext* context, jsval maybe_regexp)
0
 {
0
- PREPARE_RUBY_JROOTS(context, 1, 0);
0
+ PREPARE_RUBY_JROOTS(context, 1);
0
   JROOT(maybe_regexp);
0
   JSBool result = JS_InstanceOf(context->js, JSVAL_TO_OBJECT(maybe_regexp), &js_RegExpClass, NULL);
0
   JRETURN_RUBY(result ? true : false);
0
@@ -151,7 +151,7 @@ static bool js_value_is_symbol(OurContext* context, jsval maybe_symbol)
0
 {
0
   jsval nsJohnson, cSymbol;
0
 
0
- PREPARE_RUBY_JROOTS(context, 3, 0);
0
+ PREPARE_RUBY_JROOTS(context, 3);
0
   JROOT(maybe_symbol);
0
 
0
   JCHECK(JS_GetProperty(context->js, context->global, "Johnson", &nsJohnson));
0
@@ -174,7 +174,7 @@ VALUE convert_to_ruby(OurContext* context, jsval js)
0
 {
0
   if (JSVAL_NULL == js) return Qnil;
0
 
0
- PREPARE_RUBY_JROOTS(context, 1, 0);
0
+ PREPARE_RUBY_JROOTS(context, 1);
0
   
0
   switch (JS_TypeOfValue(context->js, js))
0
   {
...
14
15
16
17
 
18
19
20
...
14
15
16
 
17
18
19
20
0
@@ -14,7 +14,7 @@ define_property(JSContext *js_context, JSObject* UNUSED(obj), uintN argc, jsval
0
 
0
 VALUE init_spidermonkey_extensions(OurContext* context, VALUE self)
0
 {
0
- PREPARE_RUBY_JROOTS(context, 1, 0);
0
+ PREPARE_RUBY_JROOTS(context, 1);
0
   
0
   jsval Object;
0
   JCHECK(JS_GetProperty(context->js, context->global, "Object", &Object));
...
4
5
6
 
 
7
8
9
...
12
13
14
15
 
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
 
31
32
33
34
35
36
37
38
39
40
41
42
 
 
43
44
45
 
 
46
47
48
...
53
54
55
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56
57
58
59
60
61
62
 
 
63
64
65
66
 
 
 
67
68
69
...
71
72
73
74
75
76
77
78
 
 
 
 
79
 
80
81
82
...
4
5
6
7
8
9
10
11
...
14
15
16
 
17
18
 
 
19
20
21
22
 
 
 
 
 
 
 
 
23
24
 
 
 
 
 
 
 
 
 
 
 
25
26
27
 
 
28
29
30
31
32
...
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
 
 
59
60
61
 
 
 
62
63
64
65
66
67
...
69
70
71
 
 
 
72
 
73
74
75
76
77
78
79
80
81
0
@@ -4,6 +4,8 @@
0
 #define _JROOT_NAMESIZE 200
0
 #define _JROOT_ERRSIZE 500
0
 
0
+#define _JROOT_ROOT (void*)(1)
0
+
0
 #define OUR_CONTEXT(js_context) \
0
   ({ \
0
     const OurContext* _context; \
0
@@ -12,37 +14,19 @@
0
     _context; \
0
   })
0
 
0
-#define _PREPARE_JROOTS(rb, context, rootcount, cleancount) \
0
+#define _PREPARE_JROOTS(rb, context, cleancount) \
0
   const bool _jroot_ruby = (rb); \
0
- const int _jroot_roots = (rootcount); \
0
- void* _jroot_map[_jroot_roots]; \
0
   const int _jroot_cleans = (cleancount); \
0
   void (*_jroot_cleanup[_jroot_cleans])(OurContext*, void*); \
0
   void* _jroot_cleanup_data[_jroot_cleans]; \
0
   OurContext* _jroot_context = (context); \
0
- int _jroot_cleanidx = 0; \
0
- int _jroot_rootidx = 0
0
-
0
-#define PREPARE_JROOTS(context, rootcount, cleancount) \
0
- _PREPARE_JROOTS(false, context, rootcount, cleancount)
0
-
0
-#define PREPARE_RUBY_JROOTS(context, rootcount, cleancount) \
0
- _PREPARE_JROOTS(true, context, rootcount, cleancount)
0
+ int _jroot_cleanidx = 0;
0
 
0
-#define _JROOT(ptr, name) \
0
- do \
0
- { \
0
- static char _jroot_rootname[_JROOT_NAMESIZE] = ""; \
0
- assert(_jroot_rootidx < _jroot_roots); \
0
- _jroot_map[_jroot_rootidx] = (ptr); \
0
- if (*_jroot_rootname == '\0') \
0
- snprintf(_jroot_rootname, _JROOT_NAMESIZE, "%s[%d]:%s: %s", __FILE__, __LINE__, __func__, (name)); \
0
- JCHECK(JS_AddNamedRoot(_jroot_context->js, _jroot_map[_jroot_rootidx], _jroot_rootname)); \
0
- _jroot_rootidx++; \
0
- } while(0)
0
+#define PREPARE_JROOTS(context, cleancount) \
0
+ _PREPARE_JROOTS(false, context, cleancount)
0
 
0
-#define JROOT(var) _JROOT(&(var), #var)
0
-#define JROOT_PTR(ptr) _JROOT(ptr, #ptr)
0
+#define PREPARE_RUBY_JROOTS(context, cleancount) \
0
+ _PREPARE_JROOTS(true, context, cleancount)
0
 
0
 #define JCLEANUP(func, data) \
0
   do \
0
@@ -53,17 +37,31 @@
0
     _jroot_cleanidx++; \
0
   } while(0)
0
 
0
+#define _JROOT(ptr, name) \
0
+ do \
0
+ { \
0
+ static char _name[_JROOT_NAMESIZE] = ""; \
0
+ void* const _root = (ptr); \
0
+ if (*_name == '\0') \
0
+ snprintf(_name, _JROOT_NAMESIZE, "%s[%d]:%s: %s", __FILE__, __LINE__, __func__, (name)); \
0
+ JCHECK(JS_AddNamedRoot(_jroot_context->js, _root, _name)); \
0
+ JCLEANUP(_JROOT_ROOT, _root); \
0
+ } while(0)
0
+
0
+#define JROOT(var) _JROOT(&(var), #var)
0
+#define JROOT_PTR(ptr) _JROOT(ptr, #ptr)
0
+
0
 #define JUNROOT(var) \
0
   do \
0
   { \
0
     void* const _jroot_match = &(var); \
0
     int _jroot_i; \
0
- for (_jroot_i = _jroot_rootidx - 1; _jroot_i >= 0; _jroot_i--) \
0
- if (_jroot_map[_jroot_i] == _jroot_match) \
0
+ for (_jroot_i = _jroot_cleanidx - 1; _jroot_i >= 0; _jroot_i--) \
0
+ if (_jroot_cleanup[_jroot_i] == _JROOT_ROOT && _jroot_cleanup_data[_jroot_i] == _jroot_match) \
0
       { \
0
- JS_RemoveRoot(_jroot_context->js, _jroot_map[_jroot_i]); \
0
- if (_jroot_i == _jroot_rootidx - 1) _jroot_rootidx--; \
0
- _jroot_map[_jroot_i] = NULL; \
0
+ JS_RemoveRoot(_jroot_context->js, _jroot_cleanup_data[_jroot_i]); \
0
+ if (_jroot_i == _jroot_cleanidx - 1) _jroot_cleanidx--; \
0
+ _jroot_cleanup[_jroot_i] = NULL; \
0
       } \
0
   } while (0)
0
 
0
@@ -71,12 +69,13 @@
0
   do \
0
   { \
0
     int _jroot_i; \
0
- for (_jroot_i = _jroot_rootidx - 1; _jroot_i >= 0; _jroot_i--) \
0
- if (_jroot_map[_jroot_i]) \
0
- JS_RemoveRoot(_jroot_context->js, _jroot_map[_jroot_i]); \
0
     for (_jroot_i = _jroot_cleanidx - 1; _jroot_i >= 0; _jroot_i--) \
0
- if (_jroot_cleanup[_jroot_i]) \
0
+ { \
0
+ if (_jroot_cleanup[_jroot_i] == _JROOT_ROOT) \
0
+ JS_RemoveRoot(_jroot_context->js, _jroot_cleanup_data[_jroot_i]); \
0
+ else if (_jroot_cleanup[_jroot_i]) \
0
         (_jroot_cleanup[_jroot_i])(_jroot_context, _jroot_cleanup_data[_jroot_i]); \
0
+ } \
0
   } while (0)
0
 
0
 #define JCHECK(cond) \
...
196
197
198
199
 
200
201
202
...
284
285
286
287
 
288
289
290
...
299
300
301
302
 
303
304
305
...
365
366
367
368
 
369
370
371
...
386
387
388
389
 
390
391
392
...
409
410
411
412
 
413
414
415
...
424
425
426
427
 
428
429
430
...
439
440
441
442
 
443
444
445
...
465
466
467
468
 
469
470
471
...
530
531
532
533
 
534
535
536
...
196
197
198
 
199
200
201
202
...
284
285
286
 
287
288
289
290
...
299
300
301
 
302
303
304
305
...
365
366
367
 
368
369
370
371
...
386
387
388
 
389
390
391
392
...
409
410
411
 
412
413
414
415
...
424
425
426
 
427
428
429
430
...
439
440
441
 
442
443
444
445
...
465
466
467
 
468
469
470
471
...
530
531
532
 
533
534
535
536
0
@@ -196,7 +196,7 @@ static JSBool get(JSContext* js_context, JSObject* obj, jsval id, jsval* retval)
0
   OurContext* context;
0
   Data_Get_Struct(ruby_context, OurContext, context);
0
 
0
- PREPARE_JROOTS(context, 1, 0);
0
+ PREPARE_JROOTS(context, 1);
0
   JROOT(id);
0
     
0
   // get the Ruby object that backs this proxy
0
@@ -284,7 +284,7 @@ static JSBool get(JSContext* js_context, JSObject* obj, jsval id, jsval* retval)
0
 static JSBool get_and_destroy_resolved_property(
0
   JSContext* js_context, JSObject* obj, jsval id, jsval* retval)
0
 {
0
- PREPARE_JROOTS(OUR_CONTEXT(js_context), 1, 0);
0
+ PREPARE_JROOTS(OUR_CONTEXT(js_context), 1);
0
   JROOT(id);
0
   char* name = JS_GetStringBytes(JSVAL_TO_STRING(id));
0
   JCHECK(JS_DeleteProperty(js_context, obj, name));
0
@@ -299,7 +299,7 @@ static JSBool set(JSContext* js_context, JSObject* obj, jsval id, jsval* value)
0
   OurContext* context;
0
   Data_Get_Struct(ruby_context, OurContext, context);
0
 
0
- PREPARE_JROOTS(context, 2, 0);
0
+ PREPARE_JROOTS(context, 2);
0
   JROOT(id);
0
   JROOT_PTR(value);
0
     
0
@@ -365,7 +365,7 @@ static JSBool construct(JSContext* js_context, JSObject* UNUSED(obj), uintN argc
0
   OurContext* context;
0
   Data_Get_Struct(ruby_context, OurContext, context);
0
 
0
- PREPARE_JROOTS(context, 0, 0);
0
+ PREPARE_JROOTS(context, 0);
0
 
0
   VALUE klass = CONVERT_TO_RUBY(context, JS_ARGV_CALLEE(argv));
0
   VALUE args = rb_ary_new();
0
@@ -386,7 +386,7 @@ static JSBool resolve(JSContext *js_context, JSObject *obj, jsval id, uintN UNUS
0
   OurContext* context;
0
   Data_Get_Struct(ruby_context, OurContext, context);
0
 
0
- PREPARE_JROOTS(context, 1, 0);
0
+ PREPARE_JROOTS(context, 1);
0
   JROOT(id);
0
   
0
   char* name = JS_GetStringBytes(JS_ValueToString(js_context, id));
0
@@ -409,7 +409,7 @@ static JSBool to_string(JSContext* js_context, JSObject* obj, uintN UNUSED(argc)
0
   OurContext* context;
0
   Data_Get_Struct(ruby_context, OurContext, context);
0
 
0
- PREPARE_JROOTS(context, 0, 0);
0
+ PREPARE_JROOTS(context, 0);
0
 
0
   VALUE self = (VALUE)JS_GetInstancePrivate(context->js, obj, JS_GET_CLASS(context->js, obj), NULL);
0
 
0
@@ -424,7 +424,7 @@ static JSBool to_array(JSContext* js_context, JSObject* obj, uintN UNUSED(argc),
0
   OurContext* context;
0
   Data_Get_Struct(ruby_context, OurContext, context);
0
 
0
- PREPARE_JROOTS(context, 0, 0);
0
+ PREPARE_JROOTS(context, 0);
0
 
0
   VALUE self = (VALUE)JS_GetInstancePrivate(context->js, obj, JS_GET_CLASS(context->js, obj), NULL);
0
 
0
@@ -439,7 +439,7 @@ static JSBool method_missing(JSContext* js_context, JSObject* obj, uintN argc, j
0
   OurContext* context;
0
   Data_Get_Struct(ruby_context, OurContext, context);
0
 
0
- PREPARE_JROOTS(context, 0, 0);
0
+ PREPARE_JROOTS(context, 0);
0
     
0
   VALUE self = (VALUE)JS_GetInstancePrivate(context->js, obj, JS_GET_CLASS(context->js, obj), NULL);
0
   
0
@@ -465,7 +465,7 @@ static JSBool call(JSContext* js_context, JSObject* UNUSED(obj), uintN argc, jsv
0
   OurContext* context;
0
   Data_Get_Struct(ruby_context, OurContext, context);
0
 
0
- PREPARE_JROOTS(context, 0, 0);
0
+ PREPARE_JROOTS(context, 0);
0
   
0
   VALUE self = (VALUE)JS_GetInstancePrivate(context->js, JSVAL_TO_OBJECT(JS_ARGV_CALLEE(argv)), &JSLandCallableProxyClass, NULL);
0
   
0
@@ -530,7 +530,7 @@ JSBool make_js_land_proxy(OurContext* context, VALUE value, jsval* retval)
0
   }
0
   else
0
   {
0
- PREPARE_JROOTS(context, 1, 0);
0
+ PREPARE_JROOTS(context, 1);
0
 
0
     JSObject *jsobj;
0
     
...
12
13
14
15
 
16
17
18
...
41
42
43
44
 
45
46
47
...
68
69
70
71
 
72
73
74
...
105
106
107
108
 
109
110
111
...
138
139
140
141
 
142
143
144
...
159
160
161
162
 
163
164
165
...
228
229
230
231
 
232
233
234
...
270
271
272
273
 
274
275
276
...
295
296
297
298
 
299
300
301
...
370
371
372
373
 
374
375
376
...
393
394
395
396
 
397
398
399
...
12
13
14
 
15
16
17
18
...
41
42
43
 
44
45
46
47
...
68
69
70
 
71
72
73
74
...
105
106
107
 
108
109
110
111
...
138
139
140
 
141
142
143
144
...
159
160
161
 
162
163
164
165
...
228
229
230
 
231
232
233
234
...
270
271
272
 
273
274
275
276
...
295
296
297
 
298
299
300
301
...
370
371
372
 
373
374
375
376
...
393
394
395
 
396
397
398
399
0
@@ -12,7 +12,7 @@ static VALUE proxy_class = Qnil;
0
 
0
 static VALUE call_js_function_value(OurContext* context, jsval target, jsval function, int argc, VALUE* argv)
0
 {
0
- PREPARE_RUBY_JROOTS(context, argc + 2, 0);
0
+ PREPARE_RUBY_JROOTS(context, argc + 2);
0
 
0
   JROOT(target);
0
   JROOT(function);
0
@@ -41,7 +41,7 @@ get(VALUE self, VALUE name)
0
   RubyLandProxy* proxy;
0
   Data_Get_Struct(self, RubyLandProxy, proxy);
0
 
0
- PREPARE_RUBY_JROOTS(proxy->context, 1, 0);
0
+ PREPARE_RUBY_JROOTS(proxy->context, 1);
0
 
0
   JROOT(proxy->value);
0
 
0
@@ -68,7 +68,7 @@ set(VALUE self, VALUE name, VALUE value)
0
   RubyLandProxy* proxy;
0
   Data_Get_Struct(self, RubyLandProxy, proxy);
0
   
0
- PREPARE_RUBY_JROOTS(proxy->context, 2, 0);
0
+ PREPARE_RUBY_JROOTS(proxy->context, 2);
0
   JROOT(proxy->value);
0
 
0
   jsval js_value;
0
@@ -105,7 +105,7 @@ respond_to_p(VALUE self, VALUE sym)
0
   RubyLandProxy* proxy;
0
   Data_Get_Struct(self, RubyLandProxy, proxy);
0
 
0
- PREPARE_RUBY_JROOTS(proxy->context, 2, 0);
0
+ PREPARE_RUBY_JROOTS(proxy->context, 2);
0
   
0
   char* name = rb_id2name(SYM2ID(sym));
0
   
0
@@ -138,7 +138,7 @@ native_call(int argc, VALUE* argv, VALUE self)
0
   RubyLandProxy* proxy;
0
   Data_Get_Struct(self, RubyLandProxy, proxy);
0
   
0
- PREPARE_RUBY_JROOTS(proxy->context, 1, 0);
0
+ PREPARE_RUBY_JROOTS(proxy->context, 1);
0
   JROOT(proxy->value);
0
 
0
   jsval global;
0
@@ -159,7 +159,7 @@ each(VALUE self)
0
   RubyLandProxy* proxy;
0
   Data_Get_Struct(self, RubyLandProxy, proxy);
0
   
0
- PREPARE_RUBY_JROOTS(proxy->context, 4, 1);
0
+ PREPARE_RUBY_JROOTS(proxy->context, 5);
0
   JROOT(proxy->value);
0
 
0
   JSObject* value = JSVAL_TO_OBJECT(proxy->value);
0
@@ -228,7 +228,7 @@ length(VALUE self)
0
   RubyLandProxy* proxy;
0
   Data_Get_Struct(self, RubyLandProxy, proxy);
0
 
0
- PREPARE_RUBY_JROOTS(proxy->context, 2, 0);
0
+ PREPARE_RUBY_JROOTS(proxy->context, 2);
0
   
0
   JROOT(proxy->value);
0
 
0
@@ -270,7 +270,7 @@ function_property_p(VALUE self, VALUE name)
0
   RubyLandProxy* proxy;
0
   Data_Get_Struct(self, RubyLandProxy, proxy);
0
 
0
- PREPARE_RUBY_JROOTS(proxy->context, 2, 0);
0
+ PREPARE_RUBY_JROOTS(proxy->context, 2);
0
 
0
   JROOT(proxy->value);
0
 
0
@@ -295,7 +295,7 @@ call_function_property(int argc, VALUE* argv, VALUE self)
0
   if (argc < 1)
0
     rb_raise(rb_eArgError, "Function name required");
0
 
0
- PREPARE_RUBY_JROOTS(proxy->context, 2, 0);
0
+ PREPARE_RUBY_JROOTS(proxy->context, 2);
0
   JROOT(proxy->value);
0
 
0
   jsval function;
0
@@ -370,7 +370,7 @@ VALUE make_ruby_land_proxy(OurContext* context, jsval value)
0
     RubyLandProxy* our_proxy;
0
     VALUE proxy = Data_Make_Struct(proxy_class, RubyLandProxy, 0, finalize, our_proxy);
0
 
0
- PREPARE_RUBY_JROOTS(context, 1, 0);
0
+ PREPARE_RUBY_JROOTS(context, 1);
0
     JROOT(value);
0
 
0
     our_proxy->value = value;
0
@@ -393,7 +393,7 @@ static VALUE to_s(VALUE self)
0
   RubyLandProxy* proxy;
0
   Data_Get_Struct(self, RubyLandProxy, proxy);
0
 
0
- PREPARE_RUBY_JROOTS(proxy->context, 1, 0);
0
+ PREPARE_RUBY_JROOTS(proxy->context, 1);
0
   JROOT(proxy->value);
0
   JSString* str = JS_ValueToString(proxy->context->js, proxy->value);
0
 

Comments

    No one has commented yet.