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

Commit

Permalink
More JSBool conversions.
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewd committed Apr 21, 2008
1 parent a5d745e commit 9149af0
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 39 deletions.
44 changes: 16 additions & 28 deletions ext/spidermonkey/conversions.c
Expand Up @@ -2,14 +2,12 @@
#include "js_land_proxy.h"
#include "ruby_land_proxy.h"

static jsval convert_float_or_bignum_to_js(OurContext* context, VALUE float_or_bignum)
static JSBool convert_float_or_bignum_to_js(OurContext* context, VALUE float_or_bignum, jsval* retval)
{
jsval js;
assert(JS_NewDoubleValue(context->js, NUM2DBL(float_or_bignum), &js));
return js;
return JS_NewDoubleValue(context->js, NUM2DBL(float_or_bignum), retval);
}

static jsval convert_symbol_to_js(OurContext* context, VALUE symbol)
static JSBool convert_symbol_to_js(OurContext* context, VALUE symbol, jsval* retval)
{
VALUE to_s = rb_funcall(symbol, rb_intern("to_s"), 0);
jsval name = STRING_TO_JSVAL(JS_NewStringCopyZ(context->js, StringValuePtr(to_s)));
Expand All @@ -19,21 +17,19 @@ static jsval convert_symbol_to_js(OurContext* context, VALUE symbol)
jsval nsJohnson;
assert(JS_GetProperty(context->js, context->global, "Johnson", &nsJohnson) || JSVAL_VOID == nsJohnson);

jsval js = JSVAL_NULL;
assert(JS_CallFunctionName(context->js, JSVAL_TO_OBJECT(nsJohnson), "symbolize", 1, &name, &js));

return js;
return JS_CallFunctionName(context->js, JSVAL_TO_OBJECT(nsJohnson), "symbolize", 1, &name, retval);
}

static jsval convert_regexp_to_js(OurContext* context, VALUE regexp)
static JSBool convert_regexp_to_js(OurContext* context, VALUE regexp, jsval* retval)
{
VALUE source = rb_funcall(regexp, rb_intern("source"), 0);
int options = NUM2INT(rb_funcall(regexp, rb_intern("options"), 0));

return OBJECT_TO_JSVAL(JS_NewRegExpObject(context->js,
*retval = OBJECT_TO_JSVAL(JS_NewRegExpObject(context->js,
StringValuePtr(source),
strlen(StringValuePtr(source)),
options));
return JS_TRUE;
}

JSBool convert_to_js(OurContext* context, VALUE ruby, jsval* retval)
Expand Down Expand Up @@ -62,12 +58,10 @@ JSBool convert_to_js(OurContext* context, VALUE ruby, jsval* retval)

case T_FLOAT:
case T_BIGNUM:
*retval = convert_float_or_bignum_to_js(context, ruby);
return JS_TRUE;
return convert_float_or_bignum_to_js(context, ruby, retval);

case T_SYMBOL:
*retval = convert_symbol_to_js(context, ruby);
return JS_TRUE;
return convert_symbol_to_js(context, ruby, retval);

case T_CLASS:
case T_ARRAY:
Expand All @@ -76,23 +70,17 @@ JSBool convert_to_js(OurContext* context, VALUE ruby, jsval* retval)
case T_FILE:
case T_STRUCT:
case T_OBJECT:
*retval = make_js_land_proxy(context, ruby);
return JS_TRUE;
return make_js_land_proxy(context, ruby, retval);

case T_REGEXP:
*retval = convert_regexp_to_js(context, ruby);
return JS_TRUE;
return convert_regexp_to_js(context, ruby, retval);

case T_DATA: // HEY! keep T_DATA last for fall-through
if (ruby_value_is_proxy(ruby)) {
*retval = unwrap_ruby_land_proxy(context, ruby);
return JS_TRUE;
}

if (rb_cProc == rb_class_of(ruby) || rb_cMethod == rb_class_of(ruby)) {
*retval = make_js_land_proxy(context, ruby);
return JS_TRUE;
}
if (ruby_value_is_proxy(ruby))
return unwrap_ruby_land_proxy(context, ruby, retval);

if (rb_cProc == rb_class_of(ruby) || rb_cMethod == rb_class_of(ruby))
return make_js_land_proxy(context, ruby, retval);

default:
Johnson_Error_raise("unknown ruby type in switch");
Expand Down
13 changes: 6 additions & 7 deletions ext/spidermonkey/js_land_proxy.c
Expand Up @@ -426,14 +426,13 @@ static void finalize(JSContext* js_context, JSObject* obj)
}
}

jsval make_js_land_proxy(OurContext* context, VALUE value)
JSBool make_js_land_proxy(OurContext* context, VALUE value, jsval* retval)
{
jsid id = (jsid)JS_HashTableLookup(context->rbids, (void *)rb_obj_id(value));
jsval js;

if (id)
{
assert(JS_IdToValue(context->js, id, &js));
return JS_IdToValue(context->js, id, retval);
}
else
{
Expand All @@ -460,18 +459,18 @@ jsval make_js_land_proxy(OurContext* context, VALUE value)
assert(JS_DefineFunction(context->js, jsobj,
"__noSuchMethod__", method_missing, 2, 0));

js = OBJECT_TO_JSVAL(jsobj);
*retval = OBJECT_TO_JSVAL(jsobj);

jsval newid;
assert(JS_ValueToId(context->js, js, &newid));
assert(JS_ValueToId(context->js, *retval, &newid));

// put the proxy OID in the id map
assert(JS_HashTableAdd(context->rbids, (void *)rb_obj_id(value), (void *)newid));

// root the ruby value for GC
VALUE ruby_context = (VALUE)JS_GetContextPrivate(context->js);
rb_funcall(ruby_context, rb_intern("add_gcthing"), 1, value);

return JS_TRUE;
}

return js;
}
2 changes: 1 addition & 1 deletion ext/spidermonkey/js_land_proxy.h
Expand Up @@ -8,7 +8,7 @@

JSBool js_value_is_proxy(OurContext* context, jsval maybe_proxy);
VALUE unwrap_js_land_proxy(OurContext* context, jsval proxy);
jsval make_js_land_proxy(OurContext* context, VALUE value);
JSBool make_js_land_proxy(OurContext* context, VALUE value, jsval* retval);

// FIXME: WTF
#include "node.h"
Expand Down
5 changes: 3 additions & 2 deletions ext/spidermonkey/ruby_land_proxy.c
Expand Up @@ -270,14 +270,15 @@ JSBool ruby_value_is_proxy(VALUE maybe_proxy)
return proxy_class == CLASS_OF(maybe_proxy);
}

jsval unwrap_ruby_land_proxy(OurContext* context, VALUE wrapped)
JSBool unwrap_ruby_land_proxy(OurContext* context, VALUE wrapped, jsval* retval)
{
assert(ruby_value_is_proxy(wrapped));

RubyLandProxy* proxy;
Data_Get_Struct(wrapped, RubyLandProxy, proxy);

return proxy->value;
*retval = proxy->value;
return JS_TRUE;
}

VALUE make_ruby_land_proxy(OurContext* context, jsval value)
Expand Down
2 changes: 1 addition & 1 deletion ext/spidermonkey/ruby_land_proxy.h
Expand Up @@ -10,7 +10,7 @@ typedef struct {
} RubyLandProxy;

JSBool ruby_value_is_proxy(VALUE maybe_proxy);
jsval unwrap_ruby_land_proxy(OurContext* context, VALUE proxy);
JSBool unwrap_ruby_land_proxy(OurContext* context, VALUE proxy, jsval* retval);
VALUE make_ruby_land_proxy(OurContext* context, jsval value);

#endif

0 comments on commit 9149af0

Please sign in to comment.