2010-06-06 07:34:10 -04:00
|
|
|
#include "rr.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-26 12:54:56 -04:00
|
|
|
#include "v8_external.h"
|
2010-05-19 08:55:11 -04:00
|
|
|
#include "v8_callbacks.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 {
|
2010-05-22 02:23:22 -04:00
|
|
|
|
|
|
|
VALUE rb_hash_lookup(VALUE hash, const char *key) {
|
2010-05-27 15:01:27 -04:00
|
|
|
return rb_funcall(hash, rb_intern("[]"), 1, rb_str_new2(key));
|
2010-05-22 02:23:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
VALUE rb_hash_aset(VALUE hash, const char *key, VALUE value) {
|
|
|
|
return ::rb_hash_aset(hash, rb_str_new2(key), value);
|
|
|
|
}
|
|
|
|
|
2010-05-19 08:55:11 -04:00
|
|
|
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 {
|
|
|
|
|
2010-05-22 02:23:22 -04:00
|
|
|
/**
|
|
|
|
* NamedProperty[Getter|Setter] are used as interceptors on object.
|
|
|
|
* See ObjectTemplate::SetNamedPropertyHandler.
|
|
|
|
*/
|
|
|
|
Handle<Value> RubyNamedPropertyGetter(Local<String> property, const AccessorInfo& info) {
|
|
|
|
VALUE code = (VALUE)External::Unwrap(info.Data());
|
|
|
|
VALUE getter = rb_hash_lookup(code, "getter");
|
|
|
|
return rr_rb2v8(rb_funcall(getter, rb_intern("call"), 2, rr_v82rb(property), rr_v82rb(info)));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the value if the setter intercepts the request.
|
|
|
|
* Otherwise, returns an empty handle.
|
|
|
|
*/
|
|
|
|
Handle<Value> RubyNamedPropertySetter(Local<String> property, Local<Value> value, const AccessorInfo& info) {
|
|
|
|
VALUE code = (VALUE)External::Unwrap(info.Data());
|
|
|
|
VALUE setter = rb_hash_lookup(code, "setter");
|
|
|
|
VALUE result = rb_funcall(setter, rb_intern("call"), 3, rr_v82rb(property), rr_v82rb(value), rr_v82rb(info));
|
|
|
|
return rr_rb2v8(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a non-empty handle if the interceptor intercepts the request.
|
|
|
|
* The result is true if the property exists and false otherwise.
|
|
|
|
*/
|
|
|
|
Handle<Boolean> RubyNamedPropertyQuery(Local<String> property, const AccessorInfo& info) {
|
|
|
|
VALUE code = (VALUE)External::Unwrap(info.Data());
|
|
|
|
VALUE query = rb_hash_lookup(code, "query");
|
|
|
|
VALUE result = rb_funcall(query, rb_intern("call"), 2, rr_v82rb(property), rr_v82rb(info));
|
|
|
|
Handle<Value> intercepts = rr_rb2v8(result);
|
|
|
|
return intercepts.IsEmpty() ? Handle<Boolean>() : intercepts->ToBoolean();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a non-empty handle if the deleter intercepts the request.
|
|
|
|
* The return value is true if the property could be deleted and false
|
|
|
|
* otherwise.
|
|
|
|
*/
|
|
|
|
Handle<Boolean> RubyNamedPropertyDeleter(Local<String> property, const AccessorInfo& info) {
|
|
|
|
VALUE code = (VALUE)External::Unwrap(info.Data());
|
|
|
|
VALUE deleter = rb_hash_lookup(code, "deleter");
|
|
|
|
VALUE result = rb_funcall(deleter, rb_intern("call"), 2, rr_v82rb(property), rr_v82rb(info));
|
|
|
|
Handle<Value> intercepts = rr_rb2v8(result);
|
|
|
|
return intercepts.IsEmpty() ? Handle<Boolean>() : intercepts->ToBoolean();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an array containing the names of the properties the named
|
|
|
|
* property getter intercepts.
|
|
|
|
*/
|
|
|
|
Handle<Array> RubyNamedPropertyEnumerator(const AccessorInfo& info) {
|
|
|
|
VALUE code = (VALUE)External::Unwrap(info.Data());
|
|
|
|
VALUE enumerator = rb_hash_lookup(code, "enumerator");
|
|
|
|
VALUE result = rb_funcall(enumerator, rb_intern("call"), 1, rr_v82rb(info));
|
|
|
|
Handle<Value> v(rr_rb2v8(result));
|
|
|
|
if (v.IsEmpty()) {
|
|
|
|
return Array::New();
|
|
|
|
} else if (!v->IsArray()) {
|
|
|
|
Local<Array> a = Array::New();
|
|
|
|
a->Set(Integer::New(0), v->ToString());
|
|
|
|
return a;
|
|
|
|
} else {
|
|
|
|
return (Handle<Array>)Array::Cast(*v);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-07 03:50:33 -04:00
|
|
|
VALUE New(VALUE rbclass) {
|
2010-05-22 02:23:22 -04:00
|
|
|
HandleScope handles;
|
2010-06-07 03:50:33 -04:00
|
|
|
return rr_v8_ref_create(rbclass, ObjectTemplate::New());
|
2010-05-22 02:23:22 -04:00
|
|
|
}
|
|
|
|
VALUE NewInstance(VALUE self) {
|
|
|
|
HandleScope scope;
|
|
|
|
if (!Context::InContext()) {
|
|
|
|
rb_raise(rb_eScriptError, "ObjectTemplate::NewInstance() called without an entered Context");
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
Local<Object> object(obj(self)->NewInstance());
|
2010-05-26 12:54:56 -04:00
|
|
|
if (object.IsEmpty()) {
|
|
|
|
rb_raise(rb_eFatal, "V8 returned empty handle on call to ObjectTemplate::NewInstance()");
|
|
|
|
return Qnil;
|
|
|
|
}
|
2010-05-22 02:23:22 -04:00
|
|
|
return rr_v82rb(object);
|
|
|
|
}
|
|
|
|
VALUE SetNamedPropertyHandler(VALUE self, VALUE getter, VALUE setter, VALUE query, VALUE deleter, VALUE enumerator) {
|
|
|
|
HandleScope handles;
|
|
|
|
if (!RTEST(getter)) {
|
|
|
|
rb_raise(rb_eArgError, "you must supply at least a getter to V8::C::ObjectTemplate#SetNamedPropertyHandler()");
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
VALUE data = rb_hash_new();
|
|
|
|
rb_hash_aset(data, "getter", getter);
|
|
|
|
rb_hash_aset(data, "setter", setter);
|
|
|
|
rb_hash_aset(data, "query", query);
|
|
|
|
rb_hash_aset(data, "deleter", deleter);
|
|
|
|
rb_hash_aset(data, "enumerator", enumerator);
|
|
|
|
rr_v8_ref_setref(self, "data", data);
|
|
|
|
obj(self)->SetNamedPropertyHandler(
|
|
|
|
RubyNamedPropertyGetter,
|
|
|
|
RTEST(setter) ? RubyNamedPropertySetter : 0,
|
|
|
|
RTEST(query) ? RubyNamedPropertyQuery : 0,
|
|
|
|
RTEST(deleter) ? RubyNamedPropertyDeleter : 0,
|
|
|
|
RTEST(enumerator) ? RubyNamedPropertyEnumerator : 0,
|
2010-05-26 12:54:56 -04:00
|
|
|
rr_v8_external_create(data)
|
2010-05-22 02:23:22 -04:00
|
|
|
);
|
|
|
|
return Qnil;
|
|
|
|
}
|
2010-05-19 08:55:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace Func {
|
2010-05-22 02:23:22 -04:00
|
|
|
|
|
|
|
Handle<Value> RubyInvocationCallback(const Arguments& args) {
|
|
|
|
VALUE code = (VALUE)External::Unwrap(args.Data());
|
|
|
|
VALUE rb_args = rr_v82rb(args);
|
|
|
|
VALUE result = rb_funcall(code, rb_intern("call"), 1, rb_args);
|
|
|
|
return rr_rb2v8(result);
|
|
|
|
}
|
|
|
|
|
2010-05-19 08:55:11 -04:00
|
|
|
VALUE New(VALUE function_template) {
|
|
|
|
HandleScope handles;
|
|
|
|
VALUE code = rb_block_proc();
|
|
|
|
if (NIL_P(code)) {
|
|
|
|
return Qnil;
|
|
|
|
}
|
2010-05-26 12:54:56 -04:00
|
|
|
Local<FunctionTemplate> templ = FunctionTemplate::New(RubyInvocationCallback, rr_v8_external_create(code));
|
2010-06-07 03:50:33 -04:00
|
|
|
VALUE ref = rr_v8_ref_create(function_template,templ);
|
|
|
|
rr_v8_ref_setref(ref, "code", code);
|
|
|
|
return ref;
|
2010-05-19 08:55:11 -04:00
|
|
|
}
|
2010-06-08 03:33:12 -04:00
|
|
|
VALUE PrototypeTemplate(VALUE self) {
|
|
|
|
HandleScope scope;
|
|
|
|
return rr_v8_ref_create(ObjectTemplate, func(self)->PrototypeTemplate());
|
|
|
|
}
|
|
|
|
VALUE InstanceTemplate(VALUE self) {
|
|
|
|
HandleScope scope;
|
|
|
|
return rr_v8_ref_create(ObjectTemplate, func(self)->InstanceTemplate());
|
|
|
|
}
|
|
|
|
VALUE Inherit(VALUE self, VALUE function_template) {
|
|
|
|
HandleScope scope;
|
|
|
|
func(self)->Inherit(func(function_template));
|
|
|
|
return Qnil;
|
|
|
|
}
|
2010-05-19 08:55:11 -04:00
|
|
|
VALUE GetFunction(VALUE self) {
|
2010-05-22 02:23:22 -04:00
|
|
|
HandleScope handles;
|
2010-05-19 08:55:11 -04:00
|
|
|
if (!Context::InContext()) {
|
|
|
|
rb_raise(rb_eScriptError, "calls to FunctionTemplate::GetFunction() require a Context to be entered");
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
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-22 02:23:22 -04:00
|
|
|
|
2010-05-10 08:56:52 -04:00
|
|
|
VALUE ObjectTemplate = rr_define_class("ObjectTemplate", Template);
|
2010-05-22 02:23:22 -04:00
|
|
|
rr_define_singleton_method(ObjectTemplate, "New", Obj::New, 0);
|
|
|
|
rr_define_method(ObjectTemplate, "NewInstance", Obj::NewInstance, 0);
|
|
|
|
rr_define_method(ObjectTemplate, "SetNamedPropertyHandler", Obj::SetNamedPropertyHandler, 5);
|
|
|
|
|
2010-05-10 08:56:52 -04:00
|
|
|
VALUE FunctionTemplate = rr_define_class("FunctionTemplate", Template);
|
2010-05-19 08:55:11 -04:00
|
|
|
rr_define_singleton_method(FunctionTemplate, "New", Func::New, 0);
|
2010-06-08 03:33:12 -04:00
|
|
|
rr_define_method(FunctionTemplate, "PrototypeTemplate", Func::PrototypeTemplate, 0);
|
|
|
|
rr_define_method(FunctionTemplate, "InstanceTemplate", Func::InstanceTemplate, 0);
|
|
|
|
rr_define_method(FunctionTemplate, "Inherit", Func::Inherit, 1);
|
2010-05-19 08:55:11 -04:00
|
|
|
rr_define_method(FunctionTemplate, "GetFunction", Func::GetFunction, 0);
|
2010-01-17 17:25:40 -05:00
|
|
|
}
|