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 4825 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4836 | 4836 |
4837 // Getting the prototype of o0 should get the first visible one | 4837 // Getting the prototype of o0 should get the first visible one |
4838 // which is o3. Therefore, z should not be defined on the prototype | 4838 // which is o3. Therefore, z should not be defined on the prototype |
4839 // object. | 4839 // object. |
4840 Local<Value> proto = o0->Get(v8_str("__proto__")); | 4840 Local<Value> proto = o0->Get(v8_str("__proto__")); |
4841 CHECK(proto->IsObject()); | 4841 CHECK(proto->IsObject()); |
4842 CHECK(Local<v8::Object>::Cast(proto)->Get(v8_str("z"))->IsUndefined()); | 4842 CHECK(Local<v8::Object>::Cast(proto)->Get(v8_str("z"))->IsUndefined()); |
4843 } | 4843 } |
4844 | 4844 |
4845 | 4845 |
| 4846 THREADED_TEST(SetPrototype) { |
| 4847 v8::HandleScope handle_scope; |
| 4848 LocalContext context; |
| 4849 |
| 4850 Local<v8::FunctionTemplate> t0 = v8::FunctionTemplate::New(); |
| 4851 t0->InstanceTemplate()->Set(v8_str("x"), v8_num(0)); |
| 4852 Local<v8::FunctionTemplate> t1 = v8::FunctionTemplate::New(); |
| 4853 t1->SetHiddenPrototype(true); |
| 4854 t1->InstanceTemplate()->Set(v8_str("y"), v8_num(1)); |
| 4855 Local<v8::FunctionTemplate> t2 = v8::FunctionTemplate::New(); |
| 4856 t2->SetHiddenPrototype(true); |
| 4857 t2->InstanceTemplate()->Set(v8_str("z"), v8_num(2)); |
| 4858 Local<v8::FunctionTemplate> t3 = v8::FunctionTemplate::New(); |
| 4859 t3->InstanceTemplate()->Set(v8_str("u"), v8_num(3)); |
| 4860 |
| 4861 Local<v8::Object> o0 = t0->GetFunction()->NewInstance(); |
| 4862 Local<v8::Object> o1 = t1->GetFunction()->NewInstance(); |
| 4863 Local<v8::Object> o2 = t2->GetFunction()->NewInstance(); |
| 4864 Local<v8::Object> o3 = t3->GetFunction()->NewInstance(); |
| 4865 |
| 4866 // Setting the prototype on an object does not skip hidden prototypes. |
| 4867 CHECK_EQ(0, o0->Get(v8_str("x"))->Int32Value()); |
| 4868 CHECK(o0->SetPrototype(o1)); |
| 4869 CHECK_EQ(0, o0->Get(v8_str("x"))->Int32Value()); |
| 4870 CHECK_EQ(1, o0->Get(v8_str("y"))->Int32Value()); |
| 4871 CHECK(o1->SetPrototype(o2)); |
| 4872 CHECK_EQ(0, o0->Get(v8_str("x"))->Int32Value()); |
| 4873 CHECK_EQ(1, o0->Get(v8_str("y"))->Int32Value()); |
| 4874 CHECK_EQ(2, o0->Get(v8_str("z"))->Int32Value()); |
| 4875 CHECK(o2->SetPrototype(o3)); |
| 4876 CHECK_EQ(0, o0->Get(v8_str("x"))->Int32Value()); |
| 4877 CHECK_EQ(1, o0->Get(v8_str("y"))->Int32Value()); |
| 4878 CHECK_EQ(2, o0->Get(v8_str("z"))->Int32Value()); |
| 4879 CHECK_EQ(3, o0->Get(v8_str("u"))->Int32Value()); |
| 4880 |
| 4881 // Getting the prototype of o0 should get the first visible one |
| 4882 // which is o3. Therefore, z should not be defined on the prototype |
| 4883 // object. |
| 4884 Local<Value> proto = o0->Get(v8_str("__proto__")); |
| 4885 CHECK(proto->IsObject()); |
| 4886 CHECK_EQ(v8::Handle<v8::Object>::Cast(proto), o3); |
| 4887 |
| 4888 // However, Object::GetPrototype ignores hidden prototype. |
| 4889 Local<Value> proto0 = o0->GetPrototype(); |
| 4890 CHECK(proto0->IsObject()); |
| 4891 CHECK_EQ(v8::Handle<v8::Object>::Cast(proto0), o1); |
| 4892 |
| 4893 Local<Value> proto1 = o1->GetPrototype(); |
| 4894 CHECK(proto1->IsObject()); |
| 4895 CHECK_EQ(v8::Handle<v8::Object>::Cast(proto1), o2); |
| 4896 |
| 4897 Local<Value> proto2 = o2->GetPrototype(); |
| 4898 CHECK(proto2->IsObject()); |
| 4899 CHECK_EQ(v8::Handle<v8::Object>::Cast(proto2), o3); |
| 4900 } |
| 4901 |
| 4902 |
| 4903 THREADED_TEST(SetPrototypeThrows) { |
| 4904 v8::HandleScope handle_scope; |
| 4905 LocalContext context; |
| 4906 |
| 4907 Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New(); |
| 4908 |
| 4909 Local<v8::Object> o0 = t->GetFunction()->NewInstance(); |
| 4910 Local<v8::Object> o1 = t->GetFunction()->NewInstance(); |
| 4911 |
| 4912 CHECK(o0->SetPrototype(o1)); |
| 4913 // If setting the prototype leads to the cycle, SetPrototype should |
| 4914 // return false and keep VM in sane state. |
| 4915 v8::TryCatch try_catch; |
| 4916 CHECK(!o1->SetPrototype(o0)); |
| 4917 CHECK(!try_catch.HasCaught()); |
| 4918 ASSERT(!i::Top::has_pending_exception()); |
| 4919 |
| 4920 CHECK_EQ(42, CompileRun("function f() { return 42; }; f()")->Int32Value()); |
| 4921 } |
| 4922 |
| 4923 |
4846 THREADED_TEST(GetterSetterExceptions) { | 4924 THREADED_TEST(GetterSetterExceptions) { |
4847 v8::HandleScope handle_scope; | 4925 v8::HandleScope handle_scope; |
4848 LocalContext context; | 4926 LocalContext context; |
4849 CompileRun( | 4927 CompileRun( |
4850 "function Foo() { };" | 4928 "function Foo() { };" |
4851 "function Throw() { throw 5; };" | 4929 "function Throw() { throw 5; };" |
4852 "var x = { };" | 4930 "var x = { };" |
4853 "x.__defineSetter__('set', Throw);" | 4931 "x.__defineSetter__('set', Throw);" |
4854 "x.__defineGetter__('get', Throw);"); | 4932 "x.__defineGetter__('get', Throw);"); |
4855 Local<v8::Object> x = | 4933 Local<v8::Object> x = |
(...skipping 4351 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
9207 CompileRun(source_exception); | 9285 CompileRun(source_exception); |
9208 other_context->Exit(); | 9286 other_context->Exit(); |
9209 v8::internal::Heap::CollectAllGarbage(false); | 9287 v8::internal::Heap::CollectAllGarbage(false); |
9210 if (GetGlobalObjectsCount() == 1) break; | 9288 if (GetGlobalObjectsCount() == 1) break; |
9211 } | 9289 } |
9212 CHECK_GE(2, gc_count); | 9290 CHECK_GE(2, gc_count); |
9213 CHECK_EQ(1, GetGlobalObjectsCount()); | 9291 CHECK_EQ(1, GetGlobalObjectsCount()); |
9214 | 9292 |
9215 other_context.Dispose(); | 9293 other_context.Dispose(); |
9216 } | 9294 } |
OLD | NEW |