Commit 5f82dbbe authored by Sathya Gunasekaran's avatar Sathya Gunasekaran Committed by V8 LUCI CQ

[API] Allow embedders to assign instance types

Constructors define instance types for their instances while accessors
define a range of permissable instance types for receiver checks.\

Bug: v8:11476
Change-Id: I48b5326ec0a4e847283c2fa5c8f1705302727453
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2821430Reviewed-by: 's avatarCamillo Bruni <cbruni@chromium.org>
Commit-Queue: Sathya Gunasekaran  <gsathya@chromium.org>
Cr-Commit-Position: refs/heads/master@{#75131}
parent 1837c6f9
......@@ -6498,7 +6498,9 @@ class V8_EXPORT FunctionTemplate : public Template {
Local<Signature> signature = Local<Signature>(), int length = 0,
ConstructorBehavior behavior = ConstructorBehavior::kAllow,
SideEffectType side_effect_type = SideEffectType::kHasSideEffect,
const CFunction* c_function = nullptr);
const CFunction* c_function = nullptr, uint8_t instance_type = 0,
uint8_t allowed_receiver_range_start = 0,
uint8_t allowed_receiver_range_end = 0);
/** Creates a function template for multiple overloaded fast API calls.*/
static Local<FunctionTemplate> NewWithCFunctionOverloads(
......
......@@ -1222,7 +1222,9 @@ static Local<FunctionTemplate> FunctionTemplateNew(
bool do_not_cache,
v8::Local<Private> cached_property_name = v8::Local<Private>(),
SideEffectType side_effect_type = SideEffectType::kHasSideEffect,
const MemorySpan<const CFunction>& c_function_overloads = {}) {
const MemorySpan<const CFunction>& c_function_overloads = {},
uint8_t instance_type = 0, uint8_t allowed_receiver_range_start = 0,
uint8_t allowed_receiver_range_end = 0) {
i::Handle<i::Struct> struct_obj = isolate->factory()->NewStruct(
i::FUNCTION_TEMPLATE_INFO_TYPE, i::AllocationType::kOld);
i::Handle<i::FunctionTemplateInfo> obj =
......@@ -1244,6 +1246,9 @@ static Local<FunctionTemplate> FunctionTemplateNew(
? i::ReadOnlyRoots(isolate).the_hole_value()
: *Utils::OpenHandle(*cached_property_name));
if (behavior == ConstructorBehavior::kThrow) raw.set_remove_prototype(true);
raw.SetInstanceType(instance_type);
raw.set_allowed_receiver_range_start(allowed_receiver_range_start);
raw.set_allowed_receiver_range_end(allowed_receiver_range_end);
}
if (callback != nullptr) {
Utils::ToLocal(obj)->SetCallHandler(callback, data, side_effect_type,
......@@ -1255,7 +1260,9 @@ static Local<FunctionTemplate> FunctionTemplateNew(
Local<FunctionTemplate> FunctionTemplate::New(
Isolate* isolate, FunctionCallback callback, v8::Local<Value> data,
v8::Local<Signature> signature, int length, ConstructorBehavior behavior,
SideEffectType side_effect_type, const CFunction* c_function) {
SideEffectType side_effect_type, const CFunction* c_function,
uint8_t instance_type, uint8_t allowed_receiver_range_start,
uint8_t allowed_receiver_range_end) {
i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
// Changes to the environment cannot be captured in the snapshot. Expect no
// function templates when the isolate is created for serialization.
......@@ -1265,7 +1272,8 @@ Local<FunctionTemplate> FunctionTemplate::New(
i_isolate, callback, data, signature, length, behavior, false,
Local<Private>(), side_effect_type,
c_function ? MemorySpan<const CFunction>{c_function, 1}
: MemorySpan<const CFunction>{});
: MemorySpan<const CFunction>{},
instance_type, allowed_receiver_range_start, allowed_receiver_range_end);
}
Local<FunctionTemplate> FunctionTemplate::NewWithCFunctionOverloads(
......
......@@ -38,6 +38,11 @@ BOOL_ACCESSORS(FunctionTemplateInfo, flag, accept_any_receiver,
AcceptAnyReceiverBit::kShift)
BOOL_ACCESSORS(FunctionTemplateInfo, flag, published, PublishedBit::kShift)
BIT_FIELD_ACCESSORS(FunctionTemplateInfo, flag, allowed_receiver_range_start,
FunctionTemplateInfo::AllowedReceiverRangeStartBits)
BIT_FIELD_ACCESSORS(FunctionTemplateInfo, flag, allowed_receiver_range_end,
FunctionTemplateInfo::AllowedReceiverRangeEndBits)
// static
FunctionTemplateRareData FunctionTemplateInfo::EnsureFunctionTemplateRareData(
Isolate* isolate, Handle<FunctionTemplateInfo> function_template_info) {
......@@ -80,6 +85,12 @@ RARE_ACCESSORS(c_function_overloads, CFunctionOverloads, FixedArray,
GetReadOnlyRoots(cage_base).empty_fixed_array())
#undef RARE_ACCESSORS
int FunctionTemplateInfo::InstanceType() const { return instance_type(); }
void FunctionTemplateInfo::SetInstanceType(int instance_type) {
set_instance_type(instance_type);
}
bool TemplateInfo::should_cache() const {
return serial_number() != kDoNotCache;
}
......
......@@ -122,11 +122,17 @@ class FunctionTemplateInfo
// SharedFunctionInfo or an accessor), because TF relies on immutability to
// safely read concurrently.
DECL_BOOLEAN_ACCESSORS(published)
DECL_INT_ACCESSORS(allowed_receiver_range_start)
DECL_INT_ACCESSORS(allowed_receiver_range_end)
// End flag bits ---------------------
// Dispatched behavior.
DECL_PRINTER(FunctionTemplateInfo)
inline int InstanceType() const;
inline void SetInstanceType(int instance_type);
static Handle<SharedFunctionInfo> GetOrCreateSharedFunctionInfo(
Isolate* isolate, Handle<FunctionTemplateInfo> info,
MaybeHandle<Name> maybe_name);
......
......@@ -34,6 +34,11 @@ bitfield struct FunctionTemplateInfoFlags extends uint31 {
remove_prototype: bool: 1 bit;
accept_any_receiver: bool: 1 bit;
published: bool: 1 bit;
// Allowed receiver ranges are used for instance type checking to check
// whether the receiver calling the associated JSFunction is a compatible
// receiver.
allowed_receiver_range_start: int32: 12 bit;
allowed_receiver_range_end: int32: 12 bit;
}
@generateCppClass
......@@ -62,6 +67,9 @@ extern class FunctionTemplateInfo extends TemplateInfo {
// the receiver under the the cached_property_name when this
// FunctionTemplateInfo is used as a getter.
cached_property_name: Object;
// This will be set as the instance type of the objects that are created from
// this FunctionTemplateInfo.
instance_type: Smi;
}
bitfield struct ObjectTemplateInfoFlags extends uint31 {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment