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/code-factory.h" | 6 #include "src/code-factory.h" |
7 #include "src/code-stub-assembler.h" | 7 #include "src/code-stub-assembler.h" |
8 #include "src/compiler/node.h" | 8 #include "src/compiler/node.h" |
9 #include "src/isolate.h" | 9 #include "src/isolate.h" |
| 10 #include "src/promise-utils.h" |
10 #include "test/cctest/compiler/code-assembler-tester.h" | 11 #include "test/cctest/compiler/code-assembler-tester.h" |
11 #include "test/cctest/compiler/function-tester.h" | 12 #include "test/cctest/compiler/function-tester.h" |
12 | 13 |
13 namespace v8 { | 14 namespace v8 { |
14 namespace internal { | 15 namespace internal { |
15 | 16 |
16 using compiler::CodeAssemblerTester; | 17 using compiler::CodeAssemblerTester; |
17 using compiler::FunctionTester; | 18 using compiler::FunctionTester; |
18 using compiler::Node; | 19 using compiler::Node; |
19 | 20 |
(...skipping 2042 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2062 FunctionTester ft(code, kNumParams); | 2063 FunctionTester ft(code, kNumParams); |
2063 Handle<Object> result = | 2064 Handle<Object> result = |
2064 ft.Call(isolate->factory()->undefined_value()).ToHandleChecked(); | 2065 ft.Call(isolate->factory()->undefined_value()).ToHandleChecked(); |
2065 CHECK(result->IsJSPromise()); | 2066 CHECK(result->IsJSPromise()); |
2066 Handle<JSPromise> js_promise = Handle<JSPromise>::cast(result); | 2067 Handle<JSPromise> js_promise = Handle<JSPromise>::cast(result); |
2067 CHECK_EQ(kPromisePending, js_promise->status()); | 2068 CHECK_EQ(kPromisePending, js_promise->status()); |
2068 CHECK_EQ(Smi::FromInt(1), js_promise->result()); | 2069 CHECK_EQ(Smi::FromInt(1), js_promise->result()); |
2069 CHECK(!js_promise->has_handler()); | 2070 CHECK(!js_promise->has_handler()); |
2070 } | 2071 } |
2071 | 2072 |
| 2073 TEST(CreatePromiseResolvingFunctionsContext) { |
| 2074 Isolate* isolate(CcTest::InitIsolateOnce()); |
| 2075 |
| 2076 const int kNumParams = 1; |
| 2077 CodeAssemblerTester data(isolate, kNumParams); |
| 2078 CodeStubAssembler m(data.state()); |
| 2079 |
| 2080 Node* const context = m.Parameter(kNumParams + 2); |
| 2081 Node* const native_context = m.LoadNativeContext(context); |
| 2082 Node* const promise = m.AllocateJSPromise(context); |
| 2083 m.PromiseSet(promise, m.SmiConstant(kPromisePending), m.SmiConstant(1)); |
| 2084 Node* const promise_context = m.CreatePromiseResolvingFunctionsContext( |
| 2085 promise, m.BooleanConstant(false), native_context); |
| 2086 m.Return(promise_context); |
| 2087 |
| 2088 Handle<Code> code = data.GenerateCode(); |
| 2089 CHECK(!code.is_null()); |
| 2090 |
| 2091 FunctionTester ft(code, kNumParams); |
| 2092 Handle<Object> result = |
| 2093 ft.Call(isolate->factory()->undefined_value()).ToHandleChecked(); |
| 2094 CHECK(result->IsContext()); |
| 2095 Handle<Context> context_js = Handle<Context>::cast(result); |
| 2096 CHECK_EQ(isolate->native_context()->closure(), context_js->closure()); |
| 2097 CHECK_EQ(isolate->heap()->the_hole_value(), context_js->extension()); |
| 2098 CHECK_EQ(*isolate->native_context(), context_js->native_context()); |
| 2099 CHECK_EQ(Smi::FromInt(0), context_js->get(PromiseUtils::kAlreadyVisitedSlot)); |
| 2100 CHECK(context_js->get(PromiseUtils::kPromiseSlot)->IsJSPromise()); |
| 2101 CHECK_EQ(isolate->heap()->false_value(), |
| 2102 context_js->get(PromiseUtils::kDebugEventSlot)); |
| 2103 } |
| 2104 |
| 2105 TEST(CreatePromiseResolvingFunctions) { |
| 2106 Isolate* isolate(CcTest::InitIsolateOnce()); |
| 2107 |
| 2108 const int kNumParams = 1; |
| 2109 CodeAssemblerTester data(isolate, kNumParams); |
| 2110 CodeStubAssembler m(data.state()); |
| 2111 |
| 2112 Node* const context = m.Parameter(kNumParams + 2); |
| 2113 Node* const native_context = m.LoadNativeContext(context); |
| 2114 Node* const promise = m.AllocateJSPromise(context); |
| 2115 m.PromiseSet(promise, m.SmiConstant(kPromisePending), m.SmiConstant(1)); |
| 2116 Node *resolve, *reject; |
| 2117 std::tie(resolve, reject) = m.CreatePromiseResolvingFunctions( |
| 2118 promise, m.BooleanConstant(false), native_context); |
| 2119 Node* const kSize = m.Int32Constant(2); |
| 2120 Node* const arr = m.AllocateFixedArray(FAST_ELEMENTS, kSize); |
| 2121 m.StoreFixedArrayElement(arr, 0, resolve); |
| 2122 m.StoreFixedArrayElement(arr, 1, reject); |
| 2123 m.Return(arr); |
| 2124 |
| 2125 Handle<Code> code = data.GenerateCode(); |
| 2126 CHECK(!code.is_null()); |
| 2127 |
| 2128 FunctionTester ft(code, kNumParams); |
| 2129 Handle<Object> result_obj = |
| 2130 ft.Call(isolate->factory()->undefined_value()).ToHandleChecked(); |
| 2131 CHECK(result_obj->IsFixedArray()); |
| 2132 Handle<FixedArray> result_arr = Handle<FixedArray>::cast(result_obj); |
| 2133 CHECK(result_arr->get(0)->IsJSFunction()); |
| 2134 CHECK(result_arr->get(1)->IsJSFunction()); |
| 2135 } |
| 2136 |
| 2137 TEST(AllocateFunctionWithMapAndContext) { |
| 2138 Isolate* isolate(CcTest::InitIsolateOnce()); |
| 2139 |
| 2140 const int kNumParams = 1; |
| 2141 CodeAssemblerTester data(isolate, kNumParams); |
| 2142 CodeStubAssembler m(data.state()); |
| 2143 |
| 2144 Node* const context = m.Parameter(kNumParams + 2); |
| 2145 Node* const native_context = m.LoadNativeContext(context); |
| 2146 Node* const promise = m.AllocateJSPromise(context); |
| 2147 m.PromiseSet(promise, m.SmiConstant(kPromisePending), m.SmiConstant(1)); |
| 2148 Node* promise_context = m.CreatePromiseResolvingFunctionsContext( |
| 2149 promise, m.BooleanConstant(false), native_context); |
| 2150 Node* resolve_info = |
| 2151 m.LoadContextElement(native_context, Context::PROMISE_RESOLVE_SHARED_FUN); |
| 2152 Node* const map = m.LoadContextElement( |
| 2153 native_context, Context::STRICT_FUNCTION_WITHOUT_PROTOTYPE_MAP_INDEX); |
| 2154 Node* const resolve = |
| 2155 m.AllocateFunctionWithMapAndContext(map, resolve_info, promise_context); |
| 2156 m.Return(resolve); |
| 2157 |
| 2158 Handle<Code> code = data.GenerateCode(); |
| 2159 CHECK(!code.is_null()); |
| 2160 |
| 2161 FunctionTester ft(code, kNumParams); |
| 2162 Handle<Object> result_obj = |
| 2163 ft.Call(isolate->factory()->undefined_value()).ToHandleChecked(); |
| 2164 CHECK(result_obj->IsJSFunction()); |
| 2165 Handle<JSFunction> fun = Handle<JSFunction>::cast(result_obj); |
| 2166 CHECK_EQ(isolate->heap()->empty_fixed_array(), fun->properties()); |
| 2167 CHECK_EQ(isolate->heap()->empty_fixed_array(), fun->elements()); |
| 2168 CHECK_EQ(isolate->heap()->empty_literals_array(), fun->literals()); |
| 2169 CHECK_EQ(isolate->heap()->the_hole_value(), fun->prototype_or_initial_map()); |
| 2170 CHECK_EQ(*isolate->promise_resolve_shared_fun(), fun->shared()); |
| 2171 CHECK_EQ(isolate->promise_resolve_shared_fun()->code(), fun->code()); |
| 2172 CHECK_EQ(isolate->heap()->undefined_value(), fun->next_function_link()); |
| 2173 } |
| 2174 |
2072 } // namespace internal | 2175 } // namespace internal |
2073 } // namespace v8 | 2176 } // namespace v8 |
OLD | NEW |