OLD | NEW |
1 // Copyright 2007-2009 the V8 project authors. All rights reserved. | 1 // Copyright 2007-2009 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 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
44 | 44 |
45 static bool IsNaN(double x) { | 45 static bool IsNaN(double x) { |
46 #ifdef WIN32 | 46 #ifdef WIN32 |
47 return _isnan(x); | 47 return _isnan(x); |
48 #else | 48 #else |
49 return isnan(x); | 49 return isnan(x); |
50 #endif | 50 #endif |
51 } | 51 } |
52 | 52 |
53 using ::v8::ObjectTemplate; | 53 using ::v8::ObjectTemplate; |
| 54 using ::v8::FunctionTemplate; |
54 using ::v8::Value; | 55 using ::v8::Value; |
55 using ::v8::Context; | 56 using ::v8::Context; |
56 using ::v8::Local; | 57 using ::v8::Local; |
57 using ::v8::String; | 58 using ::v8::String; |
58 using ::v8::Script; | 59 using ::v8::Script; |
59 using ::v8::Function; | 60 using ::v8::Function; |
60 using ::v8::AccessorInfo; | 61 using ::v8::AccessorInfo; |
61 using ::v8::Extension; | 62 using ::v8::Extension; |
| 63 using ::v8::Handle; |
| 64 using ::v8::HandleScope; |
| 65 using ::v8::Undefined; |
| 66 using ::v8::Object; |
| 67 using ::v8::TryCatch; |
| 68 using ::v8::Message; |
| 69 using ::v8::MessageCallback; |
| 70 using ::v8::V8; |
| 71 using ::v8::StackTrace; |
| 72 using ::v8::Arguments; |
62 | 73 |
63 namespace i = ::i; | 74 namespace i = ::i; |
64 | 75 |
65 | 76 |
66 static void ExpectString(const char* code, const char* expected) { | 77 static void ExpectString(const char* code, const char* expected) { |
67 Local<Value> result = CompileRun(code); | 78 Local<Value> result = CompileRun(code); |
68 CHECK(result->IsString()); | 79 CHECK(result->IsString()); |
69 String::AsciiValue ascii(result); | 80 String::AsciiValue ascii(result); |
70 CHECK_EQ(expected, *ascii); | 81 CHECK_EQ(expected, *ascii); |
71 } | 82 } |
(...skipping 7899 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
7971 v8::Handle<v8::FunctionTemplate> templ = v8::FunctionTemplate::New(); | 7982 v8::Handle<v8::FunctionTemplate> templ = v8::FunctionTemplate::New(); |
7972 templ->InstanceTemplate()->SetNamedPropertyHandler(InterceptorLoadXICGetter); | 7983 templ->InstanceTemplate()->SetNamedPropertyHandler(InterceptorLoadXICGetter); |
7973 LocalContext env; | 7984 LocalContext env; |
7974 env->Global()->Set(v8_str("obj"), | 7985 env->Global()->Set(v8_str("obj"), |
7975 templ->GetFunction()->NewInstance()); | 7986 templ->GetFunction()->NewInstance()); |
7976 ExpectTrue("obj.x === 42"); | 7987 ExpectTrue("obj.x === 42"); |
7977 ExpectTrue("!obj.propertyIsEnumerable('x')"); | 7988 ExpectTrue("!obj.propertyIsEnumerable('x')"); |
7978 } | 7989 } |
7979 | 7990 |
7980 | 7991 |
| 7992 static Handle<Value> ThrowingGetter(Local<String> name, |
| 7993 const AccessorInfo& info) { |
| 7994 ApiTestFuzzer::Fuzz(); |
| 7995 ThrowException(Handle<Value>()); |
| 7996 return Undefined(); |
| 7997 } |
| 7998 |
| 7999 |
| 8000 THREADED_TEST(VariousGetPropertiesAndThrowingCallbacks) { |
| 8001 HandleScope scope; |
| 8002 LocalContext context; |
| 8003 |
| 8004 Local<FunctionTemplate> templ = FunctionTemplate::New(); |
| 8005 Local<ObjectTemplate> instance_templ = templ->InstanceTemplate(); |
| 8006 instance_templ->SetAccessor(v8_str("f"), ThrowingGetter); |
| 8007 |
| 8008 Local<Object> instance = templ->GetFunction()->NewInstance(); |
| 8009 |
| 8010 Local<Object> withJsGetter = CompileRun( |
| 8011 "o = {};\n" |
| 8012 "o.__defineGetter__('f', function() { throw undefined; });\n" |
| 8013 "o\n").As<Object>(); |
| 8014 CHECK(!withJsGetter.IsEmpty()); |
| 8015 |
| 8016 Local<Object> another = Object::New(); |
| 8017 another->SetPrototype(instance); |
| 8018 |
| 8019 TryCatch try_catch; |
| 8020 |
| 8021 Local<Value> result = instance->GetRealNamedProperty(v8_str("f")); |
| 8022 CHECK(try_catch.HasCaught()); |
| 8023 try_catch.Reset(); |
| 8024 CHECK(result.IsEmpty()); |
| 8025 |
| 8026 result = another->GetRealNamedProperty(v8_str("f")); |
| 8027 CHECK(try_catch.HasCaught()); |
| 8028 try_catch.Reset(); |
| 8029 CHECK(result.IsEmpty()); |
| 8030 |
| 8031 result = another->GetRealNamedPropertyInPrototypeChain(v8_str("f")); |
| 8032 CHECK(try_catch.HasCaught()); |
| 8033 try_catch.Reset(); |
| 8034 CHECK(result.IsEmpty()); |
| 8035 |
| 8036 result = another->Get(v8_str("f")); |
| 8037 CHECK(try_catch.HasCaught()); |
| 8038 try_catch.Reset(); |
| 8039 CHECK(result.IsEmpty()); |
| 8040 |
| 8041 result = withJsGetter->GetRealNamedProperty(v8_str("f")); |
| 8042 CHECK(try_catch.HasCaught()); |
| 8043 try_catch.Reset(); |
| 8044 CHECK(result.IsEmpty()); |
| 8045 |
| 8046 result = withJsGetter->Get(v8_str("f")); |
| 8047 CHECK(try_catch.HasCaught()); |
| 8048 try_catch.Reset(); |
| 8049 CHECK(result.IsEmpty()); |
| 8050 } |
| 8051 |
| 8052 |
| 8053 static Handle<Value> ThrowingCallbackWithTryCatch(const Arguments& args) { |
| 8054 TryCatch try_catch; |
| 8055 // Verboseness is important: it triggers message delivery which can call into |
| 8056 // external code. |
| 8057 try_catch.SetVerbose(true); |
| 8058 CompileRun("throw 'from JS';"); |
| 8059 CHECK(try_catch.HasCaught()); |
| 8060 CHECK(!i::Top::has_pending_exception()); |
| 8061 CHECK(!i::Top::has_scheduled_exception()); |
| 8062 return Undefined(); |
| 8063 } |
| 8064 |
| 8065 |
| 8066 static void WithTryCatch(Handle<Message> message, Handle<Value> data) { |
| 8067 TryCatch try_catch; |
| 8068 } |
| 8069 |
| 8070 |
| 8071 static void ThrowFromJS(Handle<Message> message, Handle<Value> data) { |
| 8072 CompileRun("throw 'ThrowInJS';"); |
| 8073 } |
| 8074 |
| 8075 |
| 8076 static void ThrowViaApi(Handle<Message> message, Handle<Value> data) { |
| 8077 ThrowException(v8_str("ThrowViaApi")); |
| 8078 } |
| 8079 |
| 8080 |
| 8081 static void WebKitLike(Handle<Message> message, Handle<Value> data) { |
| 8082 Handle<String> errorMessageString = message->Get(); |
| 8083 CHECK(!errorMessageString.IsEmpty()); |
| 8084 message->GetStackTrace(); |
| 8085 message->GetScriptResourceName(); |
| 8086 } |
| 8087 |
| 8088 THREADED_TEST(ExceptionsDoNotPropagatePastTryCatch) { |
| 8089 HandleScope scope; |
| 8090 LocalContext context; |
| 8091 |
| 8092 Local<Function> func = |
| 8093 FunctionTemplate::New(ThrowingCallbackWithTryCatch)->GetFunction(); |
| 8094 context->Global()->Set(v8_str("func"), func); |
| 8095 |
| 8096 MessageCallback callbacks[] = |
| 8097 { NULL, WebKitLike, ThrowViaApi, ThrowFromJS, WithTryCatch }; |
| 8098 for (unsigned i = 0; i < sizeof(callbacks)/sizeof(callbacks[0]); i++) { |
| 8099 MessageCallback callback = callbacks[i]; |
| 8100 if (callback != NULL) { |
| 8101 V8::AddMessageListener(callback); |
| 8102 } |
| 8103 ExpectFalse( |
| 8104 "var thrown = false;\n" |
| 8105 "try { func(); } catch(e) { thrown = true; }\n" |
| 8106 "thrown\n"); |
| 8107 if (callback != NULL) { |
| 8108 V8::RemoveMessageListeners(callback); |
| 8109 } |
| 8110 } |
| 8111 } |
| 8112 |
| 8113 |
7981 static v8::Handle<Value> ParentGetter(Local<String> name, | 8114 static v8::Handle<Value> ParentGetter(Local<String> name, |
7982 const AccessorInfo& info) { | 8115 const AccessorInfo& info) { |
7983 ApiTestFuzzer::Fuzz(); | 8116 ApiTestFuzzer::Fuzz(); |
7984 return v8_num(1); | 8117 return v8_num(1); |
7985 } | 8118 } |
7986 | 8119 |
7987 | 8120 |
7988 static v8::Handle<Value> ChildGetter(Local<String> name, | 8121 static v8::Handle<Value> ChildGetter(Local<String> name, |
7989 const AccessorInfo& info) { | 8122 const AccessorInfo& info) { |
7990 ApiTestFuzzer::Fuzz(); | 8123 ApiTestFuzzer::Fuzz(); |
(...skipping 4219 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
12210 v8::Context::Scope context_scope(context.local()); | 12343 v8::Context::Scope context_scope(context.local()); |
12211 | 12344 |
12212 v8::Handle<v8::ObjectTemplate> tmpl = v8::ObjectTemplate::New(); | 12345 v8::Handle<v8::ObjectTemplate> tmpl = v8::ObjectTemplate::New(); |
12213 tmpl->SetNamedPropertyHandler(Getter, NULL, NULL, NULL, Enumerator); | 12346 tmpl->SetNamedPropertyHandler(Getter, NULL, NULL, NULL, Enumerator); |
12214 context->Global()->Set(v8_str("o"), tmpl->NewInstance()); | 12347 context->Global()->Set(v8_str("o"), tmpl->NewInstance()); |
12215 v8::Handle<v8::Array> result = v8::Handle<v8::Array>::Cast(CompileRun( | 12348 v8::Handle<v8::Array> result = v8::Handle<v8::Array>::Cast(CompileRun( |
12216 "var result = []; for (var k in o) result.push(k); result")); | 12349 "var result = []; for (var k in o) result.push(k); result")); |
12217 CHECK_EQ(1, result->Length()); | 12350 CHECK_EQ(1, result->Length()); |
12218 CHECK_EQ(v8_str("universalAnswer"), result->Get(0)); | 12351 CHECK_EQ(v8_str("universalAnswer"), result->Get(0)); |
12219 } | 12352 } |
OLD | NEW |