2009-12-18 02:48:06 -05:00
|
|
|
#include "v8_obj.h"
|
|
|
|
#include "v8_ref.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"
|
2009-12-18 02:48:06 -05:00
|
|
|
#include "converters.h"
|
|
|
|
|
|
|
|
using namespace v8;
|
|
|
|
|
2009-12-25 21:59:46 -05:00
|
|
|
#include <cstdio>
|
|
|
|
|
2010-05-12 18:30:53 -04:00
|
|
|
VALUE rr_cV8_C_Object;
|
2009-12-18 02:48:06 -05:00
|
|
|
|
2010-05-17 11:30:31 -04:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
Local<Object> unwrap(VALUE robj) {
|
|
|
|
return V8_Ref_Get<Object>(robj);
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE Get(VALUE self, VALUE key) {
|
|
|
|
HandleScope handles;
|
|
|
|
Local<Object> obj(unwrap(self));
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2010-05-18 14:00:28 -04:00
|
|
|
|
|
|
|
VALUE New(VALUE clazz) {
|
|
|
|
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;
|
|
|
|
}
|
2010-05-18 14:00:28 -04:00
|
|
|
return V8_Ref_Create(clazz, Object::New());
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE Set(VALUE self, VALUE key, VALUE value) {
|
|
|
|
HandleScope handles;
|
|
|
|
Local<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;
|
|
|
|
Local<Object> object = unwrap(self);
|
|
|
|
Local<Value> names = object->GetPropertyNames();
|
|
|
|
return rr_v82rb(names);
|
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");
|
|
|
|
}
|
|
|
|
//TODO: need to store a reference here? what's the best way
|
|
|
|
// rr_v8_ref_setref(self, "RubyPeer", )
|
|
|
|
return Qnil;
|
|
|
|
}
|
2010-05-17 11:30:31 -04:00
|
|
|
}
|
|
|
|
|
2010-05-10 08:56:52 -04:00
|
|
|
void rr_init_obj() {
|
2010-05-12 18:30:53 -04:00
|
|
|
rr_cV8_C_Object = rr_define_class("Object", rr_cV8_C_Value);
|
2010-05-17 08:13:23 -04:00
|
|
|
rb_define_attr(rr_cV8_C_Object, "context", 1, 0);
|
2010-05-18 14:00:28 -04:00
|
|
|
rr_define_singleton_method(rr_cV8_C_Object, "New", New, 0);
|
2010-05-17 11:30:31 -04:00
|
|
|
rr_define_method(rr_cV8_C_Object, "Get", Get, 1);
|
2010-05-18 14:00:28 -04:00
|
|
|
rr_define_method(rr_cV8_C_Object, "Set", Set, 2);
|
|
|
|
rr_define_method(rr_cV8_C_Object, "GetPropertyNames", GetPropertyNames, 0);
|
2010-05-22 02:23:22 -04:00
|
|
|
rr_define_method(rr_cV8_C_Object, "SetHiddenValue", SetHiddenValue, 2);
|
2010-05-10 08:56:52 -04:00
|
|
|
}
|
|
|
|
|
2010-05-13 18:14:54 -04:00
|
|
|
VALUE rr_reflect_v8_object(Handle<Value> value) {
|
2010-05-13 20:43:46 -04:00
|
|
|
Local<Object> object(Object::Cast(*value));
|
2010-05-17 09:07:43 -04:00
|
|
|
Local<Value> peer = object->GetHiddenValue(String::New("TheRubyRacer::RubyObject"));
|
2010-05-25 16:49:43 -04:00
|
|
|
return peer.IsEmpty() ? rr_v8_ref_create(rr_cV8_C_Object, object) : (VALUE)External::Unwrap(peer);
|
2010-05-17 09:07:43 -04:00
|
|
|
}
|
|
|
|
|
2010-05-17 11:30:31 -04:00
|
|
|
|
2010-05-17 09:07:43 -04:00
|
|
|
v8::Handle<v8::Value> rr_reflect_rb_object(VALUE value) {
|
|
|
|
Local<Object> o = Racer_Create_V8_ObjectTemplate(value)->NewInstance();
|
|
|
|
o->SetHiddenValue(String::New("TheRubyRacer::RubyObject"), External::Wrap((void *) value));
|
|
|
|
return o;
|
2010-05-13 18:14:54 -04:00
|
|
|
}
|