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

cleanup native function.Call()

This commit is contained in:
Charles Lowell 2010-06-03 13:09:52 +03:00
parent b82d5b7154
commit 7a656e8e46
2 changed files with 15 additions and 32 deletions

View file

@ -13,40 +13,23 @@ namespace {
Local<Function> unwrap(VALUE value) { Local<Function> unwrap(VALUE value) {
return V8_Ref_Get<Function>(value); return V8_Ref_Get<Function>(value);
} }
VALUE Call(int argc, VALUE *argv, VALUE self) { VALUE Call(VALUE self, VALUE recv, VALUE arguments) {
HandleScope handles; HandleScope handles;
VALUE recv; VALUE f_argv; if (!Context::InContext()) {
rb_scan_args(argc, argv, "1*", &recv, &f_argv); rb_raise(rb_eScriptError, "no open V8 Context in V8::C::Function::Call()");
return Qnil;
Local<Function> function = V8_Ref_Get<Function>(self);
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();
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();
}
} }
int f_argc = argc - 1; Local<Function> function = unwrap(self);
Local<Value> arguments[f_argc]; Local<Object> thisObj = rr_rb2v8(recv)->ToObject();
for (int i = 0; i < f_argc; i++) { Handle<Array> args = V8_Ref_Get<Array>(arguments);
arguments[i] = *rr_rb2v8(rb_ary_entry(f_argv, i)); int argc = args->Length();
Handle<Value> argv[argc];
for (int i = 0; i < argc; i++) {
argv[i] = args->Get(i);
} }
Local<Value> result = function->Call(thisObject, f_argc, arguments); return rr_v82rb(function->Call(thisObj, argc, argv));
return rr_v82rb(result);
} }
VALUE NewInstance(VALUE self, VALUE argc, VALUE args) { VALUE NewInstance(VALUE self, VALUE argc, VALUE args) {
HandleScope scope; HandleScope scope;
Local<Function> function = unwrap(self); Local<Function> function = unwrap(self);
@ -72,7 +55,7 @@ namespace {
void rr_init_func() { void rr_init_func() {
FunctionClass = rr_define_class("Function", rr_cV8_C_Object); FunctionClass = rr_define_class("Function", rr_cV8_C_Object);
rr_define_method(FunctionClass, "Call", Call, -1); rr_define_method(FunctionClass, "Call", Call, 2);
rr_define_method(FunctionClass, "NewInstance", NewInstance, 2); rr_define_method(FunctionClass, "NewInstance", NewInstance, 2);
rr_define_method(FunctionClass, "GetName", GetName, 0); rr_define_method(FunctionClass, "GetName", GetName, 0);
rr_define_method(FunctionClass, "SetName", SetName, 1); rr_define_method(FunctionClass, "SetName", SetName, 1);

View file

@ -7,7 +7,7 @@ module V8
C::TryCatch.try do |try| C::TryCatch.try do |try|
@context.enter do @context.enter do
this = To.v8(thisObject) this = To.v8(thisObject)
return_value = To.ruby(@native.Call(this, *args.map {|a| To.v8(a)})) return_value = To.ruby(@native.Call(this, To.v8(args)))
err = JavascriptError.new(try) if try.HasCaught() err = JavascriptError.new(try) if try.HasCaught()
end end
end end