Permalink
Browse files

#28 #23 arrays work in js land and so does for .. in

  • Loading branch information...
1 parent 90c5aab commit 576e1fe96c40ceb0313a62e54a145d1c0a01488a Aaron Patterson committed Apr 18, 2008
@@ -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:
@@ -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");
@@ -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
@@ -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);
View
@@ -1,3 +1,4 @@
+require 'generator'
require "johnson/version"
# the native SpiderMonkey extension
View
@@ -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
@@ -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")

0 comments on commit 576e1fe

Please sign in to comment.