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

Commit

Permalink
Simplified the PREPARE_JROOTS API back to just one number.
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewd committed May 5, 2008
1 parent 8567c74 commit 2b17b4d
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 61 deletions.
12 changes: 6 additions & 6 deletions ext/spidermonkey/conversions.c
Expand Up @@ -18,7 +18,7 @@ static JSBool convert_float_or_bignum_to_js(OurContext* context, VALUE float_or_

static JSBool convert_symbol_to_js(OurContext* context, VALUE symbol, jsval* retval)
{
PREPARE_JROOTS(context, 2, 0);
PREPARE_JROOTS(context, 2);

VALUE to_s = CALL_RUBY_WRAPPER(rb_funcall_0, symbol, rb_intern("to_s"), 0);
jsval name = STRING_TO_JSVAL(JS_NewStringCopyN(context->js, StringValuePtr(to_s), (unsigned) StringValueLen(to_s)));
Expand Down Expand Up @@ -121,7 +121,7 @@ JSBool convert_to_js(OurContext* context, VALUE ruby, jsval* retval)

VALUE convert_jsstring_to_ruby(OurContext* context, JSString* str)
{
PREPARE_RUBY_JROOTS(context, 1, 0);
PREPARE_RUBY_JROOTS(context, 1);
JROOT(str);
char* bytes = JS_GetStringBytes(str);
JCHECK(bytes);
Expand All @@ -130,7 +130,7 @@ VALUE convert_jsstring_to_ruby(OurContext* context, JSString* str)

static VALUE convert_regexp_to_ruby(OurContext* context, jsval regexp)
{
PREPARE_RUBY_JROOTS(context, 1, 0);
PREPARE_RUBY_JROOTS(context, 1);
JROOT(regexp);
JSRegExp* re = (JSRegExp*)JS_GetPrivate(context->js, JSVAL_TO_OBJECT(regexp));

Expand All @@ -141,7 +141,7 @@ static VALUE convert_regexp_to_ruby(OurContext* context, jsval regexp)

static bool js_value_is_regexp(OurContext* context, jsval maybe_regexp)
{
PREPARE_RUBY_JROOTS(context, 1, 0);
PREPARE_RUBY_JROOTS(context, 1);
JROOT(maybe_regexp);
JSBool result = JS_InstanceOf(context->js, JSVAL_TO_OBJECT(maybe_regexp), &js_RegExpClass, NULL);
JRETURN_RUBY(result ? true : false);
Expand All @@ -151,7 +151,7 @@ static bool js_value_is_symbol(OurContext* context, jsval maybe_symbol)
{
jsval nsJohnson, cSymbol;

PREPARE_RUBY_JROOTS(context, 3, 0);
PREPARE_RUBY_JROOTS(context, 3);
JROOT(maybe_symbol);

JCHECK(JS_GetProperty(context->js, context->global, "Johnson", &nsJohnson));
Expand All @@ -174,7 +174,7 @@ VALUE convert_to_ruby(OurContext* context, jsval js)
{
if (JSVAL_NULL == js) return Qnil;

PREPARE_RUBY_JROOTS(context, 1, 0);
PREPARE_RUBY_JROOTS(context, 1);

switch (JS_TypeOfValue(context->js, js))
{
Expand Down
2 changes: 1 addition & 1 deletion ext/spidermonkey/extensions.c
Expand Up @@ -14,7 +14,7 @@ define_property(JSContext *js_context, JSObject* UNUSED(obj), uintN argc, jsval

VALUE init_spidermonkey_extensions(OurContext* context, VALUE self)
{
PREPARE_RUBY_JROOTS(context, 1, 0);
PREPARE_RUBY_JROOTS(context, 1);

jsval Object;
JCHECK(JS_GetProperty(context->js, context->global, "Object", &Object));
Expand Down
65 changes: 32 additions & 33 deletions ext/spidermonkey/jroot.h
Expand Up @@ -4,6 +4,8 @@
#define _JROOT_NAMESIZE 200
#define _JROOT_ERRSIZE 500

#define _JROOT_ROOT (void*)(1)

#define OUR_CONTEXT(js_context) \
({ \
const OurContext* _context; \
Expand All @@ -12,37 +14,19 @@
_context; \
})

#define _PREPARE_JROOTS(rb, context, rootcount, cleancount) \
#define _PREPARE_JROOTS(rb, context, cleancount) \
const bool _jroot_ruby = (rb); \
const int _jroot_roots = (rootcount); \
void* _jroot_map[_jroot_roots]; \
const int _jroot_cleans = (cleancount); \
void (*_jroot_cleanup[_jroot_cleans])(OurContext*, void*); \
void* _jroot_cleanup_data[_jroot_cleans]; \
OurContext* _jroot_context = (context); \
int _jroot_cleanidx = 0; \
int _jroot_rootidx = 0

#define PREPARE_JROOTS(context, rootcount, cleancount) \
_PREPARE_JROOTS(false, context, rootcount, cleancount)

#define PREPARE_RUBY_JROOTS(context, rootcount, cleancount) \
_PREPARE_JROOTS(true, context, rootcount, cleancount)
int _jroot_cleanidx = 0;

#define _JROOT(ptr, name) \
do \
{ \
static char _jroot_rootname[_JROOT_NAMESIZE] = ""; \
assert(_jroot_rootidx < _jroot_roots); \
_jroot_map[_jroot_rootidx] = (ptr); \
if (*_jroot_rootname == '\0') \
snprintf(_jroot_rootname, _JROOT_NAMESIZE, "%s[%d]:%s: %s", __FILE__, __LINE__, __func__, (name)); \
JCHECK(JS_AddNamedRoot(_jroot_context->js, _jroot_map[_jroot_rootidx], _jroot_rootname)); \
_jroot_rootidx++; \
} while(0)
#define PREPARE_JROOTS(context, cleancount) \
_PREPARE_JROOTS(false, context, cleancount)

#define JROOT(var) _JROOT(&(var), #var)
#define JROOT_PTR(ptr) _JROOT(ptr, #ptr)
#define PREPARE_RUBY_JROOTS(context, cleancount) \
_PREPARE_JROOTS(true, context, cleancount)

#define JCLEANUP(func, data) \
do \
Expand All @@ -53,30 +37,45 @@
_jroot_cleanidx++; \
} while(0)

#define _JROOT(ptr, name) \
do \
{ \
static char _name[_JROOT_NAMESIZE] = ""; \
void* const _root = (ptr); \
if (*_name == '\0') \
snprintf(_name, _JROOT_NAMESIZE, "%s[%d]:%s: %s", __FILE__, __LINE__, __func__, (name)); \
JCHECK(JS_AddNamedRoot(_jroot_context->js, _root, _name)); \
JCLEANUP(_JROOT_ROOT, _root); \
} while(0)

#define JROOT(var) _JROOT(&(var), #var)
#define JROOT_PTR(ptr) _JROOT(ptr, #ptr)

#define JUNROOT(var) \
do \
{ \
void* const _jroot_match = &(var); \
int _jroot_i; \
for (_jroot_i = _jroot_rootidx - 1; _jroot_i >= 0; _jroot_i--) \
if (_jroot_map[_jroot_i] == _jroot_match) \
for (_jroot_i = _jroot_cleanidx - 1; _jroot_i >= 0; _jroot_i--) \
if (_jroot_cleanup[_jroot_i] == _JROOT_ROOT && _jroot_cleanup_data[_jroot_i] == _jroot_match) \
{ \
JS_RemoveRoot(_jroot_context->js, _jroot_map[_jroot_i]); \
if (_jroot_i == _jroot_rootidx - 1) _jroot_rootidx--; \
_jroot_map[_jroot_i] = NULL; \
JS_RemoveRoot(_jroot_context->js, _jroot_cleanup_data[_jroot_i]); \
if (_jroot_i == _jroot_cleanidx - 1) _jroot_cleanidx--; \
_jroot_cleanup[_jroot_i] = NULL; \
} \
} while (0)

#define REMOVE_JROOTS \
do \
{ \
int _jroot_i; \
for (_jroot_i = _jroot_rootidx - 1; _jroot_i >= 0; _jroot_i--) \
if (_jroot_map[_jroot_i]) \
JS_RemoveRoot(_jroot_context->js, _jroot_map[_jroot_i]); \
for (_jroot_i = _jroot_cleanidx - 1; _jroot_i >= 0; _jroot_i--) \
if (_jroot_cleanup[_jroot_i]) \
{ \
if (_jroot_cleanup[_jroot_i] == _JROOT_ROOT) \
JS_RemoveRoot(_jroot_context->js, _jroot_cleanup_data[_jroot_i]); \
else if (_jroot_cleanup[_jroot_i]) \
(_jroot_cleanup[_jroot_i])(_jroot_context, _jroot_cleanup_data[_jroot_i]); \
} \
} while (0)

#define JCHECK(cond) \
Expand Down
20 changes: 10 additions & 10 deletions ext/spidermonkey/js_land_proxy.c
Expand Up @@ -196,7 +196,7 @@ static JSBool get(JSContext* js_context, JSObject* obj, jsval id, jsval* retval)
OurContext* context;
Data_Get_Struct(ruby_context, OurContext, context);

PREPARE_JROOTS(context, 1, 0);
PREPARE_JROOTS(context, 1);
JROOT(id);

// get the Ruby object that backs this proxy
Expand Down Expand Up @@ -284,7 +284,7 @@ static JSBool get(JSContext* js_context, JSObject* obj, jsval id, jsval* retval)
static JSBool get_and_destroy_resolved_property(
JSContext* js_context, JSObject* obj, jsval id, jsval* retval)
{
PREPARE_JROOTS(OUR_CONTEXT(js_context), 1, 0);
PREPARE_JROOTS(OUR_CONTEXT(js_context), 1);
JROOT(id);
char* name = JS_GetStringBytes(JSVAL_TO_STRING(id));
JCHECK(JS_DeleteProperty(js_context, obj, name));
Expand All @@ -299,7 +299,7 @@ static JSBool set(JSContext* js_context, JSObject* obj, jsval id, jsval* value)
OurContext* context;
Data_Get_Struct(ruby_context, OurContext, context);

PREPARE_JROOTS(context, 2, 0);
PREPARE_JROOTS(context, 2);
JROOT(id);
JROOT_PTR(value);

Expand Down Expand Up @@ -365,7 +365,7 @@ static JSBool construct(JSContext* js_context, JSObject* UNUSED(obj), uintN argc
OurContext* context;
Data_Get_Struct(ruby_context, OurContext, context);

PREPARE_JROOTS(context, 0, 0);
PREPARE_JROOTS(context, 0);

VALUE klass = CONVERT_TO_RUBY(context, JS_ARGV_CALLEE(argv));
VALUE args = rb_ary_new();
Expand All @@ -386,7 +386,7 @@ static JSBool resolve(JSContext *js_context, JSObject *obj, jsval id, uintN UNUS
OurContext* context;
Data_Get_Struct(ruby_context, OurContext, context);

PREPARE_JROOTS(context, 1, 0);
PREPARE_JROOTS(context, 1);
JROOT(id);

char* name = JS_GetStringBytes(JS_ValueToString(js_context, id));
Expand All @@ -409,7 +409,7 @@ static JSBool to_string(JSContext* js_context, JSObject* obj, uintN UNUSED(argc)
OurContext* context;
Data_Get_Struct(ruby_context, OurContext, context);

PREPARE_JROOTS(context, 0, 0);
PREPARE_JROOTS(context, 0);

VALUE self = (VALUE)JS_GetInstancePrivate(context->js, obj, JS_GET_CLASS(context->js, obj), NULL);

Expand All @@ -424,7 +424,7 @@ static JSBool to_array(JSContext* js_context, JSObject* obj, uintN UNUSED(argc),
OurContext* context;
Data_Get_Struct(ruby_context, OurContext, context);

PREPARE_JROOTS(context, 0, 0);
PREPARE_JROOTS(context, 0);

VALUE self = (VALUE)JS_GetInstancePrivate(context->js, obj, JS_GET_CLASS(context->js, obj), NULL);

Expand All @@ -439,7 +439,7 @@ static JSBool method_missing(JSContext* js_context, JSObject* obj, uintN argc, j
OurContext* context;
Data_Get_Struct(ruby_context, OurContext, context);

PREPARE_JROOTS(context, 0, 0);
PREPARE_JROOTS(context, 0);

VALUE self = (VALUE)JS_GetInstancePrivate(context->js, obj, JS_GET_CLASS(context->js, obj), NULL);

Expand All @@ -465,7 +465,7 @@ static JSBool call(JSContext* js_context, JSObject* UNUSED(obj), uintN argc, jsv
OurContext* context;
Data_Get_Struct(ruby_context, OurContext, context);

PREPARE_JROOTS(context, 0, 0);
PREPARE_JROOTS(context, 0);

VALUE self = (VALUE)JS_GetInstancePrivate(context->js, JSVAL_TO_OBJECT(JS_ARGV_CALLEE(argv)), &JSLandCallableProxyClass, NULL);

Expand Down Expand Up @@ -530,7 +530,7 @@ JSBool make_js_land_proxy(OurContext* context, VALUE value, jsval* retval)
}
else
{
PREPARE_JROOTS(context, 1, 0);
PREPARE_JROOTS(context, 1);

JSObject *jsobj;

Expand Down
22 changes: 11 additions & 11 deletions ext/spidermonkey/ruby_land_proxy.c
Expand Up @@ -12,7 +12,7 @@ static VALUE proxy_class = Qnil;

static VALUE call_js_function_value(OurContext* context, jsval target, jsval function, int argc, VALUE* argv)
{
PREPARE_RUBY_JROOTS(context, argc + 2, 0);
PREPARE_RUBY_JROOTS(context, argc + 2);

JROOT(target);
JROOT(function);
Expand Down Expand Up @@ -41,7 +41,7 @@ get(VALUE self, VALUE name)
RubyLandProxy* proxy;
Data_Get_Struct(self, RubyLandProxy, proxy);

PREPARE_RUBY_JROOTS(proxy->context, 1, 0);
PREPARE_RUBY_JROOTS(proxy->context, 1);

JROOT(proxy->value);

Expand All @@ -68,7 +68,7 @@ set(VALUE self, VALUE name, VALUE value)
RubyLandProxy* proxy;
Data_Get_Struct(self, RubyLandProxy, proxy);

PREPARE_RUBY_JROOTS(proxy->context, 2, 0);
PREPARE_RUBY_JROOTS(proxy->context, 2);
JROOT(proxy->value);

jsval js_value;
Expand Down Expand Up @@ -105,7 +105,7 @@ respond_to_p(VALUE self, VALUE sym)
RubyLandProxy* proxy;
Data_Get_Struct(self, RubyLandProxy, proxy);

PREPARE_RUBY_JROOTS(proxy->context, 2, 0);
PREPARE_RUBY_JROOTS(proxy->context, 2);

char* name = rb_id2name(SYM2ID(sym));

Expand Down Expand Up @@ -138,7 +138,7 @@ native_call(int argc, VALUE* argv, VALUE self)
RubyLandProxy* proxy;
Data_Get_Struct(self, RubyLandProxy, proxy);

PREPARE_RUBY_JROOTS(proxy->context, 1, 0);
PREPARE_RUBY_JROOTS(proxy->context, 1);
JROOT(proxy->value);

jsval global;
Expand All @@ -159,7 +159,7 @@ each(VALUE self)
RubyLandProxy* proxy;
Data_Get_Struct(self, RubyLandProxy, proxy);

PREPARE_RUBY_JROOTS(proxy->context, 4, 1);
PREPARE_RUBY_JROOTS(proxy->context, 5);
JROOT(proxy->value);

JSObject* value = JSVAL_TO_OBJECT(proxy->value);
Expand Down Expand Up @@ -228,7 +228,7 @@ length(VALUE self)
RubyLandProxy* proxy;
Data_Get_Struct(self, RubyLandProxy, proxy);

PREPARE_RUBY_JROOTS(proxy->context, 2, 0);
PREPARE_RUBY_JROOTS(proxy->context, 2);

JROOT(proxy->value);

Expand Down Expand Up @@ -270,7 +270,7 @@ function_property_p(VALUE self, VALUE name)
RubyLandProxy* proxy;
Data_Get_Struct(self, RubyLandProxy, proxy);

PREPARE_RUBY_JROOTS(proxy->context, 2, 0);
PREPARE_RUBY_JROOTS(proxy->context, 2);

JROOT(proxy->value);

Expand All @@ -295,7 +295,7 @@ call_function_property(int argc, VALUE* argv, VALUE self)
if (argc < 1)
rb_raise(rb_eArgError, "Function name required");

PREPARE_RUBY_JROOTS(proxy->context, 2, 0);
PREPARE_RUBY_JROOTS(proxy->context, 2);
JROOT(proxy->value);

jsval function;
Expand Down Expand Up @@ -370,7 +370,7 @@ VALUE make_ruby_land_proxy(OurContext* context, jsval value)
RubyLandProxy* our_proxy;
VALUE proxy = Data_Make_Struct(proxy_class, RubyLandProxy, 0, finalize, our_proxy);

PREPARE_RUBY_JROOTS(context, 1, 0);
PREPARE_RUBY_JROOTS(context, 1);
JROOT(value);

our_proxy->value = value;
Expand All @@ -393,7 +393,7 @@ static VALUE to_s(VALUE self)
RubyLandProxy* proxy;
Data_Get_Struct(self, RubyLandProxy, proxy);

PREPARE_RUBY_JROOTS(proxy->context, 1, 0);
PREPARE_RUBY_JROOTS(proxy->context, 1);
JROOT(proxy->value);
JSString* str = JS_ValueToString(proxy->context->js, proxy->value);

Expand Down

0 comments on commit 2b17b4d

Please sign in to comment.