// Copyright 2016 the V8 project authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #ifndef V8_API_API_ARGUMENTS_INL_H_ #define V8_API_API_ARGUMENTS_INL_H_ #include "src/api/api-arguments.h" #include "src/api/api-inl.h" #include "src/debug/debug.h" #include "src/execution/vm-state-inl.h" #include "src/logging/runtime-call-stats-scope.h" #include "src/objects/api-callbacks.h" #include "src/objects/instance-type.h" #include "src/objects/slots-inl.h" namespace v8 { namespace internal { #if DEBUG bool IsApiCallResultType(Tagged obj) { if (IsSmi(obj)) return true; DCHECK(IsHeapObject(obj)); return (IsString(obj) || IsSymbol(obj) || IsJSReceiver(obj) || IsHeapNumber(obj) || IsBigInt(obj) || IsUndefined(obj) || IsTrue(obj) || IsFalse(obj) || IsNull(obj)); } #endif // DEBUG CustomArgumentsBase::CustomArgumentsBase(Isolate* isolate) : Relocatable(isolate) {} template CustomArguments::~CustomArguments() { slot_at(kReturnValueIndex).store(Object(kHandleZapValue)); } template template Handle CustomArguments::GetReturnValue(Isolate* isolate) const { // Check the ReturnValue. FullObjectSlot slot = slot_at(kReturnValueIndex); // Nothing was set, return empty handle as per previous behaviour. Tagged raw_object = *slot; if (IsTheHole(raw_object, isolate)) return Handle(); DCHECK(IsApiCallResultType(raw_object)); return Handle::cast(Handle(slot.location())); } inline Tagged PropertyCallbackArguments::holder() const { return JSObject::cast(*slot_at(T::kHolderIndex)); } inline Tagged PropertyCallbackArguments::receiver() const { return *slot_at(T::kThisIndex); } inline Tagged FunctionCallbackArguments::holder() const { return JSReceiver::cast(*slot_at(T::kHolderIndex)); } #define DCHECK_NAME_COMPATIBLE(interceptor, name) \ DCHECK(interceptor->is_named()); \ DCHECK(!name->IsPrivate()); \ DCHECK_IMPLIES(IsSymbol(*name), interceptor->can_intercept_symbols()); #define PREPARE_CALLBACK_INFO_ACCESSOR(ISOLATE, F, API_RETURN_TYPE, \ ACCESSOR_INFO, RECEIVER, ACCESSOR_KIND) \ if (ISOLATE->should_check_side_effects() && \ !ISOLATE->debug()->PerformSideEffectCheckForAccessor( \ ACCESSOR_INFO, RECEIVER, ACCESSOR_KIND)) { \ return {}; \ } \ ExternalCallbackScope call_scope(ISOLATE, FUNCTION_ADDR(F)); \ PropertyCallbackInfo callback_info(values_); #define PREPARE_CALLBACK_INFO_INTERCEPTOR(ISOLATE, F, API_RETURN_TYPE, \ INTERCEPTOR_INFO) \ if (ISOLATE->should_check_side_effects() && \ !ISOLATE->debug()->PerformSideEffectCheckForInterceptor( \ INTERCEPTOR_INFO)) { \ return {}; \ } \ ExternalCallbackScope call_scope(ISOLATE, FUNCTION_ADDR(F)); \ PropertyCallbackInfo callback_info(values_); Handle FunctionCallbackArguments::Call( Tagged handler) { Isolate* isolate = this->isolate(); RCS_SCOPE(isolate, RuntimeCallCounterId::kFunctionCallback); v8::FunctionCallback f = reinterpret_cast(handler->callback(isolate)); Handle receiver_check_unsupported; if (isolate->should_check_side_effects() && !isolate->debug()->PerformSideEffectCheckForCallback( handle(handler, isolate))) { return {}; } ExternalCallbackScope call_scope(isolate, FUNCTION_ADDR(f)); FunctionCallbackInfo info(values_, argv_, argc_); f(info); return GetReturnValue(isolate); } PropertyCallbackArguments::~PropertyCallbackArguments(){ #ifdef DEBUG // TODO(chromium:1310062): enable this check. // if (javascript_execution_counter_) { // CHECK_WITH_MSG(javascript_execution_counter_ == // isolate()->javascript_execution_counter(), // "Unexpected side effect detected"); // } #endif // DEBUG } // ------------------------------------------------------------------------- // Named Interceptor callbacks. Handle PropertyCallbackArguments::CallNamedEnumerator( Handle interceptor) { DCHECK(interceptor->is_named()); RCS_SCOPE(isolate(), RuntimeCallCounterId::kNamedEnumeratorCallback); return CallPropertyEnumerator(interceptor); } Handle PropertyCallbackArguments::CallNamedQuery( Handle interceptor, Handle name) { DCHECK_NAME_COMPATIBLE(interceptor, name); Isolate* isolate = this->isolate(); RCS_SCOPE(isolate, RuntimeCallCounterId::kNamedQueryCallback); Handle receiver_check_unsupported; GenericNamedPropertyQueryCallback f = ToCData(interceptor->query()); PREPARE_CALLBACK_INFO_INTERCEPTOR(isolate, f, v8::Integer, interceptor); f(v8::Utils::ToLocal(name), callback_info); return GetReturnValue(isolate); } Handle PropertyCallbackArguments::CallNamedGetter( Handle interceptor, Handle name) { DCHECK_NAME_COMPATIBLE(interceptor, name); Isolate* isolate = this->isolate(); RCS_SCOPE(isolate, RuntimeCallCounterId::kNamedGetterCallback); GenericNamedPropertyGetterCallback f = ToCData(interceptor->getter()); PREPARE_CALLBACK_INFO_INTERCEPTOR(isolate, f, v8::Value, interceptor); f(v8::Utils::ToLocal(name), callback_info); return GetReturnValue(isolate); } Handle PropertyCallbackArguments::CallNamedDescriptor( Handle interceptor, Handle name) { DCHECK_NAME_COMPATIBLE(interceptor, name); Isolate* isolate = this->isolate(); RCS_SCOPE(isolate, RuntimeCallCounterId::kNamedDescriptorCallback); GenericNamedPropertyDescriptorCallback f = ToCData( interceptor->descriptor()); PREPARE_CALLBACK_INFO_INTERCEPTOR(isolate, f, v8::Value, interceptor); f(v8::Utils::ToLocal(name), callback_info); return GetReturnValue(isolate); } Handle PropertyCallbackArguments::CallNamedSetter( Handle interceptor, Handle name, Handle value) { DCHECK_NAME_COMPATIBLE(interceptor, name); Isolate* isolate = this->isolate(); RCS_SCOPE(isolate, RuntimeCallCounterId::kNamedSetterCallback); GenericNamedPropertySetterCallback f = ToCData(interceptor->setter()); Handle has_side_effects; PREPARE_CALLBACK_INFO_INTERCEPTOR(isolate, f, v8::Value, has_side_effects); f(v8::Utils::ToLocal(name), v8::Utils::ToLocal(value), callback_info); return GetReturnValue(isolate); } Handle PropertyCallbackArguments::CallNamedDefiner( Handle interceptor, Handle name, const v8::PropertyDescriptor& desc) { DCHECK_NAME_COMPATIBLE(interceptor, name); Isolate* isolate = this->isolate(); RCS_SCOPE(isolate, RuntimeCallCounterId::kNamedDefinerCallback); GenericNamedPropertyDefinerCallback f = ToCData(interceptor->definer()); Handle has_side_effects; PREPARE_CALLBACK_INFO_INTERCEPTOR(isolate, f, v8::Value, has_side_effects); f(v8::Utils::ToLocal(name), desc, callback_info); return GetReturnValue(isolate); } Handle PropertyCallbackArguments::CallNamedDeleter( Handle interceptor, Handle name) { DCHECK_NAME_COMPATIBLE(interceptor, name); Isolate* isolate = this->isolate(); RCS_SCOPE(isolate, RuntimeCallCounterId::kNamedDeleterCallback); GenericNamedPropertyDeleterCallback f = ToCData(interceptor->deleter()); Handle has_side_effects; PREPARE_CALLBACK_INFO_INTERCEPTOR(isolate, f, v8::Boolean, has_side_effects); f(v8::Utils::ToLocal(name), callback_info); return GetReturnValue(isolate); } // ------------------------------------------------------------------------- // Indexed Interceptor callbacks. Handle PropertyCallbackArguments::CallIndexedEnumerator( Handle interceptor) { DCHECK(!interceptor->is_named()); RCS_SCOPE(isolate(), RuntimeCallCounterId::kIndexedEnumeratorCallback); return CallPropertyEnumerator(interceptor); } Handle PropertyCallbackArguments::CallIndexedQuery( Handle interceptor, uint32_t index) { DCHECK(!interceptor->is_named()); Isolate* isolate = this->isolate(); RCS_SCOPE(isolate, RuntimeCallCounterId::kIndexedQueryCallback); IndexedPropertyQueryCallback f = ToCData(interceptor->query()); PREPARE_CALLBACK_INFO_INTERCEPTOR(isolate, f, v8::Integer, interceptor); f(index, callback_info); return GetReturnValue(isolate); } Handle PropertyCallbackArguments::CallIndexedGetter( Handle interceptor, uint32_t index) { DCHECK(!interceptor->is_named()); RCS_SCOPE(isolate(), RuntimeCallCounterId::kNamedGetterCallback); IndexedPropertyGetterCallback f = ToCData(interceptor->getter()); Isolate* isolate = this->isolate(); PREPARE_CALLBACK_INFO_INTERCEPTOR(isolate, f, v8::Value, interceptor); f(index, callback_info); return GetReturnValue(isolate); } Handle PropertyCallbackArguments::CallIndexedDescriptor( Handle interceptor, uint32_t index) { DCHECK(!interceptor->is_named()); Isolate* isolate = this->isolate(); RCS_SCOPE(isolate, RuntimeCallCounterId::kIndexedDescriptorCallback); IndexedPropertyDescriptorCallback f = ToCData(interceptor->descriptor()); PREPARE_CALLBACK_INFO_INTERCEPTOR(isolate, f, v8::Value, interceptor); f(index, callback_info); return GetReturnValue(isolate); } Handle PropertyCallbackArguments::CallIndexedSetter( Handle interceptor, uint32_t index, Handle value) { DCHECK(!interceptor->is_named()); Isolate* isolate = this->isolate(); RCS_SCOPE(isolate, RuntimeCallCounterId::kIndexedSetterCallback); IndexedPropertySetterCallback f = ToCData(interceptor->setter()); Handle has_side_effects; PREPARE_CALLBACK_INFO_INTERCEPTOR(isolate, f, v8::Value, has_side_effects); f(index, v8::Utils::ToLocal(value), callback_info); return GetReturnValue(isolate); } Handle PropertyCallbackArguments::CallIndexedDefiner( Handle interceptor, uint32_t index, const v8::PropertyDescriptor& desc) { DCHECK(!interceptor->is_named()); Isolate* isolate = this->isolate(); RCS_SCOPE(isolate, RuntimeCallCounterId::kIndexedDefinerCallback); IndexedPropertyDefinerCallback f = ToCData(interceptor->definer()); Handle has_side_effects; PREPARE_CALLBACK_INFO_INTERCEPTOR(isolate, f, v8::Value, has_side_effects); f(index, desc, callback_info); return GetReturnValue(isolate); } Handle PropertyCallbackArguments::CallIndexedDeleter( Handle interceptor, uint32_t index) { DCHECK(!interceptor->is_named()); Isolate* isolate = this->isolate(); RCS_SCOPE(isolate, RuntimeCallCounterId::kIndexedDeleterCallback); IndexedPropertyDeleterCallback f = ToCData(interceptor->deleter()); PREPARE_CALLBACK_INFO_INTERCEPTOR(isolate, f, v8::Boolean, interceptor); f(index, callback_info); return GetReturnValue(isolate); } Handle PropertyCallbackArguments::CallPropertyEnumerator( Handle interceptor) { // For now there is a single enumerator for indexed and named properties. IndexedPropertyEnumeratorCallback f = v8::ToCData(interceptor->enumerator()); // TODO(cbruni): assert same type for indexed and named callback. Isolate* isolate = this->isolate(); PREPARE_CALLBACK_INFO_INTERCEPTOR(isolate, f, v8::Array, interceptor); f(callback_info); return GetReturnValue(isolate); } // ------------------------------------------------------------------------- // Accessors Handle PropertyCallbackArguments::CallAccessorGetter( Handle info, Handle name) { Isolate* isolate = this->isolate(); RCS_SCOPE(isolate, RuntimeCallCounterId::kAccessorGetterCallback); // Unlike interceptor callbacks we know that the property exists, so // the callback is allowed to have side effects. AcceptSideEffects(); AccessorNameGetterCallback f = reinterpret_cast(info->getter(isolate)); PREPARE_CALLBACK_INFO_ACCESSOR(isolate, f, v8::Value, info, handle(receiver(), isolate), ACCESSOR_GETTER); f(v8::Utils::ToLocal(name), callback_info); return GetReturnValue(isolate); } Handle PropertyCallbackArguments::CallAccessorSetter( Handle accessor_info, Handle name, Handle value) { Isolate* isolate = this->isolate(); RCS_SCOPE(isolate, RuntimeCallCounterId::kAccessorSetterCallback); // Unlike interceptor callbacks we know that the property exists, so // the callback is allowed to have side effects. AcceptSideEffects(); AccessorNameSetterCallback f = reinterpret_cast( accessor_info->setter(isolate)); PREPARE_CALLBACK_INFO_ACCESSOR(isolate, f, void, accessor_info, handle(receiver(), isolate), ACCESSOR_SETTER); f(v8::Utils::ToLocal(name), v8::Utils::ToLocal(value), callback_info); return GetReturnValue(isolate); } #undef PREPARE_CALLBACK_INFO_ACCESSOR #undef PREPARE_CALLBACK_INFO_INTERCEPTOR } // namespace internal } // namespace v8 #endif // V8_API_API_ARGUMENTS_INL_H_