OLD | NEW |
1 // Copyright 2014 the V8 project authors. All rights reserved. | 1 // Copyright 2014 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "src/runtime/runtime-utils.h" | 5 #include "src/runtime/runtime-utils.h" |
6 | 6 |
7 #include "src/arguments.h" | 7 #include "src/arguments.h" |
8 #include "src/conversions-inl.h" | 8 #include "src/conversions-inl.h" |
9 #include "src/isolate-inl.h" | 9 #include "src/isolate-inl.h" |
10 #include "src/messages.h" | 10 #include "src/messages.h" |
(...skipping 1482 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1493 DCHECK(args.length() == 3); | 1493 DCHECK(args.length() == 3); |
1494 | 1494 |
1495 CONVERT_ARG_HANDLE_CHECKED(JSReceiver, recv, 0); | 1495 CONVERT_ARG_HANDLE_CHECKED(JSReceiver, recv, 0); |
1496 CONVERT_ARG_HANDLE_CHECKED(String, string, 1); | 1496 CONVERT_ARG_HANDLE_CHECKED(String, string, 1); |
1497 Handle<Object> replace_obj = args.at<Object>(2); | 1497 Handle<Object> replace_obj = args.at<Object>(2); |
1498 | 1498 |
1499 Factory* factory = isolate->factory(); | 1499 Factory* factory = isolate->factory(); |
1500 | 1500 |
1501 string = String::Flatten(string); | 1501 string = String::Flatten(string); |
1502 | 1502 |
| 1503 // Fast-path for unmodified JSRegExps. |
| 1504 if (RegExpUtils::IsUnmodifiedRegExp(isolate, recv)) { |
| 1505 RETURN_RESULT_OR_FAILURE( |
| 1506 isolate, RegExpReplace(isolate, Handle<JSRegExp>::cast(recv), string, |
| 1507 replace_obj)); |
| 1508 } |
| 1509 |
1503 const int length = string->length(); | 1510 const int length = string->length(); |
1504 const bool functional_replace = replace_obj->IsCallable(); | 1511 const bool functional_replace = replace_obj->IsCallable(); |
1505 | 1512 |
1506 Handle<String> replace; | 1513 Handle<String> replace; |
1507 if (!functional_replace) { | 1514 if (!functional_replace) { |
1508 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, replace, | 1515 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, replace, |
1509 Object::ToString(isolate, replace_obj)); | 1516 Object::ToString(isolate, replace_obj)); |
1510 } | 1517 } |
1511 | 1518 |
1512 Handle<Object> global_obj; | 1519 Handle<Object> global_obj; |
1513 ASSIGN_RETURN_FAILURE_ON_EXCEPTION( | 1520 ASSIGN_RETURN_FAILURE_ON_EXCEPTION( |
1514 isolate, global_obj, | 1521 isolate, global_obj, |
1515 JSReceiver::GetProperty(recv, factory->global_string())); | 1522 JSReceiver::GetProperty(recv, factory->global_string())); |
1516 const bool global = global_obj->BooleanValue(); | 1523 const bool global = global_obj->BooleanValue(); |
1517 | 1524 |
1518 bool unicode = false; | 1525 bool unicode = false; |
1519 if (global) { | 1526 if (global) { |
1520 Handle<Object> unicode_obj; | 1527 Handle<Object> unicode_obj; |
1521 ASSIGN_RETURN_FAILURE_ON_EXCEPTION( | 1528 ASSIGN_RETURN_FAILURE_ON_EXCEPTION( |
1522 isolate, unicode_obj, | 1529 isolate, unicode_obj, |
1523 JSReceiver::GetProperty(recv, factory->unicode_string())); | 1530 JSReceiver::GetProperty(recv, factory->unicode_string())); |
1524 unicode = unicode_obj->BooleanValue(); | 1531 unicode = unicode_obj->BooleanValue(); |
1525 | 1532 |
1526 RETURN_FAILURE_ON_EXCEPTION(isolate, | 1533 RETURN_FAILURE_ON_EXCEPTION(isolate, |
1527 RegExpUtils::SetLastIndex(isolate, recv, 0)); | 1534 RegExpUtils::SetLastIndex(isolate, recv, 0)); |
1528 } | 1535 } |
1529 | 1536 |
1530 // TODO(jgruber): Here and at all other fast path checks, rely on map checks | |
1531 // instead. | |
1532 // TODO(jgruber): We could speed up the fast path by checking flags | |
1533 // afterwards, but that would violate the spec (which states that exec is | |
1534 // accessed after global and unicode). | |
1535 // TODO(adamk): this fast path is wrong as we doesn't ensure that 'exec' | |
1536 // is actually a data property on RegExp.prototype. | |
1537 Handle<Object> exec = factory->undefined_value(); | |
1538 if (recv->IsJSRegExp()) { | |
1539 ASSIGN_RETURN_FAILURE_ON_EXCEPTION( | |
1540 isolate, exec, JSObject::GetProperty( | |
1541 recv, factory->NewStringFromAsciiChecked("exec"))); | |
1542 if (RegExpUtils::IsBuiltinExec(exec)) { | |
1543 RETURN_RESULT_OR_FAILURE( | |
1544 isolate, RegExpReplace(isolate, Handle<JSRegExp>::cast(recv), string, | |
1545 replace_obj)); | |
1546 } | |
1547 } | |
1548 | |
1549 Zone zone(isolate->allocator(), ZONE_NAME); | 1537 Zone zone(isolate->allocator(), ZONE_NAME); |
1550 ZoneVector<Handle<Object>> results(&zone); | 1538 ZoneVector<Handle<Object>> results(&zone); |
1551 | 1539 |
1552 while (true) { | 1540 while (true) { |
1553 Handle<Object> result; | 1541 Handle<Object> result; |
1554 ASSIGN_RETURN_FAILURE_ON_EXCEPTION( | 1542 ASSIGN_RETURN_FAILURE_ON_EXCEPTION( |
1555 isolate, result, RegExpUtils::RegExpExec(isolate, recv, string, exec)); | 1543 isolate, result, RegExpUtils::RegExpExec(isolate, recv, string, |
1556 | 1544 factory->undefined_value())); |
1557 // Ensure exec will be read again on the next loop through. | |
1558 exec = factory->undefined_value(); | |
1559 | 1545 |
1560 if (result->IsNull(isolate)) break; | 1546 if (result->IsNull(isolate)) break; |
1561 | 1547 |
1562 results.push_back(result); | 1548 results.push_back(result); |
1563 if (!global) break; | 1549 if (!global) break; |
1564 | 1550 |
1565 Handle<Object> match_obj; | 1551 Handle<Object> match_obj; |
1566 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, match_obj, | 1552 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, match_obj, |
1567 Object::GetElement(isolate, result, 0)); | 1553 Object::GetElement(isolate, result, 0)); |
1568 | 1554 |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1681 | 1667 |
1682 RUNTIME_FUNCTION(Runtime_IsRegExp) { | 1668 RUNTIME_FUNCTION(Runtime_IsRegExp) { |
1683 SealHandleScope shs(isolate); | 1669 SealHandleScope shs(isolate); |
1684 DCHECK(args.length() == 1); | 1670 DCHECK(args.length() == 1); |
1685 CONVERT_ARG_CHECKED(Object, obj, 0); | 1671 CONVERT_ARG_CHECKED(Object, obj, 0); |
1686 return isolate->heap()->ToBoolean(obj->IsJSRegExp()); | 1672 return isolate->heap()->ToBoolean(obj->IsJSRegExp()); |
1687 } | 1673 } |
1688 | 1674 |
1689 } // namespace internal | 1675 } // namespace internal |
1690 } // namespace v8 | 1676 } // namespace v8 |
OLD | NEW |