2011-04-11 00:19:26 -04:00
|
|
|
#include "v8_handle.h"
|
2011-04-13 17:20:48 -04:00
|
|
|
#include "v8_weakref.h"
|
2011-04-11 10:34:25 -04:00
|
|
|
#include "v8_object.h"
|
2010-05-13 17:53:09 -04:00
|
|
|
#include "v8_value.h"
|
2010-05-17 09:07:43 -04:00
|
|
|
#include "v8_template.h"
|
2010-05-26 12:54:56 -04:00
|
|
|
#include "v8_external.h"
|
2009-12-18 02:48:06 -05:00
|
|
|
|
|
|
|
using namespace v8;
|
|
|
|
|
2009-12-25 21:59:46 -05:00
|
|
|
#include <cstdio>
|
|
|
|
|
2010-05-17 11:30:31 -04:00
|
|
|
namespace {
|
2011-04-11 14:32:14 -04:00
|
|
|
|
|
|
|
VALUE ObjectClass;
|
|
|
|
|
2011-04-11 00:19:26 -04:00
|
|
|
Persistent<Object>& unwrap(VALUE object) {
|
|
|
|
return rr_v8_handle<Object>(object);
|
2010-05-17 11:30:31 -04:00
|
|
|
}
|
2011-04-11 14:32:14 -04:00
|
|
|
|
2010-05-17 11:30:31 -04:00
|
|
|
VALUE Get(VALUE self, VALUE key) {
|
|
|
|
HandleScope handles;
|
2011-04-11 00:19:26 -04:00
|
|
|
Persistent<Object> obj(unwrap(self));
|
2010-05-17 11:30:31 -04:00
|
|
|
if (rb_obj_is_kind_of(key, rb_cNumeric)) {
|
|
|
|
return rr_v82rb(obj->Get(NUM2UINT(key)));
|
|
|
|
} else {
|
2010-05-19 08:55:11 -04:00
|
|
|
return rr_v82rb(obj->Get(rr_rb2v8(key)->ToString()));
|
2010-05-17 11:30:31 -04:00
|
|
|
}
|
|
|
|
}
|
2011-04-11 14:32:14 -04:00
|
|
|
|
2010-06-07 03:50:33 -04:00
|
|
|
VALUE New(VALUE rbclass) {
|
2010-05-18 14:00:28 -04:00
|
|
|
HandleScope handles;
|
2010-05-19 08:55:11 -04:00
|
|
|
if (!Context::InContext()) {
|
2010-05-22 02:23:22 -04:00
|
|
|
rb_raise(rb_eScriptError, "Object::New() called without an entered Context");
|
2010-05-19 08:55:11 -04:00
|
|
|
return Qnil;
|
|
|
|
}
|
2011-04-11 00:19:26 -04:00
|
|
|
return rr_v8_handle_new(rbclass, Object::New());
|
2010-05-18 14:00:28 -04:00
|
|
|
}
|
2011-04-11 14:32:14 -04:00
|
|
|
|
2010-05-18 14:00:28 -04:00
|
|
|
VALUE Set(VALUE self, VALUE key, VALUE value) {
|
|
|
|
HandleScope handles;
|
2011-04-11 00:19:26 -04:00
|
|
|
Persistent<Object> obj = unwrap(self);
|
2010-05-19 08:55:11 -04:00
|
|
|
if (rb_obj_is_kind_of(key, rb_cNumeric)) {
|
2010-05-22 02:23:22 -04:00
|
|
|
return rr_v82rb(obj->Set(NUM2UINT(key), rr_rb2v8(value)));
|
2010-05-19 08:55:11 -04:00
|
|
|
} else {
|
|
|
|
return rr_v82rb(obj->Set(rr_rb2v8(key), rr_rb2v8(value)));
|
|
|
|
}
|
2010-05-18 14:00:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
VALUE GetPropertyNames(VALUE self) {
|
|
|
|
HandleScope handles;
|
2011-04-11 14:32:14 -04:00
|
|
|
Persistent<Object> object = unwrap(self);
|
2010-05-18 14:00:28 -04:00
|
|
|
Local<Value> names = object->GetPropertyNames();
|
|
|
|
return rr_v82rb(names);
|
2010-05-22 02:23:22 -04:00
|
|
|
}
|
2011-05-10 10:36:33 -04:00
|
|
|
VALUE GetIdentityHash(VALUE self) {
|
|
|
|
return rr_v82rb(unwrap(self)->GetIdentityHash());
|
|
|
|
}
|
2010-05-22 02:23:22 -04:00
|
|
|
VALUE SetHiddenValue(VALUE self, VALUE key, VALUE value) {
|
|
|
|
HandleScope scope;
|
|
|
|
if (Context::InContext()) {
|
|
|
|
unwrap(self)->SetHiddenValue(rr_rb2v8(key)->ToString(), rr_rb2v8(value));
|
|
|
|
} else {
|
|
|
|
rb_raise(rb_eScriptError, "Object::SetHiddenValue() called without an entered Context");
|
|
|
|
}
|
|
|
|
return Qnil;
|
|
|
|
}
|
2010-05-31 03:06:04 -04:00
|
|
|
VALUE GetHiddenValue(VALUE self, VALUE key) {
|
|
|
|
HandleScope scope;
|
|
|
|
return rr_v82rb(unwrap(self)->GetHiddenValue(rr_rb2v8(key)->ToString()));
|
|
|
|
}
|
2010-08-05 12:34:23 -04:00
|
|
|
VALUE GetPrototype(VALUE self) {
|
|
|
|
HandleScope scope;
|
|
|
|
return rr_v82rb(unwrap(self)->GetPrototype());
|
|
|
|
}
|
|
|
|
VALUE SetPrototype(VALUE self, VALUE prototype) {
|
|
|
|
HandleScope scope;
|
2011-04-11 00:19:26 -04:00
|
|
|
Handle<Value> proto(rr_rb2v8(prototype));
|
2010-08-17 08:32:05 -04:00
|
|
|
return rr_v82rb(unwrap(self)->SetPrototype(rr_rb2v8(prototype)));
|
2010-08-05 12:34:23 -04:00
|
|
|
}
|
2010-05-17 11:30:31 -04:00
|
|
|
}
|
|
|
|
|
2011-04-11 14:32:14 -04:00
|
|
|
VALUE rr_v8_object_class() {
|
|
|
|
return ObjectClass;
|
|
|
|
}
|
|
|
|
|
2011-04-11 10:34:25 -04:00
|
|
|
void rr_init_object() {
|
2011-04-11 14:32:14 -04:00
|
|
|
ObjectClass = rr_define_class("Object", rr_v8_value_class());
|
|
|
|
rr_define_singleton_method(ObjectClass, "New", New, 0);
|
|
|
|
rr_define_method(ObjectClass, "Get", Get, 1);
|
|
|
|
rr_define_method(ObjectClass, "Set", Set, 2);
|
|
|
|
rr_define_method(ObjectClass, "GetPropertyNames", GetPropertyNames, 0);
|
2011-05-10 10:36:33 -04:00
|
|
|
rr_define_method(ObjectClass, "GetIdentityHash", GetIdentityHash, 0);
|
2011-04-11 14:32:14 -04:00
|
|
|
rr_define_method(ObjectClass, "GetHiddenValue", GetHiddenValue, 1);
|
|
|
|
rr_define_method(ObjectClass, "SetHiddenValue", SetHiddenValue, 2);
|
|
|
|
rr_define_method(ObjectClass, "GetPrototype", GetPrototype, 0);
|
|
|
|
rr_define_method(ObjectClass, "SetPrototype", SetPrototype, 1);
|
2010-05-10 08:56:52 -04:00
|
|
|
}
|
|
|
|
|
2011-04-22 09:50:01 -04:00
|
|
|
VALUE rr_reflect_v8_object_as(Handle<Value> value, VALUE ruby_class) {
|
|
|
|
Handle<Object> object = Handle<Object>::Cast(value);
|
2011-04-13 17:20:48 -04:00
|
|
|
VALUE handle;
|
|
|
|
v8_weakref* backref;
|
|
|
|
Local<Value> holder = object->GetHiddenValue(String::NewSymbol("TheRubyRacer::Backref"));
|
|
|
|
if (holder.IsEmpty()) {
|
|
|
|
handle = rr_v8_handle_new(ruby_class, object);
|
|
|
|
backref = new v8_weakref(handle);
|
|
|
|
object->SetHiddenValue(String::NewSymbol("TheRubyRacer::Backref"), backref->external);
|
|
|
|
} else {
|
|
|
|
backref = (v8_weakref*)External::Unwrap(holder);
|
|
|
|
handle = backref->get();
|
|
|
|
if (!RTEST(handle)) {
|
|
|
|
handle = rr_v8_handle_new(ruby_class, object);
|
|
|
|
backref->set(handle);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return handle;
|
|
|
|
}
|
|
|
|
|
2010-05-13 18:14:54 -04:00
|
|
|
VALUE rr_reflect_v8_object(Handle<Value> value) {
|
2011-04-22 09:50:01 -04:00
|
|
|
return rr_reflect_v8_object_as(value, ObjectClass);
|
2010-05-17 09:07:43 -04:00
|
|
|
}
|
|
|
|
|