1
0
Fork 0
mirror of https://github.com/rubyjs/therubyracer synced 2023-03-27 23:21:42 -04:00

extract core conversion out of the type library

This commit is contained in:
Charles Lowell 2012-05-03 22:35:50 -05:00
parent 1ff80bd128
commit 6902e37787
5 changed files with 63 additions and 93 deletions

44
ext/v8/convert.cc Normal file
View file

@ -0,0 +1,44 @@
#include "rr.h"
namespace rr {
Convert::Convert(v8::Handle<v8::Value> value) {
this->value = value;
}
Convert::operator VALUE() {
if (value.IsEmpty() || value->IsUndefined() || value->IsNull()) {
return Qnil;
}
if (value->IsExternal()) {
return Qnil;
}
if (value->IsUint32()) {
return UINT2NUM(value->Uint32Value());
}
if (value->IsInt32()) {
return INT2FIX(value->Int32Value());
}
if (value->IsBoolean()) {
return value->BooleanValue() ? Qtrue : Qfalse;
}
if (value->IsNumber()) {
return rb_float_new(value->NumberValue());
}
if (value->IsString()) {
return String::Convert(value->ToString());
}
if (value->IsFunction()) {
// return Function(value);
}
if (value->IsArray()) {
// return Array(value);
}
if (value->IsDate()) {
// return rr_reflect_v8_date(value);
}
if (value->IsObject()) {
// return Object(value);
}
return Qnil;
}
}

View file

@ -6,16 +6,13 @@
namespace rr {
class Value {
class Convert {
public:
Value(v8::Handle<v8::Value> handle);
Value(VALUE value);
Convert(v8::Handle<v8::Value> handle);
virtual operator VALUE();
virtual operator v8::Handle<v8::Value>();
static void Init();
protected:
private:
v8::Handle<v8::Value> value;
VALUE object;
};
class GC {
@ -119,13 +116,20 @@ private:
inline Script(VALUE value) : Ref<v8::Script>(value) {}
};
class Value : public Ref<v8::Value> {
public:
static void Init();
inline Value(VALUE value) : Ref<v8::Value>(value) {}
};
class String: public Ref<v8::String> {
public:
static void Init();
static VALUE New(VALUE self, VALUE value);
static VALUE Utf8Value(VALUE self);
static VALUE ToRuby(v8::Handle<v8::String> value);
static VALUE Convert(v8::Handle<v8::String> value);
inline String(VALUE value) : Ref<v8::String>(value) {}
private:

View file

@ -15,7 +15,7 @@ VALUE Script::New(VALUE klass, VALUE source, VALUE filename) {
VALUE Script::Run(VALUE self) {
v8::HandleScope scope;
return Value(Script(self)->Run());
return Convert(Script(self)->Run());
}
} //namespace rr

View file

@ -13,7 +13,7 @@ void String::Init() {
VALUE String::New(VALUE StringClass, VALUE string) {
v8::HandleScope h;
return String::ToRuby(v8::String::New(RSTRING_PTR(string), (int)RSTRING_LEN(string)));
return String::Convert(v8::String::New(RSTRING_PTR(string), (int)RSTRING_LEN(string)));
}
VALUE String::Utf8Value(VALUE self) {
@ -22,7 +22,7 @@ VALUE String::Utf8Value(VALUE self) {
return rb_str_new(*v8::String::Utf8Value(str.GetHandle()), str->Utf8Length());
}
VALUE String::ToRuby(v8::Handle<v8::String> string) {
VALUE String::Convert(v8::Handle<v8::String> string) {
return String::create(string, String::Class);
}

View file

@ -1,86 +1,8 @@
#include "rr.h"
namespace rr {
Value::Value(v8::Handle<v8::Value> value) {
this->value = value;
}
Value::Value(VALUE object) {
this->object = object;
}
Value::operator VALUE() {
if (value.IsEmpty() || value->IsUndefined() || value->IsNull()) {
return Qnil;
}
if (value->IsExternal()) {
return Qnil;
}
if (value->IsUint32()) {
return UINT2NUM(value->Uint32Value());
}
if (value->IsInt32()) {
return INT2FIX(value->Int32Value());
}
if (value->IsBoolean()) {
return value->BooleanValue() ? Qtrue : Qfalse;
}
if (value->IsNumber()) {
return rb_float_new(value->NumberValue());
}
if (value->IsString()) {
return String::ToRuby(value->ToString());
}
if (value->IsFunction()) {
// return Function(value);
}
if (value->IsArray()) {
// return Array(value);
}
if (value->IsDate()) {
// return rr_reflect_v8_date(value);
}
if (value->IsObject()) {
// return Object(value);
}
return Qnil;
}
Value::operator v8::Handle<v8::Value>() {
switch (TYPE(object)) {
case T_FIXNUM:
// TODO: use this conversion if value will fit in 32 bits.
// return Integer::New(FIX2LONG(value));
case T_FLOAT:
// return Number::New(NUM2DBL(value));
case T_STRING:
return v8::String::New(RSTRING_PTR(object), (int)RSTRING_LEN(object));
case T_NIL:
return v8::Null();
case T_TRUE:
return v8::True();
case T_FALSE:
return v8::False();
case T_DATA:
return Ref<v8::Value>(object);
case T_OBJECT:
case T_CLASS:
case T_ICLASS:
case T_MODULE:
case T_REGEXP:
case T_MATCH:
case T_ARRAY:
case T_HASH:
case T_STRUCT:
case T_BIGNUM:
case T_FILE:
case T_SYMBOL:
// case T_BLKTAG: (not in 1.9)
case T_UNDEF:
// case T_VARMAP: (not in 1.9)
// case T_SCOPE: (not in 1.9)
case T_NODE:
default:
rb_warn("unknown conversion to V8 for: %s", RSTRING_PTR(rb_inspect(object)));
return v8::String::New("Undefined Conversion");
}
return v8::Undefined();
}
void Value::Init() {
ClassBuilder("Value");
}
}