Chromium Code Reviews| 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 #<Object> is not a function"); | |
|
Mads Ager (chromium)
2011/05/04 09:28:54
Instead of checking the actual text here I think w
| |
| 7031 try_catch.Reset(); | |
| 7032 | |
| 7033 // Call an object without call-as-function handler through the API | |
| 7034 value = CompileRun("obj2(28)"); | |
| 7035 v8::Handle<Value> args[] = { v8_num(28) }; | |
| 7036 value = instance->CallAsFunction(instance, 1, args); | |
| 7037 CHECK(value.IsEmpty()); | |
| 7038 CHECK(try_catch.HasCaught()); | |
| 7039 String::AsciiValue exception_value2(try_catch.Exception()); | |
| 7040 CHECK_EQ(*exception_value2, "TypeError: [object Object] is not a function"); | |
|
Mads Ager (chromium)
2011/05/04 09:28:54
Same here.
| |
| 7041 try_catch.Reset(); | |
| 7042 } | |
| 7043 | |
| 7044 { Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New(); | |
| 7045 Local<ObjectTemplate> instance_template = t->InstanceTemplate(); | |
| 7046 instance_template->SetCallAsFunctionHandler(ThrowValue); | |
| 7047 Local<v8::Object> instance = t->GetFunction()->NewInstance(); | |
| 7048 context->Global()->Set(v8_str("obj3"), instance); | |
| 7049 v8::TryCatch try_catch; | |
| 7050 Local<Value> value; | |
| 7051 CHECK(!try_catch.HasCaught()); | |
| 7052 | |
| 7053 // Catch the exception which is thrown by call-as-function handler | |
| 7054 value = CompileRun("obj3(22)"); | |
| 7055 CHECK(try_catch.HasCaught()); | |
| 7056 String::AsciiValue exception_value1(try_catch.Exception()); | |
| 7057 CHECK_EQ(*exception_value1, "22"); | |
| 7058 try_catch.Reset(); | |
| 7059 | |
| 7060 v8::Handle<Value> args[] = { v8_num(23) }; | |
| 7061 value = instance->CallAsFunction(instance, 1, args); | |
| 7062 CHECK(try_catch.HasCaught()); | |
| 7063 String::AsciiValue exception_value2(try_catch.Exception()); | |
| 7064 CHECK_EQ(*exception_value2, "23"); | |
| 7065 try_catch.Reset(); | |
| 7066 } | |
| 7007 } | 7067 } |
| 7008 | 7068 |
| 7009 | 7069 |
| 7010 static int CountHandles() { | 7070 static int CountHandles() { |
| 7011 return v8::HandleScope::NumberOfHandles(); | 7071 return v8::HandleScope::NumberOfHandles(); |
| 7012 } | 7072 } |
| 7013 | 7073 |
| 7014 | 7074 |
| 7015 static int Recurse(int depth, int iterations) { | 7075 static int Recurse(int depth, int iterations) { |
| 7016 v8::HandleScope scope; | 7076 v8::HandleScope scope; |
| (...skipping 7073 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 14090 | 14150 |
| 14091 // Disallow but setting a global callback that will allow the calls. | 14151 // Disallow but setting a global callback that will allow the calls. |
| 14092 context->AllowCodeGenerationFromStrings(false); | 14152 context->AllowCodeGenerationFromStrings(false); |
| 14093 V8::SetAllowCodeGenerationFromStringsCallback(&CodeGenerationAllowed); | 14153 V8::SetAllowCodeGenerationFromStringsCallback(&CodeGenerationAllowed); |
| 14094 CheckCodeGenerationAllowed(); | 14154 CheckCodeGenerationAllowed(); |
| 14095 | 14155 |
| 14096 // Set a callback that disallows the code generation. | 14156 // Set a callback that disallows the code generation. |
| 14097 V8::SetAllowCodeGenerationFromStringsCallback(&CodeGenerationDisallowed); | 14157 V8::SetAllowCodeGenerationFromStringsCallback(&CodeGenerationDisallowed); |
| 14098 CheckCodeGenerationDisallowed(); | 14158 CheckCodeGenerationDisallowed(); |
| 14099 } | 14159 } |
| OLD | NEW |