ext/v8/v8_obj.cpp in therubyracer-0.6.3 vs ext/v8/v8_obj.cpp in therubyracer-0.7.0.pre

- old
+ new

@@ -1,58 +1,89 @@ #include "v8_obj.h" #include "v8_ref.h" +#include "v8_value.h" +#include "v8_template.h" +#include "v8_external.h" #include "converters.h" using namespace v8; #include <cstdio> -VALUE V8_C_Object; +VALUE rr_cV8_C_Object; 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 { + return rr_v82rb(obj->Get(rr_rb2v8(key)->ToString())); + } + } + + VALUE New(VALUE clazz) { + HandleScope handles; + if (!Context::InContext()) { + rb_raise(rb_eScriptError, "Object::New() called without an entered Context"); + return Qnil; + } + return V8_Ref_Create(clazz, Object::New()); + } + + VALUE Set(VALUE self, VALUE key, VALUE value) { + HandleScope handles; + Local<Object> obj = unwrap(self); + if (rb_obj_is_kind_of(key, rb_cNumeric)) { + return rr_v82rb(obj->Set(NUM2UINT(key), rr_rb2v8(value))); + } else { + return rr_v82rb(obj->Set(rr_rb2v8(key), rr_rb2v8(value))); + } + } -VALUE v8_Object_New(VALUE clazz) { - HandleScope handles; - return V8_Ref_Create(clazz, Object::New()); + VALUE GetPropertyNames(VALUE self) { + HandleScope handles; + Local<Object> object = unwrap(self); + Local<Value> names = object->GetPropertyNames(); + return rr_v82rb(names); + } + 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; + } } -VALUE v8_Object_Get(VALUE self, VALUE key) { - HandleScope handles; - Local<Object> obj = unwrap(self); - VALUE keystr = rb_str_to_str(key); - Local<Value> value = obj->Get(RB2V8(keystr)); - return V82RB(value); +void rr_init_obj() { + rr_cV8_C_Object = rr_define_class("Object", rr_cV8_C_Value); + rb_define_attr(rr_cV8_C_Object, "context", 1, 0); + rr_define_singleton_method(rr_cV8_C_Object, "New", New, 0); + rr_define_method(rr_cV8_C_Object, "Get", Get, 1); + rr_define_method(rr_cV8_C_Object, "Set", Set, 2); + rr_define_method(rr_cV8_C_Object, "GetPropertyNames", GetPropertyNames, 0); + rr_define_method(rr_cV8_C_Object, "SetHiddenValue", SetHiddenValue, 2); } -VALUE v8_Object_Set(VALUE self, VALUE key, VALUE value) { - HandleScope handles; - Local<Object> obj = unwrap(self); - - VALUE keystr = rb_funcall(key, rb_intern("to_s"), 0); - obj->Set(RB2V8(keystr), RB2V8(value)); - return Qnil; +VALUE rr_reflect_v8_object(Handle<Value> value) { + Local<Object> object(Object::Cast(*value)); + Local<Value> peer = object->GetHiddenValue(String::New("TheRubyRacer::RubyObject")); + return peer.IsEmpty() ? rr_v8_ref_create(rr_cV8_C_Object, object) : (VALUE)External::Unwrap(peer); } -VALUE v8_Object_GetPropertyNames(VALUE self) { - HandleScope handles; - Local<Object> object = unwrap(self); - Local<Value> names = object->GetPropertyNames(); - return V82RB(names); -} -VALUE v8_Object_context(VALUE self) { - HandleScope handles; - Local<Object> object = unwrap(self); - Local<Value> cxt = object->GetHiddenValue(String::New("TheRubyRacer::Context")); - return cxt.IsEmpty() ? Qnil : (VALUE)External::Unwrap(cxt); +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"), rr_v8_external_create(value)); + return o; } - -VALUE v8_Object_ToString(VALUE self) { - HandleScope handles; - Local<Object> object = unwrap(self); - Local<Value> string = object->ToString(); - return V82RB(string); -} \ No newline at end of file