2009-12-13 14:44:28 -05:00
|
|
|
#include <ruby.h>
|
|
|
|
#include <v8.h>
|
2009-12-09 19:40:31 -05:00
|
|
|
#include "v8_ref.h"
|
2009-12-18 02:48:06 -05:00
|
|
|
#include "v8_func.h"
|
2009-12-09 19:40:31 -05:00
|
|
|
#include "v8_template.h"
|
2009-12-13 14:44:28 -05:00
|
|
|
#include "converters.h"
|
2009-12-09 19:40:31 -05:00
|
|
|
|
|
|
|
using namespace v8;
|
|
|
|
|
|
|
|
Handle<Value> RubyInvocationCallback(const Arguments& args) {
|
2009-12-11 12:11:05 -05:00
|
|
|
VALUE code = (VALUE)External::Unwrap(args.Data());
|
|
|
|
if (NIL_P(code)) {
|
2009-12-13 10:08:55 -05:00
|
|
|
return Null();
|
2009-12-13 11:07:34 -05:00
|
|
|
} else {
|
2009-12-13 14:44:28 -05:00
|
|
|
convert_v8_to_rb_t arg_cvt;
|
|
|
|
convert_rb_to_v8_t ret_cvt;
|
2009-12-13 11:07:34 -05:00
|
|
|
|
|
|
|
VALUE* arguments = new VALUE[args.Length()];
|
|
|
|
for(int c=0;c<args.Length(); ++c) {
|
|
|
|
Handle<Value> val = args[c];
|
2009-12-13 14:44:28 -05:00
|
|
|
arguments[c] = arg_cvt(val);
|
2009-12-13 11:07:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
VALUE result = rb_funcall2(code, rb_intern("call"), args.Length(), arguments);
|
|
|
|
delete [] arguments;
|
|
|
|
|
2009-12-13 14:44:28 -05:00
|
|
|
Handle<Value> convertedResult = ret_cvt(result);
|
2009-12-13 11:07:34 -05:00
|
|
|
return convertedResult ;
|
2009-12-11 12:11:05 -05:00
|
|
|
}
|
2009-12-09 19:40:31 -05:00
|
|
|
}
|
|
|
|
|
2009-12-11 12:11:05 -05:00
|
|
|
VALUE v8_Template_Set(VALUE self, VALUE name, VALUE value) {
|
2009-12-09 19:40:31 -05:00
|
|
|
HandleScope handles;
|
2009-12-13 10:08:55 -05:00
|
|
|
Local<Template> tmpl = V8_Ref_Get<Template>(self);
|
|
|
|
Local<Data> data = V8_Ref_Get<Data>(value);
|
2009-12-09 19:40:31 -05:00
|
|
|
tmpl->Set(RSTRING(name)->ptr, data);
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
2009-12-11 12:11:05 -05:00
|
|
|
VALUE v8_ObjectTemplate_New(VALUE clazz) {
|
2009-12-09 19:40:31 -05:00
|
|
|
HandleScope handles;
|
2009-12-11 12:11:05 -05:00
|
|
|
return V8_Ref_Create(clazz, ObjectTemplate::New());
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE v8_FunctionTemplate_New(int argc, VALUE *argv, VALUE self) {
|
|
|
|
VALUE code;
|
|
|
|
rb_scan_args(argc, argv, "00&", &code);
|
|
|
|
HandleScope handles;
|
|
|
|
Local<FunctionTemplate> t = FunctionTemplate::New(RubyInvocationCallback, External::Wrap((void *)code));
|
2009-12-13 10:08:55 -05:00
|
|
|
return V8_Ref_Create(self,t,code);
|
2009-12-09 19:40:31 -05:00
|
|
|
}
|
|
|
|
|
2009-12-18 02:48:06 -05:00
|
|
|
VALUE v8_FunctionTemplate_GetFunction(VALUE self) {
|
|
|
|
HandleScope handles;
|
|
|
|
Local<FunctionTemplate> t = V8_Ref_Get<FunctionTemplate>(self);
|
|
|
|
return V8_Wrap_Function(t->GetFunction());
|
|
|
|
}
|
|
|
|
|