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

Side by Side Diff: src/runtime/runtime-internal.cc

Issue 2452833003: [promises] Create runtime-promise.cc (Closed)
Patch Set: rebase Created 4 years, 1 month 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
« no previous file with comments | « BUILD.gn ('k') | src/runtime/runtime-promise.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 2014 the V8 project authors. All rights reserved. 1 // Copyright 2014 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/runtime/runtime-utils.h" 5 #include "src/runtime/runtime-utils.h"
6 6
7 #include <memory> 7 #include <memory>
8 8
9 #include "src/arguments.h" 9 #include "src/arguments.h"
10 #include "src/ast/prettyprinter.h" 10 #include "src/ast/prettyprinter.h"
11 #include "src/bootstrapper.h" 11 #include "src/bootstrapper.h"
12 #include "src/conversions.h" 12 #include "src/conversions.h"
13 #include "src/debug/debug.h" 13 #include "src/debug/debug.h"
14 #include "src/elements.h"
15 #include "src/frames-inl.h" 14 #include "src/frames-inl.h"
16 #include "src/isolate-inl.h" 15 #include "src/isolate-inl.h"
17 #include "src/messages.h" 16 #include "src/messages.h"
18 #include "src/parsing/parse-info.h" 17 #include "src/parsing/parse-info.h"
19 #include "src/parsing/parser.h" 18 #include "src/parsing/parser.h"
20 #include "src/wasm/wasm-module.h" 19 #include "src/wasm/wasm-module.h"
21 20
22 namespace v8 { 21 namespace v8 {
23 namespace internal { 22 namespace internal {
24 23
(...skipping 256 matching lines...) Expand 10 before | Expand all | Expand 10 after
281 280
282 RUNTIME_FUNCTION(Runtime_ThrowApplyNonFunction) { 281 RUNTIME_FUNCTION(Runtime_ThrowApplyNonFunction) {
283 HandleScope scope(isolate); 282 HandleScope scope(isolate);
284 DCHECK_EQ(1, args.length()); 283 DCHECK_EQ(1, args.length());
285 CONVERT_ARG_HANDLE_CHECKED(Object, object, 0); 284 CONVERT_ARG_HANDLE_CHECKED(Object, object, 0);
286 Handle<String> type = Object::TypeOf(isolate, object); 285 Handle<String> type = Object::TypeOf(isolate, object);
287 THROW_NEW_ERROR_RETURN_FAILURE( 286 THROW_NEW_ERROR_RETURN_FAILURE(
288 isolate, NewTypeError(MessageTemplate::kApplyNonFunction, object, type)); 287 isolate, NewTypeError(MessageTemplate::kApplyNonFunction, object, type));
289 } 288 }
290 289
291 namespace {
292
293 void PromiseRejectEvent(Isolate* isolate, Handle<JSObject> promise,
294 Handle<Object> rejected_promise, Handle<Object> value,
295 bool debug_event) {
296 if (isolate->debug()->is_active() && debug_event) {
297 isolate->debug()->OnPromiseReject(rejected_promise, value);
298 }
299 Handle<Symbol> key = isolate->factory()->promise_has_handler_symbol();
300 // Do not report if we actually have a handler.
301 if (JSReceiver::GetDataProperty(promise, key)->IsUndefined(isolate)) {
302 isolate->ReportPromiseReject(promise, value,
303 v8::kPromiseRejectWithNoHandler);
304 }
305 }
306
307 } // namespace
308
309 RUNTIME_FUNCTION(Runtime_PromiseRejectEvent) {
310 DCHECK(args.length() == 3);
311 HandleScope scope(isolate);
312 CONVERT_ARG_HANDLE_CHECKED(JSObject, promise, 0);
313 CONVERT_ARG_HANDLE_CHECKED(Object, value, 1);
314 CONVERT_BOOLEAN_ARG_CHECKED(debug_event, 2);
315
316 PromiseRejectEvent(isolate, promise, promise, value, debug_event);
317 return isolate->heap()->undefined_value();
318 }
319
320 RUNTIME_FUNCTION(Runtime_PromiseRejectEventFromStack) {
321 DCHECK(args.length() == 2);
322 HandleScope scope(isolate);
323 CONVERT_ARG_HANDLE_CHECKED(JSObject, promise, 0);
324 CONVERT_ARG_HANDLE_CHECKED(Object, value, 1);
325
326 Handle<Object> rejected_promise = promise;
327 if (isolate->debug()->is_active()) {
328 // If the Promise.reject call is caught, then this will return
329 // undefined, which will be interpreted by PromiseRejectEvent
330 // as being a caught exception event.
331 rejected_promise = isolate->GetPromiseOnStackOnThrow();
332 }
333 PromiseRejectEvent(isolate, promise, rejected_promise, value, true);
334 return isolate->heap()->undefined_value();
335 }
336
337 RUNTIME_FUNCTION(Runtime_PromiseRevokeReject) {
338 DCHECK(args.length() == 1);
339 HandleScope scope(isolate);
340 CONVERT_ARG_HANDLE_CHECKED(JSObject, promise, 0);
341 Handle<Symbol> key = isolate->factory()->promise_has_handler_symbol();
342 // At this point, no revocation has been issued before
343 CHECK(JSReceiver::GetDataProperty(promise, key)->IsUndefined(isolate));
344 isolate->ReportPromiseReject(promise, Handle<Object>(),
345 v8::kPromiseHandlerAddedAfterReject);
346 return isolate->heap()->undefined_value();
347 }
348
349 290
350 RUNTIME_FUNCTION(Runtime_StackGuard) { 291 RUNTIME_FUNCTION(Runtime_StackGuard) {
351 SealHandleScope shs(isolate); 292 SealHandleScope shs(isolate);
352 DCHECK(args.length() == 0); 293 DCHECK(args.length() == 0);
353 294
354 // First check if this is a real stack overflow. 295 // First check if this is a real stack overflow.
355 StackLimitCheck check(isolate); 296 StackLimitCheck check(isolate);
356 if (check.JsHasOverflowed()) { 297 if (check.JsHasOverflowed()) {
357 return isolate->StackOverflow(); 298 return isolate->StackOverflow();
358 } 299 }
(...skipping 206 matching lines...) Expand 10 before | Expand all | Expand 10 after
565 isolate->counters()->runtime_call_stats()->Print(stats_stream); 506 isolate->counters()->runtime_call_stats()->Print(stats_stream);
566 isolate->counters()->runtime_call_stats()->Reset(); 507 isolate->counters()->runtime_call_stats()->Reset();
567 if (args[0]->IsString()) 508 if (args[0]->IsString())
568 std::fclose(f); 509 std::fclose(f);
569 else 510 else
570 std::fflush(f); 511 std::fflush(f);
571 return isolate->heap()->undefined_value(); 512 return isolate->heap()->undefined_value();
572 } 513 }
573 } 514 }
574 515
575 namespace {
576 void EnqueuePromiseReactionJob(Isolate* isolate, Handle<Object> value,
577 Handle<Object> tasks, Handle<Object> deferred,
578 Handle<Object> status) {
579 Handle<Object> debug_id = isolate->factory()->undefined_value();
580 Handle<Object> debug_name = isolate->factory()->undefined_value();
581 if (isolate->debug()->is_active()) {
582 MaybeHandle<Object> maybe_result;
583 Handle<Object> argv[] = {deferred, status};
584 maybe_result = Execution::TryCall(
585 isolate, isolate->promise_debug_get_info(),
586 isolate->factory()->undefined_value(), arraysize(argv), argv);
587 Handle<Object> result;
588 if ((maybe_result).ToHandle(&result)) {
589 CHECK(result->IsJSArray());
590 Handle<JSArray> array = Handle<JSArray>::cast(result);
591 ElementsAccessor* accessor = array->GetElementsAccessor();
592 DCHECK(accessor->HasElement(array, 0));
593 DCHECK(accessor->HasElement(array, 1));
594 debug_id = accessor->Get(array, 0);
595 debug_name = accessor->Get(array, 1);
596 }
597 }
598 Handle<PromiseReactionJobInfo> info =
599 isolate->factory()->NewPromiseReactionJobInfo(value, tasks, deferred,
600 debug_id, debug_name,
601 isolate->native_context());
602 isolate->EnqueueMicrotask(info);
603 }
604
605 void PromiseFulfill(Isolate* isolate, Handle<JSReceiver> promise,
606 Handle<Smi> status, Handle<Object> value,
607 Handle<Symbol> reaction) {
608 Handle<Object> tasks = JSReceiver::GetDataProperty(promise, reaction);
609 if (!tasks->IsUndefined(isolate)) {
610 Handle<Object> deferred = JSReceiver::GetDataProperty(
611 promise, isolate->factory()->promise_deferred_reaction_symbol());
612 EnqueuePromiseReactionJob(isolate, value, tasks, deferred, status);
613 }
614 }
615 } // namespace
616
617 RUNTIME_FUNCTION(Runtime_PromiseReject) {
618 DCHECK(args.length() == 3);
619 HandleScope scope(isolate);
620 CONVERT_ARG_HANDLE_CHECKED(JSObject, promise, 0);
621 CONVERT_ARG_HANDLE_CHECKED(Object, reason, 1);
622 CONVERT_BOOLEAN_ARG_CHECKED(debug_event, 2);
623
624 PromiseRejectEvent(isolate, promise, promise, reason, debug_event);
625
626 Handle<Smi> status = handle(Smi::FromInt(kPromiseRejected), isolate);
627 Handle<Symbol> reaction =
628 isolate->factory()->promise_reject_reactions_symbol();
629 PromiseFulfill(isolate, promise, status, reason, reaction);
630 return isolate->heap()->undefined_value();
631 }
632
633 RUNTIME_FUNCTION(Runtime_PromiseFulfill) {
634 DCHECK(args.length() == 4);
635 HandleScope scope(isolate);
636 CONVERT_ARG_HANDLE_CHECKED(JSReceiver, promise, 0);
637 CONVERT_ARG_HANDLE_CHECKED(Smi, status, 1);
638 CONVERT_ARG_HANDLE_CHECKED(Object, value, 2);
639 CONVERT_ARG_HANDLE_CHECKED(Symbol, reaction, 3);
640 PromiseFulfill(isolate, promise, status, value, reaction);
641 return isolate->heap()->undefined_value();
642 }
643
644 RUNTIME_FUNCTION(Runtime_EnqueuePromiseReactionJob) {
645 HandleScope scope(isolate);
646 DCHECK(args.length() == 4);
647 CONVERT_ARG_HANDLE_CHECKED(Object, value, 0);
648 CONVERT_ARG_HANDLE_CHECKED(Object, tasks, 1);
649 CONVERT_ARG_HANDLE_CHECKED(Object, deferred, 2);
650 CONVERT_ARG_HANDLE_CHECKED(Object, status, 3);
651 EnqueuePromiseReactionJob(isolate, value, tasks, deferred, status);
652 return isolate->heap()->undefined_value();
653 }
654
655 RUNTIME_FUNCTION(Runtime_EnqueuePromiseResolveThenableJob) {
656 HandleScope scope(isolate);
657 DCHECK(args.length() == 4);
658 CONVERT_ARG_HANDLE_CHECKED(JSReceiver, resolution, 0);
659 CONVERT_ARG_HANDLE_CHECKED(JSReceiver, then, 1);
660 CONVERT_ARG_HANDLE_CHECKED(JSFunction, resolve, 2);
661 CONVERT_ARG_HANDLE_CHECKED(JSFunction, reject, 3);
662 Handle<Object> debug_id;
663 Handle<Object> debug_name;
664 if (isolate->debug()->is_active()) {
665 debug_id =
666 handle(Smi::FromInt(isolate->GetNextDebugMicrotaskId()), isolate);
667 debug_name = isolate->factory()->PromiseResolveThenableJob_string();
668 isolate->debug()->OnAsyncTaskEvent(isolate->factory()->enqueue_string(),
669 debug_id,
670 Handle<String>::cast(debug_name));
671 } else {
672 debug_id = isolate->factory()->undefined_value();
673 debug_name = isolate->factory()->undefined_value();
674 }
675 Handle<PromiseResolveThenableJobInfo> info =
676 isolate->factory()->NewPromiseResolveThenableJobInfo(
677 resolution, then, resolve, reject, debug_id, debug_name);
678 isolate->EnqueueMicrotask(info);
679 return isolate->heap()->undefined_value();
680 }
681
682 RUNTIME_FUNCTION(Runtime_EnqueueMicrotask) {
683 HandleScope scope(isolate);
684 DCHECK(args.length() == 1);
685 CONVERT_ARG_HANDLE_CHECKED(JSFunction, microtask, 0);
686 isolate->EnqueueMicrotask(microtask);
687 return isolate->heap()->undefined_value();
688 }
689
690 RUNTIME_FUNCTION(Runtime_RunMicrotasks) {
691 HandleScope scope(isolate);
692 DCHECK(args.length() == 0);
693 isolate->RunMicrotasks();
694 return isolate->heap()->undefined_value();
695 }
696
697 RUNTIME_FUNCTION(Runtime_OrdinaryHasInstance) { 516 RUNTIME_FUNCTION(Runtime_OrdinaryHasInstance) {
698 HandleScope scope(isolate); 517 HandleScope scope(isolate);
699 DCHECK_EQ(2, args.length()); 518 DCHECK_EQ(2, args.length());
700 CONVERT_ARG_HANDLE_CHECKED(Object, callable, 0); 519 CONVERT_ARG_HANDLE_CHECKED(Object, callable, 0);
701 CONVERT_ARG_HANDLE_CHECKED(Object, object, 1); 520 CONVERT_ARG_HANDLE_CHECKED(Object, object, 1);
702 RETURN_RESULT_OR_FAILURE( 521 RETURN_RESULT_OR_FAILURE(
703 isolate, Object::OrdinaryHasInstance(isolate, callable, object)); 522 isolate, Object::OrdinaryHasInstance(isolate, callable, object));
704 } 523 }
705 524
706 RUNTIME_FUNCTION(Runtime_IsWasmInstance) { 525 RUNTIME_FUNCTION(Runtime_IsWasmInstance) {
707 HandleScope scope(isolate); 526 HandleScope scope(isolate);
708 DCHECK_EQ(1, args.length()); 527 DCHECK_EQ(1, args.length());
709 CONVERT_ARG_CHECKED(Object, object, 0); 528 CONVERT_ARG_CHECKED(Object, object, 0);
710 bool is_wasm_instance = 529 bool is_wasm_instance =
711 object->IsJSObject() && wasm::IsWasmInstance(JSObject::cast(object)); 530 object->IsJSObject() && wasm::IsWasmInstance(JSObject::cast(object));
712 return *isolate->factory()->ToBoolean(is_wasm_instance); 531 return *isolate->factory()->ToBoolean(is_wasm_instance);
713 } 532 }
714 533
715 RUNTIME_FUNCTION(Runtime_Typeof) { 534 RUNTIME_FUNCTION(Runtime_Typeof) {
716 HandleScope scope(isolate); 535 HandleScope scope(isolate);
717 DCHECK_EQ(1, args.length()); 536 DCHECK_EQ(1, args.length());
718 CONVERT_ARG_HANDLE_CHECKED(Object, object, 0); 537 CONVERT_ARG_HANDLE_CHECKED(Object, object, 0);
719 return *Object::TypeOf(isolate, object); 538 return *Object::TypeOf(isolate, object);
720 } 539 }
721 540
722 } // namespace internal 541 } // namespace internal
723 } // namespace v8 542 } // namespace v8
OLDNEW
« no previous file with comments | « BUILD.gn ('k') | src/runtime/runtime-promise.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698