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

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

Issue 2452833003: [promises] Create runtime-promise.cc (Closed)
Patch Set: adding dependency 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
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<JSReceiver> 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(Handle<JSObject>::cast(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
350 RUNTIME_FUNCTION(Runtime_StackGuard) { 290 RUNTIME_FUNCTION(Runtime_StackGuard) {
351 SealHandleScope shs(isolate); 291 SealHandleScope shs(isolate);
352 DCHECK(args.length() == 0); 292 DCHECK(args.length() == 0);
353 293
354 // First check if this is a real stack overflow. 294 // First check if this is a real stack overflow.
355 StackLimitCheck check(isolate); 295 StackLimitCheck check(isolate);
356 if (check.JsHasOverflowed()) { 296 if (check.JsHasOverflowed()) {
357 return isolate->StackOverflow(); 297 return isolate->StackOverflow();
358 } 298 }
359 299
(...skipping 205 matching lines...) Expand 10 before | Expand all | Expand 10 after
565 isolate->counters()->runtime_call_stats()->Print(stats_stream); 505 isolate->counters()->runtime_call_stats()->Print(stats_stream);
566 isolate->counters()->runtime_call_stats()->Reset(); 506 isolate->counters()->runtime_call_stats()->Reset();
567 if (args[0]->IsString()) 507 if (args[0]->IsString())
568 std::fclose(f); 508 std::fclose(f);
569 else 509 else
570 std::fflush(f); 510 std::fflush(f);
571 return isolate->heap()->undefined_value(); 511 return isolate->heap()->undefined_value();
572 } 512 }
573 } 513 }
574 514
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(JSReceiver, 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 // This value should be kept in sync with kRejected in promise.js
627 const int kRejected = 2;
628
629 Handle<Smi> status = handle(Smi::FromInt(kRejected), isolate);
630 Handle<Symbol> reaction =
631 isolate->factory()->promise_reject_reactions_symbol();
632 PromiseFulfill(isolate, promise, status, reason, reaction);
633 return isolate->heap()->undefined_value();
634 }
635
636 RUNTIME_FUNCTION(Runtime_PromiseFulfill) {
637 DCHECK(args.length() == 4);
638 HandleScope scope(isolate);
639 CONVERT_ARG_HANDLE_CHECKED(JSReceiver, promise, 0);
640 CONVERT_ARG_HANDLE_CHECKED(Smi, status, 1);
641 CONVERT_ARG_HANDLE_CHECKED(Object, value, 2);
642 CONVERT_ARG_HANDLE_CHECKED(Symbol, reaction, 3);
643 PromiseFulfill(isolate, promise, status, value, reaction);
644 return isolate->heap()->undefined_value();
645 }
646
647 RUNTIME_FUNCTION(Runtime_EnqueuePromiseReactionJob) {
648 HandleScope scope(isolate);
649 DCHECK(args.length() == 4);
650 CONVERT_ARG_HANDLE_CHECKED(Object, value, 0);
651 CONVERT_ARG_HANDLE_CHECKED(Object, tasks, 1);
652 CONVERT_ARG_HANDLE_CHECKED(Object, deferred, 2);
653 CONVERT_ARG_HANDLE_CHECKED(Object, status, 3);
654 EnqueuePromiseReactionJob(isolate, value, tasks, deferred, status);
655 return isolate->heap()->undefined_value();
656 }
657
658 RUNTIME_FUNCTION(Runtime_EnqueuePromiseResolveThenableJob) {
659 HandleScope scope(isolate);
660 DCHECK(args.length() == 4);
661 CONVERT_ARG_HANDLE_CHECKED(JSReceiver, resolution, 0);
662 CONVERT_ARG_HANDLE_CHECKED(JSReceiver, then, 1);
663 CONVERT_ARG_HANDLE_CHECKED(JSFunction, resolve, 2);
664 CONVERT_ARG_HANDLE_CHECKED(JSFunction, reject, 3);
665 Handle<Object> debug_id;
666 Handle<Object> debug_name;
667 if (isolate->debug()->is_active()) {
668 debug_id =
669 handle(Smi::FromInt(isolate->GetNextDebugMicrotaskId()), isolate);
670 debug_name = isolate->factory()->PromiseResolveThenableJob_string();
671 isolate->debug()->OnAsyncTaskEvent(isolate->factory()->enqueue_string(),
672 debug_id,
673 Handle<String>::cast(debug_name));
674 } else {
675 debug_id = isolate->factory()->undefined_value();
676 debug_name = isolate->factory()->undefined_value();
677 }
678 Handle<PromiseResolveThenableJobInfo> info =
679 isolate->factory()->NewPromiseResolveThenableJobInfo(
680 resolution, then, resolve, reject, debug_id, debug_name);
681 isolate->EnqueueMicrotask(info);
682 return isolate->heap()->undefined_value();
683 }
684
685 RUNTIME_FUNCTION(Runtime_EnqueueMicrotask) {
686 HandleScope scope(isolate);
687 DCHECK(args.length() == 1);
688 CONVERT_ARG_HANDLE_CHECKED(JSFunction, microtask, 0);
689 isolate->EnqueueMicrotask(microtask);
690 return isolate->heap()->undefined_value();
691 }
692
693 RUNTIME_FUNCTION(Runtime_RunMicrotasks) {
694 HandleScope scope(isolate);
695 DCHECK(args.length() == 0);
696 isolate->RunMicrotasks();
697 return isolate->heap()->undefined_value();
698 }
699
700 RUNTIME_FUNCTION(Runtime_OrdinaryHasInstance) { 515 RUNTIME_FUNCTION(Runtime_OrdinaryHasInstance) {
701 HandleScope scope(isolate); 516 HandleScope scope(isolate);
702 DCHECK_EQ(2, args.length()); 517 DCHECK_EQ(2, args.length());
703 CONVERT_ARG_HANDLE_CHECKED(Object, callable, 0); 518 CONVERT_ARG_HANDLE_CHECKED(Object, callable, 0);
704 CONVERT_ARG_HANDLE_CHECKED(Object, object, 1); 519 CONVERT_ARG_HANDLE_CHECKED(Object, object, 1);
705 RETURN_RESULT_OR_FAILURE( 520 RETURN_RESULT_OR_FAILURE(
706 isolate, Object::OrdinaryHasInstance(isolate, callable, object)); 521 isolate, Object::OrdinaryHasInstance(isolate, callable, object));
707 } 522 }
708 523
709 RUNTIME_FUNCTION(Runtime_IsWasmInstance) { 524 RUNTIME_FUNCTION(Runtime_IsWasmInstance) {
710 HandleScope scope(isolate); 525 HandleScope scope(isolate);
711 DCHECK_EQ(1, args.length()); 526 DCHECK_EQ(1, args.length());
712 CONVERT_ARG_CHECKED(Object, object, 0); 527 CONVERT_ARG_CHECKED(Object, object, 0);
713 bool is_wasm_instance = 528 bool is_wasm_instance =
714 object->IsJSObject() && wasm::IsWasmInstance(JSObject::cast(object)); 529 object->IsJSObject() && wasm::IsWasmInstance(JSObject::cast(object));
715 return *isolate->factory()->ToBoolean(is_wasm_instance); 530 return *isolate->factory()->ToBoolean(is_wasm_instance);
716 } 531 }
717 532
718 RUNTIME_FUNCTION(Runtime_Typeof) { 533 RUNTIME_FUNCTION(Runtime_Typeof) {
719 HandleScope scope(isolate); 534 HandleScope scope(isolate);
720 DCHECK_EQ(1, args.length()); 535 DCHECK_EQ(1, args.length());
721 CONVERT_ARG_HANDLE_CHECKED(Object, object, 0); 536 CONVERT_ARG_HANDLE_CHECKED(Object, object, 0);
722 return *Object::TypeOf(isolate, object); 537 return *Object::TypeOf(isolate, object);
723 } 538 }
724 539
725 } // namespace internal 540 } // namespace internal
726 } // namespace v8 541 } // namespace v8
OLDNEW
« no previous file with comments | « BUILD.gn ('k') | src/runtime/runtime-promise.cc » ('j') | src/runtime/runtime-promise.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698