From a8f6c0610883ffd6ac3f2e06206e9b23335a2f48 Mon Sep 17 00:00:00 2001 From: Daniel Clark Date: Thu, 3 Dec 2020 18:50:17 -0800 Subject: [PATCH] Allow casting to Primitive types from Data MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Although every Primitive is a Data, the Cast operations for the subclasses of Primitive do not allow casting directly from Data to the subclasses without first going through Value. Because of this, Primitives extracted from a V8::FixedArray require two casts to get to the "real" type. Thus, as a convenience to embedders, this change makes it possible to cast directly from Data to all the subtypes of Primitive. Also, this change makes the parameter names in the declarations match those in the definitions, though there does not seem to be a universally followed convention regarding these. Bug: v8:10958 Change-Id: I18dc3fbb9a9bccb2cb3b75efd829af64d46d8eb9 Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2573816 Reviewed-by: Marja Hölttä Reviewed-by: Camillo Bruni Commit-Queue: Dan Clark Cr-Commit-Position: refs/heads/master@{#71649} --- include/v8.h | 99 ++++++++++++++++++------------------- src/api/api.cc | 24 ++++----- test/cctest/test-modules.cc | 13 ++--- 3 files changed, 63 insertions(+), 73 deletions(-) diff --git a/include/v8.h b/include/v8.h index a46e30459f..e2f3d2b3e4 100644 --- a/include/v8.h +++ b/include/v8.h @@ -3090,11 +3090,11 @@ class V8_EXPORT Primitive : public Value { }; class V8_EXPORT Boolean : public Primitive { public: bool Value() const; - V8_INLINE static Boolean* Cast(v8::Value* obj); + V8_INLINE static Boolean* Cast(v8::Data* data); V8_INLINE static Local New(Isolate* isolate, bool value); private: - static void CheckCast(v8::Value* obj); + static void CheckCast(v8::Data* that); }; @@ -3112,10 +3112,10 @@ class V8_EXPORT Name : public Primitive { */ int GetIdentityHash(); - V8_INLINE static Name* Cast(Value* obj); + V8_INLINE static Name* Cast(Data* data); private: - static void CheckCast(Value* obj); + static void CheckCast(Data* that); }; /** @@ -3363,7 +3363,7 @@ class V8_EXPORT String : public Name { */ const ExternalOneByteStringResource* GetExternalOneByteStringResource() const; - V8_INLINE static String* Cast(v8::Value* obj); + V8_INLINE static String* Cast(v8::Data* data); /** * Allocates a new string from a UTF-8 literal. This is equivalent to calling @@ -3520,7 +3520,7 @@ class V8_EXPORT String : public Name { const char* literal, NewStringType type, int length); - static void CheckCast(v8::Value* obj); + static void CheckCast(v8::Data* that); }; // Zero-length string specialization (templated string size includes @@ -3580,11 +3580,11 @@ class V8_EXPORT Symbol : public Name { static Local GetToStringTag(Isolate* isolate); static Local GetUnscopables(Isolate* isolate); - V8_INLINE static Symbol* Cast(Value* obj); + V8_INLINE static Symbol* Cast(Data* data); private: Symbol(); - static void CheckCast(Value* obj); + static void CheckCast(Data* that); }; @@ -3633,10 +3633,11 @@ class V8_EXPORT Number : public Primitive { public: double Value() const; static Local New(Isolate* isolate, double value); - V8_INLINE static Number* Cast(v8::Value* obj); + V8_INLINE static Number* Cast(v8::Data* data); + private: Number(); - static void CheckCast(v8::Value* obj); + static void CheckCast(v8::Data* that); }; @@ -3648,10 +3649,11 @@ class V8_EXPORT Integer : public Number { static Local New(Isolate* isolate, int32_t value); static Local NewFromUnsigned(Isolate* isolate, uint32_t value); int64_t Value() const; - V8_INLINE static Integer* Cast(v8::Value* obj); + V8_INLINE static Integer* Cast(v8::Data* data); + private: Integer(); - static void CheckCast(v8::Value* obj); + static void CheckCast(v8::Data* that); }; @@ -3661,11 +3663,11 @@ class V8_EXPORT Integer : public Number { class V8_EXPORT Int32 : public Integer { public: int32_t Value() const; - V8_INLINE static Int32* Cast(v8::Value* obj); + V8_INLINE static Int32* Cast(v8::Data* data); private: Int32(); - static void CheckCast(v8::Value* obj); + static void CheckCast(v8::Data* that); }; @@ -3675,11 +3677,11 @@ class V8_EXPORT Int32 : public Integer { class V8_EXPORT Uint32 : public Integer { public: uint32_t Value() const; - V8_INLINE static Uint32* Cast(v8::Value* obj); + V8_INLINE static Uint32* Cast(v8::Data* data); private: Uint32(); - static void CheckCast(v8::Value* obj); + static void CheckCast(v8::Data* that); }; /** @@ -3730,11 +3732,11 @@ class V8_EXPORT BigInt : public Primitive { */ void ToWordsArray(int* sign_bit, int* word_count, uint64_t* words) const; - V8_INLINE static BigInt* Cast(v8::Value* obj); + V8_INLINE static BigInt* Cast(v8::Data* data); private: BigInt(); - static void CheckCast(v8::Value* obj); + static void CheckCast(v8::Data* that); }; /** @@ -11648,14 +11650,13 @@ void* Object::GetAlignedPointerFromInternalField(int index) { return SlowGetAlignedPointerFromInternalField(index); } -String* String::Cast(v8::Value* value) { +String* String::Cast(v8::Data* data) { #ifdef V8_ENABLE_CHECKS - CheckCast(value); + CheckCast(data); #endif - return static_cast(value); + return static_cast(data); } - Local String::Empty(Isolate* isolate) { typedef internal::Address S; typedef internal::Internals I; @@ -11794,30 +11795,27 @@ V8_INLINE Value* Value::Cast(Data* value) { return static_cast(value); } -Boolean* Boolean::Cast(v8::Value* value) { +Boolean* Boolean::Cast(v8::Data* data) { #ifdef V8_ENABLE_CHECKS - CheckCast(value); + CheckCast(data); #endif - return static_cast(value); + return static_cast(data); } - -Name* Name::Cast(v8::Value* value) { +Name* Name::Cast(v8::Data* data) { #ifdef V8_ENABLE_CHECKS - CheckCast(value); + CheckCast(data); #endif - return static_cast(value); + return static_cast(data); } - -Symbol* Symbol::Cast(v8::Value* value) { +Symbol* Symbol::Cast(v8::Data* data) { #ifdef V8_ENABLE_CHECKS - CheckCast(value); + CheckCast(data); #endif - return static_cast(value); + return static_cast(data); } - Private* Private::Cast(Data* data) { #ifdef V8_ENABLE_CHECKS CheckCast(data); @@ -11839,42 +11837,39 @@ Module* Module::Cast(Data* data) { return reinterpret_cast(data); } -Number* Number::Cast(v8::Value* value) { +Number* Number::Cast(v8::Data* data) { #ifdef V8_ENABLE_CHECKS - CheckCast(value); + CheckCast(data); #endif - return static_cast(value); + return static_cast(data); } - -Integer* Integer::Cast(v8::Value* value) { +Integer* Integer::Cast(v8::Data* data) { #ifdef V8_ENABLE_CHECKS - CheckCast(value); + CheckCast(data); #endif - return static_cast(value); + return static_cast(data); } - -Int32* Int32::Cast(v8::Value* value) { +Int32* Int32::Cast(v8::Data* data) { #ifdef V8_ENABLE_CHECKS - CheckCast(value); + CheckCast(data); #endif - return static_cast(value); + return static_cast(data); } - -Uint32* Uint32::Cast(v8::Value* value) { +Uint32* Uint32::Cast(v8::Data* data) { #ifdef V8_ENABLE_CHECKS - CheckCast(value); + CheckCast(data); #endif - return static_cast(value); + return static_cast(data); } -BigInt* BigInt::Cast(v8::Value* value) { +BigInt* BigInt::Cast(v8::Data* data) { #ifdef V8_ENABLE_CHECKS - CheckCast(value); + CheckCast(data); #endif - return static_cast(value); + return static_cast(data); } Date* Date::Cast(v8::Value* value) { diff --git a/src/api/api.cc b/src/api/api.cc index 16ca1fa7e1..c1a2fbfdc5 100644 --- a/src/api/api.cc +++ b/src/api/api.cc @@ -3809,23 +3809,23 @@ void v8::Function::CheckCast(Value* that) { "Value is not a Function"); } -void v8::Boolean::CheckCast(v8::Value* that) { +void v8::Boolean::CheckCast(v8::Data* that) { i::Handle obj = Utils::OpenHandle(that); Utils::ApiCheck(obj->IsBoolean(), "v8::Boolean::Cast", "Value is not a Boolean"); } -void v8::Name::CheckCast(v8::Value* that) { +void v8::Name::CheckCast(v8::Data* that) { i::Handle obj = Utils::OpenHandle(that); Utils::ApiCheck(obj->IsName(), "v8::Name::Cast", "Value is not a Name"); } -void v8::String::CheckCast(v8::Value* that) { +void v8::String::CheckCast(v8::Data* that) { i::Handle obj = Utils::OpenHandle(that); Utils::ApiCheck(obj->IsString(), "v8::String::Cast", "Value is not a String"); } -void v8::Symbol::CheckCast(v8::Value* that) { +void v8::Symbol::CheckCast(v8::Data* that) { i::Handle obj = Utils::OpenHandle(that); Utils::ApiCheck(obj->IsSymbol(), "v8::Symbol::Cast", "Value is not a Symbol"); } @@ -3848,30 +3848,30 @@ void v8::Module::CheckCast(v8::Data* that) { Utils::ApiCheck(obj->IsModule(), "v8::Module::Cast", "Value is not a Module"); } -void v8::Number::CheckCast(v8::Value* that) { +void v8::Number::CheckCast(v8::Data* that) { i::Handle obj = Utils::OpenHandle(that); Utils::ApiCheck(obj->IsNumber(), "v8::Number::Cast()", "Value is not a Number"); } -void v8::Integer::CheckCast(v8::Value* that) { +void v8::Integer::CheckCast(v8::Data* that) { i::Handle obj = Utils::OpenHandle(that); Utils::ApiCheck(obj->IsNumber(), "v8::Integer::Cast", "Value is not an Integer"); } -void v8::Int32::CheckCast(v8::Value* that) { - Utils::ApiCheck(that->IsInt32(), "v8::Int32::Cast", +void v8::Int32::CheckCast(v8::Data* that) { + Utils::ApiCheck(Value::Cast(that)->IsInt32(), "v8::Int32::Cast", "Value is not a 32-bit signed integer"); } -void v8::Uint32::CheckCast(v8::Value* that) { - Utils::ApiCheck(that->IsUint32(), "v8::Uint32::Cast", +void v8::Uint32::CheckCast(v8::Data* that) { + Utils::ApiCheck(Value::Cast(that)->IsUint32(), "v8::Uint32::Cast", "Value is not a 32-bit unsigned integer"); } -void v8::BigInt::CheckCast(v8::Value* that) { - Utils::ApiCheck(that->IsBigInt(), "v8::BigInt::Cast", +void v8::BigInt::CheckCast(v8::Data* that) { + Utils::ApiCheck(Value::Cast(that)->IsBigInt(), "v8::BigInt::Cast", "Value is not a BigInt"); } diff --git a/test/cctest/test-modules.cc b/test/cctest/test-modules.cc index 7e2a03a785..b7754b2b31 100644 --- a/test/cctest/test-modules.cc +++ b/test/cctest/test-modules.cc @@ -217,18 +217,13 @@ TEST(ModuleInstantiationWithImportAssertions) { module_request_1->GetImportAssertions(); CHECK_EQ(3, import_assertions_1->Length()); Local assertion_key = - import_assertions_1->Get(env.local(), 0).As().As(); + import_assertions_1->Get(env.local(), 0).As(); CHECK(v8_str("a")->StrictEquals(assertion_key)); Local assertion_value = - import_assertions_1->Get(env.local(), 1).As().As(); + import_assertions_1->Get(env.local(), 1).As(); CHECK(v8_str("b")->StrictEquals(assertion_value)); - Local assertion_source_offset_data = - import_assertions_1->Get(env.local(), 2); - Local assertion_source_offset_int32 = - assertion_source_offset_data.As() - ->ToInt32(env.local()) - .ToLocalChecked(); - int32_t assertion_source_offset = assertion_source_offset_int32->Value(); + int32_t assertion_source_offset = + import_assertions_1->Get(env.local(), 2).As()->Value(); CHECK_EQ(65, assertion_source_offset); loc = module->SourceOffsetToLocation(assertion_source_offset); CHECK_EQ(1, loc.GetLineNumber());