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 1300 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1311 const char* code = "var str = 'oddle'; obj[str] + obj.poddle;"; | 1311 const char* code = "var str = 'oddle'; obj[str] + obj.poddle;"; |
1312 v8::Handle<Value> str = CompileRun(code); | 1312 v8::Handle<Value> str = CompileRun(code); |
1313 String::AsciiValue value(str); | 1313 String::AsciiValue value(str); |
1314 CHECK_EQ(*value, "oddlepoddle"); | 1314 CHECK_EQ(*value, "oddlepoddle"); |
1315 // Check default behavior | 1315 // Check default behavior |
1316 CHECK_EQ(v8_compile("obj.flob = 10;")->Run()->Int32Value(), 10); | 1316 CHECK_EQ(v8_compile("obj.flob = 10;")->Run()->Int32Value(), 10); |
1317 CHECK(v8_compile("'myProperty' in obj")->Run()->BooleanValue()); | 1317 CHECK(v8_compile("'myProperty' in obj")->Run()->BooleanValue()); |
1318 CHECK(v8_compile("delete obj.myProperty")->Run()->BooleanValue()); | 1318 CHECK(v8_compile("delete obj.myProperty")->Run()->BooleanValue()); |
1319 } | 1319 } |
1320 | 1320 |
| 1321 int do_nothing_call_count = 0; |
| 1322 int foo_setter_call_count = 0; |
| 1323 int bar_setter_call_count = 0; |
| 1324 |
| 1325 static v8::Handle<v8::Value> AccessorGetterFoo(Local<String> property, |
| 1326 const AccessorInfo& info) { |
| 1327 return v8::String::New("fooget"); |
| 1328 } |
| 1329 |
| 1330 static void AccessorSetterFoo(Local<String> property, |
| 1331 Local<Value> value, |
| 1332 const AccessorInfo& info) { |
| 1333 foo_setter_call_count++; |
| 1334 String::AsciiValue name(property); |
| 1335 CHECK_EQ(*name, "foo"); |
| 1336 return; |
| 1337 } |
| 1338 |
| 1339 static v8::Handle<v8::Value> AccessorGetterBar(Local<String> property, |
| 1340 const AccessorInfo& info) { |
| 1341 return v8::String::New("barget"); |
| 1342 } |
| 1343 |
| 1344 static void AccessorSetterBar(Local<String> property, |
| 1345 Local<Value> value, |
| 1346 const AccessorInfo& info) { |
| 1347 bar_setter_call_count++; |
| 1348 String::AsciiValue name(property); |
| 1349 CHECK_EQ(*name, "bar"); |
| 1350 return; |
| 1351 } |
| 1352 |
| 1353 static v8::Handle<Value> DoNothingGetterHandler(Local<String> name, |
| 1354 const AccessorInfo& info) { |
| 1355 do_nothing_call_count++; |
| 1356 return v8::Handle<Value>(); |
| 1357 } |
| 1358 |
| 1359 THREADED_TEST(NamedPropertyHandlerWalkingUpPrototypeChain) { |
| 1360 do_nothing_call_count = 0; |
| 1361 foo_setter_call_count = 0; |
| 1362 bar_setter_call_count = 0; |
| 1363 v8::HandleScope scope; |
| 1364 v8::Handle<v8::FunctionTemplate> grandparent = v8::FunctionTemplate::New(); |
| 1365 v8::Handle<v8::FunctionTemplate> parent = v8::FunctionTemplate::New(); |
| 1366 v8::Handle<v8::FunctionTemplate> child = v8::FunctionTemplate::New(); |
| 1367 parent->Inherit(grandparent); |
| 1368 child->Inherit(parent); |
| 1369 child->InstanceTemplate()->SetNamedPropertyHandler(DoNothingGetterHandler, |
| 1370 0, 0, 0, 0, |
| 1371 v8_str("data")); |
| 1372 grandparent->PrototypeTemplate()->SetAccessor(v8::String::New("foo"), |
| 1373 AccessorGetterFoo, |
| 1374 AccessorSetterFoo); |
| 1375 parent->PrototypeTemplate()->SetAccessor(v8::String::New("bar"), |
| 1376 AccessorGetterBar, |
| 1377 AccessorSetterBar); |
| 1378 |
| 1379 LocalContext env; |
| 1380 env->Global()->Set(v8_str("obj"), |
| 1381 child->GetFunction()->NewInstance()); |
| 1382 CHECK_EQ(do_nothing_call_count, 0); |
| 1383 { |
| 1384 v8::Handle<v8::Value> result = v8_compile("obj.foo")->Run(); |
| 1385 CHECK_EQ(do_nothing_call_count, 1); |
| 1386 String::AsciiValue Result(result); |
| 1387 CHECK_EQ(*Result, "fooget"); |
| 1388 } |
| 1389 { |
| 1390 v8::Handle<v8::Value> result = v8_compile("obj.bar")->Run(); |
| 1391 CHECK_EQ(do_nothing_call_count, 2); |
| 1392 String::AsciiValue Result(result); |
| 1393 CHECK_EQ(*Result, "barget"); |
| 1394 } |
| 1395 { |
| 1396 v8::Handle<Value> result = v8_compile("obj.foo = 1")->Run(); |
| 1397 CHECK_EQ(do_nothing_call_count, 2); // shoudn't change |
| 1398 CHECK_EQ(foo_setter_call_count, 1); |
| 1399 CHECK_EQ(result->Int32Value(), 1); |
| 1400 } |
| 1401 { |
| 1402 v8::Handle<Value> result = v8_compile("obj.bar = 2")->Run(); |
| 1403 CHECK_EQ(do_nothing_call_count, 2); // shoudn't change |
| 1404 CHECK_EQ(bar_setter_call_count, 1); |
| 1405 CHECK_EQ(result->Int32Value(), 2); |
| 1406 } |
| 1407 // The values should not be on the instance (for Accessors) |
| 1408 { |
| 1409 v8::Handle<v8::Value> result = v8_compile("obj.foo")->Run(); |
| 1410 CHECK_EQ(do_nothing_call_count, 3); |
| 1411 String::AsciiValue Result(result); |
| 1412 CHECK_EQ(*Result, "fooget"); |
| 1413 } |
| 1414 { |
| 1415 v8::Handle<v8::Value> result = v8_compile("obj.bar")->Run(); |
| 1416 CHECK_EQ(do_nothing_call_count, 4); |
| 1417 String::AsciiValue Result(result); |
| 1418 CHECK_EQ(*Result, "barget"); |
| 1419 } |
| 1420 } |
| 1421 |
| 1422 int catch_all_propety_call_count = 0; |
| 1423 |
| 1424 static v8::Handle<Value> CatchAllPropertyHandler(Local<String> name, |
| 1425 const AccessorInfo& info) { |
| 1426 catch_all_propety_call_count++; |
| 1427 CHECK_EQ(v8_str("SOMETHING"), info.Data()); |
| 1428 return v8::String::New("GOTCHA"); |
| 1429 } |
| 1430 |
| 1431 int baz_setter_call_count = 0; |
| 1432 |
| 1433 static v8::Handle<v8::Value> AccessorGetterBaz(Local<String> property, |
| 1434 const AccessorInfo& info) { |
| 1435 return v8::String::New("bazget"); |
| 1436 } |
| 1437 |
| 1438 static void AccessorSetterBaz(Local<String> property, |
| 1439 Local<Value> value, |
| 1440 const AccessorInfo& info) { |
| 1441 baz_setter_call_count++; |
| 1442 String::AsciiValue name(property); |
| 1443 CHECK_EQ(*name, "baz"); |
| 1444 return; |
| 1445 } |
| 1446 |
| 1447 THREADED_TEST(WalkingUpPrototypeChainWithNamedPropertyHandlerOnPrototype) { |
| 1448 do_nothing_call_count = 0; |
| 1449 catch_all_propety_call_count = 0; |
| 1450 foo_setter_call_count = 0; |
| 1451 bar_setter_call_count = 0; |
| 1452 baz_setter_call_count = 0; |
| 1453 v8::HandleScope scope; |
| 1454 v8::Handle<v8::FunctionTemplate> grandgrandparent = |
| 1455 v8::FunctionTemplate::New(); |
| 1456 v8::Handle<v8::FunctionTemplate> grandparent = v8::FunctionTemplate::New(); |
| 1457 v8::Handle<v8::FunctionTemplate> parent = v8::FunctionTemplate::New(); |
| 1458 v8::Handle<v8::FunctionTemplate> child = v8::FunctionTemplate::New(); |
| 1459 grandparent->Inherit(grandgrandparent); |
| 1460 parent->Inherit(grandparent); |
| 1461 child->Inherit(parent); |
| 1462 child->InstanceTemplate()->SetNamedPropertyHandler(DoNothingGetterHandler, |
| 1463 0, 0, 0, 0, |
| 1464 v8_str("data")); |
| 1465 grandparent->PrototypeTemplate()->SetNamedPropertyHandler(CatchAllPropertyHand
ler, |
| 1466 0, 0, 0, 0, |
| 1467 v8_str("SOMETHING")); |
| 1468 grandgrandparent->PrototypeTemplate()->SetAccessor(v8::String::New("baz"), |
| 1469 AccessorGetterBaz, |
| 1470 AccessorSetterBaz); |
| 1471 grandparent->PrototypeTemplate()->SetAccessor(v8::String::New("foo"), |
| 1472 AccessorGetterFoo, |
| 1473 AccessorSetterFoo); |
| 1474 parent->PrototypeTemplate()->SetAccessor(v8::String::New("bar"), |
| 1475 AccessorGetterBar, |
| 1476 AccessorSetterBar); |
| 1477 |
| 1478 LocalContext env; |
| 1479 env->Global()->Set(v8_str("obj"), |
| 1480 child->GetFunction()->NewInstance()); |
| 1481 CHECK_EQ(do_nothing_call_count, 0); |
| 1482 { |
| 1483 v8::Handle<v8::Value> result = v8_compile("obj.foo")->Run(); |
| 1484 CHECK_EQ(do_nothing_call_count, 1); |
| 1485 CHECK_EQ(catch_all_propety_call_count, 1); |
| 1486 String::AsciiValue Result(result); |
| 1487 CHECK_EQ(*Result, "GOTCHA"); |
| 1488 } |
| 1489 { |
| 1490 v8::Handle<v8::Value> result = v8_compile("obj.bar")->Run(); |
| 1491 CHECK_EQ(do_nothing_call_count, 2); |
| 1492 CHECK_EQ(catch_all_propety_call_count, 1); // shoudn't get called |
| 1493 String::AsciiValue Result(result); |
| 1494 CHECK_EQ(*Result, "barget"); |
| 1495 } |
| 1496 { |
| 1497 v8::Handle<v8::Value> result = v8_compile("obj.baz")->Run(); |
| 1498 CHECK_EQ(do_nothing_call_count, 3); |
| 1499 CHECK_EQ(catch_all_propety_call_count, 2); |
| 1500 String::AsciiValue Result(result); |
| 1501 CHECK_EQ(*Result, "GOTCHA"); |
| 1502 } |
| 1503 { |
| 1504 v8::Handle<v8::Value> result = v8_compile("obj.foo = 1")->Run(); |
| 1505 CHECK_EQ(do_nothing_call_count, 3); // shoudn't change |
| 1506 CHECK_EQ(catch_all_propety_call_count, 2); |
| 1507 CHECK_EQ(foo_setter_call_count, 1); |
| 1508 CHECK_EQ(result->Int32Value(), 1); |
| 1509 } |
| 1510 { |
| 1511 v8::Handle<v8::Value> result = v8_compile("obj.bar = 2")->Run(); |
| 1512 CHECK_EQ(do_nothing_call_count, 3); |
| 1513 CHECK_EQ(catch_all_propety_call_count, 2); |
| 1514 CHECK_EQ(bar_setter_call_count, 1); |
| 1515 CHECK_EQ(result->Int32Value(), 2); |
| 1516 } |
| 1517 { |
| 1518 v8::Handle<v8::Value> result = v8_compile("obj.baz = 3")->Run(); |
| 1519 CHECK_EQ(do_nothing_call_count, 3); |
| 1520 CHECK_EQ(catch_all_propety_call_count, 2); |
| 1521 CHECK_EQ(baz_setter_call_count, 1); |
| 1522 CHECK_EQ(result->Int32Value(), 3); |
| 1523 } |
| 1524 // The values should not be on the instance (for Accessors) |
| 1525 { |
| 1526 v8::Handle<v8::Value> result = v8_compile("obj.foo")->Run(); |
| 1527 CHECK_EQ(do_nothing_call_count, 4); |
| 1528 CHECK_EQ(catch_all_propety_call_count, 3); |
| 1529 String::AsciiValue Result(result); |
| 1530 CHECK_EQ(*Result, "GOTCHA"); |
| 1531 } |
| 1532 { |
| 1533 v8::Handle<v8::Value> result = v8_compile("obj.bar")->Run(); |
| 1534 CHECK_EQ(do_nothing_call_count, 5); |
| 1535 CHECK_EQ(catch_all_propety_call_count, 3); |
| 1536 String::AsciiValue Result(result); |
| 1537 CHECK_EQ(*Result, "barget"); |
| 1538 } |
| 1539 { |
| 1540 v8::Handle<v8::Value> result = v8_compile("obj.baz")->Run(); |
| 1541 CHECK_EQ(do_nothing_call_count, 6); |
| 1542 CHECK_EQ(catch_all_propety_call_count, 4); |
| 1543 String::AsciiValue Result(result); |
| 1544 CHECK_EQ(*Result, "GOTCHA"); |
| 1545 } |
| 1546 } |
| 1547 |
| 1548 THREADED_TEST(NamedPropertyHandlerPropertyNotOnPrototype) { |
| 1549 do_nothing_call_count = 0; |
| 1550 foo_setter_call_count = 0; |
| 1551 bar_setter_call_count = 0; |
| 1552 v8::HandleScope scope; |
| 1553 v8::Handle<v8::FunctionTemplate> grandparent = v8::FunctionTemplate::New(); |
| 1554 v8::Handle<v8::FunctionTemplate> parent = v8::FunctionTemplate::New(); |
| 1555 v8::Handle<v8::FunctionTemplate> child = v8::FunctionTemplate::New(); |
| 1556 parent->Inherit(grandparent); |
| 1557 child->Inherit(parent); |
| 1558 child->InstanceTemplate()->SetNamedPropertyHandler(DoNothingGetterHandler, |
| 1559 0, 0, 0, 0, |
| 1560 v8_str("data")); |
| 1561 grandparent->PrototypeTemplate()->SetAccessor(v8::String::New("foo"), |
| 1562 AccessorGetterFoo, |
| 1563 AccessorSetterFoo); |
| 1564 parent->PrototypeTemplate()->SetAccessor(v8::String::New("bar"), |
| 1565 AccessorGetterBar, |
| 1566 AccessorSetterBar); |
| 1567 |
| 1568 LocalContext env; |
| 1569 env->Global()->Set(v8_str("obj"), |
| 1570 child->GetFunction()->NewInstance()); |
| 1571 CHECK_EQ(do_nothing_call_count, 0); |
| 1572 { |
| 1573 v8::Handle<v8::Value> result = v8_compile("obj.a")->Run(); |
| 1574 CHECK_EQ(do_nothing_call_count, 1); |
| 1575 String::AsciiValue Result(result); |
| 1576 CHECK_EQ(*Result, "undefined"); |
| 1577 } |
| 1578 { |
| 1579 v8::Handle<v8::Value> result = v8_compile("obj.b")->Run(); |
| 1580 CHECK_EQ(do_nothing_call_count, 2); |
| 1581 String::AsciiValue Result(result); |
| 1582 CHECK_EQ(*Result, "undefined"); |
| 1583 } |
| 1584 { |
| 1585 v8::Handle<Value> result = v8_compile("obj.a = 1;")->Run(); |
| 1586 CHECK_EQ(do_nothing_call_count, 2); // shoudn't change |
| 1587 CHECK_EQ(result->Int32Value(), 1); |
| 1588 } |
| 1589 { |
| 1590 v8::Handle<Value> result = v8_compile("obj.b = 2;")->Run(); |
| 1591 CHECK_EQ(do_nothing_call_count, 2); // shoudn't change |
| 1592 CHECK_EQ(result->Int32Value(), 2); |
| 1593 } |
| 1594 // The values should be on the instance (not handled by Accessors) |
| 1595 { |
| 1596 v8::Handle<v8::Value> result = v8_compile("obj.a")->Run(); |
| 1597 CHECK_EQ(do_nothing_call_count, 3); |
| 1598 CHECK_EQ(result->Int32Value(), 1); |
| 1599 } |
| 1600 { |
| 1601 v8::Handle<v8::Value> result = v8_compile("obj.b")->Run(); |
| 1602 CHECK_EQ(do_nothing_call_count, 4); |
| 1603 CHECK_EQ(result->Int32Value(), 2); |
| 1604 } |
| 1605 } |
| 1606 |
1321 | 1607 |
1322 int echo_indexed_call_count = 0; | 1608 int echo_indexed_call_count = 0; |
1323 | 1609 |
1324 | 1610 |
1325 static v8::Handle<Value> EchoIndexedProperty(uint32_t index, | 1611 static v8::Handle<Value> EchoIndexedProperty(uint32_t index, |
1326 const AccessorInfo& info) { | 1612 const AccessorInfo& info) { |
1327 ApiTestFuzzer::Fuzz(); | 1613 ApiTestFuzzer::Fuzz(); |
1328 CHECK_EQ(v8_num(637), info.Data()); | 1614 CHECK_EQ(v8_num(637), info.Data()); |
1329 echo_indexed_call_count++; | 1615 echo_indexed_call_count++; |
1330 return v8_num(index); | 1616 return v8_num(index); |
1331 } | 1617 } |
1332 | 1618 |
1333 | 1619 |
| 1620 |
1334 THREADED_TEST(IndexedPropertyHandlerGetter) { | 1621 THREADED_TEST(IndexedPropertyHandlerGetter) { |
1335 v8::HandleScope scope; | 1622 v8::HandleScope scope; |
1336 v8::Handle<v8::FunctionTemplate> templ = v8::FunctionTemplate::New(); | 1623 v8::Handle<v8::FunctionTemplate> templ = v8::FunctionTemplate::New(); |
1337 templ->InstanceTemplate()->SetIndexedPropertyHandler(EchoIndexedProperty, | 1624 templ->InstanceTemplate()->SetIndexedPropertyHandler(EchoIndexedProperty, |
1338 0, 0, 0, 0, | 1625 0, 0, 0, 0, |
1339 v8_num(637)); | 1626 v8_num(637)); |
1340 LocalContext env; | 1627 LocalContext env; |
1341 env->Global()->Set(v8_str("obj"), | 1628 env->Global()->Set(v8_str("obj"), |
1342 templ->GetFunction()->NewInstance()); | 1629 templ->GetFunction()->NewInstance()); |
1343 Local<Script> script = v8_compile("obj[900]"); | 1630 Local<Script> script = v8_compile("obj[900]"); |
(...skipping 13552 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
14896 } | 15183 } |
14897 | 15184 |
14898 i::Isolate::Current()->heap()->CollectAllGarbage(true); | 15185 i::Isolate::Current()->heap()->CollectAllGarbage(true); |
14899 { i::Object* raw_map_cache = i::Isolate::Current()->context()->map_cache(); | 15186 { i::Object* raw_map_cache = i::Isolate::Current()->context()->map_cache(); |
14900 if (raw_map_cache != i::Isolate::Current()->heap()->undefined_value()) { | 15187 if (raw_map_cache != i::Isolate::Current()->heap()->undefined_value()) { |
14901 i::MapCache* map_cache = i::MapCache::cast(raw_map_cache); | 15188 i::MapCache* map_cache = i::MapCache::cast(raw_map_cache); |
14902 CHECK_GT(elements, map_cache->NumberOfElements()); | 15189 CHECK_GT(elements, map_cache->NumberOfElements()); |
14903 } | 15190 } |
14904 } | 15191 } |
14905 } | 15192 } |
OLD | NEW |