1
0
Fork 0
mirror of https://github.com/rubyjs/therubyracer synced 2023-03-27 23:21:42 -04:00
therubyracer/ext/v8/v8_func.cpp

69 lines
2 KiB
C++
Raw Normal View History

2009-12-18 02:48:06 -05:00
#include "v8_func.h"
#include "v8_obj.h"
2009-12-18 02:48:06 -05:00
using namespace v8;
2010-05-13 17:53:09 -04:00
VALUE rr_cV8_C_Function;
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
VALUE NewInstance(VALUE self, VALUE arguments) {
HandleScope scope;
Local<Function> function = unwrap(self);
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++) {
argv[i] = args->Get(i);
2010-05-13 17:53:09 -04:00
}
return rr_v82rb(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-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);
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);
}
VALUE rr_reflect_v8_function(Handle<Value> value) {
Local<Function> function(Function::Cast(*value));
return V8_Ref_Create(FunctionClass, function);
2010-04-21 19:09:13 -04:00
}