1
0
Fork 0
mirror of https://github.com/rubyjs/therubyracer synced 2023-03-27 23:21:42 -04:00

access javascript objects from ruby

This commit is contained in:
Charles Lowell 2009-10-22 23:27:41 -05:00
parent c0d8d85b95
commit 3eb5c526b1
6 changed files with 88 additions and 6 deletions

View file

@ -3,6 +3,7 @@
#include <ruby.h>
#include <v8.h>
#include <v8_object.h>
#include <stdio.h>
#include <string>
@ -89,8 +90,8 @@ class RubyDest {
return Qnil;
}
VALUE pushObject(v8::Handle<v8::Value>& value, const char* name = 0) {
return Qnil;
VALUE pushObject(v8::Handle<v8::Object>& value, const char* name = 0) {
return Data_Wrap_Struct(rb_cV8_JSObject, v8_object_mark, v8_object_free, new v8_object(value));
}
};

View file

@ -48,10 +48,10 @@ describe "The Ruby Racer" do
object['foo'].should == 'bar'
object['baz'].should == 'bang'
end
end
end
end
def eval(str)
@cxt.eval(str)
end

4
v8.cpp
View file

@ -38,6 +38,10 @@ extern "C" {
rb_cV8 = rb_define_class_under(rb_mModule, "Context", rb_cObject);
rb_define_alloc_func(rb_cV8, v8_allocate);
rb_define_method(rb_cV8, "eval", (VALUE(*)(...)) eval, 1);
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);
}
}

View file

@ -56,7 +56,8 @@ template<class T, class R> class V8HandleSource {
}
if (value->IsObject()) {
return dest.pushObject(value, name);
v8::Local<v8::Object> object(v8::Object::Cast(*value));
return dest.pushObject(object, name);
}
return dest.pushNull(name);

49
v8_object.cpp Normal file
View file

@ -0,0 +1,49 @@
#include <v8_object.h>
#include <ruby_data.h>
#include <v8_data.h>
#include <generic_data.h>
VALUE rb_cV8_JSObject;
using namespace v8;
v8_object::v8_object() : handle(Persistent<Object>(*Object::New())) {
dispose = false;
}
v8_object::v8_object(Handle<Object>& object) : handle(Persistent<Object>(*object)) {
dispose = true;
}
v8_object::~v8_object() {
if (dispose) {
handle.Dispose();
}
}
VALUE v8_object_hash_access(VALUE self, VALUE key) {
v8_object* object = 0;
Data_Get_Struct(self, struct v8_object, object);
RubyValueSource<StringDest, std::string> tostring;
const std::string cppkey(tostring.push(key));
HandleScope scope;
Handle<Value> result = object->handle->Get(String::New(cppkey.c_str()));
V8HandleSource<RubyDest, VALUE> toValue;
return toValue.push(result);
}
VALUE v8_object_allocate(VALUE clazz) {
v8_object *wrapper = new v8_object;
return Data_Wrap_Struct(clazz, v8_object_mark, v8_object_free, wrapper);
}
void v8_object_mark(v8_object *o) {
}
void v8_object_free(v8_object *o) {
delete o;
}

27
v8_object.h Normal file
View file

@ -0,0 +1,27 @@
#ifndef __RUBY_V8_OBJECT__
#define __RUBY_V8_OBJECT__
#include <ruby.h>
#include <v8.h>
extern VALUE rb_cV8_JSObject;
typedef struct v8_object {
v8_object();
v8_object(v8::Handle<v8::Object>& object);
~v8_object();
v8::Persistent<v8::Object> handle;
bool dispose;
} v8_object;
VALUE v8_object_hash_access(VALUE self, VALUE key);
//memory management
VALUE v8_object_allocate(VALUE clazz);
void v8_object_mark(v8_object *object);
void v8_object_free(v8_object *object);
#endif