From 6902e37787d0cd0e77321906f53c649cac288e06 Mon Sep 17 00:00:00 2001 From: Charles Lowell Date: Thu, 3 May 2012 22:35:50 -0500 Subject: [PATCH] extract core conversion out of the type library --- ext/v8/convert.cc | 44 ++++++++++++++++++++++++ ext/v8/rr.h | 20 ++++++----- ext/v8/script.cc | 2 +- ext/v8/string.cc | 4 +-- ext/v8/value.cc | 86 +++-------------------------------------------- 5 files changed, 63 insertions(+), 93 deletions(-) create mode 100644 ext/v8/convert.cc diff --git a/ext/v8/convert.cc b/ext/v8/convert.cc new file mode 100644 index 0000000..acf0e58 --- /dev/null +++ b/ext/v8/convert.cc @@ -0,0 +1,44 @@ +#include "rr.h" + +namespace rr { + +Convert::Convert(v8::Handle 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; +} +} \ No newline at end of file diff --git a/ext/v8/rr.h b/ext/v8/rr.h index 419a668..d187767 100644 --- a/ext/v8/rr.h +++ b/ext/v8/rr.h @@ -6,16 +6,13 @@ namespace rr { -class Value { +class Convert { public: - Value(v8::Handle handle); - Value(VALUE value); + Convert(v8::Handle handle); virtual operator VALUE(); - virtual operator v8::Handle(); - static void Init(); -protected: + +private: v8::Handle value; - VALUE object; }; class GC { @@ -119,13 +116,20 @@ private: inline Script(VALUE value) : Ref(value) {} }; +class Value : public Ref { +public: + static void Init(); + + inline Value(VALUE value) : Ref(value) {} +}; + class String: public Ref { public: static void Init(); static VALUE New(VALUE self, VALUE value); static VALUE Utf8Value(VALUE self); - static VALUE ToRuby(v8::Handle value); + static VALUE Convert(v8::Handle value); inline String(VALUE value) : Ref(value) {} private: diff --git a/ext/v8/script.cc b/ext/v8/script.cc index b5ae070..f25d15c 100644 --- a/ext/v8/script.cc +++ b/ext/v8/script.cc @@ -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 \ No newline at end of file diff --git a/ext/v8/string.cc b/ext/v8/string.cc index f2e5d57..a53a3a0 100644 --- a/ext/v8/string.cc +++ b/ext/v8/string.cc @@ -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 string) { +VALUE String::Convert(v8::Handle string) { return String::create(string, String::Class); } diff --git a/ext/v8/value.cc b/ext/v8/value.cc index a406072..0b0e847 100644 --- a/ext/v8/value.cc +++ b/ext/v8/value.cc @@ -1,86 +1,8 @@ #include "rr.h" namespace rr { - Value::Value(v8::Handle 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() { - 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(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"); +} } \ No newline at end of file