From f32420fff8fe14f94373a34441206d6e704bad31 Mon Sep 17 00:00:00 2001 From: Bill Robertson Date: Sun, 13 Dec 2009 11:07:34 -0500 Subject: [PATCH] converting args --- generic_data.h | 14 +++++++------- ruby_data.h | 34 +++++++++++++++++----------------- spike.rb | 14 ++++++++------ v8_data.h | 36 ++++++++++++++++++------------------ v8_template.cpp | 20 +++++++++++++++++--- 5 files changed, 67 insertions(+), 51 deletions(-) diff --git a/generic_data.h b/generic_data.h index b14d814..bca961b 100644 --- a/generic_data.h +++ b/generic_data.h @@ -16,12 +16,12 @@ public: StringDest(); ~StringDest(); - std::string pushString(const std::string& value, const char* name=0) { + std::string pushString(const std::string& value) { return value; } #ifdef FIGURED_OUT_INT_ISSUES - const char* pushInt(int32_t value, const char* name=0) { + const char* pushInt(int32_t value) { char buffer[64]; std::sprintf(buffer, "%d", value); convertedValue = buffer; @@ -29,31 +29,31 @@ public: } #endif - std::string pushInt(int64_t value, const char* name=0) { + std::string pushInt(int64_t value) { char buffer[64]; std::sprintf(buffer, "%lld", value); std::string convertedValue(buffer); return convertedValue; } - std::string pushDouble(double value, const char* name=0) { + std::string pushDouble(double value) { char buffer[64]; std::sprintf(buffer, "%g", value); std::string convertedValue(buffer); return convertedValue; } - std::string pushBool(bool value, const char* name=0) { + std::string pushBool(bool value) { std::string convertedValue(value ? TRUE : FALSE); return convertedValue; } - std::string pushNull(const char* name=0) { + std::string pushNull() { printf("I bet we aren't even getting here
"); return ""; // this kind of sucks } - std::string pushUndefined(const char* name=0) { + std::string pushUndefined() { return "undefined"; // this too } diff --git a/ruby_data.h b/ruby_data.h index 5f5ac57..55e4002 100644 --- a/ruby_data.h +++ b/ruby_data.h @@ -30,32 +30,32 @@ template class RubyValueSource { ~RubyValueSource() {} - RET push(VALUE& value, const char* name=0) { + RET push(VALUE& value) { switch (TYPE(value)) { case T_FIXNUM: - return dest.pushInt(FIX2INT(value), name); + return dest.pushInt(FIX2INT(value)); case T_FLOAT: - return dest.pushDouble(NUM2DBL(value), name); + return dest.pushDouble(NUM2DBL(value)); case T_STRING: - return convertString(value, name); + return convertString(value); case T_NIL: - return dest.pushNull(name); + return dest.pushNull(); case T_TRUE: - return dest.pushBool(true, name); + return dest.pushBool(true); case T_FALSE: - return dest.pushBool(false, name); + return dest.pushBool(false); // case T_DATA: // if (rb_is_function(value)) { // return dest.pushCode(new Code) // } } - return dest.pushUndefined(name); + return dest.pushUndefined(); } private: - RET convertString(VALUE& value, const char* name=0) { + RET convertString(VALUE& value) { std::string stringValue(RSTRING(value)->ptr); - return dest.pushString(stringValue, name); + return dest.pushString(stringValue); } }; @@ -70,31 +70,31 @@ class RubyDest { RubyDest(); ~RubyDest(); - VALUE pushString(const std::string& value, const char* name=0) { + VALUE pushString(const std::string& value) { return rb_str_new2(value.c_str()); } - VALUE pushInt(int64_t value, const char* name=0) { + VALUE pushInt(int64_t value) { return INT2FIX(value); } - VALUE pushDouble(double value, const char* name=0) { + VALUE pushDouble(double value) { return rb_float_new(value); } - VALUE pushBool(bool value, const char* name=0) { + VALUE pushBool(bool value) { return value ? Qtrue : Qfalse; } - VALUE pushNull(const char* name=0) { + VALUE pushNull() { return Qnil; } - VALUE pushUndefined(const char* name=0) { + VALUE pushUndefined() { return Qnil; } - VALUE pushObject(v8::Handle& value, const char* name = 0) { + VALUE pushObject(v8::Handle& value) { v8_object* wrapper = new v8_object(value); return wrapper->ruby_value; } diff --git a/spike.rb b/spike.rb index 4a3e54e..db5217f 100644 --- a/spike.rb +++ b/spike.rb @@ -22,18 +22,20 @@ class V8::C::Context end end -f = V8::C::FunctionTemplate.new do - puts "Hello!! This is ruby code!!!!" +f = V8::C::FunctionTemplate.new do | foo, bar | + "Hello!! This is ruby code #{foo} #{bar}!!!!" end -f2 = V8::C::FunctionTemplate.new +plus = V8::C::FunctionTemplate.new do |a, b| + a + b +end o = V8::C::ObjectTemplate.new o.Set("hello", f) -o.Set("nello", f2) +o.Set("plus_of_awesomeness", plus) V8::C::Context.new(o).open do |cxt| - puts "r1: " + cxt.eval('nello()').to_s - puts "r2: " + cxt.eval('hello()') + puts "r1: " + cxt.eval('plus_of_awesomeness(8, 3.4)').to_s + puts "r2: " + cxt.eval('hello("Fred", "Wilma")') end diff --git a/v8_data.h b/v8_data.h index 1c5df24..28bb31d 100644 --- a/v8_data.h +++ b/v8_data.h @@ -15,22 +15,22 @@ template class V8HandleSource { V8HandleSource() {} ~V8HandleSource() {} - R push(v8::Handle& value, const char* name = 0) { + R push(v8::Handle& value) { if (value->IsUndefined()) { - return dest.pushNull(name); + return dest.pushNull(); } if(value->IsNull()) { - return dest.pushNull(name); + return dest.pushNull(); } if(value->IsTrue()) { - return dest.pushBool(true, name); + return dest.pushBool(true); } if(value->IsFalse()) { - return dest.pushBool(false, name); + return dest.pushBool(false); } if(value->IsString()) { @@ -43,23 +43,23 @@ template class V8HandleSource { output.replace(total, written, buffer); total += written; } - return dest.pushString(output, name); + return dest.pushString(output); } if(value->IsInt32()) { - return dest.pushInt(value->Int32Value(), name); + return dest.pushInt(value->Int32Value()); } if(value->IsNumber()) { - return dest.pushDouble(value->NumberValue(), name); + return dest.pushDouble(value->NumberValue()); } if (value->IsObject()) { v8::Local object(v8::Object::Cast(*value)); - return dest.pushObject(object, name); + return dest.pushObject(object); } - return dest.pushNull(name); + return dest.pushNull(); } }; @@ -76,28 +76,28 @@ public: V8HandleDest(); ~V8HandleDest(); - v8::Local pushString(const std::string& value, const char* name=0) { + v8::Local pushString(const std::string& value) { return v8::Local::New(v8::String::New(value.c_str())); } - v8::Local pushInt(int64_t value, const char* name=0) { + v8::Local pushInt(int64_t value) { return v8::Local::New(v8::Integer::New(value)); } - v8::Local pushDouble(double value, const char* name=0) { + v8::Local pushDouble(double value) { return v8::Local::New(v8::Number::New(value)); } - v8::Local pushBool(bool value, const char* name=0) { + v8::Local pushBool(bool value) { return v8::Local::New(v8::Boolean::New(value)); } - v8::Local pushNull(const char* name=0) { - return v8::Local::New(v8::Integer::New(0)); // so WRONG!! not sure how to make a null from API + v8::Local pushNull() { + return v8::Local::New(v8::Null()); } - v8::Local pushUndefined(const char* name=0) { - return v8::Local::New(v8::Integer::New(0)); // so WRONG!! not sure how to make a null from API + v8::Local pushUndefined() { + return v8::Local::New(v8::Undefined()); } // v8:Local pushFunction(Function) { diff --git a/v8_template.cpp b/v8_template.cpp index 969608b..ba0093a 100644 --- a/v8_template.cpp +++ b/v8_template.cpp @@ -2,6 +2,8 @@ #include "v8.h" #include "v8_ref.h" #include "v8_template.h" +#include "ruby_data.h" +#include "v8_data.h" using namespace v8; @@ -9,9 +11,21 @@ Handle RubyInvocationCallback(const Arguments& args) { VALUE code = (VALUE)External::Unwrap(args.Data()); if (NIL_P(code)) { return Null(); - } else { - VALUE result = rb_funcall(code, rb_intern("call"), 0); - return String::New("Function Was Called"); + } else { + V8HandleSource argConverter; + RubyValueSource > retvalConverter; + + VALUE* arguments = new VALUE[args.Length()]; + for(int c=0;c val = args[c]; + arguments[c] = argConverter.push(val); + } + + VALUE result = rb_funcall2(code, rb_intern("call"), args.Length(), arguments); + delete [] arguments; + + Handle convertedResult = retvalConverter.push(result); + return convertedResult ; } }