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

converting args

This commit is contained in:
Bill Robertson 2009-12-13 11:07:34 -05:00
parent 125497a742
commit f32420fff8
5 changed files with 67 additions and 51 deletions

View file

@ -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<br/>");
return ""; // this kind of sucks
}
std::string pushUndefined(const char* name=0) {
std::string pushUndefined() {
return "undefined"; // this too
}

View file

@ -30,32 +30,32 @@ template<class DEST, class RET> 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<RubyCaller>)
// }
}
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<v8::Object>& value, const char* name = 0) {
VALUE pushObject(v8::Handle<v8::Object>& value) {
v8_object* wrapper = new v8_object(value);
return wrapper->ruby_value;
}

View file

@ -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

View file

@ -15,22 +15,22 @@ template<class T, class R> class V8HandleSource {
V8HandleSource() {}
~V8HandleSource() {}
R push(v8::Handle<v8::Value>& value, const char* name = 0) {
R push(v8::Handle<v8::Value>& 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 T, class R> 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<v8::Object> 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<v8::Value> pushString(const std::string& value, const char* name=0) {
v8::Local<v8::Value> pushString(const std::string& value) {
return v8::Local<v8::Value>::New(v8::String::New(value.c_str()));
}
v8::Local<v8::Value> pushInt(int64_t value, const char* name=0) {
v8::Local<v8::Value> pushInt(int64_t value) {
return v8::Local<v8::Value>::New(v8::Integer::New(value));
}
v8::Local<v8::Value> pushDouble(double value, const char* name=0) {
v8::Local<v8::Value> pushDouble(double value) {
return v8::Local<v8::Value>::New(v8::Number::New(value));
}
v8::Local<v8::Value> pushBool(bool value, const char* name=0) {
v8::Local<v8::Value> pushBool(bool value) {
return v8::Local<v8::Value>::New(v8::Boolean::New(value));
}
v8::Local<v8::Value> pushNull(const char* name=0) {
return v8::Local<v8::Value>::New(v8::Integer::New(0)); // so WRONG!! not sure how to make a null from API
v8::Local<v8::Value> pushNull() {
return v8::Local<v8::Value>::New(v8::Null());
}
v8::Local<v8::Value> pushUndefined(const char* name=0) {
return v8::Local<v8::Value>::New(v8::Integer::New(0)); // so WRONG!! not sure how to make a null from API
v8::Local<v8::Value> pushUndefined() {
return v8::Local<v8::Value>::New(v8::Undefined());
}
// v8:Local<v8:Value> pushFunction(Function) {

View file

@ -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<Value> 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<RubyDest, VALUE> argConverter;
RubyValueSource<V8HandleDest, Local<Value> > retvalConverter;
VALUE* arguments = new VALUE[args.Length()];
for(int c=0;c<args.Length(); ++c) {
Handle<Value> val = args[c];
arguments[c] = argConverter.push(val);
}
VALUE result = rb_funcall2(code, rb_intern("call"), args.Length(), arguments);
delete [] arguments;
Handle<Value> convertedResult = retvalConverter.push(result);
return convertedResult ;
}
}