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 #include "src/runtime/runtime-utils.h" | 4 #include "src/runtime/runtime-utils.h" |
5 | 5 |
6 #include "src/debug/debug.h" | 6 #include "src/debug/debug.h" |
7 #include "src/elements.h" | 7 #include "src/elements.h" |
8 | 8 |
9 namespace v8 { | 9 namespace v8 { |
10 namespace internal { | 10 namespace internal { |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
64 DCHECK_EQ(1, args.length()); | 64 DCHECK_EQ(1, args.length()); |
65 HandleScope scope(isolate); | 65 HandleScope scope(isolate); |
66 CONVERT_ARG_HANDLE_CHECKED(JSPromise, promise, 0); | 66 CONVERT_ARG_HANDLE_CHECKED(JSPromise, promise, 0); |
67 // At this point, no revocation has been issued before | 67 // At this point, no revocation has been issued before |
68 CHECK(!promise->has_handler()); | 68 CHECK(!promise->has_handler()); |
69 isolate->ReportPromiseReject(promise, Handle<Object>(), | 69 isolate->ReportPromiseReject(promise, Handle<Object>(), |
70 v8::kPromiseHandlerAddedAfterReject); | 70 v8::kPromiseHandlerAddedAfterReject); |
71 return isolate->heap()->undefined_value(); | 71 return isolate->heap()->undefined_value(); |
72 } | 72 } |
73 | 73 |
74 namespace { | |
75 | |
76 // In an async function, reuse the existing stack related to the outer | |
77 // Promise. Otherwise, e.g. in a direct call to then, save a new stack. | |
78 // Promises with multiple reactions with one or more of them being async | |
79 // functions will not get a good stack trace, as async functions require | |
80 // different stacks from direct Promise use, but we save and restore a | |
81 // stack once for all reactions. | |
82 // | |
83 // If this isn't a case of async function, we return false, otherwise | |
84 // we set the correct id and return true. | |
85 // | |
86 // TODO(littledan): Improve this case. | |
87 bool GetDebugIdForAsyncFunction(Isolate* isolate, | |
88 Handle<PromiseReactionJobInfo> info, | |
89 int* debug_id) { | |
90 // deferred_promise can be Undefined, FixedArray or userland promise object. | |
91 if (!info->deferred_promise()->IsJSPromise()) { | |
92 return false; | |
93 } | |
94 | |
95 Handle<JSPromise> deferred_promise(JSPromise::cast(info->deferred_promise()), | |
96 isolate); | |
97 Handle<Symbol> handled_by_symbol = | |
98 isolate->factory()->promise_handled_by_symbol(); | |
99 Handle<Object> handled_by_promise = | |
100 JSObject::GetDataProperty(deferred_promise, handled_by_symbol); | |
101 | |
102 if (!handled_by_promise->IsJSPromise()) { | |
103 return false; | |
104 } | |
105 | |
106 Handle<JSPromise> handled_by_promise_js = | |
107 Handle<JSPromise>::cast(handled_by_promise); | |
108 Handle<Symbol> async_stack_id_symbol = | |
109 isolate->factory()->promise_async_stack_id_symbol(); | |
110 Handle<Object> id = | |
111 JSObject::GetDataProperty(handled_by_promise_js, async_stack_id_symbol); | |
112 | |
113 // id can be Undefined or Smi. | |
114 if (!id->IsSmi()) { | |
115 return false; | |
116 } | |
117 | |
118 *debug_id = Handle<Smi>::cast(id)->value(); | |
119 return true; | |
120 } | |
121 | |
122 void SetDebugInfo(Isolate* isolate, Handle<JSPromise> promise, | |
123 Handle<PromiseReactionJobInfo> info, int status) { | |
124 int id = kDebugPromiseNoID; | |
125 if (!GetDebugIdForAsyncFunction(isolate, info, &id)) { | |
126 id = isolate->debug()->NextAsyncTaskId(promise); | |
127 DCHECK(status != v8::Promise::kPending); | |
128 } | |
129 info->set_debug_id(id); | |
130 } | |
131 | |
132 void EnqueuePromiseReactionJob(Isolate* isolate, Handle<JSPromise> promise, | |
133 Handle<PromiseReactionJobInfo> info, | |
134 int status) { | |
135 if (isolate->debug()->is_active()) { | |
136 SetDebugInfo(isolate, promise, info, status); | |
137 } | |
138 | |
139 isolate->EnqueueMicrotask(info); | |
140 } | |
141 | |
142 } // namespace | |
143 | |
144 RUNTIME_FUNCTION(Runtime_EnqueuePromiseReactionJob) { | 74 RUNTIME_FUNCTION(Runtime_EnqueuePromiseReactionJob) { |
145 HandleScope scope(isolate); | 75 HandleScope scope(isolate); |
146 DCHECK_EQ(3, args.length()); | 76 DCHECK_EQ(1, args.length()); |
147 CONVERT_ARG_HANDLE_CHECKED(JSPromise, promise, 0); | 77 CONVERT_ARG_HANDLE_CHECKED(PromiseReactionJobInfo, info, 0); |
148 CONVERT_ARG_HANDLE_CHECKED(PromiseReactionJobInfo, info, 1); | 78 isolate->EnqueueMicrotask(info); |
149 CONVERT_SMI_ARG_CHECKED(status, 2); | |
150 EnqueuePromiseReactionJob(isolate, promise, info, status); | |
151 return isolate->heap()->undefined_value(); | 79 return isolate->heap()->undefined_value(); |
152 } | 80 } |
153 | 81 |
154 RUNTIME_FUNCTION(Runtime_EnqueuePromiseResolveThenableJob) { | 82 RUNTIME_FUNCTION(Runtime_EnqueuePromiseResolveThenableJob) { |
155 HandleScope scope(isolate); | 83 HandleScope scope(isolate); |
156 DCHECK(args.length() == 1); | 84 DCHECK(args.length() == 1); |
157 CONVERT_ARG_HANDLE_CHECKED(PromiseResolveThenableJobInfo, info, 0); | 85 CONVERT_ARG_HANDLE_CHECKED(PromiseResolveThenableJobInfo, info, 0); |
158 isolate->EnqueueMicrotask(info); | 86 isolate->EnqueueMicrotask(info); |
159 return isolate->heap()->undefined_value(); | 87 return isolate->heap()->undefined_value(); |
160 } | 88 } |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
212 DCHECK_EQ(1, args.length()); | 140 DCHECK_EQ(1, args.length()); |
213 CONVERT_ARG_HANDLE_CHECKED(JSPromise, promise, 0); | 141 CONVERT_ARG_HANDLE_CHECKED(JSPromise, promise, 0); |
214 isolate->RunPromiseHook(PromiseHookType::kResolve, promise, | 142 isolate->RunPromiseHook(PromiseHookType::kResolve, promise, |
215 isolate->factory()->undefined_value()); | 143 isolate->factory()->undefined_value()); |
216 return isolate->heap()->undefined_value(); | 144 return isolate->heap()->undefined_value(); |
217 } | 145 } |
218 | 146 |
219 RUNTIME_FUNCTION(Runtime_PromiseHookBefore) { | 147 RUNTIME_FUNCTION(Runtime_PromiseHookBefore) { |
220 HandleScope scope(isolate); | 148 HandleScope scope(isolate); |
221 DCHECK_EQ(1, args.length()); | 149 DCHECK_EQ(1, args.length()); |
222 CONVERT_ARG_HANDLE_CHECKED(JSPromise, promise, 0); | 150 CONVERT_ARG_HANDLE_CHECKED(JSObject, promise, 0); |
gsathya
2017/01/24 20:18:07
Why do we have this change?
kozy
2017/01/24 20:40:23
In this CL we started to call promise hooks when d
| |
223 isolate->RunPromiseHook(PromiseHookType::kBefore, promise, | 151 if (promise->IsJSPromise()) { |
224 isolate->factory()->undefined_value()); | 152 isolate->RunPromiseHook(PromiseHookType::kBefore, |
153 Handle<JSPromise>::cast(promise), | |
154 isolate->factory()->undefined_value()); | |
155 } | |
225 return isolate->heap()->undefined_value(); | 156 return isolate->heap()->undefined_value(); |
226 } | 157 } |
227 | 158 |
228 RUNTIME_FUNCTION(Runtime_PromiseHookAfter) { | 159 RUNTIME_FUNCTION(Runtime_PromiseHookAfter) { |
229 HandleScope scope(isolate); | 160 HandleScope scope(isolate); |
230 DCHECK_EQ(1, args.length()); | 161 DCHECK_EQ(1, args.length()); |
231 CONVERT_ARG_HANDLE_CHECKED(JSPromise, promise, 0); | 162 CONVERT_ARG_HANDLE_CHECKED(JSObject, promise, 0); |
232 isolate->RunPromiseHook(PromiseHookType::kAfter, promise, | 163 if (promise->IsJSPromise()) { |
233 isolate->factory()->undefined_value()); | 164 isolate->RunPromiseHook(PromiseHookType::kAfter, |
165 Handle<JSPromise>::cast(promise), | |
166 isolate->factory()->undefined_value()); | |
167 } | |
234 return isolate->heap()->undefined_value(); | 168 return isolate->heap()->undefined_value(); |
235 } | 169 } |
236 | 170 |
237 } // namespace internal | 171 } // namespace internal |
238 } // namespace v8 | 172 } // namespace v8 |
OLD | NEW |