Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1)

Side by Side Diff: src/builtins/builtins-promise.cc

Issue 2695753002: [ESnext] Implement Promise.prototype.finally (Closed)
Patch Set: add tests Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/builtins/builtins-promise.h ('k') | src/contexts.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2016 the V8 project authors. All rights reserved. 1 // Copyright 2016 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/builtins/builtins-promise.h" 5 #include "src/builtins/builtins-promise.h"
6 #include "src/builtins/builtins-constructor.h" 6 #include "src/builtins/builtins-constructor.h"
7 #include "src/builtins/builtins-utils.h" 7 #include "src/builtins/builtins-utils.h"
8 #include "src/builtins/builtins.h" 8 #include "src/builtins/builtins.h"
9 #include "src/code-factory.h" 9 #include "src/code-factory.h"
10 #include "src/code-stub-assembler.h" 10 #include "src/code-stub-assembler.h"
(...skipping 420 matching lines...) Expand 10 before | Expand all | Expand 10 after
431 append_callbacks(this); 431 append_callbacks(this);
432 GotoIf(TaggedIsSmi(on_resolve), &if_onresolvenotcallable); 432 GotoIf(TaggedIsSmi(on_resolve), &if_onresolvenotcallable);
433 433
434 Isolate* isolate = this->isolate(); 434 Isolate* isolate = this->isolate();
435 Node* const on_resolve_map = LoadMap(on_resolve); 435 Node* const on_resolve_map = LoadMap(on_resolve);
436 Branch(IsCallableMap(on_resolve_map), &onrejectcheck, 436 Branch(IsCallableMap(on_resolve_map), &onrejectcheck,
437 &if_onresolvenotcallable); 437 &if_onresolvenotcallable);
438 438
439 Bind(&if_onresolvenotcallable); 439 Bind(&if_onresolvenotcallable);
440 { 440 {
441 Isolate* isolate = this->isolate();
442 Node* const default_resolve_handler_symbol = HeapConstant( 441 Node* const default_resolve_handler_symbol = HeapConstant(
443 isolate->factory()->promise_default_resolve_handler_symbol()); 442 isolate->factory()->promise_default_resolve_handler_symbol());
444 var_on_resolve.Bind(default_resolve_handler_symbol); 443 var_on_resolve.Bind(default_resolve_handler_symbol);
445 Goto(&onrejectcheck); 444 Goto(&onrejectcheck);
446 } 445 }
447 446
448 Bind(&onrejectcheck); 447 Bind(&onrejectcheck);
449 { 448 {
450 Label if_onrejectnotcallable(this); 449 Label if_onrejectnotcallable(this);
451 GotoIf(TaggedIsSmi(on_reject), &if_onrejectnotcallable); 450 GotoIf(TaggedIsSmi(on_reject), &if_onrejectnotcallable);
(...skipping 1105 matching lines...) Expand 10 before | Expand all | Expand 10 after
1557 TF_BUILTIN(InternalPromiseReject, PromiseBuiltinsAssembler) { 1556 TF_BUILTIN(InternalPromiseReject, PromiseBuiltinsAssembler) {
1558 Node* const promise = Parameter(1); 1557 Node* const promise = Parameter(1);
1559 Node* const reason = Parameter(2); 1558 Node* const reason = Parameter(2);
1560 Node* const debug_event = Parameter(3); 1559 Node* const debug_event = Parameter(3);
1561 Node* const context = Parameter(6); 1560 Node* const context = Parameter(6);
1562 1561
1563 InternalPromiseReject(context, promise, reason, debug_event); 1562 InternalPromiseReject(context, promise, reason, debug_event);
1564 Return(UndefinedConstant()); 1563 Return(UndefinedConstant());
1565 } 1564 }
1566 1565
1566 Node* PromiseBuiltinsAssembler::CreatePromiseFinallyContext(
1567 Node* on_finally, Node* native_context) {
1568 Node* const context =
1569 CreatePromiseContext(native_context, kOnFinallyContextLength);
1570 StoreContextElementNoWriteBarrier(context, kOnFinallySlot, on_finally);
1571 return context;
1572 }
1573
1574 std::pair<Node*, Node*> PromiseBuiltinsAssembler::CreatePromiseFinallyFunctions(
1575 Node* on_finally, Node* native_context) {
1576 Node* const promise_context =
1577 CreatePromiseFinallyContext(on_finally, native_context);
1578 Node* const map = LoadContextElement(
1579 native_context, Context::STRICT_FUNCTION_WITHOUT_PROTOTYPE_MAP_INDEX);
1580 Node* const then_finally_info = LoadContextElement(
1581 native_context, Context::PROMISE_THEN_FINALLY_SHARED_FUN);
1582 Node* const then_finally = AllocateFunctionWithMapAndContext(
1583 map, then_finally_info, promise_context);
1584 Node* const catch_finally_info = LoadContextElement(
1585 native_context, Context::PROMISE_CATCH_FINALLY_SHARED_FUN);
1586 Node* const catch_finally = AllocateFunctionWithMapAndContext(
1587 map, catch_finally_info, promise_context);
1588 return std::make_pair(then_finally, catch_finally);
1589 }
1590
1591 TF_BUILTIN(PromiseValueThunkFinally, PromiseBuiltinsAssembler) {
1592 Node* const context = Parameter(3);
1593
1594 Node* const value = LoadContextElement(context, kOnFinallySlot);
1595 Return(value);
1596 }
1597
1598 Node* PromiseBuiltinsAssembler::CreateValueThunkFunctionContext(
1599 Node* value, Node* native_context) {
1600 Node* const context =
1601 CreatePromiseContext(native_context, kOnFinallyContextLength);
1602 StoreContextElementNoWriteBarrier(context, kOnFinallySlot, value);
1603 return context;
1604 }
1605
1606 Node* PromiseBuiltinsAssembler::CreateValueThunkFunction(Node* value,
1607 Node* native_context) {
1608 Node* const value_thunk_context =
1609 CreateValueThunkFunctionContext(value, native_context);
1610 Node* const map = LoadContextElement(
1611 native_context, Context::STRICT_FUNCTION_WITHOUT_PROTOTYPE_MAP_INDEX);
1612 Node* const value_thunk_info = LoadContextElement(
1613 native_context, Context::PROMISE_VALUE_THUNK_FINALLY_SHARED_FUN);
1614 Node* const value_thunk = AllocateFunctionWithMapAndContext(
1615 map, value_thunk_info, value_thunk_context);
1616 return value_thunk;
1617 }
1618
1619 TF_BUILTIN(PromiseThenFinally, PromiseBuiltinsAssembler) {
1620 CSA_ASSERT_JS_ARGC_EQ(this, 1);
1621
1622 Node* const value = Parameter(1);
1623 Node* const context = Parameter(4);
1624
1625 Node* const on_finally = LoadContextElement(context, kOnFinallySlot);
1626
1627 // 2.a Let result be ? Call(onFinally, undefined).
1628 Callable call_callable = CodeFactory::Call(isolate());
1629 Node* result =
1630 CallJS(call_callable, context, on_finally, UndefinedConstant());
1631
1632 // 2.b Let promise be ! PromiseResolve( %Promise%, result).
1633 Node* const promise = AllocateAndInitJSPromise(context);
1634 InternalResolvePromise(context, promise, result);
1635
1636 // 2.c Let valueThunk be equivalent to a function that returns value.
1637 Node* native_context = LoadNativeContext(context);
1638 Node* const value_thunk = CreateValueThunkFunction(value, native_context);
1639
1640 // 2.d Let promiseCapability be ! NewPromiseCapability( %Promise%).
1641 Node* const promise_capability = AllocateAndInitJSPromise(context, promise);
1642
1643 // 2.e Return PerformPromiseThen(promise, valueThunk, undefined,
1644 // promiseCapability).
1645 InternalPerformPromiseThen(context, promise, value_thunk, UndefinedConstant(),
1646 promise_capability, UndefinedConstant(),
1647 UndefinedConstant());
1648 Return(promise_capability);
1649 }
1650
1651 TF_BUILTIN(PromiseThrowerFinally, PromiseBuiltinsAssembler) {
1652 Node* const context = Parameter(3);
1653
1654 Node* const reason = LoadContextElement(context, kOnFinallySlot);
1655 CallRuntime(Runtime::kThrow, context, reason);
1656 Return(UndefinedConstant());
1657 }
1658
1659 Node* PromiseBuiltinsAssembler::CreateThrowerFunctionContext(
1660 Node* reason, Node* native_context) {
1661 Node* const context =
1662 CreatePromiseContext(native_context, kOnFinallyContextLength);
1663 StoreContextElementNoWriteBarrier(context, kOnFinallySlot, reason);
1664 return context;
1665 }
1666
1667 Node* PromiseBuiltinsAssembler::CreateThrowerFunction(Node* reason,
1668 Node* native_context) {
1669 Node* const thrower_context =
1670 CreateThrowerFunctionContext(reason, native_context);
1671 Node* const map = LoadContextElement(
1672 native_context, Context::STRICT_FUNCTION_WITHOUT_PROTOTYPE_MAP_INDEX);
1673 Node* const thrower_info = LoadContextElement(
1674 native_context, Context::PROMISE_THROWER_FINALLY_SHARED_FUN);
1675 Node* const thrower =
1676 AllocateFunctionWithMapAndContext(map, thrower_info, thrower_context);
1677 return thrower;
1678 }
1679
1680 TF_BUILTIN(PromiseCatchFinally, PromiseBuiltinsAssembler) {
1681 CSA_ASSERT_JS_ARGC_EQ(this, 1);
1682
1683 Node* const reason = Parameter(1);
1684 Node* const context = Parameter(4);
1685
1686 Node* const on_finally = LoadContextElement(context, kOnFinallySlot);
1687
1688 // 2.a Let result be ? Call(onFinally, undefined).
1689 Callable call_callable = CodeFactory::Call(isolate());
1690 Node* result =
1691 CallJS(call_callable, context, on_finally, UndefinedConstant());
1692
1693 // 2.b Let promise be ! PromiseResolve( %Promise%, result).
1694 Node* const promise = AllocateAndInitJSPromise(context);
1695 InternalResolvePromise(context, promise, result);
1696
1697 // 2.c Let thrower be equivalent to a function that throws reason.
1698 Node* native_context = LoadNativeContext(context);
1699 Node* const thrower = CreateThrowerFunction(reason, native_context);
1700
1701 // 2.d Let promiseCapability be ! NewPromiseCapability( %Promise%).
1702 Node* const promise_capability = AllocateAndInitJSPromise(context, promise);
1703
1704 // 2.e Return PerformPromiseThen(promise, thrower, undefined,
1705 // promiseCapability).
1706 InternalPerformPromiseThen(context, promise, thrower, UndefinedConstant(),
1707 promise_capability, UndefinedConstant(),
1708 UndefinedConstant());
1709 Return(promise_capability);
1710 }
1711
1712 TF_BUILTIN(PromiseFinally, PromiseBuiltinsAssembler) {
1713 CSA_ASSERT_JS_ARGC_EQ(this, 1);
1714
1715 // 1. Let promise be the this value.
1716 Node* const promise = Parameter(0);
1717 Node* const on_finally = Parameter(1);
1718 Node* const context = Parameter(4);
1719
1720 // 2. If IsPromise(promise) is false, throw a TypeError exception.
1721 ThrowIfNotInstanceType(context, promise, JS_PROMISE_TYPE,
1722 "Promise.prototype.finally");
1723
1724 Variable var_then_finally(this, MachineRepresentation::kTagged),
1725 var_catch_finally(this, MachineRepresentation::kTagged);
1726
1727 Label if_notcallable(this, Label::kDeferred), perform_finally(this);
1728
1729 // 3. Let thenFinally be ! CreateThenFinally(onFinally).
1730 // 4. Let catchFinally be ! CreateCatchFinally(onFinally).
1731 GotoIf(TaggedIsSmi(on_finally), &if_notcallable);
1732 Node* const on_finally_map = LoadMap(on_finally);
1733 GotoUnless(IsCallableMap(on_finally_map), &if_notcallable);
1734
1735 Node* const native_context = LoadNativeContext(context);
1736 Node* then_finally = nullptr;
1737 Node* catch_finally = nullptr;
1738 std::tie(then_finally, catch_finally) =
1739 CreatePromiseFinallyFunctions(on_finally, native_context);
1740 var_then_finally.Bind(then_finally);
1741 var_catch_finally.Bind(catch_finally);
1742 Goto(&perform_finally);
1743
1744 Bind(&if_notcallable);
1745 {
1746 var_then_finally.Bind(on_finally);
1747 var_catch_finally.Bind(on_finally);
1748 Goto(&perform_finally);
1749 }
1750
1751 Bind(&perform_finally);
1752 Label if_nativepromise(this), if_custompromise(this, Label::kDeferred);
1753 BranchIfFastPath(context, promise, &if_nativepromise, &if_custompromise);
1754
1755 // 5. Return PerformPromiseThen(promise, valueThunk, undefined,
1756 // promiseCapability).
neis 2017/02/17 10:22:54 Sorry, actually should appear above the BranchIfFa
gsathya 2017/02/17 11:26:16 Done.
1757 Bind(&if_nativepromise);
1758 {
1759 Node* deferred_promise = AllocateAndInitJSPromise(context, promise);
1760 InternalPerformPromiseThen(context, promise, var_then_finally.value(),
1761 var_catch_finally.value(), deferred_promise,
1762 UndefinedConstant(), UndefinedConstant());
1763 Return(deferred_promise);
1764 }
1765
1766 Bind(&if_custompromise);
1767 {
1768 Isolate* isolate = this->isolate();
1769 Node* const then_str = HeapConstant(isolate->factory()->then_string());
1770 Callable getproperty_callable = CodeFactory::GetProperty(isolate);
1771 Node* const then =
1772 CallStub(getproperty_callable, context, promise, then_str);
1773 Callable call_callable = CodeFactory::Call(isolate);
1774 // 5. Return ? Invoke(promise, "then", « thenFinally, catchFinally »).
1775 Node* const result =
1776 CallJS(call_callable, context, then, promise, var_then_finally.value(),
1777 var_catch_finally.value());
1778 Return(result);
1779 }
1780 }
1781
1567 } // namespace internal 1782 } // namespace internal
1568 } // namespace v8 1783 } // namespace v8
OLDNEW
« no previous file with comments | « src/builtins/builtins-promise.h ('k') | src/contexts.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698