OLD | NEW |
1 // Copyright 2011 the V8 project authors. All rights reserved. | 1 // Copyright 2011 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 6942 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
6953 } | 6953 } |
6954 | 6954 |
6955 | 6955 |
6956 // Test that a call handler can be set for objects which will allow | 6956 // Test that a call handler can be set for objects which will allow |
6957 // non-function objects created through the API to be called as | 6957 // non-function objects created through the API to be called as |
6958 // functions. | 6958 // functions. |
6959 THREADED_TEST(CallAsFunction) { | 6959 THREADED_TEST(CallAsFunction) { |
6960 v8::HandleScope scope; | 6960 v8::HandleScope scope; |
6961 LocalContext context; | 6961 LocalContext context; |
6962 | 6962 |
6963 Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New(); | 6963 { Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New(); |
6964 Local<ObjectTemplate> instance_template = t->InstanceTemplate(); | 6964 Local<ObjectTemplate> instance_template = t->InstanceTemplate(); |
6965 instance_template->SetCallAsFunctionHandler(call_as_function); | 6965 instance_template->SetCallAsFunctionHandler(call_as_function); |
6966 Local<v8::Object> instance = t->GetFunction()->NewInstance(); | 6966 Local<v8::Object> instance = t->GetFunction()->NewInstance(); |
6967 context->Global()->Set(v8_str("obj"), instance); | 6967 context->Global()->Set(v8_str("obj"), instance); |
6968 v8::TryCatch try_catch; | 6968 v8::TryCatch try_catch; |
6969 Local<Value> value; | 6969 Local<Value> value; |
6970 CHECK(!try_catch.HasCaught()); | 6970 CHECK(!try_catch.HasCaught()); |
6971 | 6971 |
6972 value = CompileRun("obj(42)"); | 6972 value = CompileRun("obj(42)"); |
6973 CHECK(!try_catch.HasCaught()); | 6973 CHECK(!try_catch.HasCaught()); |
6974 CHECK_EQ(42, value->Int32Value()); | 6974 CHECK_EQ(42, value->Int32Value()); |
6975 | 6975 |
6976 value = CompileRun("(function(o){return o(49)})(obj)"); | 6976 value = CompileRun("(function(o){return o(49)})(obj)"); |
6977 CHECK(!try_catch.HasCaught()); | 6977 CHECK(!try_catch.HasCaught()); |
6978 CHECK_EQ(49, value->Int32Value()); | 6978 CHECK_EQ(49, value->Int32Value()); |
6979 | 6979 |
6980 // test special case of call as function | 6980 // test special case of call as function |
6981 value = CompileRun("[obj]['0'](45)"); | 6981 value = CompileRun("[obj]['0'](45)"); |
6982 CHECK(!try_catch.HasCaught()); | 6982 CHECK(!try_catch.HasCaught()); |
6983 CHECK_EQ(45, value->Int32Value()); | 6983 CHECK_EQ(45, value->Int32Value()); |
6984 | 6984 |
6985 value = CompileRun("obj.call = Function.prototype.call;" | 6985 value = CompileRun("obj.call = Function.prototype.call;" |
6986 "obj.call(null, 87)"); | 6986 "obj.call(null, 87)"); |
6987 CHECK(!try_catch.HasCaught()); | 6987 CHECK(!try_catch.HasCaught()); |
6988 CHECK_EQ(87, value->Int32Value()); | 6988 CHECK_EQ(87, value->Int32Value()); |
6989 | 6989 |
6990 // Regression tests for bug #1116356: Calling call through call/apply | 6990 // Regression tests for bug #1116356: Calling call through call/apply |
6991 // must work for non-function receivers. | 6991 // must work for non-function receivers. |
6992 const char* apply_99 = "Function.prototype.call.apply(obj, [this, 99])"; | 6992 const char* apply_99 = "Function.prototype.call.apply(obj, [this, 99])"; |
6993 value = CompileRun(apply_99); | 6993 value = CompileRun(apply_99); |
6994 CHECK(!try_catch.HasCaught()); | 6994 CHECK(!try_catch.HasCaught()); |
6995 CHECK_EQ(99, value->Int32Value()); | 6995 CHECK_EQ(99, value->Int32Value()); |
6996 | 6996 |
6997 const char* call_17 = "Function.prototype.call.call(obj, this, 17)"; | 6997 const char* call_17 = "Function.prototype.call.call(obj, this, 17)"; |
6998 value = CompileRun(call_17); | 6998 value = CompileRun(call_17); |
6999 CHECK(!try_catch.HasCaught()); | 6999 CHECK(!try_catch.HasCaught()); |
7000 CHECK_EQ(17, value->Int32Value()); | 7000 CHECK_EQ(17, value->Int32Value()); |
7001 | 7001 |
7002 // Check that the call-as-function handler can be called through | 7002 // Check that the call-as-function handler can be called through |
7003 // new. | 7003 // new. |
7004 value = CompileRun("new obj(43)"); | 7004 value = CompileRun("new obj(43)"); |
7005 CHECK(!try_catch.HasCaught()); | 7005 CHECK(!try_catch.HasCaught()); |
7006 CHECK_EQ(-43, value->Int32Value()); | 7006 CHECK_EQ(-43, value->Int32Value()); |
| 7007 |
| 7008 // Check that the call-as-function handler can be called through |
| 7009 // the API. |
| 7010 v8::Handle<Value> args[] = { v8_num(28) }; |
| 7011 value = instance->CallAsFunction(instance, 1, args); |
| 7012 CHECK(!try_catch.HasCaught()); |
| 7013 CHECK_EQ(28, value->Int32Value()); |
| 7014 } |
| 7015 |
| 7016 { Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New(); |
| 7017 Local<ObjectTemplate> instance_template = t->InstanceTemplate(); |
| 7018 Local<v8::Object> instance = t->GetFunction()->NewInstance(); |
| 7019 context->Global()->Set(v8_str("obj2"), instance); |
| 7020 v8::TryCatch try_catch; |
| 7021 Local<Value> value; |
| 7022 CHECK(!try_catch.HasCaught()); |
| 7023 |
| 7024 // Call an object without call-as-function handler through the JS |
| 7025 value = CompileRun("obj2(28)"); |
| 7026 CHECK(value.IsEmpty()); |
| 7027 CHECK(try_catch.HasCaught()); |
| 7028 String::AsciiValue exception_value1(try_catch.Exception()); |
| 7029 CHECK_EQ(*exception_value1, |
| 7030 "TypeError: Property 'obj2' of object " |
| 7031 "#<Object> is not a function"); |
| 7032 try_catch.Reset(); |
| 7033 |
| 7034 // Call an object without call-as-function handler through the API |
| 7035 value = CompileRun("obj2(28)"); |
| 7036 v8::Handle<Value> args[] = { v8_num(28) }; |
| 7037 value = instance->CallAsFunction(instance, 1, args); |
| 7038 CHECK(value.IsEmpty()); |
| 7039 CHECK(try_catch.HasCaught()); |
| 7040 String::AsciiValue exception_value2(try_catch.Exception()); |
| 7041 CHECK_EQ(*exception_value2, "TypeError: [object Object] is not a function"); |
| 7042 try_catch.Reset(); |
| 7043 } |
| 7044 |
| 7045 { Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New(); |
| 7046 Local<ObjectTemplate> instance_template = t->InstanceTemplate(); |
| 7047 instance_template->SetCallAsFunctionHandler(ThrowValue); |
| 7048 Local<v8::Object> instance = t->GetFunction()->NewInstance(); |
| 7049 context->Global()->Set(v8_str("obj3"), instance); |
| 7050 v8::TryCatch try_catch; |
| 7051 Local<Value> value; |
| 7052 CHECK(!try_catch.HasCaught()); |
| 7053 |
| 7054 // Catch the exception which is thrown by call-as-function handler |
| 7055 value = CompileRun("obj3(22)"); |
| 7056 CHECK(try_catch.HasCaught()); |
| 7057 String::AsciiValue exception_value1(try_catch.Exception()); |
| 7058 CHECK_EQ(*exception_value1, "22"); |
| 7059 try_catch.Reset(); |
| 7060 |
| 7061 v8::Handle<Value> args[] = { v8_num(23) }; |
| 7062 value = instance->CallAsFunction(instance, 1, args); |
| 7063 CHECK(try_catch.HasCaught()); |
| 7064 String::AsciiValue exception_value2(try_catch.Exception()); |
| 7065 CHECK_EQ(*exception_value2, "23"); |
| 7066 try_catch.Reset(); |
| 7067 } |
7007 } | 7068 } |
7008 | 7069 |
7009 | 7070 |
7010 static int CountHandles() { | 7071 static int CountHandles() { |
7011 return v8::HandleScope::NumberOfHandles(); | 7072 return v8::HandleScope::NumberOfHandles(); |
7012 } | 7073 } |
7013 | 7074 |
7014 | 7075 |
7015 static int Recurse(int depth, int iterations) { | 7076 static int Recurse(int depth, int iterations) { |
7016 v8::HandleScope scope; | 7077 v8::HandleScope scope; |
(...skipping 7073 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
14090 | 14151 |
14091 // Disallow but setting a global callback that will allow the calls. | 14152 // Disallow but setting a global callback that will allow the calls. |
14092 context->AllowCodeGenerationFromStrings(false); | 14153 context->AllowCodeGenerationFromStrings(false); |
14093 V8::SetAllowCodeGenerationFromStringsCallback(&CodeGenerationAllowed); | 14154 V8::SetAllowCodeGenerationFromStringsCallback(&CodeGenerationAllowed); |
14094 CheckCodeGenerationAllowed(); | 14155 CheckCodeGenerationAllowed(); |
14095 | 14156 |
14096 // Set a callback that disallows the code generation. | 14157 // Set a callback that disallows the code generation. |
14097 V8::SetAllowCodeGenerationFromStringsCallback(&CodeGenerationDisallowed); | 14158 V8::SetAllowCodeGenerationFromStringsCallback(&CodeGenerationDisallowed); |
14098 CheckCodeGenerationDisallowed(); | 14159 CheckCodeGenerationDisallowed(); |
14099 } | 14160 } |
OLD | NEW |