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

OMG it works, embed a lamda in a context

This commit is contained in:
Bill Robertson 2010-01-09 10:43:08 -05:00
parent f208e10fde
commit 9508d79e6d
5 changed files with 58 additions and 35 deletions

25
ext/v8/callbacks.cpp Normal file
View file

@ -0,0 +1,25 @@
#include <ruby.h>
#include "callbacks.h"
#include "converters.h"
using namespace v8;
Handle<Value> RacerRubyInvocationCallback(const Arguments& args) {
VALUE code = (VALUE)External::Unwrap(args.Data());
if (NIL_P(code)) {
return Null();
} else {
VALUE* arguments = new VALUE[args.Length()];
for(int c=0;c<args.Length(); ++c) {
Handle<Value> val = args[c];
arguments[c] = V82RB(val);
}
VALUE result = rb_funcall2(code, rb_intern("call"), args.Length(), arguments);
delete [] arguments;
Handle<Value> convertedResult = RB2V8(result);
return convertedResult ;
}
}

8
ext/v8/callbacks.h Normal file
View file

@ -0,0 +1,8 @@
#ifndef CALLBACKS_H_8VK3LWBG
#define CALLBACKS_H_8VK3LWBG
#include <v8.h>
v8::Handle<v8::Value> RacerRubyInvocationCallback(const v8::Arguments& args);
#endif /* end of include guard: CALLBACKS_H_8VK3LWBG */

View file

@ -1,13 +1,15 @@
#include "converters.h" #include "converters.h"
#include "callbacks.h"
#include "v8_ref.h" #include "v8_ref.h"
#include "v8_obj.h" #include "v8_obj.h"
using namespace v8;
namespace { namespace {
std::string UNDEFINED_STR("undefined"); std::string UNDEFINED_STR("undefined");
} }
VALUE V82RB(v8::Handle<v8::Value>& value) { VALUE V82RB(Handle<Value>& value) {
convert_v8_to_rb_t convert; convert_v8_to_rb_t convert;
VALUE result; VALUE result;
if(convert(value, result)) { if(convert(value, result)) {
@ -15,14 +17,28 @@ VALUE V82RB(v8::Handle<v8::Value>& value) {
} }
if (value->IsObject()) { if (value->IsObject()) {
v8::Local<v8::Object> object(v8::Object::Cast(*value)); Local<Object> object(Object::Cast(*value));
return V8_Ref_Create(V8_C_Object, value); return V8_Ref_Create(V8_C_Object, value);
} }
return Qnil; return Qnil;
} }
v8::Local<v8::Value> RB2V8(VALUE value) { Local<Value> RB2V8(VALUE value) {
VALUE valueClass = rb_class_of(value);
if(valueClass == rb_cProc) {
Local<FunctionTemplate> t = FunctionTemplate::New(RacerRubyInvocationCallback, External::Wrap((void *)value));
return t->GetFunction();
printf("** This is a proc! We should do something different.\n");
}
else if(valueClass == rb_cMethod) {
printf("** This is a method! We should do something different.\n");
}
convert_rb_to_v8_t convert; convert_rb_to_v8_t convert;
return convert(value); return convert(value);
} }
@ -32,7 +48,7 @@ std::string RB2String(VALUE value) {
return convert(value); return convert(value);
} }
std::string V82String(v8::Handle<v8::Value>& value) { std::string V82String(Handle<Value>& value) {
convert_v8_to_string_t convert; convert_v8_to_string_t convert;
std::string result; std::string result;
if(convert(value, result)) { if(convert(value, result)) {
@ -40,8 +56,8 @@ std::string V82String(v8::Handle<v8::Value>& value) {
} }
if (value->IsObject()) { if (value->IsObject()) {
v8::Local<v8::Object> object(v8::Object::Cast(*value)); Local<Object> object(Object::Cast(*value));
v8::Local<v8::String> str = object->ToString(); Local<String> str = object->ToString();
if(convert(value, result)) { if(convert(value, result)) {
return result; return result;
} }

View file

@ -26,14 +26,6 @@ VALUE v8_Object_Set(VALUE self, VALUE key, VALUE value) {
Local<Object> obj = V8_Ref_Get<Object>(self); Local<Object> obj = V8_Ref_Get<Object>(self);
VALUE keystr = rb_funcall(key, rb_intern("to_s"), 0); VALUE keystr = rb_funcall(key, rb_intern("to_s"), 0);
VALUE valueClass = rb_class_of(value);
if(valueClass == rb_cProc) {
//printf("** This is a proc! We should do something different.\n");
}
else if(valueClass == rb_cMethod) {
//printf("** This is a method! We should do something different.\n");
}
obj->Set(RB2V8(keystr), RB2V8(value)); obj->Set(RB2V8(keystr), RB2V8(value));
return Qnil; return Qnil;
} }

View file

@ -4,28 +4,10 @@
#include "v8_func.h" #include "v8_func.h"
#include "v8_template.h" #include "v8_template.h"
#include "converters.h" #include "converters.h"
#include "callbacks.h"
using namespace v8; using namespace v8;
Handle<Value> RubyInvocationCallback(const Arguments& args) {
VALUE code = (VALUE)External::Unwrap(args.Data());
if (NIL_P(code)) {
return Null();
} else {
VALUE* arguments = new VALUE[args.Length()];
for(int c=0;c<args.Length(); ++c) {
Handle<Value> val = args[c];
arguments[c] = V82RB(val);
}
VALUE result = rb_funcall2(code, rb_intern("call"), args.Length(), arguments);
delete [] arguments;
Handle<Value> convertedResult = RB2V8(result);
return convertedResult ;
}
}
VALUE v8_Template_Set(VALUE self, VALUE name, VALUE value) { VALUE v8_Template_Set(VALUE self, VALUE name, VALUE value) {
HandleScope handles; HandleScope handles;
Local<Template> tmpl = V8_Ref_Get<Template>(self); Local<Template> tmpl = V8_Ref_Get<Template>(self);
@ -43,7 +25,7 @@ VALUE v8_FunctionTemplate_New(int argc, VALUE *argv, VALUE self) {
VALUE code; VALUE code;
rb_scan_args(argc, argv, "00&", &code); rb_scan_args(argc, argv, "00&", &code);
HandleScope handles; HandleScope handles;
Local<FunctionTemplate> t = FunctionTemplate::New(RubyInvocationCallback, External::Wrap((void *)code)); Local<FunctionTemplate> t = FunctionTemplate::New(RacerRubyInvocationCallback, External::Wrap((void *)code));
return V8_Ref_Create(self,t,code); return V8_Ref_Create(self,t,code);
} }