2009-12-22 14:44:11 +02: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-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;
|
|
|
|
if(convert(value, result)) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (value->IsObject()) {
|
2010-01-09 10:43:08 -05:00
|
|
|
Local<Object> object(Object::Cast(*value));
|
2009-12-25 23:47:42 -05:00
|
|
|
return V8_Ref_Create(V8_C_Object, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Qnil;
|
2009-12-25 22:16:11 -05:00
|
|
|
}
|
|
|
|
|
2010-01-10 00:13:03 +02: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-10 00:13:03 +02:00
|
|
|
Local<Value> result;
|
|
|
|
if (convert(value, result)) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
Local<ObjectTemplate> tmpl = ObjectTemplate::New();
|
|
|
|
VALUE methods = rb_funcall(value, rb_intern("public_methods"), 1, Qfalse);
|
|
|
|
int len = RARRAY(methods)->len;
|
|
|
|
for (int i = 0; i < len; i++) {
|
|
|
|
VALUE method_name = RARRAY(methods)->ptr[i];
|
|
|
|
VALUE method = rb_funcall(value, rb_intern("method"), 1, method_name);
|
|
|
|
Local<String> keystr = (String *)*RB2V8(method_name);
|
|
|
|
tmpl->Set(keystr, RB2V8(method));
|
|
|
|
}
|
|
|
|
return tmpl->NewInstance();
|
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;
|
2009-12-25 22:16:11 -05:00
|
|
|
}
|