OLD | NEW |
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 | |
5 #include "src/runtime/runtime-utils.h" | 4 #include "src/runtime/runtime-utils.h" |
6 | 5 |
7 #include "src/debug/debug.h" | 6 #include "src/debug/debug.h" |
8 #include "src/elements.h" | 7 #include "src/elements.h" |
9 #include "src/promise-utils.h" | 8 #include "src/promise-utils.h" |
10 | 9 |
11 namespace v8 { | 10 namespace v8 { |
12 namespace internal { | 11 namespace internal { |
13 | 12 |
14 namespace { | 13 namespace { |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
59 } | 58 } |
60 | 59 |
61 namespace { | 60 namespace { |
62 void EnqueuePromiseReactionJob(Isolate* isolate, Handle<Object> value, | 61 void EnqueuePromiseReactionJob(Isolate* isolate, Handle<Object> value, |
63 Handle<Object> tasks, Handle<Object> deferred, | 62 Handle<Object> tasks, Handle<Object> deferred, |
64 Handle<Object> status) { | 63 Handle<Object> status) { |
65 Handle<Object> debug_id = isolate->factory()->undefined_value(); | 64 Handle<Object> debug_id = isolate->factory()->undefined_value(); |
66 Handle<Object> debug_name = isolate->factory()->undefined_value(); | 65 Handle<Object> debug_name = isolate->factory()->undefined_value(); |
67 if (isolate->debug()->is_active()) { | 66 if (isolate->debug()->is_active()) { |
68 MaybeHandle<Object> maybe_result; | 67 MaybeHandle<Object> maybe_result; |
69 Handle<Object> argv[] = {deferred, status}; | 68 Handle<Object> deferred_obj(deferred); |
| 69 |
| 70 if (deferred->IsFixedArray()) { |
| 71 deferred_obj = isolate->factory()->undefined_value(); |
| 72 } |
| 73 |
| 74 Handle<Object> argv[] = {deferred_obj, status}; |
70 maybe_result = Execution::TryCall( | 75 maybe_result = Execution::TryCall( |
71 isolate, isolate->promise_debug_get_info(), | 76 isolate, isolate->promise_debug_get_info(), |
72 isolate->factory()->undefined_value(), arraysize(argv), argv); | 77 isolate->factory()->undefined_value(), arraysize(argv), argv); |
| 78 |
73 Handle<Object> result; | 79 Handle<Object> result; |
74 if ((maybe_result).ToHandle(&result)) { | 80 if ((maybe_result).ToHandle(&result)) { |
75 CHECK(result->IsJSArray()); | 81 CHECK(result->IsJSArray()); |
76 Handle<JSArray> array = Handle<JSArray>::cast(result); | 82 Handle<JSArray> array = Handle<JSArray>::cast(result); |
77 ElementsAccessor* accessor = array->GetElementsAccessor(); | 83 ElementsAccessor* accessor = array->GetElementsAccessor(); |
78 DCHECK(accessor->HasElement(array, 0)); | 84 DCHECK(accessor->HasElement(array, 0)); |
79 DCHECK(accessor->HasElement(array, 1)); | 85 DCHECK(accessor->HasElement(array, 1)); |
80 debug_id = accessor->Get(array, 0); | 86 debug_id = accessor->Get(array, 0); |
81 debug_name = accessor->Get(array, 1); | 87 debug_name = accessor->Get(array, 1); |
82 } | 88 } |
83 } | 89 } |
84 Handle<PromiseReactionJobInfo> info = | 90 Handle<PromiseReactionJobInfo> info = |
85 isolate->factory()->NewPromiseReactionJobInfo(value, tasks, deferred, | 91 isolate->factory()->NewPromiseReactionJobInfo(value, tasks, deferred, |
86 debug_id, debug_name, | 92 debug_id, debug_name, |
87 isolate->native_context()); | 93 isolate->native_context()); |
88 isolate->EnqueueMicrotask(info); | 94 isolate->EnqueueMicrotask(info); |
89 } | 95 } |
90 | 96 |
91 void PromiseFulfill(Isolate* isolate, Handle<JSReceiver> promise, | 97 void PromiseSet(Handle<JSPromise> promise, int status, Handle<Object> result) { |
92 Handle<Smi> status, Handle<Object> value, | 98 promise->set_status(status); |
93 Handle<Symbol> reaction) { | 99 promise->set_result(*result); |
94 Handle<Object> tasks = JSReceiver::GetDataProperty(promise, reaction); | 100 // TODO(gsathya): reset reactions? |
95 if (!tasks->IsUndefined(isolate)) { | 101 } |
96 Handle<Object> deferred = JSReceiver::GetDataProperty( | 102 |
97 promise, isolate->factory()->promise_deferred_reaction_symbol()); | 103 void PromiseFulfill(Isolate* isolate, Handle<JSPromise> promise, |
| 104 Handle<Smi> status, Handle<Object> value) { |
| 105 // Check if there are any callbacks. |
| 106 if (!promise->deferred()->IsUndefined(isolate)) { |
| 107 Handle<Object> tasks((status->value() == kPromiseFulfilled) |
| 108 ? promise->fulfill_reactions() |
| 109 : promise->reject_reactions(), |
| 110 isolate); |
| 111 Handle<Object> deferred(promise->deferred(), isolate); |
98 EnqueuePromiseReactionJob(isolate, value, tasks, deferred, status); | 112 EnqueuePromiseReactionJob(isolate, value, tasks, deferred, status); |
99 } | 113 } |
| 114 |
| 115 PromiseSet(promise, status->value(), value); |
100 } | 116 } |
| 117 |
101 } // namespace | 118 } // namespace |
102 | 119 |
103 RUNTIME_FUNCTION(Runtime_PromiseReject) { | 120 RUNTIME_FUNCTION(Runtime_PromiseReject) { |
104 DCHECK(args.length() == 3); | 121 DCHECK(args.length() == 3); |
105 HandleScope scope(isolate); | 122 HandleScope scope(isolate); |
106 CONVERT_ARG_HANDLE_CHECKED(JSReceiver, promise, 0); | 123 CONVERT_ARG_HANDLE_CHECKED(JSPromise, promise, 0); |
107 CONVERT_ARG_HANDLE_CHECKED(Object, reason, 1); | 124 CONVERT_ARG_HANDLE_CHECKED(Object, reason, 1); |
108 CONVERT_BOOLEAN_ARG_CHECKED(debug_event, 2); | 125 CONVERT_BOOLEAN_ARG_CHECKED(debug_event, 2); |
109 | 126 |
110 PromiseRejectEvent(isolate, promise, promise, reason, debug_event); | 127 PromiseRejectEvent(isolate, promise, promise, reason, debug_event); |
111 | 128 |
112 Handle<Smi> status = handle(Smi::FromInt(kPromiseRejected), isolate); | 129 Handle<Smi> status(Smi::FromInt(kPromiseRejected), isolate); |
113 Handle<Symbol> reaction = | 130 PromiseFulfill(isolate, promise, status, reason); |
114 isolate->factory()->promise_reject_reactions_symbol(); | |
115 PromiseFulfill(isolate, promise, status, reason, reaction); | |
116 return isolate->heap()->undefined_value(); | 131 return isolate->heap()->undefined_value(); |
117 } | 132 } |
118 | 133 |
119 RUNTIME_FUNCTION(Runtime_PromiseFulfill) { | 134 RUNTIME_FUNCTION(Runtime_PromiseFulfill) { |
120 DCHECK(args.length() == 4); | 135 DCHECK(args.length() == 3); |
121 HandleScope scope(isolate); | 136 HandleScope scope(isolate); |
122 CONVERT_ARG_HANDLE_CHECKED(JSReceiver, promise, 0); | 137 CONVERT_ARG_HANDLE_CHECKED(JSPromise, promise, 0); |
123 CONVERT_ARG_HANDLE_CHECKED(Smi, status, 1); | 138 CONVERT_ARG_HANDLE_CHECKED(Smi, status, 1); |
124 CONVERT_ARG_HANDLE_CHECKED(Object, value, 2); | 139 CONVERT_ARG_HANDLE_CHECKED(Object, value, 2); |
125 CONVERT_ARG_HANDLE_CHECKED(Symbol, reaction, 3); | 140 PromiseFulfill(isolate, promise, status, value); |
126 PromiseFulfill(isolate, promise, status, value, reaction); | |
127 return isolate->heap()->undefined_value(); | 141 return isolate->heap()->undefined_value(); |
128 } | 142 } |
129 | 143 |
130 RUNTIME_FUNCTION(Runtime_EnqueuePromiseReactionJob) { | 144 RUNTIME_FUNCTION(Runtime_EnqueuePromiseReactionJob) { |
131 HandleScope scope(isolate); | 145 HandleScope scope(isolate); |
132 DCHECK(args.length() == 4); | 146 DCHECK(args.length() == 4); |
133 CONVERT_ARG_HANDLE_CHECKED(Object, value, 0); | 147 CONVERT_ARG_HANDLE_CHECKED(Object, value, 0); |
134 CONVERT_ARG_HANDLE_CHECKED(Object, tasks, 1); | 148 CONVERT_ARG_HANDLE_CHECKED(Object, tasks, 1); |
135 CONVERT_ARG_HANDLE_CHECKED(Object, deferred, 2); | 149 CONVERT_ARG_HANDLE_CHECKED(Object, deferred, 2); |
136 CONVERT_ARG_HANDLE_CHECKED(Object, status, 3); | 150 CONVERT_ARG_HANDLE_CHECKED(Object, status, 3); |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
185 RUNTIME_FUNCTION(Runtime_RunMicrotasks) { | 199 RUNTIME_FUNCTION(Runtime_RunMicrotasks) { |
186 HandleScope scope(isolate); | 200 HandleScope scope(isolate); |
187 DCHECK(args.length() == 0); | 201 DCHECK(args.length() == 0); |
188 isolate->RunMicrotasks(); | 202 isolate->RunMicrotasks(); |
189 return isolate->heap()->undefined_value(); | 203 return isolate->heap()->undefined_value(); |
190 } | 204 } |
191 | 205 |
192 RUNTIME_FUNCTION(Runtime_CreateResolvingFunctions) { | 206 RUNTIME_FUNCTION(Runtime_CreateResolvingFunctions) { |
193 HandleScope scope(isolate); | 207 HandleScope scope(isolate); |
194 CONVERT_ARG_HANDLE_CHECKED(JSObject, promise, 0); | 208 CONVERT_ARG_HANDLE_CHECKED(JSObject, promise, 0); |
| 209 DCHECK(args.length() == 1); |
195 Handle<JSFunction> resolve, reject; | 210 Handle<JSFunction> resolve, reject; |
196 | 211 |
197 PromiseUtils::CreateResolvingFunctions( | 212 PromiseUtils::CreateResolvingFunctions( |
198 isolate, promise, isolate->factory()->true_value(), &resolve, &reject); | 213 isolate, promise, isolate->factory()->true_value(), &resolve, &reject); |
199 | 214 |
200 Handle<FixedArray> result = isolate->factory()->NewFixedArray(2); | 215 Handle<FixedArray> result = isolate->factory()->NewFixedArray(2); |
201 result->set(0, *resolve); | 216 result->set(0, *resolve); |
202 result->set(1, *reject); | 217 result->set(1, *reject); |
203 | 218 |
204 return *result; | 219 return *result; |
205 } | 220 } |
206 | 221 |
| 222 RUNTIME_FUNCTION(Runtime_PromiseStatus) { |
| 223 HandleScope scope(isolate); |
| 224 DCHECK(args.length() == 1); |
| 225 CONVERT_ARG_HANDLE_CHECKED(JSPromise, promise, 0); |
| 226 |
| 227 return Smi::FromInt(promise->status()); |
| 228 } |
| 229 |
| 230 RUNTIME_FUNCTION(Runtime_PromiseResult) { |
| 231 HandleScope scope(isolate); |
| 232 DCHECK(args.length() == 1); |
| 233 CONVERT_ARG_HANDLE_CHECKED(JSPromise, promise, 0); |
| 234 return promise->result(); |
| 235 } |
| 236 |
| 237 RUNTIME_FUNCTION(Runtime_PromiseDeferred) { |
| 238 HandleScope scope(isolate); |
| 239 DCHECK(args.length() == 1); |
| 240 CONVERT_ARG_HANDLE_CHECKED(JSPromise, promise, 0); |
| 241 |
| 242 Handle<Object> deferred(promise->deferred(), isolate); |
| 243 if (deferred->IsUndefined(isolate)) { |
| 244 return isolate->heap()->undefined_value(); |
| 245 } |
| 246 |
| 247 if (deferred->IsJSObject()) { |
| 248 return *deferred; |
| 249 } |
| 250 |
| 251 DCHECK(deferred->IsFixedArray()); |
| 252 return *isolate->factory()->NewJSArrayWithElements( |
| 253 Handle<FixedArray>::cast(deferred)); |
| 254 } |
| 255 |
| 256 RUNTIME_FUNCTION(Runtime_PromiseRejectReactions) { |
| 257 HandleScope scope(isolate); |
| 258 DCHECK(args.length() == 1); |
| 259 CONVERT_ARG_HANDLE_CHECKED(JSPromise, promise, 0); |
| 260 |
| 261 Handle<Object> reject_reactions(promise->reject_reactions(), isolate); |
| 262 if (reject_reactions->IsUndefined(isolate)) { |
| 263 return isolate->heap()->undefined_value(); |
| 264 } |
| 265 |
| 266 if (reject_reactions->IsJSObject()) { |
| 267 return *reject_reactions; |
| 268 } |
| 269 |
| 270 DCHECK(reject_reactions->IsFixedArray()); |
| 271 return *isolate->factory()->NewJSArrayWithElements( |
| 272 Handle<FixedArray>::cast(reject_reactions)); |
| 273 } |
| 274 |
207 } // namespace internal | 275 } // namespace internal |
208 } // namespace v8 | 276 } // namespace v8 |
OLD | NEW |