#include "converters.h" #include "callbacks.h" #include "v8_ref.h" #include "v8_obj.h" #include "v8_cxt.h" #include "v8_template.h" using namespace v8; namespace { std::string UNDEFINED_STR("undefined"); } VALUE V8_To; VALUE V82RB(Handle& value) { convert_v8_to_rb_t convert; VALUE result; if(convert(value, result)) { return result; } if (value->IsArray()) { Local array(Array::Cast(*value)); VALUE rb_array = rb_ary_new2(array->Length()); for (unsigned int i = 0; i < array->Length(); i++) { Local value = array->Get(Integer::New(i)); rb_ary_push(rb_array, V82RB(value)); } return rb_array; } if (value->IsObject()) { Local object(Object::Cast(*value)); Local peer = object->GetHiddenValue(String::New("TheRubyRacer::RubyObject")); // Local peer = object->GetInternalField(0); if (peer.IsEmpty()) { VALUE context_ref = V8_Ref_Create(V8_C_Context, Context::GetCurrent()); // object->SetPointerInInternalField(1, (void *)context_ref); object->SetHiddenValue(String::New("TheRubyRacer::Context"), External::Wrap((void *)context_ref)); return V8_Ref_Create(V8_C_Object, value, context_ref); } else { return (VALUE)External::Unwrap(peer); } } return Qnil; } Local RB2V8(VALUE value) { VALUE valueClass = rb_class_of(value); if(valueClass == rb_cProc || valueClass == rb_cMethod) { Local t = FunctionTemplate::New(RacerRubyInvocationCallback, External::Wrap((void *)value)); return t->GetFunction(); } convert_rb_to_v8_t convert; Local result; if (convert(value, result)) { return result; } Local o = Racer_Create_V8_ObjectTemplate(value)->NewInstance(); o->SetHiddenValue(String::New("TheRubyRacer::RubyObject"), External::Wrap((void *) value)); // o->SetPointerInInternalField(0, (void*)value); return o; } std::string V82String(Handle& value) { convert_v8_to_string_t convert; std::string result; if(convert(value, result)) { return result; } if (value->IsObject()) { Local object(Object::Cast(*value)); Local str = object->ToString(); if(convert(value, result)) { return result; } } return UNDEFINED_STR; }