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

Side by Side Diff: src/api.cc

Issue 962983002: Convert v8::Value::To* to use MaybeLocal (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Created 5 years, 9 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
« no previous file with comments | « src/api.h ('k') | no next file » | 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 "src/api.h" 5 #include "src/api.h"
6 6
7 #include <string.h> // For memcpy, strlen. 7 #include <string.h> // For memcpy, strlen.
8 #ifdef V8_USE_ADDRESS_SANITIZER 8 #ifdef V8_USE_ADDRESS_SANITIZER
9 #include <sanitizer/asan_interface.h> 9 #include <sanitizer/asan_interface.h>
10 #endif // V8_USE_ADDRESS_SANITIZER 10 #endif // V8_USE_ADDRESS_SANITIZER
(...skipping 2609 matching lines...) Expand 10 before | Expand all | Expand 10 after
2620 bool Value::IsMapIterator() const { 2620 bool Value::IsMapIterator() const {
2621 return Utils::OpenHandle(this)->IsJSMapIterator(); 2621 return Utils::OpenHandle(this)->IsJSMapIterator();
2622 } 2622 }
2623 2623
2624 2624
2625 bool Value::IsSetIterator() const { 2625 bool Value::IsSetIterator() const {
2626 return Utils::OpenHandle(this)->IsJSSetIterator(); 2626 return Utils::OpenHandle(this)->IsJSSetIterator();
2627 } 2627 }
2628 2628
2629 2629
2630 Local<String> Value::ToString(Isolate* v8_isolate) const { 2630 #define CONTEXT_SCOPE_GET_ISOLATE(context, function_name) \
2631 i::Handle<i::Object> obj = Utils::OpenHandle(this); 2631 v8::Context::Scope context_scope(context); \
2632 i::Handle<i::Object> str; 2632 auto isolate = reinterpret_cast<i::Isolate*>(context->GetIsolate()); \
2633 if (obj->IsString()) { 2633 LOG_API(isolate, function_name); \
2634 str = obj; 2634 ENTER_V8(isolate);
2635 } else { 2635
2636 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(v8_isolate); 2636
2637 LOG_API(isolate, "ToString"); 2637 #define RETURN_TO_LOCAL_UNCHECKED(maybe_local, T) \
2638 ENTER_V8(isolate); 2638 do { \
2639 EXCEPTION_PREAMBLE(isolate); 2639 Local<T> result; \
2640 has_pending_exception = !i::Execution::ToString( 2640 bool ignored = maybe_local.ToLocal(&result); \
2641 isolate, obj).ToHandle(&str); 2641 USE(ignored); \
2642 EXCEPTION_BAILOUT_CHECK(isolate, Local<String>()); 2642 return result; \
2643 } 2643 } while (false);
2644 return ToApiHandle<String>(str); 2644
2645
2646 MaybeLocal<String> Value::ToString(Local<Context> context) const {
2647 auto obj = Utils::OpenHandle(this);
2648 if (obj->IsString()) return ToApiHandle<String>(obj);
2649 CONTEXT_SCOPE_GET_ISOLATE(context, "ToString");
2650 EXCEPTION_PREAMBLE(isolate);
2651 Local<String> result;
2652 has_pending_exception =
2653 !ToLocal<String>(i::Execution::ToString(isolate, obj), &result);
2654 EXCEPTION_BAILOUT_CHECK(isolate, result);
2655 return result;
2645 } 2656 }
2646 2657
2647 2658
2648 Local<String> Value::ToDetailString(Isolate* v8_isolate) const { 2659 Local<String> Value::ToString(Isolate* isolate) const {
2649 i::Handle<i::Object> obj = Utils::OpenHandle(this); 2660 RETURN_TO_LOCAL_UNCHECKED(ToString(isolate->GetCurrentContext()), String);
2650 i::Handle<i::Object> str;
2651 if (obj->IsString()) {
2652 str = obj;
2653 } else {
2654 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(v8_isolate);
2655 LOG_API(isolate, "ToDetailString");
2656 ENTER_V8(isolate);
2657 EXCEPTION_PREAMBLE(isolate);
2658 has_pending_exception = !i::Execution::ToDetailString(
2659 isolate, obj).ToHandle(&str);
2660 EXCEPTION_BAILOUT_CHECK(isolate, Local<String>());
2661 }
2662 return ToApiHandle<String>(str);
2663 } 2661 }
2664 2662
2665 2663
2666 Local<v8::Object> Value::ToObject(Isolate* v8_isolate) const { 2664 MaybeLocal<String> Value::ToDetailString(Local<Context> context) const {
2667 i::Handle<i::Object> obj = Utils::OpenHandle(this); 2665 auto obj = Utils::OpenHandle(this);
2668 i::Handle<i::Object> val; 2666 if (obj->IsString()) return ToApiHandle<String>(obj);
2669 if (obj->IsJSObject()) { 2667 CONTEXT_SCOPE_GET_ISOLATE(context, "ToDetailString");
2670 val = obj; 2668 EXCEPTION_PREAMBLE(isolate);
2671 } else { 2669 Local<String> result;
2672 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(v8_isolate); 2670 has_pending_exception =
2673 LOG_API(isolate, "ToObject"); 2671 !ToLocal<String>(i::Execution::ToDetailString(isolate, obj), &result);
2674 ENTER_V8(isolate); 2672 EXCEPTION_BAILOUT_CHECK(isolate, result);
2675 EXCEPTION_PREAMBLE(isolate); 2673 return result;
2676 has_pending_exception = !i::Execution::ToObject( 2674 }
2677 isolate, obj).ToHandle(&val); 2675
2678 EXCEPTION_BAILOUT_CHECK(isolate, Local<v8::Object>()); 2676
2679 } 2677 Local<String> Value::ToDetailString(Isolate* isolate) const {
2680 return ToApiHandle<Object>(val); 2678 RETURN_TO_LOCAL_UNCHECKED(ToDetailString(isolate->GetCurrentContext()),
2679 String);
2680 }
2681
2682
2683 MaybeLocal<Object> Value::ToObject(Local<Context> context) const {
2684 auto obj = Utils::OpenHandle(this);
2685 if (obj->IsJSObject()) return ToApiHandle<Object>(obj);
2686 CONTEXT_SCOPE_GET_ISOLATE(context, "ToObject");
2687 EXCEPTION_PREAMBLE(isolate);
2688 Local<Object> result;
2689 has_pending_exception =
2690 !ToLocal<Object>(i::Execution::ToObject(isolate, obj), &result);
2691 EXCEPTION_BAILOUT_CHECK(isolate, result);
2692 return result;
2693 }
2694
2695
2696 Local<v8::Object> Value::ToObject(Isolate* isolate) const {
2697 RETURN_TO_LOCAL_UNCHECKED(ToObject(isolate->GetCurrentContext()), Object);
2698 }
2699
2700
2701 MaybeLocal<Boolean> Value::ToBoolean(Local<Context> context) const {
2702 auto obj = Utils::OpenHandle(this);
2703 if (obj->IsBoolean()) return ToApiHandle<Boolean>(obj);
2704 auto isolate = reinterpret_cast<i::Isolate*>(context->GetIsolate());
2705 auto val = isolate->factory()->ToBoolean(obj->BooleanValue());
2706 return ToApiHandle<Boolean>(val);
2681 } 2707 }
2682 2708
2683 2709
2684 Local<Boolean> Value::ToBoolean(Isolate* v8_isolate) const { 2710 Local<Boolean> Value::ToBoolean(Isolate* v8_isolate) const {
2685 i::Handle<i::Object> obj = Utils::OpenHandle(this); 2711 return ToBoolean(v8_isolate->GetCurrentContext()).ToLocalChecked();
2686 if (obj->IsBoolean()) {
2687 return ToApiHandle<Boolean>(obj);
2688 } else {
2689 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(v8_isolate);
2690 LOG_API(isolate, "ToBoolean");
2691 ENTER_V8(isolate);
2692 i::Handle<i::Object> val =
2693 isolate->factory()->ToBoolean(obj->BooleanValue());
2694 return ToApiHandle<Boolean>(val);
2695 }
2696 } 2712 }
2697 2713
2698 2714
2699 Local<Number> Value::ToNumber(Isolate* v8_isolate) const { 2715 MaybeLocal<Number> Value::ToNumber(Local<Context> context) const {
2700 i::Handle<i::Object> obj = Utils::OpenHandle(this); 2716 auto obj = Utils::OpenHandle(this);
2701 i::Handle<i::Object> num; 2717 if (obj->IsNumber()) return ToApiHandle<Number>(obj);
2702 if (obj->IsNumber()) { 2718 CONTEXT_SCOPE_GET_ISOLATE(context, "ToNumber");
2703 num = obj; 2719 EXCEPTION_PREAMBLE(isolate);
2704 } else { 2720 Local<Number> result;
2705 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(v8_isolate); 2721 has_pending_exception =
2706 LOG_API(isolate, "ToNumber"); 2722 !ToLocal<Number>(i::Execution::ToNumber(isolate, obj), &result);
2707 ENTER_V8(isolate); 2723 EXCEPTION_BAILOUT_CHECK(isolate, result);
2708 EXCEPTION_PREAMBLE(isolate); 2724 return result;
2709 has_pending_exception = !i::Execution::ToNumber(
2710 isolate, obj).ToHandle(&num);
2711 EXCEPTION_BAILOUT_CHECK(isolate, Local<Number>());
2712 }
2713 return ToApiHandle<Number>(num);
2714 } 2725 }
2715 2726
2716 2727
2717 Local<Integer> Value::ToInteger(Isolate* v8_isolate) const { 2728 Local<Number> Value::ToNumber(Isolate* isolate) const {
2718 i::Handle<i::Object> obj = Utils::OpenHandle(this); 2729 RETURN_TO_LOCAL_UNCHECKED(ToNumber(isolate->GetCurrentContext()), Number);
2719 i::Handle<i::Object> num;
2720 if (obj->IsSmi()) {
2721 num = obj;
2722 } else {
2723 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(v8_isolate);
2724 LOG_API(isolate, "ToInteger");
2725 ENTER_V8(isolate);
2726 EXCEPTION_PREAMBLE(isolate);
2727 has_pending_exception = !i::Execution::ToInteger(
2728 isolate, obj).ToHandle(&num);
2729 EXCEPTION_BAILOUT_CHECK(isolate, Local<Integer>());
2730 }
2731 return ToApiHandle<Integer>(num);
2732 } 2730 }
2733 2731
2734 2732
2733 MaybeLocal<Integer> Value::ToInteger(Local<Context> context) const {
2734 auto obj = Utils::OpenHandle(this);
2735 if (obj->IsSmi()) return ToApiHandle<Integer>(obj);
2736 CONTEXT_SCOPE_GET_ISOLATE(context, "ToInteger");
2737 EXCEPTION_PREAMBLE(isolate);
2738 Local<Integer> result;
2739 has_pending_exception =
2740 !ToLocal<Integer>(i::Execution::ToInteger(isolate, obj), &result);
2741 EXCEPTION_BAILOUT_CHECK(isolate, result);
2742 return result;
2743 }
2744
2745
2746 Local<Integer> Value::ToInteger(Isolate* isolate) const {
2747 RETURN_TO_LOCAL_UNCHECKED(ToInteger(isolate->GetCurrentContext()), Integer);
2748 }
2749
2750
2751 MaybeLocal<Int32> Value::ToInt32(Local<Context> context) const {
2752 auto obj = Utils::OpenHandle(this);
2753 if (obj->IsSmi()) return ToApiHandle<Int32>(obj);
2754 CONTEXT_SCOPE_GET_ISOLATE(context, "ToInt32");
2755 EXCEPTION_PREAMBLE(isolate);
2756 Local<Int32> result;
2757 has_pending_exception =
2758 !ToLocal<Int32>(i::Execution::ToInt32(isolate, obj), &result);
2759 EXCEPTION_BAILOUT_CHECK(isolate, result);
2760 return result;
2761 }
2762
2763
2764 Local<Int32> Value::ToInt32(Isolate* isolate) const {
2765 RETURN_TO_LOCAL_UNCHECKED(ToInt32(isolate->GetCurrentContext()), Int32);
2766 }
2767
2768
2769 MaybeLocal<Uint32> Value::ToUint32(Local<Context> context) const {
2770 auto obj = Utils::OpenHandle(this);
2771 if (obj->IsSmi()) return ToApiHandle<Uint32>(obj);
2772 CONTEXT_SCOPE_GET_ISOLATE(context, "ToUInt32");
2773 EXCEPTION_PREAMBLE(isolate);
2774 Local<Uint32> result;
2775 has_pending_exception =
2776 !ToLocal<Uint32>(i::Execution::ToUint32(isolate, obj), &result);
2777 EXCEPTION_BAILOUT_CHECK(isolate, result);
2778 return result;
2779 }
2780
2781
2782 Local<Uint32> Value::ToUint32(Isolate* isolate) const {
2783 RETURN_TO_LOCAL_UNCHECKED(ToUint32(isolate->GetCurrentContext()), Uint32);
2784 }
2785
2786
2735 void i::Internals::CheckInitializedImpl(v8::Isolate* external_isolate) { 2787 void i::Internals::CheckInitializedImpl(v8::Isolate* external_isolate) {
2736 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(external_isolate); 2788 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(external_isolate);
2737 Utils::ApiCheck(isolate != NULL && 2789 Utils::ApiCheck(isolate != NULL &&
2738 !isolate->IsDead(), 2790 !isolate->IsDead(),
2739 "v8::internal::Internals::CheckInitialized()", 2791 "v8::internal::Internals::CheckInitialized()",
2740 "Isolate is not initialized or V8 has died"); 2792 "Isolate is not initialized or V8 has died");
2741 } 2793 }
2742 2794
2743 2795
2744 void External::CheckCast(v8::Value* that) { 2796 void External::CheckCast(v8::Value* that) {
(...skipping 230 matching lines...) Expand 10 before | Expand all | Expand 10 after
2975 EXCEPTION_BAILOUT_CHECK(isolate, 0); 3027 EXCEPTION_BAILOUT_CHECK(isolate, 0);
2976 } 3028 }
2977 if (num->IsSmi()) { 3029 if (num->IsSmi()) {
2978 return i::Smi::cast(*num)->value(); 3030 return i::Smi::cast(*num)->value();
2979 } else { 3031 } else {
2980 return static_cast<int64_t>(num->Number()); 3032 return static_cast<int64_t>(num->Number());
2981 } 3033 }
2982 } 3034 }
2983 3035
2984 3036
2985 Local<Int32> Value::ToInt32(Isolate* v8_isolate) const {
2986 i::Handle<i::Object> obj = Utils::OpenHandle(this);
2987 i::Handle<i::Object> num;
2988 if (obj->IsSmi()) {
2989 num = obj;
2990 } else {
2991 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(v8_isolate);
2992 LOG_API(isolate, "ToInt32");
2993 ENTER_V8(isolate);
2994 EXCEPTION_PREAMBLE(isolate);
2995 has_pending_exception = !i::Execution::ToInt32(isolate, obj).ToHandle(&num);
2996 EXCEPTION_BAILOUT_CHECK(isolate, Local<Int32>());
2997 }
2998 return ToApiHandle<Int32>(num);
2999 }
3000
3001
3002 Local<Uint32> Value::ToUint32(Isolate* v8_isolate) const {
3003 i::Handle<i::Object> obj = Utils::OpenHandle(this);
3004 i::Handle<i::Object> num;
3005 if (obj->IsSmi()) {
3006 num = obj;
3007 } else {
3008 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(v8_isolate);
3009 LOG_API(isolate, "ToUInt32");
3010 ENTER_V8(isolate);
3011 EXCEPTION_PREAMBLE(isolate);
3012 has_pending_exception = !i::Execution::ToUint32(
3013 isolate, obj).ToHandle(&num);
3014 EXCEPTION_BAILOUT_CHECK(isolate, Local<Uint32>());
3015 }
3016 return ToApiHandle<Uint32>(num);
3017 }
3018
3019
3020 Local<Uint32> Value::ToArrayIndex() const { 3037 Local<Uint32> Value::ToArrayIndex() const {
3021 i::Handle<i::Object> obj = Utils::OpenHandle(this); 3038 i::Handle<i::Object> obj = Utils::OpenHandle(this);
3022 if (obj->IsSmi()) { 3039 if (obj->IsSmi()) {
3023 if (i::Smi::cast(*obj)->value() >= 0) return Utils::Uint32ToLocal(obj); 3040 if (i::Smi::cast(*obj)->value() >= 0) return Utils::Uint32ToLocal(obj);
3024 return Local<Uint32>(); 3041 return Local<Uint32>();
3025 } 3042 }
3026 i::Isolate* isolate = i::HeapObject::cast(*obj)->GetIsolate(); 3043 i::Isolate* isolate = i::HeapObject::cast(*obj)->GetIsolate();
3027 LOG_API(isolate, "ToArrayIndex"); 3044 LOG_API(isolate, "ToArrayIndex");
3028 ENTER_V8(isolate); 3045 ENTER_V8(isolate);
3029 EXCEPTION_PREAMBLE(isolate); 3046 EXCEPTION_PREAMBLE(isolate);
(...skipping 4798 matching lines...) Expand 10 before | Expand all | Expand 10 after
7828 Isolate* isolate = reinterpret_cast<Isolate*>(info.GetIsolate()); 7845 Isolate* isolate = reinterpret_cast<Isolate*>(info.GetIsolate());
7829 Address callback_address = 7846 Address callback_address =
7830 reinterpret_cast<Address>(reinterpret_cast<intptr_t>(callback)); 7847 reinterpret_cast<Address>(reinterpret_cast<intptr_t>(callback));
7831 VMState<EXTERNAL> state(isolate); 7848 VMState<EXTERNAL> state(isolate);
7832 ExternalCallbackScope call_scope(isolate, callback_address); 7849 ExternalCallbackScope call_scope(isolate, callback_address);
7833 callback(info); 7850 callback(info);
7834 } 7851 }
7835 7852
7836 7853
7837 } } // namespace v8::internal 7854 } } // namespace v8::internal
OLDNEW
« no previous file with comments | « src/api.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698