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"
|
2010-05-19 08:55:11 -04:00
|
|
|
#include "v8_callbacks.h"
|
2010-01-09 10:43:08 -05:00
|
|
|
#include "callbacks.h"
|
2010-05-19 08:55:11 -04:00
|
|
|
#include "converters.h"
|
2009-12-09 19:40:31 -05:00
|
|
|
|
|
|
|
using namespace v8;
|
2010-01-17 17:25:40 -05:00
|
|
|
|
2010-05-19 08:55:11 -04:00
|
|
|
namespace {
|
|
|
|
Local<Template> tmpl(VALUE self) {
|
|
|
|
return V8_Ref_Get<Template>(self);
|
|
|
|
}
|
|
|
|
Local<ObjectTemplate> obj(VALUE self) {
|
|
|
|
return V8_Ref_Get<ObjectTemplate>(self);
|
|
|
|
}
|
|
|
|
Local<FunctionTemplate> func(VALUE self) {
|
|
|
|
return V8_Ref_Get<FunctionTemplate>(self);
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE Set(VALUE self, VALUE name, VALUE value) {
|
|
|
|
HandleScope handles;
|
|
|
|
Local<String> key = rr_rb2v8(name)->ToString();
|
|
|
|
Local<Data> data = V8_Ref_Get<Data>(value);
|
|
|
|
tmpl(self)->Set(key, data);
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace Obj {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace Func {
|
|
|
|
VALUE New(VALUE function_template) {
|
|
|
|
HandleScope handles;
|
|
|
|
rb_need_block();
|
|
|
|
VALUE code = rb_block_proc();
|
|
|
|
if (NIL_P(code)) {
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
Local<FunctionTemplate> templ = FunctionTemplate::New(RubyInvocationCallback, External::Wrap((void *)code));
|
|
|
|
return V8_Ref_Create(function_template,templ,code);
|
|
|
|
}
|
|
|
|
VALUE GetFunction(VALUE self) {
|
|
|
|
if (!Context::InContext()) {
|
|
|
|
rb_raise(rb_eScriptError, "calls to FunctionTemplate::GetFunction() require a Context to be entered");
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
HandleScope handles;
|
|
|
|
Local<FunctionTemplate> templ = func(self);
|
|
|
|
Local<Function> fun = templ->GetFunction();
|
|
|
|
return rr_v82rb(func(self)->GetFunction());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-05-10 08:56:52 -04:00
|
|
|
|
|
|
|
void rr_init_template() {
|
|
|
|
VALUE Template = rr_define_class("Template");
|
2010-05-19 08:55:11 -04:00
|
|
|
rr_define_method(Template, "Set", Set, 2);
|
2010-05-10 08:56:52 -04:00
|
|
|
|
|
|
|
VALUE ObjectTemplate = rr_define_class("ObjectTemplate", Template);
|
|
|
|
rb_define_singleton_method(ObjectTemplate, "new", (VALUE(*)(...))v8_ObjectTemplate_New, 0);
|
|
|
|
|
|
|
|
VALUE FunctionTemplate = rr_define_class("FunctionTemplate", Template);
|
2010-05-19 08:55:11 -04:00
|
|
|
rr_define_singleton_method(FunctionTemplate, "New", Func::New, 0);
|
|
|
|
// rb_define_singleton_method(FunctionTemplate, "new", (VALUE(*)(...))v8_FunctionTemplate_New, -1);
|
|
|
|
rr_define_method(FunctionTemplate, "GetFunction", Func::GetFunction, 0);
|
2010-05-10 08:56:52 -04:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2010-01-17 17:25:40 -05:00
|
|
|
Local<ObjectTemplate> Racer_Create_V8_ObjectTemplate(VALUE value) {
|
|
|
|
Local<ObjectTemplate> tmpl = ObjectTemplate::New();
|
|
|
|
// tmpl->SetInternalFieldCount(2);
|
|
|
|
tmpl->SetNamedPropertyHandler(
|
|
|
|
RacerRubyNamedPropertyGetter,
|
|
|
|
RacerRubyNamedPropertySetter,
|
|
|
|
0, // RacerRubyNamedPropertyQuery,
|
|
|
|
0, // RacerRubyNamedPropertyDeleter,
|
2010-01-17 18:12:23 -05:00
|
|
|
RacerRubyNamedPropertyEnumerator,
|
2010-01-17 17:25:40 -05:00
|
|
|
External::Wrap((void *)value)
|
|
|
|
);
|
|
|
|
return tmpl;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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);
|
2010-02-11 18:14:47 -05:00
|
|
|
tmpl->Set(RSTRING_PTR(name), data);
|
2009-12-09 19:40:31 -05:00
|
|
|
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;
|
2010-01-09 10:43:08 -05:00
|
|
|
Local<FunctionTemplate> t = FunctionTemplate::New(RacerRubyInvocationCallback, 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);
|
2010-05-13 17:53:09 -04:00
|
|
|
return rr_v82rb(t->GetFunction());
|
2009-12-18 02:48:06 -05:00
|
|
|
}
|
|
|
|
|