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 7901 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
7973 v8::Handle<v8::FunctionTemplate> templ = v8::FunctionTemplate::New(); | 7984 v8::Handle<v8::FunctionTemplate> templ = v8::FunctionTemplate::New(); |
7974 templ->InstanceTemplate()->SetNamedPropertyHandler(InterceptorLoadXICGetter); | 7985 templ->InstanceTemplate()->SetNamedPropertyHandler(InterceptorLoadXICGetter); |
7975 LocalContext env; | 7986 LocalContext env; |
7976 env->Global()->Set(v8_str("obj"), | 7987 env->Global()->Set(v8_str("obj"), |
7977 templ->GetFunction()->NewInstance()); | 7988 templ->GetFunction()->NewInstance()); |
7978 ExpectTrue("obj.x === 42"); | 7989 ExpectTrue("obj.x === 42"); |
7979 ExpectTrue("!obj.propertyIsEnumerable('x')"); | 7990 ExpectTrue("!obj.propertyIsEnumerable('x')"); |
7980 } | 7991 } |
7981 | 7992 |
7982 | 7993 |
7994 static Handle<Value> ThrowingGetter(Local<String> name, | |
7995 const AccessorInfo& info) { | |
7996 ApiTestFuzzer::Fuzz(); | |
7997 ThrowException(Handle<Value>()); | |
7998 return Undefined(); | |
7999 } | |
8000 | |
8001 | |
8002 THREADED_TEST(VariousGetPropertiesAndThrowingCallbacks) { | |
8003 HandleScope scope; | |
8004 LocalContext context; | |
8005 | |
8006 Local<FunctionTemplate> templ = FunctionTemplate::New(); | |
8007 Local<ObjectTemplate> instance_templ = templ->InstanceTemplate(); | |
8008 instance_templ->SetAccessor(v8_str("f"), ThrowingGetter); | |
8009 | |
8010 Local<Object> instance = templ->GetFunction()->NewInstance(); | |
8011 | |
8012 Local<Object> withJsGetter = CompileRun( | |
8013 "o = {};\n" | |
8014 "o.__defineGetter__('f', function() { throw undefined; });\n" | |
8015 "o\n").As<Object>(); | |
8016 CHECK(!withJsGetter.IsEmpty()); | |
Vitaly Repeshko
2011/02/01 19:07:17
nit: Avoid camel case.
antonm
2011/02/01 20:14:39
Done.
| |
8017 | |
8018 Local<Object> another = Object::New(); | |
8019 another->SetPrototype(instance); | |
8020 | |
8021 TryCatch try_catch; | |
8022 | |
8023 Local<Value> result = instance->GetRealNamedProperty(v8_str("f")); | |
8024 CHECK(try_catch.HasCaught()); | |
8025 try_catch.Reset(); | |
8026 CHECK(result.IsEmpty()); | |
8027 | |
8028 result = another->GetRealNamedProperty(v8_str("f")); | |
8029 CHECK(try_catch.HasCaught()); | |
8030 try_catch.Reset(); | |
8031 CHECK(result.IsEmpty()); | |
8032 | |
8033 result = another->GetRealNamedPropertyInPrototypeChain(v8_str("f")); | |
8034 CHECK(try_catch.HasCaught()); | |
8035 try_catch.Reset(); | |
8036 CHECK(result.IsEmpty()); | |
8037 | |
8038 result = another->Get(v8_str("f")); | |
8039 CHECK(try_catch.HasCaught()); | |
8040 try_catch.Reset(); | |
8041 CHECK(result.IsEmpty()); | |
8042 | |
8043 result = withJsGetter->GetRealNamedProperty(v8_str("f")); | |
8044 CHECK(try_catch.HasCaught()); | |
8045 try_catch.Reset(); | |
8046 CHECK(result.IsEmpty()); | |
8047 | |
8048 result = withJsGetter->Get(v8_str("f")); | |
8049 CHECK(try_catch.HasCaught()); | |
8050 try_catch.Reset(); | |
8051 CHECK(result.IsEmpty()); | |
8052 } | |
8053 | |
8054 | |
8055 static Handle<Value> ThrowingCallbackWithTryCatch(const Arguments& args) { | |
8056 TryCatch try_catch; | |
8057 // Verboseness is important: it triggers message delivery which can call into | |
8058 // external code. | |
8059 try_catch.SetVerbose(true); | |
8060 CompileRun("throw 'from JS';"); | |
8061 CHECK(try_catch.HasCaught()); | |
8062 CHECK(!i::Top::has_pending_exception()); | |
8063 CHECK(!i::Top::has_scheduled_exception()); | |
8064 return Undefined(); | |
8065 } | |
8066 | |
8067 | |
8068 static void WithTryCatch(Handle<Message> message, Handle<Value> data) { | |
8069 TryCatch try_catch; | |
8070 } | |
8071 | |
8072 | |
8073 static void ThrowFromJS(Handle<Message> message, Handle<Value> data) { | |
8074 CompileRun("throw 'ThrowInJS';"); | |
8075 } | |
8076 | |
8077 | |
8078 static void ThrowViaApi(Handle<Message> message, Handle<Value> data) { | |
8079 ThrowException(v8_str("ThrowViaApi")); | |
8080 } | |
8081 | |
8082 | |
8083 static void WebKitLike(Handle<Message> message, Handle<Value> data) { | |
8084 Handle<String> errorMessageString = message->Get(); | |
8085 CHECK(!errorMessageString.IsEmpty()); | |
8086 message->GetStackTrace(); | |
8087 message->GetScriptResourceName(); | |
8088 } | |
8089 | |
8090 THREADED_TEST(ExceptionsDoNotPropagatePastTryCatch) { | |
8091 HandleScope scope; | |
8092 LocalContext context; | |
8093 | |
8094 Local<Function> func = | |
8095 FunctionTemplate::New(ThrowingCallbackWithTryCatch)->GetFunction(); | |
8096 context->Global()->Set(v8_str("func"), func); | |
8097 | |
8098 MessageCallback callbacks[] = | |
8099 { NULL, WebKitLike, ThrowViaApi, ThrowFromJS, WithTryCatch }; | |
8100 for (unsigned i = 0; i < sizeof(callbacks)/sizeof(callbacks[0]); i++) { | |
8101 MessageCallback callback = callbacks[i]; | |
8102 if (callback != NULL) { | |
8103 V8::AddMessageListener(callback); | |
8104 } | |
8105 ExpectFalse( | |
8106 "var thrown = false;\n" | |
8107 "try { func(); } catch(e) { thrown = true; }\n" | |
8108 "thrown\n"); | |
8109 if (callback != NULL) { | |
8110 V8::RemoveMessageListeners(callback); | |
8111 } | |
8112 } | |
8113 } | |
8114 | |
8115 | |
7983 static v8::Handle<Value> ParentGetter(Local<String> name, | 8116 static v8::Handle<Value> ParentGetter(Local<String> name, |
7984 const AccessorInfo& info) { | 8117 const AccessorInfo& info) { |
7985 ApiTestFuzzer::Fuzz(); | 8118 ApiTestFuzzer::Fuzz(); |
7986 return v8_num(1); | 8119 return v8_num(1); |
7987 } | 8120 } |
7988 | 8121 |
7989 | 8122 |
7990 static v8::Handle<Value> ChildGetter(Local<String> name, | 8123 static v8::Handle<Value> ChildGetter(Local<String> name, |
7991 const AccessorInfo& info) { | 8124 const AccessorInfo& info) { |
7992 ApiTestFuzzer::Fuzz(); | 8125 ApiTestFuzzer::Fuzz(); |
(...skipping 4219 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
12212 v8::Context::Scope context_scope(context.local()); | 12345 v8::Context::Scope context_scope(context.local()); |
12213 | 12346 |
12214 v8::Handle<v8::ObjectTemplate> tmpl = v8::ObjectTemplate::New(); | 12347 v8::Handle<v8::ObjectTemplate> tmpl = v8::ObjectTemplate::New(); |
12215 tmpl->SetNamedPropertyHandler(Getter, NULL, NULL, NULL, Enumerator); | 12348 tmpl->SetNamedPropertyHandler(Getter, NULL, NULL, NULL, Enumerator); |
12216 context->Global()->Set(v8_str("o"), tmpl->NewInstance()); | 12349 context->Global()->Set(v8_str("o"), tmpl->NewInstance()); |
12217 v8::Handle<v8::Array> result = v8::Handle<v8::Array>::Cast(CompileRun( | 12350 v8::Handle<v8::Array> result = v8::Handle<v8::Array>::Cast(CompileRun( |
12218 "var result = []; for (var k in o) result.push(k); result")); | 12351 "var result = []; for (var k in o) result.push(k); result")); |
12219 CHECK_EQ(1, result->Length()); | 12352 CHECK_EQ(1, result->Length()); |
12220 CHECK_EQ(v8_str("universalAnswer"), result->Get(0)); | 12353 CHECK_EQ(v8_str("universalAnswer"), result->Get(0)); |
12221 } | 12354 } |
OLD | NEW |