| 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 1032 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1043 CHECK_EQ(1.0, t->NumberValue()); | 1043 CHECK_EQ(1.0, t->NumberValue()); |
| 1044 v8::Handle<v8::Boolean> f = v8::False(); | 1044 v8::Handle<v8::Boolean> f = v8::False(); |
| 1045 CHECK_EQ(0.0, f->NumberValue()); | 1045 CHECK_EQ(0.0, f->NumberValue()); |
| 1046 } | 1046 } |
| 1047 | 1047 |
| 1048 | 1048 |
| 1049 THREADED_TEST(Date) { | 1049 THREADED_TEST(Date) { |
| 1050 v8::HandleScope scope; | 1050 v8::HandleScope scope; |
| 1051 LocalContext env; | 1051 LocalContext env; |
| 1052 double PI = 3.1415926; | 1052 double PI = 3.1415926; |
| 1053 Local<Value> date_obj = v8::Date::New(PI); | 1053 Local<Value> date = v8::Date::New(PI); |
| 1054 CHECK_EQ(3.0, date_obj->NumberValue()); | 1054 CHECK_EQ(3.0, date->NumberValue()); |
| 1055 date.As<v8::Date>()->Set(v8_str("property"), v8::Integer::New(42)); |
| 1056 CHECK_EQ(42, date.As<v8::Date>()->Get(v8_str("property"))->Int32Value()); |
| 1055 } | 1057 } |
| 1056 | 1058 |
| 1057 | 1059 |
| 1058 THREADED_TEST(Boolean) { | 1060 THREADED_TEST(Boolean) { |
| 1059 v8::HandleScope scope; | 1061 v8::HandleScope scope; |
| 1060 LocalContext env; | 1062 LocalContext env; |
| 1061 v8::Handle<v8::Boolean> t = v8::True(); | 1063 v8::Handle<v8::Boolean> t = v8::True(); |
| 1062 CHECK(t->Value()); | 1064 CHECK(t->Value()); |
| 1063 v8::Handle<v8::Boolean> f = v8::False(); | 1065 v8::Handle<v8::Boolean> f = v8::False(); |
| 1064 CHECK(!f->Value()); | 1066 CHECK(!f->Value()); |
| (...skipping 5888 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 6953 } | 6955 } |
| 6954 | 6956 |
| 6955 | 6957 |
| 6956 // Test that a call handler can be set for objects which will allow | 6958 // 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 | 6959 // non-function objects created through the API to be called as |
| 6958 // functions. | 6960 // functions. |
| 6959 THREADED_TEST(CallAsFunction) { | 6961 THREADED_TEST(CallAsFunction) { |
| 6960 v8::HandleScope scope; | 6962 v8::HandleScope scope; |
| 6961 LocalContext context; | 6963 LocalContext context; |
| 6962 | 6964 |
| 6963 { Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New(); | 6965 Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New(); |
| 6964 Local<ObjectTemplate> instance_template = t->InstanceTemplate(); | 6966 Local<ObjectTemplate> instance_template = t->InstanceTemplate(); |
| 6965 instance_template->SetCallAsFunctionHandler(call_as_function); | 6967 instance_template->SetCallAsFunctionHandler(call_as_function); |
| 6966 Local<v8::Object> instance = t->GetFunction()->NewInstance(); | 6968 Local<v8::Object> instance = t->GetFunction()->NewInstance(); |
| 6967 context->Global()->Set(v8_str("obj"), instance); | 6969 context->Global()->Set(v8_str("obj"), instance); |
| 6968 v8::TryCatch try_catch; | 6970 v8::TryCatch try_catch; |
| 6969 Local<Value> value; | 6971 Local<Value> value; |
| 6970 CHECK(!try_catch.HasCaught()); | 6972 CHECK(!try_catch.HasCaught()); |
| 6971 | 6973 |
| 6972 value = CompileRun("obj(42)"); | 6974 value = CompileRun("obj(42)"); |
| 6973 CHECK(!try_catch.HasCaught()); | 6975 CHECK(!try_catch.HasCaught()); |
| 6974 CHECK_EQ(42, value->Int32Value()); | 6976 CHECK_EQ(42, value->Int32Value()); |
| 6975 | 6977 |
| 6976 value = CompileRun("(function(o){return o(49)})(obj)"); | 6978 value = CompileRun("(function(o){return o(49)})(obj)"); |
| 6977 CHECK(!try_catch.HasCaught()); | 6979 CHECK(!try_catch.HasCaught()); |
| 6978 CHECK_EQ(49, value->Int32Value()); | 6980 CHECK_EQ(49, value->Int32Value()); |
| 6979 | 6981 |
| 6980 // test special case of call as function | 6982 // test special case of call as function |
| 6981 value = CompileRun("[obj]['0'](45)"); | 6983 value = CompileRun("[obj]['0'](45)"); |
| 6982 CHECK(!try_catch.HasCaught()); | 6984 CHECK(!try_catch.HasCaught()); |
| 6983 CHECK_EQ(45, value->Int32Value()); | 6985 CHECK_EQ(45, value->Int32Value()); |
| 6984 | 6986 |
| 6985 value = CompileRun("obj.call = Function.prototype.call;" | 6987 value = CompileRun("obj.call = Function.prototype.call;" |
| 6986 "obj.call(null, 87)"); | 6988 "obj.call(null, 87)"); |
| 6987 CHECK(!try_catch.HasCaught()); | 6989 CHECK(!try_catch.HasCaught()); |
| 6988 CHECK_EQ(87, value->Int32Value()); | 6990 CHECK_EQ(87, value->Int32Value()); |
| 6989 | 6991 |
| 6990 // Regression tests for bug #1116356: Calling call through call/apply | 6992 // Regression tests for bug #1116356: Calling call through call/apply |
| 6991 // must work for non-function receivers. | 6993 // must work for non-function receivers. |
| 6992 const char* apply_99 = "Function.prototype.call.apply(obj, [this, 99])"; | 6994 const char* apply_99 = "Function.prototype.call.apply(obj, [this, 99])"; |
| 6993 value = CompileRun(apply_99); | 6995 value = CompileRun(apply_99); |
| 6994 CHECK(!try_catch.HasCaught()); | 6996 CHECK(!try_catch.HasCaught()); |
| 6995 CHECK_EQ(99, value->Int32Value()); | 6997 CHECK_EQ(99, value->Int32Value()); |
| 6996 | 6998 |
| 6997 const char* call_17 = "Function.prototype.call.call(obj, this, 17)"; | 6999 const char* call_17 = "Function.prototype.call.call(obj, this, 17)"; |
| 6998 value = CompileRun(call_17); | 7000 value = CompileRun(call_17); |
| 6999 CHECK(!try_catch.HasCaught()); | 7001 CHECK(!try_catch.HasCaught()); |
| 7000 CHECK_EQ(17, value->Int32Value()); | 7002 CHECK_EQ(17, value->Int32Value()); |
| 7001 | 7003 |
| 7002 // Check that the call-as-function handler can be called through | 7004 // Check that the call-as-function handler can be called through |
| 7003 // new. | 7005 // new. |
| 7004 value = CompileRun("new obj(43)"); | 7006 value = CompileRun("new obj(43)"); |
| 7005 CHECK(!try_catch.HasCaught()); | 7007 CHECK(!try_catch.HasCaught()); |
| 7006 CHECK_EQ(-43, value->Int32Value()); | 7008 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 } | |
| 7068 } | 7009 } |
| 7069 | 7010 |
| 7070 | 7011 |
| 7071 static int CountHandles() { | 7012 static int CountHandles() { |
| 7072 return v8::HandleScope::NumberOfHandles(); | 7013 return v8::HandleScope::NumberOfHandles(); |
| 7073 } | 7014 } |
| 7074 | 7015 |
| 7075 | 7016 |
| 7076 static int Recurse(int depth, int iterations) { | 7017 static int Recurse(int depth, int iterations) { |
| 7077 v8::HandleScope scope; | 7018 v8::HandleScope scope; |
| (...skipping 6716 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 13794 static_cast<v8::RegExp::Flags>(v8::RegExp::kIgnoreCase | | 13735 static_cast<v8::RegExp::Flags>(v8::RegExp::kIgnoreCase | |
| 13795 v8::RegExp::kMultiline)); | 13736 v8::RegExp::kMultiline)); |
| 13796 CHECK(re->IsRegExp()); | 13737 CHECK(re->IsRegExp()); |
| 13797 CHECK(re->GetSource()->Equals(v8_str("foobarbaz"))); | 13738 CHECK(re->GetSource()->Equals(v8_str("foobarbaz"))); |
| 13798 CHECK_EQ(static_cast<int>(re->GetFlags()), | 13739 CHECK_EQ(static_cast<int>(re->GetFlags()), |
| 13799 v8::RegExp::kIgnoreCase | v8::RegExp::kMultiline); | 13740 v8::RegExp::kIgnoreCase | v8::RegExp::kMultiline); |
| 13800 | 13741 |
| 13801 context->Global()->Set(v8_str("re"), re); | 13742 context->Global()->Set(v8_str("re"), re); |
| 13802 ExpectTrue("re.test('FoobarbaZ')"); | 13743 ExpectTrue("re.test('FoobarbaZ')"); |
| 13803 | 13744 |
| 13745 // RegExps are objects on which you can set properties. |
| 13746 re->Set(v8_str("property"), v8::Integer::New(32)); |
| 13747 v8::Handle<v8::Value> value = CompileRun("re.property"); |
| 13748 ASSERT_EQ(32, value->Int32Value()); |
| 13749 |
| 13804 v8::TryCatch try_catch; | 13750 v8::TryCatch try_catch; |
| 13805 re = v8::RegExp::New(v8_str("foo["), v8::RegExp::kNone); | 13751 re = v8::RegExp::New(v8_str("foo["), v8::RegExp::kNone); |
| 13806 CHECK(re.IsEmpty()); | 13752 CHECK(re.IsEmpty()); |
| 13807 CHECK(try_catch.HasCaught()); | 13753 CHECK(try_catch.HasCaught()); |
| 13808 context->Global()->Set(v8_str("ex"), try_catch.Exception()); | 13754 context->Global()->Set(v8_str("ex"), try_catch.Exception()); |
| 13809 ExpectTrue("ex instanceof SyntaxError"); | 13755 ExpectTrue("ex instanceof SyntaxError"); |
| 13810 } | 13756 } |
| 13811 | 13757 |
| 13812 | 13758 |
| 13813 THREADED_TEST(Equals) { | 13759 THREADED_TEST(Equals) { |
| (...skipping 337 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 14151 | 14097 |
| 14152 // Disallow but setting a global callback that will allow the calls. | 14098 // Disallow but setting a global callback that will allow the calls. |
| 14153 context->AllowCodeGenerationFromStrings(false); | 14099 context->AllowCodeGenerationFromStrings(false); |
| 14154 V8::SetAllowCodeGenerationFromStringsCallback(&CodeGenerationAllowed); | 14100 V8::SetAllowCodeGenerationFromStringsCallback(&CodeGenerationAllowed); |
| 14155 CheckCodeGenerationAllowed(); | 14101 CheckCodeGenerationAllowed(); |
| 14156 | 14102 |
| 14157 // Set a callback that disallows the code generation. | 14103 // Set a callback that disallows the code generation. |
| 14158 V8::SetAllowCodeGenerationFromStringsCallback(&CodeGenerationDisallowed); | 14104 V8::SetAllowCodeGenerationFromStringsCallback(&CodeGenerationDisallowed); |
| 14159 CheckCodeGenerationDisallowed(); | 14105 CheckCodeGenerationDisallowed(); |
| 14160 } | 14106 } |
| OLD | NEW |