Allow casting to Primitive types from Data

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ä <marja@chromium.org>
Reviewed-by: Camillo Bruni <cbruni@chromium.org>
Commit-Queue: Dan Clark <daniec@microsoft.com>
Cr-Commit-Position: refs/heads/master@{#71649}
This commit is contained in:
Daniel Clark 2020-12-03 18:50:17 -08:00 committed by Commit Bot
parent a4283771e1
commit a8f6c06108
3 changed files with 63 additions and 73 deletions

View File

@ -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<Boolean> 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<Symbol> GetToStringTag(Isolate* isolate);
static Local<Symbol> 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<Number> 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<Integer> New(Isolate* isolate, int32_t value);
static Local<Integer> 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<String*>(value);
return static_cast<String*>(data);
}
Local<String> 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*>(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<Boolean*>(value);
return static_cast<Boolean*>(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<Name*>(value);
return static_cast<Name*>(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<Symbol*>(value);
return static_cast<Symbol*>(data);
}
Private* Private::Cast(Data* data) {
#ifdef V8_ENABLE_CHECKS
CheckCast(data);
@ -11839,42 +11837,39 @@ Module* Module::Cast(Data* data) {
return reinterpret_cast<Module*>(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<Number*>(value);
return static_cast<Number*>(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<Integer*>(value);
return static_cast<Integer*>(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<Int32*>(value);
return static_cast<Int32*>(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<Uint32*>(value);
return static_cast<Uint32*>(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<BigInt*>(value);
return static_cast<BigInt*>(data);
}
Date* Date::Cast(v8::Value* value) {

View File

@ -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<i::Object> 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<i::Object> 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<i::Object> 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<i::Object> 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<i::Object> 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<i::Object> 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");
}

View File

@ -217,18 +217,13 @@ TEST(ModuleInstantiationWithImportAssertions) {
module_request_1->GetImportAssertions();
CHECK_EQ(3, import_assertions_1->Length());
Local<String> assertion_key =
import_assertions_1->Get(env.local(), 0).As<Value>().As<String>();
import_assertions_1->Get(env.local(), 0).As<String>();
CHECK(v8_str("a")->StrictEquals(assertion_key));
Local<String> assertion_value =
import_assertions_1->Get(env.local(), 1).As<Value>().As<String>();
import_assertions_1->Get(env.local(), 1).As<String>();
CHECK(v8_str("b")->StrictEquals(assertion_value));
Local<Data> assertion_source_offset_data =
import_assertions_1->Get(env.local(), 2);
Local<Int32> assertion_source_offset_int32 =
assertion_source_offset_data.As<Value>()
->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<Int32>()->Value();
CHECK_EQ(65, assertion_source_offset);
loc = module->SourceOffsetToLocation(assertion_source_offset);
CHECK_EQ(1, loc.GetLineNumber());