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