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

Commit

Permalink
#28 #23 arrays work in js land and so does for .. in
Browse files Browse the repository at this point in the history
  • Loading branch information
Aaron Patterson committed Apr 18, 2008
1 parent 90c5aab commit 576e1fe
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 1 deletion.
2 changes: 1 addition & 1 deletion ext/spidermonkey/conversions.c
Expand Up @@ -64,6 +64,7 @@ jsval convert_to_js(OurContext* context, VALUE ruby)
return convert_symbol_to_js(context, ruby);

case T_CLASS:
case T_ARRAY:
case T_HASH:
case T_MODULE:
case T_FILE:
Expand All @@ -83,7 +84,6 @@ jsval convert_to_js(OurContext* context, VALUE ruby)

// UNIMPLEMENTED BELOW THIS LINE

case T_ARRAY:

default:
Johnson_Error_raise("unknown ruby type in switch");
Expand Down
14 changes: 14 additions & 0 deletions ext/spidermonkey/js_land_proxy.c
Expand Up @@ -52,6 +52,19 @@ static JSBool get(JSContext* js_context, JSObject* obj, jsval id, jsval* retval)

char* key = JS_GetStringBytes(JSVAL_TO_STRING(id));
VALUE ruby_id = rb_intern(key);

if(!strcasecmp("__iterator__", key)) {
jsval nsJohnson;
assert(JS_GetProperty(context->js, context->global, "Johnson", &nsJohnson) || JSVAL_VOID == nsJohnson);

jsval nsGenerator;
assert(JS_GetProperty(context->js, JSVAL_TO_OBJECT(nsJohnson), "Generator", &nsGenerator) || JSVAL_VOID == nsGenerator);

jsval create;
assert(JS_GetProperty(context->js, JSVAL_TO_OBJECT(nsGenerator), "create", &create) || JSVAL_VOID == create);
*retval = create;
return JS_TRUE;
}

// if the Ruby object has a dynamic js property with a key
// matching the property we're looking for, pull the value out of
Expand Down Expand Up @@ -253,6 +266,7 @@ jsval make_js_land_proxy(OurContext* context, VALUE value)

assert(jsobj = JS_NewObject(context->js, klass, NULL, NULL));
assert(JS_SetPrivate(context->js, jsobj, (void*)value));

assert(JS_DefineFunction(context->js, jsobj, "__noSuchMethod__", method_missing, 2, 0));

js = OBJECT_TO_JSVAL(jsobj);
Expand Down
1 change: 1 addition & 0 deletions lib/johnson.rb
@@ -1,3 +1,4 @@
require 'generator'
require "johnson/version"

# the native SpiderMonkey extension
Expand Down
15 changes: 15 additions & 0 deletions lib/prelude.js
Expand Up @@ -23,4 +23,19 @@ Johnson.symbolize = function(string) {
return Johnson.symbolCache[string];
};

Johnson.Generator = function(enumerableProxy) {
this.generator = new Ruby.Generator(enumerableProxy);
};

Johnson.Generator.prototype.next = function() {
if(this.generator['next?']) {
return this.generator.next;
}
throw StopIteration;
}

Johnson.Generator.create = function() {
return new Johnson.Generator(this);
}

null; // no need to marshal a result
14 changes: 14 additions & 0 deletions test/johnson/spidermonkey/js_land_proxy_test.rb
Expand Up @@ -62,6 +62,20 @@ def setup
@context.evaluate(Johnson::PRELUDE)
end

def test_array_gets_returned
list = [1,2,3,4]

@context['alert'] = lambda { |x| p x }
@context['list'] = list
@context.evaluate("
var new_list = [];
for(x in list) {
new_list.push(x + 1);
}
")
assert_equal(list.map { |x| x + 1}, @context['new_list'].to_a)
end

def test_proxies_get_reused
@context["foo"] = @context["bar"] = Foo.new
assert_js_equal(true, "foo === bar")
Expand Down

0 comments on commit 576e1fe

Please sign in to comment.