Chromium Code Reviews| Index: test/cctest/test-api.cc |
| diff --git a/test/cctest/test-api.cc b/test/cctest/test-api.cc |
| index 8548b441e990d03ccf85cb79518e57d922791bb5..3871c3381d7080bf39bffae9a9fb2a6a79c05711 100644 |
| --- a/test/cctest/test-api.cc |
| +++ b/test/cctest/test-api.cc |
| @@ -6960,50 +6960,110 @@ THREADED_TEST(CallAsFunction) { |
| v8::HandleScope scope; |
| LocalContext context; |
| - Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New(); |
| - Local<ObjectTemplate> instance_template = t->InstanceTemplate(); |
| - instance_template->SetCallAsFunctionHandler(call_as_function); |
| - Local<v8::Object> instance = t->GetFunction()->NewInstance(); |
| - context->Global()->Set(v8_str("obj"), instance); |
| - v8::TryCatch try_catch; |
| - Local<Value> value; |
| - CHECK(!try_catch.HasCaught()); |
| + { Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New(); |
| + Local<ObjectTemplate> instance_template = t->InstanceTemplate(); |
| + instance_template->SetCallAsFunctionHandler(call_as_function); |
| + Local<v8::Object> instance = t->GetFunction()->NewInstance(); |
| + context->Global()->Set(v8_str("obj"), instance); |
| + v8::TryCatch try_catch; |
| + Local<Value> value; |
| + CHECK(!try_catch.HasCaught()); |
| - value = CompileRun("obj(42)"); |
| - CHECK(!try_catch.HasCaught()); |
| - CHECK_EQ(42, value->Int32Value()); |
| + value = CompileRun("obj(42)"); |
| + CHECK(!try_catch.HasCaught()); |
| + CHECK_EQ(42, value->Int32Value()); |
| - value = CompileRun("(function(o){return o(49)})(obj)"); |
| - CHECK(!try_catch.HasCaught()); |
| - CHECK_EQ(49, value->Int32Value()); |
| + value = CompileRun("(function(o){return o(49)})(obj)"); |
| + CHECK(!try_catch.HasCaught()); |
| + CHECK_EQ(49, value->Int32Value()); |
| - // test special case of call as function |
| - value = CompileRun("[obj]['0'](45)"); |
| - CHECK(!try_catch.HasCaught()); |
| - CHECK_EQ(45, value->Int32Value()); |
| + // test special case of call as function |
| + value = CompileRun("[obj]['0'](45)"); |
| + CHECK(!try_catch.HasCaught()); |
| + CHECK_EQ(45, value->Int32Value()); |
| - value = CompileRun("obj.call = Function.prototype.call;" |
| - "obj.call(null, 87)"); |
| - CHECK(!try_catch.HasCaught()); |
| - CHECK_EQ(87, value->Int32Value()); |
| + value = CompileRun("obj.call = Function.prototype.call;" |
| + "obj.call(null, 87)"); |
| + CHECK(!try_catch.HasCaught()); |
| + CHECK_EQ(87, value->Int32Value()); |
| - // Regression tests for bug #1116356: Calling call through call/apply |
| - // must work for non-function receivers. |
| - const char* apply_99 = "Function.prototype.call.apply(obj, [this, 99])"; |
| - value = CompileRun(apply_99); |
| - CHECK(!try_catch.HasCaught()); |
| - CHECK_EQ(99, value->Int32Value()); |
| + // Regression tests for bug #1116356: Calling call through call/apply |
| + // must work for non-function receivers. |
| + const char* apply_99 = "Function.prototype.call.apply(obj, [this, 99])"; |
| + value = CompileRun(apply_99); |
| + CHECK(!try_catch.HasCaught()); |
| + CHECK_EQ(99, value->Int32Value()); |
| - const char* call_17 = "Function.prototype.call.call(obj, this, 17)"; |
| - value = CompileRun(call_17); |
| - CHECK(!try_catch.HasCaught()); |
| - CHECK_EQ(17, value->Int32Value()); |
| + const char* call_17 = "Function.prototype.call.call(obj, this, 17)"; |
| + value = CompileRun(call_17); |
| + CHECK(!try_catch.HasCaught()); |
| + CHECK_EQ(17, value->Int32Value()); |
| - // Check that the call-as-function handler can be called through |
| - // new. |
| - value = CompileRun("new obj(43)"); |
| - CHECK(!try_catch.HasCaught()); |
| - CHECK_EQ(-43, value->Int32Value()); |
| + // Check that the call-as-function handler can be called through |
| + // new. |
| + value = CompileRun("new obj(43)"); |
| + CHECK(!try_catch.HasCaught()); |
| + CHECK_EQ(-43, value->Int32Value()); |
| + |
| + // Check that the call-as-function handler can be called through |
| + // the API. |
| + v8::Handle<Value> args[] = { v8_num(28) }; |
| + value = instance->CallAsFunction(instance, 1, args); |
| + CHECK(!try_catch.HasCaught()); |
| + CHECK_EQ(28, value->Int32Value()); |
| + } |
| + |
| + { Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New(); |
| + Local<ObjectTemplate> instance_template = t->InstanceTemplate(); |
| + Local<v8::Object> instance = t->GetFunction()->NewInstance(); |
| + context->Global()->Set(v8_str("obj2"), instance); |
| + v8::TryCatch try_catch; |
| + Local<Value> value; |
| + CHECK(!try_catch.HasCaught()); |
| + |
| + // Call an object without call-as-function handler through the JS |
| + value = CompileRun("obj2(28)"); |
| + CHECK(value.IsEmpty()); |
| + CHECK(try_catch.HasCaught()); |
| + String::AsciiValue exception_value1(try_catch.Exception()); |
| + CHECK_EQ(*exception_value1, |
| + "TypeError: Property 'obj2' of object #<Object> is not a function"); |
|
Mads Ager (chromium)
2011/05/04 09:28:54
Instead of checking the actual text here I think w
|
| + try_catch.Reset(); |
| + |
| + // Call an object without call-as-function handler through the API |
| + value = CompileRun("obj2(28)"); |
| + v8::Handle<Value> args[] = { v8_num(28) }; |
| + value = instance->CallAsFunction(instance, 1, args); |
| + CHECK(value.IsEmpty()); |
| + CHECK(try_catch.HasCaught()); |
| + String::AsciiValue exception_value2(try_catch.Exception()); |
| + CHECK_EQ(*exception_value2, "TypeError: [object Object] is not a function"); |
|
Mads Ager (chromium)
2011/05/04 09:28:54
Same here.
|
| + try_catch.Reset(); |
| + } |
| + |
| + { Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New(); |
| + Local<ObjectTemplate> instance_template = t->InstanceTemplate(); |
| + instance_template->SetCallAsFunctionHandler(ThrowValue); |
| + Local<v8::Object> instance = t->GetFunction()->NewInstance(); |
| + context->Global()->Set(v8_str("obj3"), instance); |
| + v8::TryCatch try_catch; |
| + Local<Value> value; |
| + CHECK(!try_catch.HasCaught()); |
| + |
| + // Catch the exception which is thrown by call-as-function handler |
| + value = CompileRun("obj3(22)"); |
| + CHECK(try_catch.HasCaught()); |
| + String::AsciiValue exception_value1(try_catch.Exception()); |
| + CHECK_EQ(*exception_value1, "22"); |
| + try_catch.Reset(); |
| + |
| + v8::Handle<Value> args[] = { v8_num(23) }; |
| + value = instance->CallAsFunction(instance, 1, args); |
| + CHECK(try_catch.HasCaught()); |
| + String::AsciiValue exception_value2(try_catch.Exception()); |
| + CHECK_EQ(*exception_value2, "23"); |
| + try_catch.Reset(); |
| + } |
| } |