OLD | NEW |
1 // Copyright 2007-2008 the V8 project authors. All rights reserved. | 1 // Copyright 2007-2008 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 1237 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1248 Local<v8::ObjectTemplate> instance_templ = templ->InstanceTemplate(); | 1248 Local<v8::ObjectTemplate> instance_templ = templ->InstanceTemplate(); |
1249 instance_templ->SetInternalFieldCount(1); | 1249 instance_templ->SetInternalFieldCount(1); |
1250 Local<v8::Object> obj = templ->GetFunction()->NewInstance(); | 1250 Local<v8::Object> obj = templ->GetFunction()->NewInstance(); |
1251 CHECK_EQ(1, obj->InternalFieldCount()); | 1251 CHECK_EQ(1, obj->InternalFieldCount()); |
1252 CHECK(obj->GetInternalField(0)->IsUndefined()); | 1252 CHECK(obj->GetInternalField(0)->IsUndefined()); |
1253 obj->SetInternalField(0, v8_num(17)); | 1253 obj->SetInternalField(0, v8_num(17)); |
1254 CHECK_EQ(17, obj->GetInternalField(0)->Int32Value()); | 1254 CHECK_EQ(17, obj->GetInternalField(0)->Int32Value()); |
1255 } | 1255 } |
1256 | 1256 |
1257 | 1257 |
| 1258 THREADED_TEST(IdentityHash) { |
| 1259 v8::HandleScope scope; |
| 1260 LocalContext env; |
| 1261 |
| 1262 // Ensure that the test starts with an fresh heap to test whether the hash |
| 1263 // code is based on the address. |
| 1264 i::Heap::CollectAllGarbage(); |
| 1265 Local<v8::Object> obj = v8::Object::New(); |
| 1266 int hash = obj->GetIdentityHash(); |
| 1267 int hash1 = obj->GetIdentityHash(); |
| 1268 CHECK_EQ(hash, hash1); |
| 1269 int hash2 = v8::Object::New()->GetIdentityHash(); |
| 1270 // Since the identity hash is essentially a random number two consecutive |
| 1271 // objects should not be assigned the same hash code. If the test below fails |
| 1272 // the random number generator should be evaluated. |
| 1273 CHECK_NE(hash, hash2); |
| 1274 i::Heap::CollectAllGarbage(); |
| 1275 int hash3 = v8::Object::New()->GetIdentityHash(); |
| 1276 // Make sure that the identity hash is not based on the initial address of |
| 1277 // the object alone. If the test below fails the random number generator |
| 1278 // should be evaluated. |
| 1279 CHECK_NE(hash, hash3); |
| 1280 int hash4 = obj->GetIdentityHash(); |
| 1281 CHECK_EQ(hash, hash4); |
| 1282 } |
| 1283 |
| 1284 |
| 1285 THREADED_TEST(HiddenProperties) { |
| 1286 v8::HandleScope scope; |
| 1287 LocalContext env; |
| 1288 |
| 1289 v8::Local<v8::Object> obj = v8::Object::New(); |
| 1290 v8::Local<v8::String> key = v8_str("api-test::hidden-key"); |
| 1291 v8::Local<v8::String> empty = v8_str(""); |
| 1292 v8::Local<v8::String> prop_name = v8_str("prop_name"); |
| 1293 |
| 1294 i::Heap::CollectAllGarbage(); |
| 1295 |
| 1296 CHECK(obj->SetHiddenValue(key, v8::Integer::New(1503))); |
| 1297 CHECK_EQ(1503, obj->GetHiddenValue(key)->Int32Value()); |
| 1298 CHECK(obj->SetHiddenValue(key, v8::Integer::New(2002))); |
| 1299 CHECK_EQ(2002, obj->GetHiddenValue(key)->Int32Value()); |
| 1300 |
| 1301 i::Heap::CollectAllGarbage(); |
| 1302 |
| 1303 // Make sure we do not find the hidden property. |
| 1304 CHECK(!obj->Has(empty)); |
| 1305 CHECK_EQ(2002, obj->GetHiddenValue(key)->Int32Value()); |
| 1306 CHECK(obj->Get(empty)->IsUndefined()); |
| 1307 CHECK_EQ(2002, obj->GetHiddenValue(key)->Int32Value()); |
| 1308 CHECK(obj->Set(empty, v8::Integer::New(2003))); |
| 1309 CHECK_EQ(2002, obj->GetHiddenValue(key)->Int32Value()); |
| 1310 CHECK_EQ(2003, obj->Get(empty)->Int32Value()); |
| 1311 |
| 1312 i::Heap::CollectAllGarbage(); |
| 1313 |
| 1314 // Add another property and delete it afterwards to force the object in |
| 1315 // slow case. |
| 1316 CHECK(obj->Set(prop_name, v8::Integer::New(2008))); |
| 1317 CHECK_EQ(2002, obj->GetHiddenValue(key)->Int32Value()); |
| 1318 CHECK_EQ(2008, obj->Get(prop_name)->Int32Value()); |
| 1319 CHECK_EQ(2002, obj->GetHiddenValue(key)->Int32Value()); |
| 1320 CHECK(obj->Delete(prop_name)); |
| 1321 CHECK_EQ(2002, obj->GetHiddenValue(key)->Int32Value()); |
| 1322 |
| 1323 i::Heap::CollectAllGarbage(); |
| 1324 |
| 1325 CHECK(obj->DeleteHiddenValue(key)); |
| 1326 CHECK(obj->GetHiddenValue(key).IsEmpty()); |
| 1327 } |
| 1328 |
| 1329 |
1258 THREADED_TEST(External) { | 1330 THREADED_TEST(External) { |
1259 v8::HandleScope scope; | 1331 v8::HandleScope scope; |
1260 int x = 3; | 1332 int x = 3; |
1261 Local<v8::External> ext = v8::External::New(&x); | 1333 Local<v8::External> ext = v8::External::New(&x); |
1262 LocalContext env; | 1334 LocalContext env; |
1263 env->Global()->Set(v8_str("ext"), ext); | 1335 env->Global()->Set(v8_str("ext"), ext); |
1264 Local<Value> reext_obj = Script::Compile(v8_str("this.ext"))->Run(); | 1336 Local<Value> reext_obj = Script::Compile(v8_str("this.ext"))->Run(); |
1265 v8::Handle<v8::External> reext = v8::Handle<v8::External>::Cast(reext_obj); | 1337 v8::Handle<v8::External> reext = v8::Handle<v8::External>::Cast(reext_obj); |
1266 int* ptr = static_cast<int*>(reext->Value()); | 1338 int* ptr = static_cast<int*>(reext->Value()); |
1267 CHECK_EQ(x, 3); | 1339 CHECK_EQ(x, 3); |
(...skipping 4563 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
5831 CHECK_EQ(v8_str("hello"), clone->Get(v8_str("alpha"))); | 5903 CHECK_EQ(v8_str("hello"), clone->Get(v8_str("alpha"))); |
5832 CHECK_EQ(v8::Integer::New(123), clone->Get(v8_str("beta"))); | 5904 CHECK_EQ(v8::Integer::New(123), clone->Get(v8_str("beta"))); |
5833 CHECK_EQ(v8_str("cloneme"), clone->Get(v8_str("gamma"))); | 5905 CHECK_EQ(v8_str("cloneme"), clone->Get(v8_str("gamma"))); |
5834 | 5906 |
5835 // Set a property on the clone, verify each object. | 5907 // Set a property on the clone, verify each object. |
5836 clone->Set(v8_str("beta"), v8::Integer::New(456)); | 5908 clone->Set(v8_str("beta"), v8::Integer::New(456)); |
5837 CHECK_EQ(v8::Integer::New(123), obj->Get(v8_str("beta"))); | 5909 CHECK_EQ(v8::Integer::New(123), obj->Get(v8_str("beta"))); |
5838 CHECK_EQ(v8::Integer::New(456), clone->Get(v8_str("beta"))); | 5910 CHECK_EQ(v8::Integer::New(456), clone->Get(v8_str("beta"))); |
5839 } | 5911 } |
5840 | 5912 |
OLD | NEW |