mirror of
https://github.com/rubyjs/therubyracer
synced 2023-03-27 23:21:42 -04:00
add specs for converting ruby to javascript. Allow creating of opaque objects
This commit is contained in:
parent
3eb5c526b1
commit
0983e7f4a6
4 changed files with 52 additions and 10 deletions
|
@ -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
|
||||
|
|
1
v8.cpp
1
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -3,22 +3,26 @@
|
|||
#include <v8_data.h>
|
||||
#include <generic_data.h>
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
VALUE rb_cV8_JSObject;
|
||||
|
||||
using namespace v8;
|
||||
|
||||
v8_object::v8_object() : handle(Persistent<Object>(*Object::New())) {
|
||||
dispose = false;
|
||||
v8_object::v8_object() {
|
||||
Persistent<Context> context = Context::InContext() ? *Context::GetCurrent() : Context::New();
|
||||
Context::Scope cscope(context);
|
||||
HandleScope hscope;
|
||||
handle = (*Object::New());
|
||||
context.Dispose();
|
||||
}
|
||||
|
||||
v8_object::v8_object(Handle<Object>& object) : handle(Persistent<Object>(*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;
|
||||
|
|
|
@ -12,7 +12,6 @@ typedef struct v8_object {
|
|||
~v8_object();
|
||||
|
||||
v8::Persistent<v8::Object> handle;
|
||||
bool dispose;
|
||||
} v8_object;
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue