#include "src/builtins/accessors.h"
#include "src/api/api-inl.h"
#include "src/debug/debug.h"
#include "src/deoptimizer/deoptimizer.h"
#include "src/execution/execution.h"
#include "src/execution/frames-inl.h"
#include "src/execution/frames.h"
#include "src/execution/isolate-inl.h"
#include "src/execution/messages.h"
#include "src/heap/factory.h"
#include "src/logging/runtime-call-stats-scope.h"
#include "src/objects/api-callbacks.h"
#include "src/objects/contexts.h"
#include "src/objects/field-index-inl.h"
#include "src/objects/js-array-inl.h"
#include "src/objects/js-shared-array-inl.h"
#include "src/objects/module-inl.h"
#include "src/objects/property-details.h"
#include "src/objects/prototype.h"
namespace v8 {
namespace internal {
DirectHandle<AccessorInfo> Accessors::MakeAccessor(
Isolate* isolate, DirectHandle<Name> name,
AccessorNameGetterCallback getter,
AccessorNameBooleanSetterCallback setter) {
Factory* factory = isolate->factory();
name = factory->InternalizeName(name);
DirectHandle<AccessorInfo> info = factory->NewAccessorInfo();
{
DisallowGarbageCollection no_gc;
Tagged<AccessorInfo> raw = *info;
raw->set_is_sloppy(false);
raw->set_replace_on_access(false);
raw->set_getter_side_effect_type(SideEffectType::kHasSideEffect);
raw->set_setter_side_effect_type(SideEffectType::kHasSideEffect);
raw->set_name(*name);
raw->set_getter(isolate, reinterpret_cast<Address>(getter));
if (setter == nullptr) setter = &ReconfigureToDataProperty;
raw->set_setter(isolate, reinterpret_cast<Address>(setter));
}
return info;
}
static V8_INLINE bool CheckForName(Isolate* isolate, DirectHandle<Name> name,
DirectHandle<String> property_name,
int offset, FieldIndex::Encoding encoding,
FieldIndex* index) {
if (Name::Equals(isolate, name, property_name)) {
*index = FieldIndex::ForInObjectOffset(offset, encoding);
return true;
}
return false;
}
bool Accessors::IsJSObjectFieldAccessor(Isolate* isolate, DirectHandle<Map> map,
DirectHandle<Name> name,
FieldIndex* index) {
if (map->is_dictionary_map()) {
return false;
}
switch (map->instance_type()) {
case JS_ARRAY_TYPE:
return CheckForName(isolate, name, isolate->factory()->length_string(),
JSArray::kLengthOffset, FieldIndex::kTagged, index);
default:
if (map->instance_type() < FIRST_NONSTRING_TYPE) {
return CheckForName(isolate, name, isolate->factory()->length_string(),
offsetof(String, length_), FieldIndex::kWord32,
index);
}
return false;
}
}
V8_WARN_UNUSED_RESULT MaybeDirectHandle<Object>
Accessors::ReplaceAccessorWithDataProperty(Isolate* isolate,
DirectHandle<JSObject> holder,
DirectHandle<Name> name,
DirectHandle<Object> value) {
LookupIterator it(isolate, holder, PropertyKey(isolate, name), holder,
LookupIterator::OWN_SKIP_INTERCEPTOR);
while (it.state() == LookupIterator::ACCESS_CHECK) {
CHECK(it.HasAccess());
it.Next();
}
DCHECK(holder.is_identical_to(it.GetHolder<JSObject>()) ||
(IsJSGlobalProxy(*holder) &&
holder->map()->prototype() == *it.GetHolder<JSObject>()));
CHECK_EQ(LookupIterator::ACCESSOR, it.state());
it.ReconfigureDataProperty(value, it.property_attributes());
return value;
}
void Accessors::ReconfigureToDataProperty(
v8::Local<v8::Name> key, v8::Local<v8::Value> val,
const v8::PropertyCallbackInfo<v8::Boolean>& info) {
i::Isolate* isolate = reinterpret_cast<i::Isolate*>(info.GetIsolate());
RCS_SCOPE(isolate, RuntimeCallCounterId::kReconfigureToDataProperty);
HandleScope scope(isolate);
DirectHandle<JSObject> holder =
Cast<JSObject>(Utils::OpenDirectHandle(*info.HolderV2()));
DirectHandle<Name> name = Utils::OpenDirectHandle(*key);
DirectHandle<Object> value = Utils::OpenDirectHandle(*val);
MaybeDirectHandle<Object> result =
Accessors::ReplaceAccessorWithDataProperty(isolate, holder, name, value);
if (!result.is_null()) {
info.GetReturnValue().Set(true);
}
}
void Accessors::ArgumentsIteratorGetter(
v8::Local<v8::Name> name, const v8::PropertyCallbackInfo<v8::Value>& info) {
i::Isolate* isolate = reinterpret_cast<i::Isolate*>(info.GetIsolate());
DisallowGarbageCollection no_gc;
HandleScope scope(isolate);
Tagged<Object> result = isolate->native_context()->array_values_iterator();
info.GetReturnValue().Set(
Utils::ToLocal(DirectHandle<Object>(result, isolate)));
}
DirectHandle<AccessorInfo> Accessors::MakeArgumentsIteratorInfo(
Isolate* isolate) {
DirectHandle<Name> name = isolate->factory()->iterator_symbol();
return MakeAccessor(isolate, name, &ArgumentsIteratorGetter, nullptr);
}
void Accessors::ArrayLengthGetter(
v8::Local<v8::Name> name, const v8::PropertyCallbackInfo<v8::Value>& info) {
i::Isolate* isolate = reinterpret_cast<i::Isolate*>(info.GetIsolate());
RCS_SCOPE(isolate, RuntimeCallCounterId::kArrayLengthGetter);
DisallowGarbageCollection no_gc;
HandleScope scope(isolate);
Tagged<JSArray> holder =
Cast<JSArray>(*Utils::OpenDirectHandle(*info.HolderV2()));
Tagged<Object> result = holder->length();
info.GetReturnValue().Set(
Utils::ToLocal(DirectHandle<Object>(result, isolate)));
}
void Accessors::ArrayLengthSetter(
v8::Local<v8::Name> name, v8::Local<v8::Value> val,
const v8::PropertyCallbackInfo<v8::Boolean>& info) {
i::Isolate* isolate = reinterpret_cast<i::Isolate*>(info.GetIsolate());
RCS_SCOPE(isolate, RuntimeCallCounterId::kArrayLengthSetter);
HandleScope scope(isolate);
DCHECK(Object::SameValue(*Utils::OpenDirectHandle(*name),
ReadOnlyRoots(isolate).length_string()));
DirectHandle<JSReceiver> object = Utils::OpenDirectHandle(*info.HolderV2());
DirectHandle<JSArray> array = Cast<JSArray>(object);
DirectHandle<Object> length_obj = Utils::OpenDirectHandle(*val);
bool was_readonly = JSArray::HasReadOnlyLength(array);
uint32_t length = 0;
if (!JSArray::AnythingToArrayLength(isolate, length_obj, &length)) {
return;
}
if (!was_readonly && V8_UNLIKELY(JSArray::HasReadOnlyLength(array))) {
if (length == Object::NumberValue(array->length())) {
info.GetReturnValue().Set(true);
} else if (info.ShouldThrowOnError()) {
Factory* factory = isolate->factory();
isolate->Throw(
*factory->NewTypeError(MessageTemplate::kStrictReadOnlyProperty,
Utils::OpenDirectHandle(*name),
i::Object::TypeOf(isolate, object), object));
} else {
info.GetReturnValue().Set(false);
}
return;
}
if (JSArray::SetLength(isolate, array, length).IsNothing()) {
FATAL("Fatal JavaScript invalid array length %u", length);
UNREACHABLE();
}
uint32_t actual_new_len = 0;
CHECK(Object::ToArrayLength(array->length(), &actual_new_len));
if (actual_new_len != length) {
if (info.ShouldThrowOnError()) {
Factory* factory = isolate->factory();
isolate->Throw(*factory->NewTypeError(
MessageTemplate::kStrictCannotDeleteProperty,
factory->NewNumberFromUint(actual_new_len - 1), array));
} else {
info.GetReturnValue().Set(false);
}
} else {
info.GetReturnValue().Set(true);
}
}
DirectHandle<AccessorInfo> Accessors::MakeArrayLengthInfo(Isolate* isolate) {
return MakeAccessor(isolate, isolate->factory()->length_string(),
&ArrayLengthGetter, &ArrayLengthSetter);
}
void Accessors::ModuleNamespaceEntryGetter(
v8::Local<v8::Name> name, const v8::PropertyCallbackInfo<v8::Value>& info) {
i::Isolate* isolate = reinterpret_cast<i::Isolate*>(info.GetIsolate());
HandleScope scope(isolate);
Tagged<JSModuleNamespace> holder =
Cast<JSModuleNamespace>(*Utils::OpenDirectHandle(*info.HolderV2()));
DirectHandle<Object> result;
if (holder->GetExport(isolate, Cast<String>(Utils::OpenDirectHandle(*name)))
.ToHandle(&result)) {
info.GetReturnValue().Set(Utils::ToLocal(result));
}
}
void Accessors::ModuleNamespaceEntrySetter(
v8::Local<v8::Name> name, v8::Local<v8::Value> val,
const v8::PropertyCallbackInfo<v8::Boolean>& info) {
i::Isolate* isolate = reinterpret_cast<i::Isolate*>(info.GetIsolate());
HandleScope scope(isolate);
Factory* factory = isolate->factory();
DirectHandle<JSModuleNamespace> holder =
Cast<JSModuleNamespace>(Utils::OpenDirectHandle(*info.HolderV2()));
if (info.ShouldThrowOnError()) {
isolate->Throw(
*factory->NewTypeError(MessageTemplate::kStrictReadOnlyProperty,
Utils::OpenDirectHandle(*name),
i::Object::TypeOf(isolate, holder), holder));
} else {
info.GetReturnValue().Set(false);
}
}
DirectHandle<AccessorInfo> Accessors::MakeModuleNamespaceEntryInfo(
Isolate* isolate, DirectHandle<String> name) {
return MakeAccessor(isolate, name, &ModuleNamespaceEntryGetter,
&ModuleNamespaceEntrySetter);
}
void Accessors::StringLengthGetter(
v8::Local<v8::Name> name, const v8::PropertyCallbackInfo<v8::Value>& info) {
i::Isolate* isolate = reinterpret_cast<i::Isolate*>(info.GetIsolate());
RCS_SCOPE(isolate, RuntimeCallCounterId::kStringLengthGetter);
USE(isolate);
DisallowGarbageCollection no_gc;
Tagged<Object> value =
Cast<JSPrimitiveWrapper>(*Utils::OpenDirectHandle(*info.HolderV2()))
->value();
int length = Cast<String>(value)->length();
info.GetReturnValue().Set(length);
}
DirectHandle<AccessorInfo> Accessors::MakeStringLengthInfo(Isolate* isolate) {
return MakeAccessor(isolate, isolate->factory()->length_string(),
&StringLengthGetter, nullptr);
}
void Accessors::FunctionPrototypeGetter(
v8::Local<v8::Name> name, const v8::PropertyCallbackInfo<v8::Value>& info) {
i::Isolate* isolate = reinterpret_cast<i::Isolate*>(info.GetIsolate());
RCS_SCOPE(isolate, RuntimeCallCounterId::kFunctionPrototypeGetter);
HandleScope scope(isolate);
DirectHandle<JSFunction> function =
Cast<JSFunction>(Utils::OpenDirectHandle(*info.HolderV2()));
DCHECK(function->has_prototype_property());
DirectHandle<Object> result =
JSFunction::GetFunctionPrototype(isolate, function);
info.GetReturnValue().Set(Utils::ToLocal(result));
}
void Accessors::FunctionPrototypeSetter(
v8::Local<v8::Name> name, v8::Local<v8::Value> val,
const v8::PropertyCallbackInfo<v8::Boolean>& info) {
i::Isolate* isolate = reinterpret_cast<i::Isolate*>(info.GetIsolate());
RCS_SCOPE(isolate, RuntimeCallCounterId::kFunctionPrototypeSetter);
HandleScope scope(isolate);
DirectHandle<Object> value = Utils::OpenDirectHandle(*val);
DirectHandle<JSFunction> object =
Cast<JSFunction>(Utils::OpenDirectHandle(*info.HolderV2()));
DCHECK(object->has_prototype_property());
JSFunction::SetPrototype(isolate, object, value);
info.GetReturnValue().Set(true);
}
DirectHandle<AccessorInfo> Accessors::MakeFunctionPrototypeInfo(
Isolate* isolate) {
return MakeAccessor(isolate, isolate->factory()->prototype_string(),
&FunctionPrototypeGetter, &FunctionPrototypeSetter);
}
void Accessors::FunctionLengthGetter(
v8::Local<v8::Name> name, const v8::PropertyCallbackInfo<v8::Value>& info) {
i::Isolate* isolate = reinterpret_cast<i::Isolate*>(info.GetIsolate());
RCS_SCOPE(isolate, RuntimeCallCounterId::kFunctionLengthGetter);
USE(isolate);
auto function = Cast<JSFunction>(Utils::OpenDirectHandle(*info.HolderV2()));
int length = function->length();
info.GetReturnValue().Set(length);
}
DirectHandle<AccessorInfo> Accessors::MakeFunctionLengthInfo(Isolate* isolate) {
return MakeAccessor(isolate, isolate->factory()->length_string(),
&FunctionLengthGetter, &ReconfigureToDataProperty);
}
void Accessors::FunctionNameGetter(
v8::Local<v8::Name> name, const v8::PropertyCallbackInfo<v8::Value>& info) {
i::Isolate* isolate = reinterpret_cast<i::Isolate*>(info.GetIsolate());
HandleScope scope(isolate);
auto function = Cast<JSFunction>(Utils::OpenDirectHandle(*info.HolderV2()));
DirectHandle<Object> result = JSFunction::GetName(isolate, function);
info.GetReturnValue().Set(Utils::ToLocal(result));
}
DirectHandle<AccessorInfo> Accessors::MakeFunctionNameInfo(Isolate* isolate) {
return MakeAccessor(isolate, isolate->factory()->name_string(),
&FunctionNameGetter, &ReconfigureToDataProperty);
}
namespace {
Handle<JSObject> ArgumentsFromDeoptInfo(JavaScriptFrame* frame,
int inlined_frame_index) {
Isolate* isolate = frame->isolate();
Factory* factory = isolate->factory();
TranslatedState translated_values(frame);
translated_values.Prepare(frame->fp());
int argument_count = 0;
TranslatedFrame* translated_frame =
translated_values.GetArgumentsInfoFromJSFrameIndex(inlined_frame_index,
&argument_count);
TranslatedFrame::iterator iter = translated_frame->begin();
bool should_deoptimize = iter->IsMaterializedObject();
DirectHandle<JSFunction> function = Cast<JSFunction>(iter->GetValue());
iter++;
iter++;
argument_count--;
Handle<JSObject> arguments =
factory->NewArgumentsObject(function, argument_count);
DirectHandle<FixedArray> array = factory->NewFixedArray(argument_count);
for (int i = 0; i < argument_count; ++i) {
should_deoptimize = should_deoptimize || iter->IsMaterializedObject();
DirectHandle<Object> value = iter->GetValue();
array->set(i, *value);
iter++;
}
arguments->set_elements(*array);
if (should_deoptimize) {
translated_values.StoreMaterializedValuesAndDeopt(frame);
}
return arguments;
}
int FindFunctionInFrame(JavaScriptFrame* frame,
DirectHandle<JSFunction> function) {
FrameSummaries summaries = frame->Summarize();
for (int i = summaries.size(); i != 0; i--) {
if (*summaries.frames[i - 1].AsJavaScript().function() == *function) {
return static_cast<int>(i) - 1;
}
}
return -1;
}
Handle<JSObject> GetFrameArguments(Isolate* isolate,
JavaScriptStackFrameIterator* it,
int function_index) {
JavaScriptFrame* frame = it->frame();
if (function_index > 0) {
return ArgumentsFromDeoptInfo(frame, function_index);
}
const int length = frame->GetActualArgumentCount();
DirectHandle<JSFunction> function(frame->function(), isolate);
Handle<JSObject> arguments =
isolate->factory()->NewArgumentsObject(function, length);
DirectHandle<FixedArray> array = isolate->factory()->NewFixedArray(length);
DCHECK(array->length() == length);
for (int i = 0; i < length; i++) {
Tagged<Object> value = frame->GetParameter(i);
if (IsTheHole(value, isolate)) {
DCHECK(IsResumableFunction(function->shared()->kind()));
value = ReadOnlyRoots(isolate).undefined_value();
}
array->set(i, value);
}
arguments->set_elements(*array);
if (CodeKindCanDeoptimize(frame->LookupCode()->kind()) && length > 0) {
DirectHandle<JSObject> arguments_from_deopt_info =
ArgumentsFromDeoptInfo(frame, function_index);
DirectHandle<FixedArray> elements_from_deopt_info(
Cast<FixedArray>(arguments_from_deopt_info->elements()), isolate);
int common_length = std::min(length, elements_from_deopt_info->length());
for (int i = 0; i < common_length; i++) {
array->set(i, elements_from_deopt_info->get(i));
}
}
return arguments;
}
}
Handle<JSObject> Accessors::FunctionGetArguments(JavaScriptFrame* frame,
int inlined_jsframe_index) {
Isolate* isolate = frame->isolate();
Address requested_frame_fp = frame->fp();
for (JavaScriptStackFrameIterator it(isolate); !it.done(); it.Advance()) {
if (it.frame()->fp() != requested_frame_fp) continue;
return GetFrameArguments(isolate, &it, inlined_jsframe_index);
}
UNREACHABLE();
}
DirectHandle<Object> Accessors::GetLegacyFunctionArguments(
Isolate* isolate, DirectHandle<JSFunction> function) {
DirectHandle<Object> result = isolate->factory()->null_value();
if (!function->shared()->native()) {
for (JavaScriptStackFrameIterator it(isolate); !it.done(); it.Advance()) {
JavaScriptFrame* frame = it.frame();
int function_index = FindFunctionInFrame(frame, function);
if (function_index >= 0) {
result = GetFrameArguments(isolate, &it, function_index);
break;
}
}
}
return result;
}
#ifdef V8_FUNCTION_ARGUMENTS_CALLER_ARE_OWN_PROPS
void Accessors::FunctionArgumentsGetter(
v8::Local<v8::Name> name, const v8::PropertyCallbackInfo<v8::Value>& info) {
i::Isolate* isolate = reinterpret_cast<i::Isolate*>(info.GetIsolate());
RCS_SCOPE(isolate, RuntimeCallCounterId::kFunctionArgumentsGetter);
isolate->CountUsage(v8::Isolate::kFunctionPrototypeArguments);
HandleScope scope(isolate);
auto function = Cast<JSFunction>(Utils::OpenDirectHandle(*info.HolderV2()));
DirectHandle<Object> result = GetLegacyFunctionArguments(isolate, function);
info.GetReturnValue().Set(Utils::ToLocal(result));
}
DirectHandle<AccessorInfo> Accessors::MakeFunctionArgumentsInfo(
Isolate* isolate) {
return MakeAccessor(isolate, isolate->factory()->arguments_string(),
&FunctionArgumentsGetter, nullptr);
}
#endif
static inline bool AllowAccessToFunction(Tagged<Context> current_context,
Tagged<JSFunction> function) {
return current_context->HasSameSecurityTokenAs(function->context());
}
class FrameFunctionIterator {
public:
explicit FrameFunctionIterator(Isolate* isolate)
: isolate_(isolate), frame_iterator_(isolate), inlined_frame_index_(-1) {
GetFrames();
}
bool Find(DirectHandle<JSFunction> function) {
do {
if (!next().ToHandle(&function_)) return false;
} while (!function_.is_identical_to(function));
return true;
}
bool FindNextNonTopLevelNativeOrUserJavaScript() {
do {
if (!next().ToHandle(&function_)) return false;
} while (function_->shared()->is_toplevel() ||
(!function_->shared()->native() &&
!function_->shared()->IsUserJavaScript()));
return true;
}
DirectHandle<JSFunction> MaterializeFunction() {
if (inlined_frame_index_ == 0) return function_;
JavaScriptFrame* frame = frame_iterator_.frame();
TranslatedState translated_values(frame);
translated_values.Prepare(frame->fp());
TranslatedFrame* translated_frame =
translated_values.GetFrameFromJSFrameIndex(inlined_frame_index_);
TranslatedFrame::iterator iter = translated_frame->begin();
bool should_deoptimize = iter->IsMaterializedObject();
DirectHandle<Object> value = iter->GetValue();
if (should_deoptimize) {
translated_values.StoreMaterializedValuesAndDeopt(frame);
}
return Cast<JSFunction>(value);
}
private:
MaybeDirectHandle<JSFunction> next() {
while (true) {
if (inlined_frame_index_ <= 0) {
if (!frame_iterator_.done()) {
frame_iterator_.Advance();
summaries_.frames.clear();
inlined_frame_index_ = -1;
GetFrames();
}
if (inlined_frame_index_ == -1) return {};
}
--inlined_frame_index_;
DirectHandle<JSFunction> next_function =
summaries_.frames[inlined_frame_index_].AsJavaScript().function();
if (!AllowAccessToFunction(isolate_->context(), *next_function)) continue;
return next_function;
}
}
void GetFrames() {
DCHECK_EQ(-1, inlined_frame_index_);
if (frame_iterator_.done()) return;
JavaScriptFrame* frame = frame_iterator_.frame();
summaries_ = frame->Summarize();
inlined_frame_index_ = static_cast<int>(summaries_.size());
DCHECK_LT(0, inlined_frame_index_);
}
Isolate* isolate_;
DirectHandle<JSFunction> function_;
JavaScriptStackFrameIterator frame_iterator_;
FrameSummaries summaries_;
int inlined_frame_index_;
};
namespace {
MaybeDirectHandle<JSFunction> FindCaller(Isolate* isolate,
DirectHandle<JSFunction> function) {
FrameFunctionIterator it(isolate);
if (function->shared()->native()) {
return {};
}
if (!it.Find(function)) {
return {};
}
if (!it.FindNextNonTopLevelNativeOrUserJavaScript()) {
return {};
}
DirectHandle<JSFunction> caller = it.MaterializeFunction();
if (is_strict(caller->shared()->language_mode())) {
return {};
}
if (!AllowAccessToFunction(isolate->context(), *caller)) {
return {};
}
return caller;
}
}
DirectHandle<Object> Accessors::GetLegacyFunctionCaller(
Isolate* isolate, DirectHandle<JSFunction> function) {
DirectHandle<Object> result;
MaybeDirectHandle<JSFunction> maybe_caller;
maybe_caller = FindCaller(isolate, function);
DirectHandle<JSFunction> caller;
if (!v8_flags.correctness_fuzzer_suppressions &&
maybe_caller.ToHandle(&caller)) {
result = caller;
} else {
result = isolate->factory()->null_value();
}
return result;
}
#ifdef V8_FUNCTION_ARGUMENTS_CALLER_ARE_OWN_PROPS
void Accessors::FunctionCallerGetter(
v8::Local<v8::Name> name, const v8::PropertyCallbackInfo<v8::Value>& info) {
i::Isolate* isolate = reinterpret_cast<i::Isolate*>(info.GetIsolate());
RCS_SCOPE(isolate, RuntimeCallCounterId::kFunctionCallerGetter);
isolate->CountUsage(v8::Isolate::kFunctionPrototypeCaller);
HandleScope scope(isolate);
DirectHandle<JSFunction> function =
Cast<JSFunction>(Utils::OpenDirectHandle(*info.HolderV2()));
DirectHandle<Object> result = GetLegacyFunctionCaller(isolate, function);
info.GetReturnValue().Set(Utils::ToLocal(result));
}
DirectHandle<AccessorInfo> Accessors::MakeFunctionCallerInfo(Isolate* isolate) {
return MakeAccessor(isolate, isolate->factory()->caller_string(),
&FunctionCallerGetter, nullptr);
}
#endif
void Accessors::BoundFunctionLengthGetter(
v8::Local<v8::Name> name, const v8::PropertyCallbackInfo<v8::Value>& info) {
i::Isolate* isolate = reinterpret_cast<i::Isolate*>(info.GetIsolate());
RCS_SCOPE(isolate, RuntimeCallCounterId::kBoundFunctionLengthGetter);
DirectHandle<JSBoundFunction> function =
Cast<JSBoundFunction>(Utils::OpenDirectHandle(*info.HolderV2()));
int length = 0;
if (!JSBoundFunction::GetLength(isolate, function).To(&length)) {
return;
}
info.GetReturnValue().Set(length);
}
DirectHandle<AccessorInfo> Accessors::MakeBoundFunctionLengthInfo(
Isolate* isolate) {
return MakeAccessor(isolate, isolate->factory()->length_string(),
&BoundFunctionLengthGetter, &ReconfigureToDataProperty);
}
void Accessors::BoundFunctionNameGetter(
v8::Local<v8::Name> name, const v8::PropertyCallbackInfo<v8::Value>& info) {
i::Isolate* isolate = reinterpret_cast<i::Isolate*>(info.GetIsolate());
RCS_SCOPE(isolate, RuntimeCallCounterId::kBoundFunctionNameGetter);
HandleScope scope(isolate);
DirectHandle<JSBoundFunction> function =
Cast<JSBoundFunction>(Utils::OpenDirectHandle(*info.HolderV2()));
DirectHandle<Object> result;
if (!JSBoundFunction::GetName(isolate, function).ToHandle(&result)) {
return;
}
info.GetReturnValue().Set(Utils::ToLocal(result));
}
DirectHandle<AccessorInfo> Accessors::MakeBoundFunctionNameInfo(
Isolate* isolate) {
return MakeAccessor(isolate, isolate->factory()->name_string(),
&BoundFunctionNameGetter, &ReconfigureToDataProperty);
}
void Accessors::WrappedFunctionLengthGetter(
v8::Local<v8::Name> name, const v8::PropertyCallbackInfo<v8::Value>& info) {
i::Isolate* isolate = reinterpret_cast<i::Isolate*>(info.GetIsolate());
RCS_SCOPE(isolate, RuntimeCallCounterId::kBoundFunctionLengthGetter);
auto function =
Cast<JSWrappedFunction>(Utils::OpenDirectHandle(*info.HolderV2()));
int length = 0;
if (!JSWrappedFunction::GetLength(isolate, function).To(&length)) {
return;
}
info.GetReturnValue().Set(length);
}
DirectHandle<AccessorInfo> Accessors::MakeWrappedFunctionLengthInfo(
Isolate* isolate) {
return MakeAccessor(isolate, isolate->factory()->length_string(),
&WrappedFunctionLengthGetter, &ReconfigureToDataProperty);
}
void Accessors::ValueUnavailableGetter(
v8::Local<v8::Name> name, const v8::PropertyCallbackInfo<v8::Value>& info) {
Isolate* isolate = reinterpret_cast<Isolate*>(info.GetIsolate());
HandleScope scope(isolate);
isolate->Throw(*isolate->factory()->NewReferenceError(
MessageTemplate::kAccessedUnavailableVariable,
Utils::OpenDirectHandle(*name)));
}
DirectHandle<AccessorInfo> Accessors::MakeValueUnavailableInfo(
Isolate* isolate) {
return MakeAccessor(isolate, isolate->factory()->empty_string(),
&ValueUnavailableGetter, &ReconfigureToDataProperty);
}
void Accessors::WrappedFunctionNameGetter(
v8::Local<v8::Name> name, const v8::PropertyCallbackInfo<v8::Value>& info) {
i::Isolate* isolate = reinterpret_cast<i::Isolate*>(info.GetIsolate());
RCS_SCOPE(isolate, RuntimeCallCounterId::kWrappedFunctionNameGetter);
HandleScope scope(isolate);
auto function =
Cast<JSWrappedFunction>(Utils::OpenDirectHandle(*info.HolderV2()));
DirectHandle<Object> result;
if (!JSWrappedFunction::GetName(isolate, function).ToHandle(&result)) {
return;
}
info.GetReturnValue().Set(Utils::ToLocal(result));
}
DirectHandle<AccessorInfo> Accessors::MakeWrappedFunctionNameInfo(
Isolate* isolate) {
return MakeAccessor(isolate, isolate->factory()->name_string(),
&WrappedFunctionNameGetter, &ReconfigureToDataProperty);
}
void Accessors::ErrorStackGetter(
const v8::FunctionCallbackInfo<v8::Value>& info) {
Isolate* isolate = reinterpret_cast<Isolate*>(info.GetIsolate());
HandleScope scope(isolate);
DirectHandle<Object> formatted_stack = isolate->factory()->undefined_value();
DirectHandle<JSReceiver> maybe_error_object =
Utils::OpenDirectHandle(*info.This());
if (IsJSObject(*maybe_error_object)) {
if (!ErrorUtils::GetFormattedStack(isolate,
Cast<JSObject>(maybe_error_object))
.ToHandle(&formatted_stack)) {
return;
}
}
v8::Local<v8::Value> result = Utils::ToLocal(formatted_stack);
CHECK(result->IsValue());
info.GetReturnValue().Set(result);
}
void Accessors::ErrorStackSetter(
const v8::FunctionCallbackInfo<v8::Value>& info) {
Isolate* isolate = reinterpret_cast<Isolate*>(info.GetIsolate());
HandleScope scope(isolate);
DirectHandle<JSReceiver> maybe_error_object =
Utils::OpenDirectHandle(*info.This());
if (IsJSObject(*maybe_error_object)) {
v8::Local<v8::Value> value = info[0];
ErrorUtils::SetFormattedStack(isolate, Cast<JSObject>(maybe_error_object),
Utils::OpenDirectHandle(*value));
}
}
}
}