From 7f46bf908d240e48302f2ef5fb67aed5c347259f Mon Sep 17 00:00:00 2001 From: Charles Lowell Date: Thu, 13 May 2010 16:53:09 -0500 Subject: [PATCH] code compiles now, still broken though --- ext/v8/converters.cpp | 4 ++-- ext/v8/rr.cpp | 21 +++++++++++++++++++++ ext/v8/rr.h | 29 ++++++++++++++++------------- ext/v8/v8_cxt.cpp | 2 +- ext/v8/v8_func.cpp | 40 +++++++++++++++++++++++++++++++++------- ext/v8/v8_func.h | 5 ++--- ext/v8/v8_obj.cpp | 13 +++++++------ ext/v8/v8_template.cpp | 2 +- lib/v8/to.rb | 13 ------------- 9 files changed, 83 insertions(+), 46 deletions(-) diff --git a/ext/v8/converters.cpp b/ext/v8/converters.cpp index 05c9632..a57807a 100644 --- a/ext/v8/converters.cpp +++ b/ext/v8/converters.cpp @@ -15,7 +15,7 @@ namespace { VALUE V82RB(Handle& value) { convert_v8_to_rb_t convert; VALUE result; - VALUE type = V8_C_Object; + VALUE type = rr_cV8_C_Object; if(convert(value, result)) { return result; } @@ -31,7 +31,7 @@ VALUE V82RB(Handle& value) { } if (value->IsFunction()) { - type = V8_C_Function; + type = rr_cV8_C_Function; } if (value->IsObject()) { diff --git a/ext/v8/rr.cpp b/ext/v8/rr.cpp index 8172283..7629311 100644 --- a/ext/v8/rr.cpp +++ b/ext/v8/rr.cpp @@ -68,6 +68,9 @@ VALUE rr_v82rb(Handle value) { VALUE rr_v82rb(Handle value) { return rr_v82rb((Handle)value); } +VALUE rr_v82rb(v8::Handle value) { + return rr_v82rb((Handle)value); +} VALUE rr_v82rb(Handle value) { return rr_v82rb((Handle)value); } @@ -93,6 +96,24 @@ VALUE rr_v82rb(int32_t value) { return INT2FIX(value); } + +Handle rr_rb2v8(VALUE value) { + switch (TYPE(value)) { + case T_FIXNUM: + return Integer::New(FIX2INT(value)); + case T_FLOAT: + return Number::New(NUM2DBL(value)); + case T_STRING: + return String::New(RSTRING_PTR(value), RSTRING_LEN(value)); + case T_NIL: + return Null(); + case T_TRUE: + return True(); + case T_FALSE: + return False(); + } + return Undefined(); +} // VALUE rr_v82rb(v8::ScriptData *data) { // return rr_thunk(rr_wrap_script_data(data)); // } diff --git a/ext/v8/rr.h b/ext/v8/rr.h index df7c822..b3b03fe 100644 --- a/ext/v8/rr.h +++ b/ext/v8/rr.h @@ -11,18 +11,21 @@ VALUE rr_define_class(const char *name, VALUE superclass = rb_cObject); VALUE rr_str_to_perl_case(VALUE str); VALUE rr_str_to_camel_case(VALUE str); -VALUE rr_to_ruby(v8::Handle value); -VALUE rr_to_ruby(v8::Handle value); -VALUE rr_to_ruby(v8::Handle value); -VALUE rr_to_ruby(v8::Handle value); -VALUE rr_to_ruby(v8::Handle value); -VALUE rr_to_ruby(v8::Handle value); -VALUE rr_to_ruby(v8::Handle value); -VALUE rr_to_ruby(v8::Handle value); -VALUE rr_to_ruby(bool value); -VALUE rr_to_ruby(double value); -VALUE rr_to_ruby(int64_t value); -VALUE rr_to_ruby(uint32_t value); -VALUE rr_to_ruby(int32_t value); +VALUE rr_v82rb(v8::Handle value); +VALUE rr_v82rb(v8::Handle value); +VALUE rr_v82rb(v8::Handle value); +VALUE rr_v82rb(v8::Handle value); +VALUE rr_v82rb(v8::Handle value); +VALUE rr_v82rb(v8::Handle value); +VALUE rr_v82rb(v8::Handle value); +VALUE rr_v82rb(v8::Handle value); +VALUE rr_v82rb(v8::Handle value); +VALUE rr_v82rb(bool value); +VALUE rr_v82rb(double value); +VALUE rr_v82rb(int64_t value); +VALUE rr_v82rb(uint32_t value); +VALUE rr_v82rb(int32_t value); + +v8::Handle rr_rb2v8(VALUE value); #endif \ No newline at end of file diff --git a/ext/v8/v8_cxt.cpp b/ext/v8/v8_cxt.cpp index 246cfd5..0c31466 100644 --- a/ext/v8/v8_cxt.cpp +++ b/ext/v8/v8_cxt.cpp @@ -97,7 +97,7 @@ VALUE v8_cxt_eval(VALUE self, VALUE source, VALUE filename) { return Racer_Error_Message(exceptions); } else { printf("about to convert result\n"); - return rr_to_ruby(result); + return rr_v82rb(result); // return V82RB(result); } } diff --git a/ext/v8/v8_func.cpp b/ext/v8/v8_func.cpp index 81329f1..eadeb72 100644 --- a/ext/v8/v8_func.cpp +++ b/ext/v8/v8_func.cpp @@ -5,10 +5,15 @@ using namespace v8; +VALUE rr_cV8_C_Function; + namespace { VALUE FunctionClass; - VALUE Call(int, argc, VALUE *argv, VALUE self) { + Local unwrap(VALUE value) { + return V8_Ref_Get(value); + } + VALUE Call(int argc, VALUE *argv, VALUE self) { HandleScope handles; VALUE recv; VALUE f_argv; rb_scan_args(argc, argv, "1*", &recv, &f_argv); @@ -18,11 +23,22 @@ namespace { int f_argc = argc - 1; Local arguments[f_argc]; for (int i = 0; i < f_argc; i++) { - arguments[i] = rr_rb2v8(rb_ary_entry(f_argv, i)); + arguments[i] = *rr_rb2v8(rb_ary_entry(f_argv, i)); } Local result = function->Call(thisObject, f_argc, arguments); return rr_v82rb(result); - } + } + VALUE NewInstance(int argc, VALUE *argv, VALUE self) { + HandleScope handles; + VALUE f_argv; + rb_scan_args(argc, argv, "*", &f_argv); + Local function = V8_Ref_Get(self); + Local arguments[argc]; + for (int i = 0; i < argc; i++) { + arguments[i] = *rr_rb2v8(rb_ary_entry(f_argv, i)); + } + return rr_v82rb(function->NewInstance(argc, arguments)); + } VALUE GetName(VALUE self) { return rr_v82rb(unwrap(self)->GetName()); } @@ -30,11 +46,21 @@ namespace { Local str = V8_Ref_Get(name); unwrap(self)->SetName(str); return Qnil; - } - + } + // VALUE GetScriptOrigin(VALUE self) { + // return rr_v82rb(unwrap(self)->GetScriptOrigin()); + // } } void rr_init_func() { - FunctionClass = rr_define_class("Function", V8_C_Object); - rr_define_method(FunctionClass, "Call", Call, -1); + FunctionClass = rr_define_class("Function", rr_cV8_C_Object); + rr_define_method(FunctionClass, "Call", Call, -1); + rr_define_method(FunctionClass, "NewInstance", NewInstance, -1); + rr_define_method(FunctionClass, "GetName", GetName, 0); + rr_define_method(FunctionClass, "SetName", SetName, 1); + // rr_define_method(FunctionClass, "GetScriptOrigin", GetScriptOrigin, 0); +} + +void rr_reflec_v8_function(Handle value) { + } diff --git a/ext/v8/v8_func.h b/ext/v8/v8_func.h index 755327a..7c477f8 100644 --- a/ext/v8/v8_func.h +++ b/ext/v8/v8_func.h @@ -5,11 +5,10 @@ #include "v8.h" #include "v8_ref.h" -extern VALUE V8_C_Function; +extern VALUE rr_cV8_C_Function; void rr_init_func(); -VALUE V8_Wrap_Function(v8::Handle f); +VALUE rr_reflect_v8_function(v8::Handle value); -VALUE v8_C_Function_Call(int argc, VALUE *argv, VALUE self); #endif \ No newline at end of file diff --git a/ext/v8/v8_obj.cpp b/ext/v8/v8_obj.cpp index 92f227c..7c47d8b 100644 --- a/ext/v8/v8_obj.cpp +++ b/ext/v8/v8_obj.cpp @@ -1,5 +1,6 @@ #include "v8_obj.h" #include "v8_ref.h" +#include "v8_value.h" #include "converters.h" using namespace v8; @@ -10,12 +11,12 @@ VALUE rr_cV8_C_Object; void rr_init_obj() { rr_cV8_C_Object = rr_define_class("Object", rr_cV8_C_Value); - rr_define_singleton_method(V8_C_Object, "new", v8_Object_New, 0); - rr_define_method(V8_C_Object, "Get", v8_Object_Get, 1); - rr_define_method(V8_C_Object, "Set", v8_Object_Set, 2); - rr_define_method(V8_C_Object, "GetPropertyNames", v8_Object_GetPropertyNames, 0); - rr_define_method(V8_C_Object, "ToString", v8_Object_ToString, 0); - rr_define_method(V8_C_Object, "context", v8_Object_context, 0); + rr_define_singleton_method(rr_cV8_C_Object, "new", v8_Object_New, 0); + rr_define_method(rr_cV8_C_Object, "Get", v8_Object_Get, 1); + rr_define_method(rr_cV8_C_Object, "Set", v8_Object_Set, 2); + rr_define_method(rr_cV8_C_Object, "GetPropertyNames", v8_Object_GetPropertyNames, 0); + rr_define_method(rr_cV8_C_Object, "ToString", v8_Object_ToString, 0); + rr_define_method(rr_cV8_C_Object, "context", v8_Object_context, 0); } namespace { diff --git a/ext/v8/v8_template.cpp b/ext/v8/v8_template.cpp index 12b72ec..ba531fe 100644 --- a/ext/v8/v8_template.cpp +++ b/ext/v8/v8_template.cpp @@ -62,6 +62,6 @@ VALUE v8_FunctionTemplate_New(int argc, VALUE *argv, VALUE self) { VALUE v8_FunctionTemplate_GetFunction(VALUE self) { HandleScope handles; Local t = V8_Ref_Get(self); - return V8_Wrap_Function(t->GetFunction()); + return rr_v82rb(t->GetFunction()); } diff --git a/lib/v8/to.rb b/lib/v8/to.rb index 4b6c69f..51b9a3d 100644 --- a/lib/v8/to.rb +++ b/lib/v8/to.rb @@ -21,19 +21,6 @@ module V8 value end end - - def rb(value) - puts "hello from To.rb" - if value.IsFunction() - V8::Function.new(value) - elsif value.IsObject() - V8::Object.new(value) - elsif value.IsNumber() - value.NumberValue() - elsif value.IsBoolean() - value.BooleanValue() - end - end def camel_case(str) str.to_s.gsub(/_(\w)/) {$1.upcase}