Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(96)

Side by Side Diff: src/isolate.cc

Issue 294943009: Allow microtasks to throw exceptions and handle them gracefully (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Use existing Suppression scope Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « include/v8.h ('k') | test/cctest/test-api.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2012 the V8 project authors. All rights reserved. 1 // Copyright 2012 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 <stdlib.h> 5 #include <stdlib.h>
6 6
7 #include "v8.h" 7 #include "v8.h"
8 8
9 #include "ast.h" 9 #include "ast.h"
10 #include "bootstrapper.h" 10 #include "bootstrapper.h"
(...skipping 2216 matching lines...) Expand 10 before | Expand all | Expand 10 after
2227 2227
2228 2228
2229 void Isolate::FireCallCompletedCallback() { 2229 void Isolate::FireCallCompletedCallback() {
2230 bool has_call_completed_callbacks = !call_completed_callbacks_.is_empty(); 2230 bool has_call_completed_callbacks = !call_completed_callbacks_.is_empty();
2231 bool run_microtasks = autorun_microtasks() && pending_microtask_count(); 2231 bool run_microtasks = autorun_microtasks() && pending_microtask_count();
2232 if (!has_call_completed_callbacks && !run_microtasks) return; 2232 if (!has_call_completed_callbacks && !run_microtasks) return;
2233 2233
2234 if (!handle_scope_implementer()->CallDepthIsZero()) return; 2234 if (!handle_scope_implementer()->CallDepthIsZero()) return;
2235 if (run_microtasks) RunMicrotasks(); 2235 if (run_microtasks) RunMicrotasks();
2236 // Fire callbacks. Increase call depth to prevent recursive callbacks. 2236 // Fire callbacks. Increase call depth to prevent recursive callbacks.
2237 handle_scope_implementer()->IncrementCallDepth(); 2237 v8::Isolate::SuppressMicrotaskExecutionScope suppress(
2238 reinterpret_cast<v8::Isolate*>(this));
2238 for (int i = 0; i < call_completed_callbacks_.length(); i++) { 2239 for (int i = 0; i < call_completed_callbacks_.length(); i++) {
2239 call_completed_callbacks_.at(i)(); 2240 call_completed_callbacks_.at(i)();
2240 } 2241 }
2241 handle_scope_implementer()->DecrementCallDepth();
2242 } 2242 }
2243 2243
2244 2244
2245 void Isolate::EnqueueMicrotask(Handle<JSFunction> microtask) { 2245 void Isolate::EnqueueMicrotask(Handle<JSFunction> microtask) {
2246 Handle<FixedArray> queue(heap()->microtask_queue(), this); 2246 Handle<FixedArray> queue(heap()->microtask_queue(), this);
2247 int num_tasks = pending_microtask_count(); 2247 int num_tasks = pending_microtask_count();
2248 ASSERT(num_tasks <= queue->length()); 2248 ASSERT(num_tasks <= queue->length());
2249 if (num_tasks == 0) { 2249 if (num_tasks == 0) {
2250 queue = factory()->NewFixedArray(8); 2250 queue = factory()->NewFixedArray(8);
2251 heap()->set_microtask_queue(*queue); 2251 heap()->set_microtask_queue(*queue);
2252 } else if (num_tasks == queue->length()) { 2252 } else if (num_tasks == queue->length()) {
2253 queue = FixedArray::CopySize(queue, num_tasks * 2); 2253 queue = FixedArray::CopySize(queue, num_tasks * 2);
2254 heap()->set_microtask_queue(*queue); 2254 heap()->set_microtask_queue(*queue);
2255 } 2255 }
2256 ASSERT(queue->get(num_tasks)->IsUndefined()); 2256 ASSERT(queue->get(num_tasks)->IsUndefined());
2257 queue->set(num_tasks, *microtask); 2257 queue->set(num_tasks, *microtask);
2258 set_pending_microtask_count(num_tasks + 1); 2258 set_pending_microtask_count(num_tasks + 1);
2259 } 2259 }
2260 2260
2261 2261
2262 void Isolate::RunMicrotasks() { 2262 void Isolate::RunMicrotasks() {
2263 // TODO(adamk): This ASSERT triggers in mjsunit tests which 2263 // TODO(adamk): This ASSERT triggers in mjsunit tests which
2264 // call the %RunMicrotasks runtime function. But it should 2264 // call the %RunMicrotasks runtime function. But it should
2265 // never happen outside of tests, so it would be nice to 2265 // never happen outside of tests, so it would be nice to
2266 // uncomment it. 2266 // uncomment it.
2267 // 2267 //
2268 // ASSERT(handle_scope_implementer()->CallDepthIsZero()); 2268 // ASSERT(handle_scope_implementer()->CallDepthIsZero());
2269 2269
2270 // Increase call depth to prevent recursive callbacks. 2270 // Increase call depth to prevent recursive callbacks.
2271 handle_scope_implementer()->IncrementCallDepth(); 2271 v8::Isolate::SuppressMicrotaskExecutionScope suppress(
2272 reinterpret_cast<v8::Isolate*>(this));
2272 2273
2273 while (pending_microtask_count() > 0) { 2274 while (pending_microtask_count() > 0) {
2274 HandleScope scope(this); 2275 HandleScope scope(this);
2275 int num_tasks = pending_microtask_count(); 2276 int num_tasks = pending_microtask_count();
2276 Handle<FixedArray> queue(heap()->microtask_queue(), this); 2277 Handle<FixedArray> queue(heap()->microtask_queue(), this);
2277 ASSERT(num_tasks <= queue->length()); 2278 ASSERT(num_tasks <= queue->length());
2278 set_pending_microtask_count(0); 2279 set_pending_microtask_count(0);
2279 heap()->set_microtask_queue(heap()->empty_fixed_array()); 2280 heap()->set_microtask_queue(heap()->empty_fixed_array());
2280 2281
2281 for (int i = 0; i < num_tasks; i++) { 2282 for (int i = 0; i < num_tasks; i++) {
2282 HandleScope scope(this); 2283 HandleScope scope(this);
2283 Handle<JSFunction> microtask(JSFunction::cast(queue->get(i)), this); 2284 Handle<JSFunction> microtask(JSFunction::cast(queue->get(i)), this);
2284 // TODO(adamk): This should ignore/clear exceptions instead of Checking. 2285 Handle<Object> exception;
2285 Execution::Call(this, microtask, factory()->undefined_value(), 2286 MaybeHandle<Object> result = Execution::TryCall(
2286 0, NULL).Check(); 2287 microtask, factory()->undefined_value(), 0, NULL, &exception);
2288 // If execution is terminating, just bail out.
2289 if (result.is_null() &&
2290 !exception.is_null() &&
2291 *exception == heap()->termination_exception()) {
2292 // Clear out any remaining callbacks in the queue.
2293 heap()->set_microtask_queue(heap()->empty_fixed_array());
2294 set_pending_microtask_count(0);
2295 return;
2296 }
2287 } 2297 }
2288 } 2298 }
2289
2290 handle_scope_implementer()->DecrementCallDepth();
2291 } 2299 }
2292 2300
2293 2301
2294 } } // namespace v8::internal 2302 } } // namespace v8::internal
OLDNEW
« no previous file with comments | « include/v8.h ('k') | test/cctest/test-api.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698