#include "v8_func.h" #include "v8_obj.h" using namespace v8; namespace { VALUE FunctionClass; Local unwrap(VALUE value) { return V8_Ref_Get(value); } VALUE Call(VALUE self, VALUE recv, VALUE arguments) { HandleScope handles; if (!Context::InContext()) { rb_raise(rb_eScriptError, "no open V8 Context in V8::C::Function::Call()"); return Qnil; } Local function = unwrap(self); Local thisObj = rr_rb2v8(recv)->ToObject(); Handle args = V8_Ref_Get(arguments); int argc = args->Length(); Handle argv[argc]; for (int i = 0; i < argc; i++) { argv[i] = args->Get(i); } return rr_v82rb(function->Call(thisObj, argc, argv)); } VALUE NewInstance(VALUE self, VALUE arguments) { HandleScope scope; Local function = unwrap(self); Handle args = V8_Ref_Get(arguments); int argc = args->Length(); Handle argv[argc]; for (int i = 0; i < argc; i++) { argv[i] = args->Get(i); } return rr_v8_ref_create(rr_cV8_C_Object, function->NewInstance(argc, argv)); } VALUE GetName(VALUE self) { HandleScope scope; return rr_v82rb(unwrap(self)->GetName()); } VALUE SetName(VALUE self, VALUE name) { HandleScope scope; Local str = V8_Ref_Get(name); unwrap(self)->SetName(str); return Qnil; } // VALUE GetScriptOrigin(VALUE self) { // return rr_v82rb(unwrap(self)->GetScriptOrigin()); // } } void rr_init_func() { FunctionClass = rr_define_class("Function", rr_cV8_C_Object); rr_define_method(FunctionClass, "Call", Call, 2); rr_define_method(FunctionClass, "NewInstance", NewInstance, 1); rr_define_method(FunctionClass, "GetName", GetName, 0); rr_define_method(FunctionClass, "SetName", SetName, 1); // rr_define_method(FunctionClass, "GetScriptOrigin", GetScriptOrigin, 0); } VALUE rr_reflect_v8_function(Handle value) { Local function(Function::Cast(*value)); return rr_v8_ref_create(FunctionClass, function); }