diff --git a/ext/v8/v8_func.cpp b/ext/v8/v8_func.cpp index 0292cee..98eda77 100644 --- a/ext/v8/v8_func.cpp +++ b/ext/v8/v8_func.cpp @@ -13,40 +13,23 @@ namespace { Local unwrap(VALUE value) { return V8_Ref_Get(value); } - VALUE Call(int argc, VALUE *argv, VALUE self) { + VALUE Call(VALUE self, VALUE recv, VALUE arguments) { HandleScope handles; - VALUE recv; VALUE f_argv; - rb_scan_args(argc, argv, "1*", &recv, &f_argv); - - Local function = V8_Ref_Get(self); - Local thisObject; - if (NIL_P(recv)) { - if (Context::InContext()) { - thisObject = Context::GetEntered()->Global(); - } else { - Persistent cxt = Context::New(); - Context::Scope scope(cxt); - thisObject = Object::New(); - cxt.Dispose(); - } - } else { - if (!Context::InContext()) { - Persistent cxt = Context::New(); - cxt->Enter(); - thisObject = rr_rb2v8(recv)->ToObject(); - cxt->Exit(); - } else { - thisObject = rr_rb2v8(recv)->ToObject(); - } + if (!Context::InContext()) { + rb_raise(rb_eScriptError, "no open V8 Context in V8::C::Function::Call()"); + return Qnil; } - int f_argc = argc - 1; - Local arguments[f_argc]; - for (int i = 0; i < f_argc; i++) { - arguments[i] = *rr_rb2v8(rb_ary_entry(f_argv, i)); + 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); } - Local result = function->Call(thisObject, f_argc, arguments); - return rr_v82rb(result); + return rr_v82rb(function->Call(thisObj, argc, argv)); } + VALUE NewInstance(VALUE self, VALUE argc, VALUE args) { HandleScope scope; Local function = unwrap(self); @@ -72,7 +55,7 @@ namespace { void rr_init_func() { 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, "GetName", GetName, 0); rr_define_method(FunctionClass, "SetName", SetName, 1); diff --git a/lib/v8/function.rb b/lib/v8/function.rb index 5a471e4..287d7d9 100644 --- a/lib/v8/function.rb +++ b/lib/v8/function.rb @@ -7,7 +7,7 @@ module V8 C::TryCatch.try do |try| @context.enter do 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() end end