OLD | NEW |
1 // Copyright 2015 the V8 project authors. All rights reserved. | 1 // Copyright 2015 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/base/utils/random-number-generator.h" | 5 #include "src/base/utils/random-number-generator.h" |
6 #include "src/builtins/builtins-promise.h" | 6 #include "src/builtins/builtins-promise.h" |
7 #include "src/code-factory.h" | 7 #include "src/code-factory.h" |
8 #include "src/code-stub-assembler.h" | 8 #include "src/code-stub-assembler.h" |
9 #include "src/compiler/node.h" | 9 #include "src/compiler/node.h" |
10 #include "src/isolate.h" | 10 #include "src/isolate.h" |
(...skipping 1805 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1816 | 1816 |
1817 isolate->SetPromiseHook(CustomPromiseHook); | 1817 isolate->SetPromiseHook(CustomPromiseHook); |
1818 result = ft.Call(isolate->factory()->undefined_value()).ToHandleChecked(); | 1818 result = ft.Call(isolate->factory()->undefined_value()).ToHandleChecked(); |
1819 CHECK_EQ(isolate->heap()->true_value(), *result); | 1819 CHECK_EQ(isolate->heap()->true_value(), *result); |
1820 | 1820 |
1821 isolate->SetPromiseHook(nullptr); | 1821 isolate->SetPromiseHook(nullptr); |
1822 result = ft.Call(isolate->factory()->undefined_value()).ToHandleChecked(); | 1822 result = ft.Call(isolate->factory()->undefined_value()).ToHandleChecked(); |
1823 CHECK_EQ(isolate->heap()->false_value(), *result); | 1823 CHECK_EQ(isolate->heap()->false_value(), *result); |
1824 } | 1824 } |
1825 | 1825 |
1826 TEST(AllocateJSPromise) { | 1826 TEST(AllocateAndInitJSPromise) { |
1827 Isolate* isolate(CcTest::InitIsolateOnce()); | 1827 Isolate* isolate(CcTest::InitIsolateOnce()); |
1828 | 1828 |
1829 const int kNumParams = 1; | 1829 const int kNumParams = 1; |
1830 CodeAssemblerTester data(isolate, kNumParams); | 1830 CodeAssemblerTester data(isolate, kNumParams); |
1831 CodeStubAssembler m(data.state()); | 1831 CodeStubAssembler m(data.state()); |
1832 | 1832 |
1833 Node* const context = m.Parameter(kNumParams + 2); | 1833 Node* const context = m.Parameter(kNumParams + 2); |
1834 Node* const promise = m.AllocateJSPromise(context); | 1834 Node* const promise = m.AllocateAndInitJSPromise(context); |
1835 m.PromiseInit(promise); | |
1836 m.Return(promise); | 1835 m.Return(promise); |
1837 | 1836 |
1838 Handle<Code> code = data.GenerateCode(); | 1837 Handle<Code> code = data.GenerateCode(); |
1839 CHECK(!code.is_null()); | 1838 CHECK(!code.is_null()); |
1840 | 1839 |
1841 FunctionTester ft(code, kNumParams); | 1840 FunctionTester ft(code, kNumParams); |
1842 Handle<Object> result = | 1841 Handle<Object> result = |
1843 ft.Call(isolate->factory()->undefined_value()).ToHandleChecked(); | 1842 ft.Call(isolate->factory()->undefined_value()).ToHandleChecked(); |
1844 CHECK(result->IsJSPromise()); | 1843 CHECK(result->IsJSPromise()); |
1845 } | 1844 } |
1846 | 1845 |
1847 TEST(PromiseInit) { | 1846 TEST(AllocateAndSetJSPromise) { |
1848 Isolate* isolate(CcTest::InitIsolateOnce()); | 1847 Isolate* isolate(CcTest::InitIsolateOnce()); |
1849 | 1848 |
1850 const int kNumParams = 1; | 1849 const int kNumParams = 1; |
1851 CodeAssemblerTester data(isolate, kNumParams); | 1850 CodeAssemblerTester data(isolate, kNumParams); |
1852 CodeStubAssembler m(data.state()); | 1851 CodeStubAssembler m(data.state()); |
1853 | 1852 |
1854 Node* const context = m.Parameter(kNumParams + 2); | 1853 Node* const context = m.Parameter(kNumParams + 2); |
1855 Node* const promise = m.AllocateJSPromise(context); | 1854 Node* const promise = m.AllocateAndSetJSPromise( |
1856 m.PromiseInit(promise); | 1855 context, m.SmiConstant(v8::Promise::kPending), m.SmiConstant(1)); |
1857 m.Return(promise); | 1856 m.Return(promise); |
1858 | 1857 |
1859 Handle<Code> code = data.GenerateCode(); | 1858 Handle<Code> code = data.GenerateCode(); |
1860 CHECK(!code.is_null()); | |
1861 | |
1862 FunctionTester ft(code, kNumParams); | |
1863 Handle<Object> result = | |
1864 ft.Call(isolate->factory()->undefined_value()).ToHandleChecked(); | |
1865 CHECK(result->IsJSPromise()); | |
1866 Handle<JSPromise> js_promise = Handle<JSPromise>::cast(result); | |
1867 CHECK_EQ(v8::Promise::kPending, js_promise->status()); | |
1868 CHECK_EQ(isolate->heap()->undefined_value(), js_promise->result()); | |
1869 CHECK(!js_promise->has_handler()); | |
1870 } | |
1871 | |
1872 TEST(PromiseSet) { | |
1873 Isolate* isolate(CcTest::InitIsolateOnce()); | |
1874 | |
1875 const int kNumParams = 1; | |
1876 CodeAssemblerTester data(isolate, kNumParams); | |
1877 CodeStubAssembler m(data.state()); | |
1878 | |
1879 Node* const context = m.Parameter(kNumParams + 2); | |
1880 Node* const promise = m.AllocateJSPromise(context); | |
1881 m.PromiseSet(promise, m.SmiConstant(v8::Promise::kPending), m.SmiConstant(1)); | |
1882 m.Return(promise); | |
1883 | |
1884 Handle<Code> code = data.GenerateCode(); | |
1885 CHECK(!code.is_null()); | 1859 CHECK(!code.is_null()); |
1886 | 1860 |
1887 FunctionTester ft(code, kNumParams); | 1861 FunctionTester ft(code, kNumParams); |
1888 Handle<Object> result = | 1862 Handle<Object> result = |
1889 ft.Call(isolate->factory()->undefined_value()).ToHandleChecked(); | 1863 ft.Call(isolate->factory()->undefined_value()).ToHandleChecked(); |
1890 CHECK(result->IsJSPromise()); | 1864 CHECK(result->IsJSPromise()); |
1891 Handle<JSPromise> js_promise = Handle<JSPromise>::cast(result); | 1865 Handle<JSPromise> js_promise = Handle<JSPromise>::cast(result); |
1892 CHECK_EQ(v8::Promise::kPending, js_promise->status()); | 1866 CHECK_EQ(v8::Promise::kPending, js_promise->status()); |
1893 CHECK_EQ(Smi::FromInt(1), js_promise->result()); | 1867 CHECK_EQ(Smi::FromInt(1), js_promise->result()); |
1894 CHECK(!js_promise->has_handler()); | 1868 CHECK(!js_promise->has_handler()); |
1895 } | 1869 } |
1896 | 1870 |
1897 TEST(AllocatePromiseReactionJobInfo) { | 1871 TEST(AllocatePromiseReactionJobInfo) { |
1898 Isolate* isolate(CcTest::InitIsolateOnce()); | 1872 Isolate* isolate(CcTest::InitIsolateOnce()); |
1899 | 1873 |
1900 const int kNumParams = 1; | 1874 const int kNumParams = 1; |
1901 CodeAssemblerTester data(isolate, kNumParams); | 1875 CodeAssemblerTester data(isolate, kNumParams); |
1902 CodeStubAssembler m(data.state()); | 1876 CodeStubAssembler m(data.state()); |
1903 | 1877 |
1904 Node* const context = m.Parameter(kNumParams + 2); | 1878 Node* const context = m.Parameter(kNumParams + 2); |
1905 Node* const promise = m.AllocateJSPromise(context); | 1879 Node* const promise = m.AllocateAndInitJSPromise(context); |
1906 m.PromiseInit(promise); | |
1907 Node* const tasks = m.AllocateFixedArray(FAST_ELEMENTS, m.IntPtrConstant(1)); | 1880 Node* const tasks = m.AllocateFixedArray(FAST_ELEMENTS, m.IntPtrConstant(1)); |
1908 m.StoreFixedArrayElement(tasks, 0, m.UndefinedConstant()); | 1881 m.StoreFixedArrayElement(tasks, 0, m.UndefinedConstant()); |
1909 Node* const deferred_promise = | 1882 Node* const deferred_promise = |
1910 m.AllocateFixedArray(FAST_ELEMENTS, m.IntPtrConstant(1)); | 1883 m.AllocateFixedArray(FAST_ELEMENTS, m.IntPtrConstant(1)); |
1911 m.StoreFixedArrayElement(deferred_promise, 0, m.UndefinedConstant()); | 1884 m.StoreFixedArrayElement(deferred_promise, 0, m.UndefinedConstant()); |
1912 Node* const info = m.AllocatePromiseReactionJobInfo( | 1885 Node* const info = m.AllocatePromiseReactionJobInfo( |
1913 promise, m.SmiConstant(1), tasks, deferred_promise, m.UndefinedConstant(), | 1886 promise, m.SmiConstant(1), tasks, deferred_promise, m.UndefinedConstant(), |
1914 m.UndefinedConstant(), context); | 1887 m.UndefinedConstant(), context); |
1915 m.Return(info); | 1888 m.Return(info); |
1916 | 1889 |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1982 } | 1955 } |
1983 | 1956 |
1984 TEST(PromiseHasHandler) { | 1957 TEST(PromiseHasHandler) { |
1985 Isolate* isolate(CcTest::InitIsolateOnce()); | 1958 Isolate* isolate(CcTest::InitIsolateOnce()); |
1986 | 1959 |
1987 const int kNumParams = 1; | 1960 const int kNumParams = 1; |
1988 CodeAssemblerTester data(isolate, kNumParams); | 1961 CodeAssemblerTester data(isolate, kNumParams); |
1989 PromiseBuiltinsAssembler m(data.state()); | 1962 PromiseBuiltinsAssembler m(data.state()); |
1990 | 1963 |
1991 Node* const context = m.Parameter(kNumParams + 2); | 1964 Node* const context = m.Parameter(kNumParams + 2); |
1992 Node* const promise = m.AllocateJSPromise(context); | 1965 Node* const promise = |
1993 m.PromiseInit(promise); | 1966 m.AllocateAndInitJSPromise(context, m.UndefinedConstant()); |
1994 m.Return(m.SelectBooleanConstant(m.PromiseHasHandler(promise))); | 1967 m.Return(m.SelectBooleanConstant(m.PromiseHasHandler(promise))); |
1995 | 1968 |
1996 Handle<Code> code = data.GenerateCode(); | 1969 Handle<Code> code = data.GenerateCode(); |
1997 CHECK(!code.is_null()); | 1970 CHECK(!code.is_null()); |
1998 | 1971 |
1999 FunctionTester ft(code, kNumParams); | 1972 FunctionTester ft(code, kNumParams); |
2000 Handle<Object> result = | 1973 Handle<Object> result = |
2001 ft.Call(isolate->factory()->undefined_value()).ToHandleChecked(); | 1974 ft.Call(isolate->factory()->undefined_value()).ToHandleChecked(); |
2002 CHECK_EQ(isolate->heap()->false_value(), *result); | 1975 CHECK_EQ(isolate->heap()->false_value(), *result); |
2003 } | 1976 } |
2004 | 1977 |
2005 TEST(CreatePromiseResolvingFunctionsContext) { | 1978 TEST(CreatePromiseResolvingFunctionsContext) { |
2006 Isolate* isolate(CcTest::InitIsolateOnce()); | 1979 Isolate* isolate(CcTest::InitIsolateOnce()); |
2007 | 1980 |
2008 const int kNumParams = 1; | 1981 const int kNumParams = 1; |
2009 CodeAssemblerTester data(isolate, kNumParams); | 1982 CodeAssemblerTester data(isolate, kNumParams); |
2010 PromiseBuiltinsAssembler m(data.state()); | 1983 PromiseBuiltinsAssembler m(data.state()); |
2011 | 1984 |
2012 Node* const context = m.Parameter(kNumParams + 2); | 1985 Node* const context = m.Parameter(kNumParams + 2); |
2013 Node* const native_context = m.LoadNativeContext(context); | 1986 Node* const native_context = m.LoadNativeContext(context); |
2014 Node* const promise = m.AllocateJSPromise(context); | 1987 Node* const promise = |
2015 m.PromiseSet(promise, m.SmiConstant(v8::Promise::kPending), m.SmiConstant(1)); | 1988 m.AllocateAndInitJSPromise(context, m.UndefinedConstant()); |
2016 Node* const promise_context = m.CreatePromiseResolvingFunctionsContext( | 1989 Node* const promise_context = m.CreatePromiseResolvingFunctionsContext( |
2017 promise, m.BooleanConstant(false), native_context); | 1990 promise, m.BooleanConstant(false), native_context); |
2018 m.Return(promise_context); | 1991 m.Return(promise_context); |
2019 | 1992 |
2020 Handle<Code> code = data.GenerateCode(); | 1993 Handle<Code> code = data.GenerateCode(); |
2021 CHECK(!code.is_null()); | 1994 CHECK(!code.is_null()); |
2022 | 1995 |
2023 FunctionTester ft(code, kNumParams); | 1996 FunctionTester ft(code, kNumParams); |
2024 Handle<Object> result = | 1997 Handle<Object> result = |
2025 ft.Call(isolate->factory()->undefined_value()).ToHandleChecked(); | 1998 ft.Call(isolate->factory()->undefined_value()).ToHandleChecked(); |
(...skipping 10 matching lines...) Expand all Loading... |
2036 | 2009 |
2037 TEST(CreatePromiseResolvingFunctions) { | 2010 TEST(CreatePromiseResolvingFunctions) { |
2038 Isolate* isolate(CcTest::InitIsolateOnce()); | 2011 Isolate* isolate(CcTest::InitIsolateOnce()); |
2039 | 2012 |
2040 const int kNumParams = 1; | 2013 const int kNumParams = 1; |
2041 CodeAssemblerTester data(isolate, kNumParams); | 2014 CodeAssemblerTester data(isolate, kNumParams); |
2042 PromiseBuiltinsAssembler m(data.state()); | 2015 PromiseBuiltinsAssembler m(data.state()); |
2043 | 2016 |
2044 Node* const context = m.Parameter(kNumParams + 2); | 2017 Node* const context = m.Parameter(kNumParams + 2); |
2045 Node* const native_context = m.LoadNativeContext(context); | 2018 Node* const native_context = m.LoadNativeContext(context); |
2046 Node* const promise = m.AllocateJSPromise(context); | 2019 Node* const promise = |
2047 m.PromiseSet(promise, m.SmiConstant(v8::Promise::kPending), m.SmiConstant(1)); | 2020 m.AllocateAndInitJSPromise(context, m.UndefinedConstant()); |
2048 Node *resolve, *reject; | 2021 Node *resolve, *reject; |
2049 std::tie(resolve, reject) = m.CreatePromiseResolvingFunctions( | 2022 std::tie(resolve, reject) = m.CreatePromiseResolvingFunctions( |
2050 promise, m.BooleanConstant(false), native_context); | 2023 promise, m.BooleanConstant(false), native_context); |
2051 Node* const kSize = m.IntPtrConstant(2); | 2024 Node* const kSize = m.IntPtrConstant(2); |
2052 Node* const arr = m.AllocateFixedArray(FAST_ELEMENTS, kSize); | 2025 Node* const arr = m.AllocateFixedArray(FAST_ELEMENTS, kSize); |
2053 m.StoreFixedArrayElement(arr, 0, resolve); | 2026 m.StoreFixedArrayElement(arr, 0, resolve); |
2054 m.StoreFixedArrayElement(arr, 1, reject); | 2027 m.StoreFixedArrayElement(arr, 1, reject); |
2055 m.Return(arr); | 2028 m.Return(arr); |
2056 | 2029 |
2057 Handle<Code> code = data.GenerateCode(); | 2030 Handle<Code> code = data.GenerateCode(); |
(...skipping 10 matching lines...) Expand all Loading... |
2068 | 2041 |
2069 TEST(AllocateFunctionWithMapAndContext) { | 2042 TEST(AllocateFunctionWithMapAndContext) { |
2070 Isolate* isolate(CcTest::InitIsolateOnce()); | 2043 Isolate* isolate(CcTest::InitIsolateOnce()); |
2071 | 2044 |
2072 const int kNumParams = 1; | 2045 const int kNumParams = 1; |
2073 CodeAssemblerTester data(isolate, kNumParams); | 2046 CodeAssemblerTester data(isolate, kNumParams); |
2074 PromiseBuiltinsAssembler m(data.state()); | 2047 PromiseBuiltinsAssembler m(data.state()); |
2075 | 2048 |
2076 Node* const context = m.Parameter(kNumParams + 2); | 2049 Node* const context = m.Parameter(kNumParams + 2); |
2077 Node* const native_context = m.LoadNativeContext(context); | 2050 Node* const native_context = m.LoadNativeContext(context); |
2078 Node* const promise = m.AllocateJSPromise(context); | 2051 Node* const promise = |
2079 m.PromiseSet(promise, m.SmiConstant(v8::Promise::kPending), m.SmiConstant(1)); | 2052 m.AllocateAndInitJSPromise(context, m.UndefinedConstant()); |
2080 Node* promise_context = m.CreatePromiseResolvingFunctionsContext( | 2053 Node* promise_context = m.CreatePromiseResolvingFunctionsContext( |
2081 promise, m.BooleanConstant(false), native_context); | 2054 promise, m.BooleanConstant(false), native_context); |
2082 Node* resolve_info = | 2055 Node* resolve_info = |
2083 m.LoadContextElement(native_context, Context::PROMISE_RESOLVE_SHARED_FUN); | 2056 m.LoadContextElement(native_context, Context::PROMISE_RESOLVE_SHARED_FUN); |
2084 Node* const map = m.LoadContextElement( | 2057 Node* const map = m.LoadContextElement( |
2085 native_context, Context::STRICT_FUNCTION_WITHOUT_PROTOTYPE_MAP_INDEX); | 2058 native_context, Context::STRICT_FUNCTION_WITHOUT_PROTOTYPE_MAP_INDEX); |
2086 Node* const resolve = | 2059 Node* const resolve = |
2087 m.AllocateFunctionWithMapAndContext(map, resolve_info, promise_context); | 2060 m.AllocateFunctionWithMapAndContext(map, resolve_info, promise_context); |
2088 m.Return(resolve); | 2061 m.Return(resolve); |
2089 | 2062 |
2090 Handle<Code> code = data.GenerateCode(); | 2063 Handle<Code> code = data.GenerateCode(); |
2091 CHECK(!code.is_null()); | 2064 CHECK(!code.is_null()); |
2092 | 2065 |
2093 FunctionTester ft(code, kNumParams); | 2066 FunctionTester ft(code, kNumParams); |
2094 Handle<Object> result_obj = | 2067 Handle<Object> result_obj = |
2095 ft.Call(isolate->factory()->undefined_value()).ToHandleChecked(); | 2068 ft.Call(isolate->factory()->undefined_value()).ToHandleChecked(); |
2096 CHECK(result_obj->IsJSFunction()); | 2069 CHECK(result_obj->IsJSFunction()); |
2097 Handle<JSFunction> fun = Handle<JSFunction>::cast(result_obj); | 2070 Handle<JSFunction> fun = Handle<JSFunction>::cast(result_obj); |
2098 CHECK_EQ(isolate->heap()->empty_fixed_array(), fun->properties()); | 2071 CHECK_EQ(isolate->heap()->empty_fixed_array(), fun->properties()); |
2099 CHECK_EQ(isolate->heap()->empty_fixed_array(), fun->elements()); | 2072 CHECK_EQ(isolate->heap()->empty_fixed_array(), fun->elements()); |
2100 CHECK_EQ(isolate->heap()->empty_literals_array(), fun->literals()); | 2073 CHECK_EQ(isolate->heap()->empty_literals_array(), fun->literals()); |
2101 CHECK_EQ(isolate->heap()->the_hole_value(), fun->prototype_or_initial_map()); | 2074 CHECK_EQ(isolate->heap()->the_hole_value(), fun->prototype_or_initial_map()); |
2102 CHECK_EQ(*isolate->promise_resolve_shared_fun(), fun->shared()); | 2075 CHECK_EQ(*isolate->promise_resolve_shared_fun(), fun->shared()); |
2103 CHECK_EQ(isolate->promise_resolve_shared_fun()->code(), fun->code()); | 2076 CHECK_EQ(isolate->promise_resolve_shared_fun()->code(), fun->code()); |
2104 CHECK_EQ(isolate->heap()->undefined_value(), fun->next_function_link()); | 2077 CHECK_EQ(isolate->heap()->undefined_value(), fun->next_function_link()); |
2105 } | 2078 } |
2106 | 2079 |
2107 } // namespace internal | 2080 } // namespace internal |
2108 } // namespace v8 | 2081 } // namespace v8 |
OLD | NEW |