2009-12-22 07:44:11 -05:00
|
|
|
#include "converters.h"
|
2010-01-09 10:43:08 -05:00
|
|
|
#include "callbacks.h"
|
2009-12-25 23:47:42 -05:00
|
|
|
#include "v8_ref.h"
|
|
|
|
#include "v8_obj.h"
|
2010-01-13 17:51:46 -05:00
|
|
|
#include "v8_cxt.h"
|
2010-04-21 19:09:13 -04:00
|
|
|
#include "v8_func.h"
|
2010-01-17 17:25:40 -05:00
|
|
|
#include "v8_template.h"
|
2009-12-25 23:47:42 -05:00
|
|
|
|
2010-01-09 10:43:08 -05:00
|
|
|
using namespace v8;
|
|
|
|
|
2009-12-25 23:47:42 -05:00
|
|
|
namespace {
|
|
|
|
std::string UNDEFINED_STR("undefined");
|
|
|
|
}
|
|
|
|
|
2010-01-09 10:43:08 -05:00
|
|
|
VALUE V82RB(Handle<Value>& value) {
|
2009-12-25 22:16:11 -05:00
|
|
|
convert_v8_to_rb_t convert;
|
2009-12-25 23:47:42 -05:00
|
|
|
VALUE result;
|
2010-05-13 17:53:09 -04:00
|
|
|
VALUE type = rr_cV8_C_Object;
|
2009-12-25 23:47:42 -05:00
|
|
|
if(convert(value, result)) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2010-01-10 13:56:10 -05:00
|
|
|
if (value->IsArray()) {
|
|
|
|
Local<Array> array(Array::Cast(*value));
|
|
|
|
VALUE rb_array = rb_ary_new2(array->Length());
|
|
|
|
for (unsigned int i = 0; i < array->Length(); i++) {
|
|
|
|
Local<Value> value = array->Get(Integer::New(i));
|
|
|
|
rb_ary_push(rb_array, V82RB(value));
|
|
|
|
}
|
|
|
|
return rb_array;
|
|
|
|
}
|
|
|
|
|
2010-04-21 19:09:13 -04:00
|
|
|
if (value->IsFunction()) {
|
2010-05-13 17:53:09 -04:00
|
|
|
type = rr_cV8_C_Function;
|
2010-04-21 19:09:13 -04:00
|
|
|
}
|
|
|
|
|
2009-12-25 23:47:42 -05:00
|
|
|
if (value->IsObject()) {
|
2010-01-09 10:43:08 -05:00
|
|
|
Local<Object> object(Object::Cast(*value));
|
2010-01-13 10:13:13 -05:00
|
|
|
Local<Value> peer = object->GetHiddenValue(String::New("TheRubyRacer::RubyObject"));
|
|
|
|
if (peer.IsEmpty()) {
|
2010-01-13 17:51:46 -05:00
|
|
|
VALUE context_ref = V8_Ref_Create(V8_C_Context, Context::GetCurrent());
|
|
|
|
object->SetHiddenValue(String::New("TheRubyRacer::Context"), External::Wrap((void *)context_ref));
|
2010-04-21 19:09:13 -04:00
|
|
|
return V8_Ref_Create(type, value, context_ref);
|
2010-01-13 10:13:13 -05:00
|
|
|
} else {
|
|
|
|
return (VALUE)External::Unwrap(peer);
|
|
|
|
}
|
2009-12-25 23:47:42 -05:00
|
|
|
}
|
|
|
|
return Qnil;
|
2009-12-25 22:16:11 -05:00
|
|
|
}
|
|
|
|
|
2010-01-09 17:13:03 -05:00
|
|
|
Local<Value> RB2V8(VALUE value) {
|
2010-01-09 10:43:08 -05:00
|
|
|
VALUE valueClass = rb_class_of(value);
|
2010-01-09 10:52:30 -05:00
|
|
|
if(valueClass == rb_cProc || valueClass == rb_cMethod) {
|
2010-01-09 10:43:08 -05:00
|
|
|
Local<FunctionTemplate> t = FunctionTemplate::New(RacerRubyInvocationCallback, External::Wrap((void *)value));
|
2010-01-09 10:52:30 -05:00
|
|
|
return t->GetFunction();
|
2010-01-09 10:43:08 -05:00
|
|
|
}
|
2009-12-25 22:16:11 -05:00
|
|
|
convert_rb_to_v8_t convert;
|
2010-01-09 17:13:03 -05:00
|
|
|
Local<Value> result;
|
|
|
|
if (convert(value, result)) {
|
|
|
|
return result;
|
|
|
|
}
|
2010-01-17 17:25:40 -05:00
|
|
|
Local<Object> o = Racer_Create_V8_ObjectTemplate(value)->NewInstance();
|
|
|
|
o->SetHiddenValue(String::New("TheRubyRacer::RubyObject"), External::Wrap((void *) value));
|
|
|
|
return o;
|
2009-12-25 22:16:11 -05:00
|
|
|
}
|
|
|
|
|
2010-01-09 10:43:08 -05:00
|
|
|
std::string V82String(Handle<Value>& value) {
|
2009-12-25 22:16:11 -05:00
|
|
|
convert_v8_to_string_t convert;
|
2009-12-25 23:47:42 -05:00
|
|
|
std::string result;
|
|
|
|
if(convert(value, result)) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (value->IsObject()) {
|
2010-01-09 10:43:08 -05:00
|
|
|
Local<Object> object(Object::Cast(*value));
|
|
|
|
Local<String> str = object->ToString();
|
2009-12-25 23:47:42 -05:00
|
|
|
if(convert(value, result)) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return UNDEFINED_STR;
|
2010-01-17 17:25:40 -05:00
|
|
|
}
|