From 0983e7f4a6b5a14ac971488893df4abee44337f1 Mon Sep 17 00:00:00 2001 From: Charles Lowell Date: Sun, 1 Nov 2009 11:53:19 -0600 Subject: [PATCH] add specs for converting ruby to javascript. Allow creating of opaque objects --- spec/therubyracer_spec.rb | 40 ++++++++++++++++++++++++++++++++++++++- v8.cpp | 1 + v8_object.cpp | 20 ++++++++++++-------- v8_object.h | 1 - 4 files changed, 52 insertions(+), 10 deletions(-) diff --git a/spec/therubyracer_spec.rb b/spec/therubyracer_spec.rb index 018523d..b948ad8 100644 --- a/spec/therubyracer_spec.rb +++ b/spec/therubyracer_spec.rb @@ -43,15 +43,53 @@ describe "The Ruby Racer" do end it "can pass objects back to ruby" do - eval("({foo: 'bar', baz: 'bang'})").tap do |object| + eval("({foo: 'bar', baz: 'bang', '5': 5, segfault: {}})").tap do |object| object.should_not be_nil object['foo'].should == 'bar' object['baz'].should == 'bang' + object['5'].should == 5 + object['segfault'].should be_kind_of(V8::JSObject) end end end + describe "Passing Objects from Ruby to Javascript" do + + it "can pass strings to javascript" do + eval("this", "foo").should == "foo" + end + + it "can pass large strings from ruby to javascript" do + lorem = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis faucibus, diam vel pellentesque aliquet, nisl sapien molestie eros, vitae vehicula libero massa vel neque. Phasellus tempor pharetra ipsum vel venenatis. Quisque vitae nisl vitae quam mattis pellentesque et in sapien. Sed at lectus quis eros pharetra feugiat non ac neque. Vivamus lacus eros, feugiat at volutpat at, viverra id nisl. Vivamus ac dolor eleifend libero venenatis pharetra ut iaculis arcu. Donec neque nibh, vehicula non porta a, consectetur eu erat. Sed eleifend, metus vel euismod placerat, lectus lectus sollicitudin nisl, ac elementum sem quam nec dolor. In hac habitasse platea dictumst. Proin vitae suscipit orci. Suspendisse a ipsum vel lorem tempus scelerisque et vitae neque. Proin sodales, tellus sit amet consequat cursus, odio massa ultricies enim, eu fermentum velit lectus in lacus. Quisque eu porttitor diam. Nunc felis purus, facilisis non tristique ac, pulvinar nec nulla. Duis dolor risus, egestas nec tristique ac, ullamcorper cras amet." + eval("this", lorem).should == lorem + end + + it "can pass the empty string from ruby to javascript" do + eval("this", "").should == "" + end + + it "can pass doubles from ruby to javascript" do + eval("this", 3.14).should == 3.14 + end + + it "can pass integers from ruby to javascript" do + eval("this", 5).should == 5 + end + + it "can pass boolean values from ruby to javascript" do + eval("this", true).should be(true) + eval("this", false).should be(false) + end + + it "can pass an object value from ruby to javascript" do + V8::JSObject.new.tap do |o| + eval("this", o).should be(o) + end + end + + end + def eval(str) @cxt.eval(str) end diff --git a/v8.cpp b/v8.cpp index afe1748..bb5c18c 100644 --- a/v8.cpp +++ b/v8.cpp @@ -42,6 +42,7 @@ extern "C" { rb_cV8_JSObject = rb_define_class_under(rb_mModule, "JSObject", rb_cObject); rb_define_alloc_func(rb_cV8_JSObject, v8_object_allocate); rb_define_method(rb_cV8_JSObject, "[]", (VALUE(*)(...)) v8_object_hash_access, 1); + // rb_define_method(rb_cV8_JSObject, "[]=", (VALUE(*)(...)) v8_object_hash_assignment, 2); } } diff --git a/v8_object.cpp b/v8_object.cpp index bc7ce65..72f6b1e 100644 --- a/v8_object.cpp +++ b/v8_object.cpp @@ -3,22 +3,26 @@ #include #include +#include + VALUE rb_cV8_JSObject; using namespace v8; -v8_object::v8_object() : handle(Persistent(*Object::New())) { - dispose = false; +v8_object::v8_object() { + Persistent context = Context::InContext() ? *Context::GetCurrent() : Context::New(); + Context::Scope cscope(context); + HandleScope hscope; + handle = (*Object::New()); + context.Dispose(); } + v8_object::v8_object(Handle& object) : handle(Persistent(*object)) { - dispose = true; -} -v8_object::~v8_object() { - if (dispose) { - handle.Dispose(); - } } +v8_object::~v8_object() { + handle.Dispose(); +} VALUE v8_object_hash_access(VALUE self, VALUE key) { v8_object* object = 0; diff --git a/v8_object.h b/v8_object.h index baf9579..6913de4 100644 --- a/v8_object.h +++ b/v8_object.h @@ -12,7 +12,6 @@ typedef struct v8_object { ~v8_object(); v8::Persistent handle; - bool dispose; } v8_object;