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

86 lines
2.5 KiB
C++
Raw Normal View History

2009-12-18 02:48:06 -05:00
2010-04-21 19:09:13 -04:00
#include "converters.h"
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);
}
VALUE Call(int argc, VALUE *argv, VALUE self) {
2010-05-12 18:30:53 -04:00
HandleScope handles;
VALUE recv; VALUE f_argv;
rb_scan_args(argc, argv, "1*", &recv, &f_argv);
Local<Function> function = V8_Ref_Get<Function>(self);
2010-05-17 07:39:29 -04:00
Local<Object> thisObject;
if (NIL_P(recv)) {
if (Context::InContext()) {
thisObject = Context::GetEntered()->Global();
} else {
Persistent<Context> cxt = Context::New();
Context::Scope scope(cxt);
thisObject = Object::New();
2010-05-17 07:39:29 -04:00
cxt.Dispose();
}
} else {
if (!Context::InContext()) {
Persistent<Context> cxt = Context::New();
cxt->Enter();
thisObject = rr_rb2v8(recv)->ToObject();
cxt->Exit();
} else {
thisObject = rr_rb2v8(recv)->ToObject();
}
2010-05-17 07:39:29 -04:00
}
2010-05-12 18:30:53 -04:00
int f_argc = argc - 1;
Local<Value> arguments[f_argc];
for (int i = 0; i < f_argc; i++) {
2010-05-13 17:53:09 -04:00
arguments[i] = *rr_rb2v8(rb_ary_entry(f_argv, i));
2010-05-12 18:30:53 -04:00
}
Local<Value> result = function->Call(thisObject, f_argc, arguments);
return rr_v82rb(result);
2010-05-13 17:53:09 -04:00
}
VALUE NewInstance(VALUE self, VALUE argc, VALUE args) {
HandleScope scope;
Local<Function> function = unwrap(self);
Handle<Array> arguments = V8_Ref_Get<Array>(args);
Handle<Value> argv[arguments->Length()];
for (int i = 0; i < arguments->Length(); i++) {
argv[i] = arguments->Get(i);
2010-05-13 17:53:09 -04:00
}
return rr_v82rb(function->NewInstance(NUM2INT(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);
rr_define_method(FunctionClass, "Call", Call, -1);
rr_define_method(FunctionClass, "NewInstance", NewInstance, 2);
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
}