2009-12-18 02:48:06 -05:00
|
|
|
|
|
|
|
#include "v8_func.h"
|
2010-05-10 08:56:52 -04:00
|
|
|
#include "v8_obj.h"
|
2009-12-18 02:48:06 -05:00
|
|
|
|
|
|
|
using namespace v8;
|
|
|
|
|
2010-05-12 18:30:53 -04:00
|
|
|
namespace {
|
|
|
|
VALUE FunctionClass;
|
|
|
|
|
2010-05-13 17:53:09 -04:00
|
|
|
Local<Function> unwrap(VALUE value) {
|
|
|
|
return V8_Ref_Get<Function>(value);
|
|
|
|
}
|
2010-06-03 06:09:52 -04:00
|
|
|
VALUE Call(VALUE self, VALUE recv, VALUE arguments) {
|
2010-05-12 18:30:53 -04:00
|
|
|
HandleScope handles;
|
2010-06-03 06:09:52 -04:00
|
|
|
if (!Context::InContext()) {
|
|
|
|
rb_raise(rb_eScriptError, "no open V8 Context in V8::C::Function::Call()");
|
|
|
|
return Qnil;
|
2010-05-17 07:39:29 -04:00
|
|
|
}
|
2010-06-03 06:09:52 -04:00
|
|
|
Local<Function> function = unwrap(self);
|
|
|
|
Local<Object> thisObj = rr_rb2v8(recv)->ToObject();
|
|
|
|
Handle<Array> args = V8_Ref_Get<Array>(arguments);
|
|
|
|
int argc = args->Length();
|
|
|
|
Handle<Value> argv[argc];
|
|
|
|
for (int i = 0; i < argc; i++) {
|
|
|
|
argv[i] = args->Get(i);
|
2010-05-12 18:30:53 -04:00
|
|
|
}
|
2010-06-03 06:09:52 -04:00
|
|
|
return rr_v82rb(function->Call(thisObj, argc, argv));
|
2010-05-13 17:53:09 -04:00
|
|
|
}
|
2010-06-03 06:09:52 -04:00
|
|
|
|
2010-06-03 06:56:07 -04:00
|
|
|
VALUE NewInstance(VALUE self, VALUE arguments) {
|
2010-06-03 05:51:06 -04:00
|
|
|
HandleScope scope;
|
|
|
|
Local<Function> function = unwrap(self);
|
2010-06-03 06:56:07 -04:00
|
|
|
Handle<Array> args = V8_Ref_Get<Array>(arguments);
|
|
|
|
int argc = args->Length();
|
|
|
|
Handle<Value> argv[argc];
|
2010-06-03 07:05:23 -04:00
|
|
|
for (int i = 0; i < argc; i++) {
|
2010-06-03 06:56:07 -04:00
|
|
|
argv[i] = args->Get(i);
|
2010-05-13 17:53:09 -04:00
|
|
|
}
|
2010-06-08 07:33:54 -04:00
|
|
|
return rr_v8_ref_create(rr_cV8_C_Object, function->NewInstance(argc, argv));
|
2010-05-13 17:53:09 -04:00
|
|
|
}
|
2010-05-12 18:30:53 -04:00
|
|
|
VALUE GetName(VALUE self) {
|
|
|
|
return rr_v82rb(unwrap(self)->GetName());
|
|
|
|
}
|
|
|
|
VALUE SetName(VALUE self, VALUE name) {
|
|
|
|
Local<String> str = V8_Ref_Get<String>(name);
|
|
|
|
unwrap(self)->SetName(str);
|
|
|
|
return Qnil;
|
2010-05-13 17:53:09 -04:00
|
|
|
}
|
|
|
|
// VALUE GetScriptOrigin(VALUE self) {
|
|
|
|
// return rr_v82rb(unwrap(self)->GetScriptOrigin());
|
|
|
|
// }
|
2010-05-10 08:56:52 -04:00
|
|
|
}
|
|
|
|
|
2010-05-12 18:30:53 -04:00
|
|
|
void rr_init_func() {
|
2010-05-13 17:53:09 -04:00
|
|
|
FunctionClass = rr_define_class("Function", rr_cV8_C_Object);
|
2010-06-03 06:09:52 -04:00
|
|
|
rr_define_method(FunctionClass, "Call", Call, 2);
|
2010-06-03 06:56:07 -04:00
|
|
|
rr_define_method(FunctionClass, "NewInstance", NewInstance, 1);
|
2010-05-13 17:53:09 -04:00
|
|
|
rr_define_method(FunctionClass, "GetName", GetName, 0);
|
|
|
|
rr_define_method(FunctionClass, "SetName", SetName, 1);
|
|
|
|
// rr_define_method(FunctionClass, "GetScriptOrigin", GetScriptOrigin, 0);
|
|
|
|
}
|
|
|
|
|
2010-05-13 18:10:48 -04:00
|
|
|
VALUE rr_reflect_v8_function(Handle<Value> value) {
|
2010-05-13 20:43:46 -04:00
|
|
|
Local<Function> function(Function::Cast(*value));
|
2010-06-07 03:50:33 -04:00
|
|
|
return rr_v8_ref_create(FunctionClass, function);
|
2010-04-21 19:09:13 -04:00
|
|
|
}
|