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/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
« include/v8.h ('K') | « 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 maybe_local.ToLocal(&result); \
2641 isolate, obj).ToHandle(&str); 2641 return result; \
2642 EXCEPTION_BAILOUT_CHECK(isolate, Local<String>()); 2642 } while (false);
2643 } 2643
2644 return ToApiHandle<String>(str); 2644
2645 MaybeLocal<String> Value::ToString(Local<Context> context) const {
2646 auto obj = Utils::OpenHandle(this);
2647 if (obj->IsString()) return ToApiHandle<String>(obj);
2648 CONTEXT_SCOPE_GET_ISOLATE(context, "ToString");
2649 EXCEPTION_PREAMBLE(isolate);
2650 Local<String> result;
2651 has_pending_exception =
2652 !ToLocal<String>(i::Execution::ToString(isolate, obj), &result);
2653 EXCEPTION_BAILOUT_CHECK(isolate, result);
2654 return result;
2645 } 2655 }
2646 2656
2647 2657
2648 Local<String> Value::ToDetailString(Isolate* v8_isolate) const { 2658 Local<String> Value::ToString(Isolate* isolate) const {
2649 i::Handle<i::Object> obj = Utils::OpenHandle(this); 2659 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 } 2660 }
2664 2661
2665 2662
2666 Local<v8::Object> Value::ToObject(Isolate* v8_isolate) const { 2663 MaybeLocal<String> Value::ToDetailString(Local<Context> context) const {
2667 i::Handle<i::Object> obj = Utils::OpenHandle(this); 2664 auto obj = Utils::OpenHandle(this);
2668 i::Handle<i::Object> val; 2665 if (obj->IsString()) return ToApiHandle<String>(obj);
2669 if (obj->IsJSObject()) { 2666 CONTEXT_SCOPE_GET_ISOLATE(context, "ToDetailString");
2670 val = obj; 2667 EXCEPTION_PREAMBLE(isolate);
2671 } else { 2668 Local<String> result;
2672 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(v8_isolate); 2669 has_pending_exception =
2673 LOG_API(isolate, "ToObject"); 2670 !ToLocal<String>(i::Execution::ToDetailString(isolate, obj), &result);
2674 ENTER_V8(isolate); 2671 EXCEPTION_BAILOUT_CHECK(isolate, result);
2675 EXCEPTION_PREAMBLE(isolate); 2672 return result;
2676 has_pending_exception = !i::Execution::ToObject( 2673 }
2677 isolate, obj).ToHandle(&val); 2674
2678 EXCEPTION_BAILOUT_CHECK(isolate, Local<v8::Object>()); 2675
2679 } 2676 Local<String> Value::ToDetailString(Isolate* isolate) const {
2680 return ToApiHandle<Object>(val); 2677 RETURN_TO_LOCAL_UNCHECKED(ToDetailString(isolate->GetCurrentContext()),
2678 String);
2679 }
2680
2681
2682 MaybeLocal<Object> Value::ToObject(Local<Context> context) const {
2683 auto obj = Utils::OpenHandle(this);
2684 if (obj->IsJSObject()) return ToApiHandle<Object>(obj);
2685 CONTEXT_SCOPE_GET_ISOLATE(context, "ToObject");
2686 EXCEPTION_PREAMBLE(isolate);
2687 Local<Object> result;
2688 has_pending_exception =
2689 !ToLocal<Object>(i::Execution::ToObject(isolate, obj), &result);
2690 EXCEPTION_BAILOUT_CHECK(isolate, result);
2691 return result;
2692 }
2693
2694
2695 Local<v8::Object> Value::ToObject(Isolate* isolate) const {
2696 RETURN_TO_LOCAL_UNCHECKED(ToObject(isolate->GetCurrentContext()), Object);
2697 }
2698
2699
2700 MaybeLocal<Boolean> Value::ToBoolean(Local<Context> context) const {
2701 auto obj = Utils::OpenHandle(this);
2702 if (obj->IsBoolean()) return ToApiHandle<Boolean>(obj);
2703 auto isolate = reinterpret_cast<i::Isolate*>(context->GetIsolate());
2704 auto val = isolate->factory()->ToBoolean(obj->BooleanValue());
2705 return ToApiHandle<Boolean>(val);
2681 } 2706 }
2682 2707
2683 2708
2684 Local<Boolean> Value::ToBoolean(Isolate* v8_isolate) const { 2709 Local<Boolean> Value::ToBoolean(Isolate* v8_isolate) const {
2685 i::Handle<i::Object> obj = Utils::OpenHandle(this); 2710 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 } 2711 }
2697 2712
2698 2713
2699 Local<Number> Value::ToNumber(Isolate* v8_isolate) const { 2714 MaybeLocal<Number> Value::ToNumber(Local<Context> context) const {
2700 i::Handle<i::Object> obj = Utils::OpenHandle(this); 2715 auto obj = Utils::OpenHandle(this);
2701 i::Handle<i::Object> num; 2716 if (obj->IsNumber()) return ToApiHandle<Number>(obj);
2702 if (obj->IsNumber()) { 2717 CONTEXT_SCOPE_GET_ISOLATE(context, "ToNumber");
2703 num = obj; 2718 EXCEPTION_PREAMBLE(isolate);
2704 } else { 2719 Local<Number> result;
2705 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(v8_isolate); 2720 has_pending_exception =
2706 LOG_API(isolate, "ToNumber"); 2721 !ToLocal<Number>(i::Execution::ToNumber(isolate, obj), &result);
2707 ENTER_V8(isolate); 2722 EXCEPTION_BAILOUT_CHECK(isolate, result);
2708 EXCEPTION_PREAMBLE(isolate); 2723 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 } 2724 }
2715 2725
2716 2726
2717 Local<Integer> Value::ToInteger(Isolate* v8_isolate) const { 2727 Local<Number> Value::ToNumber(Isolate* isolate) const {
2718 i::Handle<i::Object> obj = Utils::OpenHandle(this); 2728 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 } 2729 }
2733 2730
2734 2731
2732 MaybeLocal<Integer> Value::ToInteger(Local<Context> context) const {
2733 auto obj = Utils::OpenHandle(this);
2734 if (obj->IsSmi()) return ToApiHandle<Integer>(obj);
2735 CONTEXT_SCOPE_GET_ISOLATE(context, "ToInteger");
2736 EXCEPTION_PREAMBLE(isolate);
2737 Local<Integer> result;
2738 has_pending_exception =
2739 !ToLocal<Integer>(i::Execution::ToInteger(isolate, obj), &result);
2740 EXCEPTION_BAILOUT_CHECK(isolate, result);
2741 return result;
2742 }
2743
2744
2745 Local<Integer> Value::ToInteger(Isolate* isolate) const {
2746 RETURN_TO_LOCAL_UNCHECKED(ToInteger(isolate->GetCurrentContext()), Integer);
2747 }
2748
2749
2750 MaybeLocal<Int32> Value::ToInt32(Local<Context> context) const {
2751 auto obj = Utils::OpenHandle(this);
2752 if (obj->IsSmi()) return ToApiHandle<Int32>(obj);
2753 CONTEXT_SCOPE_GET_ISOLATE(context, "ToInt32");
2754 EXCEPTION_PREAMBLE(isolate);
2755 Local<Int32> result;
2756 has_pending_exception =
2757 !ToLocal<Int32>(i::Execution::ToInt32(isolate, obj), &result);
2758 EXCEPTION_BAILOUT_CHECK(isolate, result);
2759 return result;
2760 }
2761
2762
2763 Local<Int32> Value::ToInt32(Isolate* isolate) const {
2764 RETURN_TO_LOCAL_UNCHECKED(ToInt32(isolate->GetCurrentContext()), Int32);
2765 }
2766
2767
2768 MaybeLocal<Uint32> Value::ToUint32(Local<Context> context) const {
2769 auto obj = Utils::OpenHandle(this);
2770 if (obj->IsSmi()) return ToApiHandle<Uint32>(obj);
2771 CONTEXT_SCOPE_GET_ISOLATE(context, "ToUInt32");
2772 EXCEPTION_PREAMBLE(isolate);
2773 Local<Uint32> result;
2774 has_pending_exception =
2775 !ToLocal<Uint32>(i::Execution::ToUint32(isolate, obj), &result);
2776 EXCEPTION_BAILOUT_CHECK(isolate, result);
2777 return result;
2778 }
2779
2780
2781 Local<Uint32> Value::ToUint32(Isolate* isolate) const {
2782 RETURN_TO_LOCAL_UNCHECKED(ToUint32(isolate->GetCurrentContext()), Uint32);
2783 }
2784
2785
2735 void i::Internals::CheckInitializedImpl(v8::Isolate* external_isolate) { 2786 void i::Internals::CheckInitializedImpl(v8::Isolate* external_isolate) {
2736 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(external_isolate); 2787 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(external_isolate);
2737 Utils::ApiCheck(isolate != NULL && 2788 Utils::ApiCheck(isolate != NULL &&
2738 !isolate->IsDead(), 2789 !isolate->IsDead(),
2739 "v8::internal::Internals::CheckInitialized()", 2790 "v8::internal::Internals::CheckInitialized()",
2740 "Isolate is not initialized or V8 has died"); 2791 "Isolate is not initialized or V8 has died");
2741 } 2792 }
2742 2793
2743 2794
2744 void External::CheckCast(v8::Value* that) { 2795 void External::CheckCast(v8::Value* that) {
(...skipping 230 matching lines...) Expand 10 before | Expand all | Expand 10 after
2975 EXCEPTION_BAILOUT_CHECK(isolate, 0); 3026 EXCEPTION_BAILOUT_CHECK(isolate, 0);
2976 } 3027 }
2977 if (num->IsSmi()) { 3028 if (num->IsSmi()) {
2978 return i::Smi::cast(*num)->value(); 3029 return i::Smi::cast(*num)->value();
2979 } else { 3030 } else {
2980 return static_cast<int64_t>(num->Number()); 3031 return static_cast<int64_t>(num->Number());
2981 } 3032 }
2982 } 3033 }
2983 3034
2984 3035
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 { 3036 Local<Uint32> Value::ToArrayIndex() const {
3021 i::Handle<i::Object> obj = Utils::OpenHandle(this); 3037 i::Handle<i::Object> obj = Utils::OpenHandle(this);
3022 if (obj->IsSmi()) { 3038 if (obj->IsSmi()) {
3023 if (i::Smi::cast(*obj)->value() >= 0) return Utils::Uint32ToLocal(obj); 3039 if (i::Smi::cast(*obj)->value() >= 0) return Utils::Uint32ToLocal(obj);
3024 return Local<Uint32>(); 3040 return Local<Uint32>();
3025 } 3041 }
3026 i::Isolate* isolate = i::HeapObject::cast(*obj)->GetIsolate(); 3042 i::Isolate* isolate = i::HeapObject::cast(*obj)->GetIsolate();
3027 LOG_API(isolate, "ToArrayIndex"); 3043 LOG_API(isolate, "ToArrayIndex");
3028 ENTER_V8(isolate); 3044 ENTER_V8(isolate);
3029 EXCEPTION_PREAMBLE(isolate); 3045 EXCEPTION_PREAMBLE(isolate);
(...skipping 4798 matching lines...) Expand 10 before | Expand all | Expand 10 after
7828 Isolate* isolate = reinterpret_cast<Isolate*>(info.GetIsolate()); 7844 Isolate* isolate = reinterpret_cast<Isolate*>(info.GetIsolate());
7829 Address callback_address = 7845 Address callback_address =
7830 reinterpret_cast<Address>(reinterpret_cast<intptr_t>(callback)); 7846 reinterpret_cast<Address>(reinterpret_cast<intptr_t>(callback));
7831 VMState<EXTERNAL> state(isolate); 7847 VMState<EXTERNAL> state(isolate);
7832 ExternalCallbackScope call_scope(isolate, callback_address); 7848 ExternalCallbackScope call_scope(isolate, callback_address);
7833 callback(info); 7849 callback(info);
7834 } 7850 }
7835 7851
7836 7852
7837 } } // namespace v8::internal 7853 } } // namespace v8::internal
OLDNEW
« include/v8.h ('K') | « src/api.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698