// 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_ARGUMENTS_INL_H_ #define V8_API_ARGUMENTS_INL_H_ #include "src/api-arguments.h" #include "src/api-inl.h" #include "src/debug/debug.h" #include "src/objects/api-callbacks.h" #include "src/objects/slots-inl.h" #include "src/tracing/trace-event.h" #include "src/vm-state-inl.h" namespace v8 { namespace internal { void Object::VerifyApiCallResultType() { #if DEBUG if (IsSmi()) return; DCHECK(IsHeapObject()); if (!(IsString() || IsSymbol() || IsJSReceiver() || IsHeapNumber() || IsBigInt() || IsUndefined() || IsTrue() || IsFalse() || IsNull())) { FATAL("API call returned invalid object"); } #endif // DEBUG } CustomArgumentsBase::CustomArgumentsBase(Isolate* isolate) : Relocatable(isolate) {} template CustomArguments::~CustomArguments() { slot_at(kReturnValueOffset).store(Object(kHandleZapValue)); } template template Handle CustomArguments::GetReturnValue(Isolate* isolate) { // Check the ReturnValue. FullObjectSlot slot = slot_at(kReturnValueOffset); // Nothing was set, return empty handle as per previous behaviour. if ((*slot)->IsTheHole(isolate)) return Handle(); Handle result = Handle::cast(Handle(slot.location())); result->VerifyApiCallResultType(); return result; } inline JSObject PropertyCallbackArguments::holder() { return JSObject::cast(*slot_at(T::kHolderIndex)); } inline Object PropertyCallbackArguments::receiver() { return *slot_at(T::kThisIndex); } inline JSObject FunctionCallbackArguments::holder() { return JSObject::cast(*slot_at(T::kHolderIndex)); } #define FOR_EACH_CALLBACK(F) \ F(Query, query, Object, v8::Integer, interceptor) \ F(Deleter, deleter, Object, v8::Boolean, Handle()) #define DCHECK_NAME_COMPATIBLE(interceptor, name) \ DCHECK(interceptor->is_named()); \ DCHECK(!name->IsPrivate()); \ DCHECK_IMPLIES(name->IsSymbol(), interceptor->can_intercept_symbols()); #define PREPARE_CALLBACK_INFO(ISOLATE, F, RETURN_VALUE, API_RETURN_TYPE, \ CALLBACK_INFO, RECEIVER, ACCESSOR_KIND) \ if (ISOLATE->debug_execution_mode() == DebugInfo::kSideEffects && \ !ISOLATE->debug()->PerformSideEffectCheckForCallback( \ CALLBACK_INFO, RECEIVER, Debug::k##ACCESSOR_KIND)) { \ return RETURN_VALUE(); \ } \ VMState state(ISOLATE); \ ExternalCallbackScope call_scope(ISOLATE, FUNCTION_ADDR(F)); \ PropertyCallbackInfo callback_info(values_); #define PREPARE_CALLBACK_INFO_FAIL_SIDE_EFFECT_CHECK(ISOLATE, F, RETURN_VALUE, \ API_RETURN_TYPE) \ if (ISOLATE->debug_execution_mode() == DebugInfo::kSideEffects) { \ return RETURN_VALUE(); \ } \ VMState state(ISOLATE); \ ExternalCallbackScope call_scope(ISOLATE, FUNCTION_ADDR(F)); \ PropertyCallbackInfo callback_info(values_); #define CREATE_NAMED_CALLBACK(FUNCTION, TYPE, RETURN_TYPE, API_RETURN_TYPE, \ INFO_FOR_SIDE_EFFECT) \ Handle PropertyCallbackArguments::CallNamed##FUNCTION( \ Handle interceptor, Handle name) { \ DCHECK_NAME_COMPATIBLE(interceptor, name); \ Isolate* isolate = this->isolate(); \ RuntimeCallTimerScope timer( \ isolate, RuntimeCallCounterId::kNamed##FUNCTION##Callback); \ Handle receiver_check_unsupported; \ GenericNamedProperty##FUNCTION##Callback f = \ ToCData( \ interceptor->TYPE()); \ PREPARE_CALLBACK_INFO(isolate, f, Handle, API_RETURN_TYPE, \ INFO_FOR_SIDE_EFFECT, receiver_check_unsupported, \ NotAccessor); \ LOG(isolate, \ ApiNamedPropertyAccess("interceptor-named-" #TYPE, holder(), *name)); \ f(v8::Utils::ToLocal(name), callback_info); \ return GetReturnValue(isolate); \ } FOR_EACH_CALLBACK(CREATE_NAMED_CALLBACK) #undef CREATE_NAMED_CALLBACK #define CREATE_INDEXED_CALLBACK(FUNCTION, TYPE, RETURN_TYPE, API_RETURN_TYPE, \ INFO_FOR_SIDE_EFFECT) \ Handle PropertyCallbackArguments::CallIndexed##FUNCTION( \ Handle interceptor, uint32_t index) { \ DCHECK(!interceptor->is_named()); \ Isolate* isolate = this->isolate(); \ RuntimeCallTimerScope timer( \ isolate, RuntimeCallCounterId::kIndexed##FUNCTION##Callback); \ Handle receiver_check_unsupported; \ IndexedProperty##FUNCTION##Callback f = \ ToCData(interceptor->TYPE()); \ PREPARE_CALLBACK_INFO(isolate, f, Handle, API_RETURN_TYPE, \ INFO_FOR_SIDE_EFFECT, receiver_check_unsupported, \ NotAccessor); \ LOG(isolate, ApiIndexedPropertyAccess("interceptor-indexed-" #TYPE, \ holder(), index)); \ f(index, callback_info); \ return GetReturnValue(isolate); \ } FOR_EACH_CALLBACK(CREATE_INDEXED_CALLBACK) #undef FOR_EACH_CALLBACK #undef CREATE_INDEXED_CALLBACK Handle FunctionCallbackArguments::Call(CallHandlerInfo handler) { Isolate* isolate = this->isolate(); LOG(isolate, ApiObjectAccess("call", holder())); RuntimeCallTimerScope timer(isolate, RuntimeCallCounterId::kFunctionCallback); v8::FunctionCallback f = v8::ToCData(handler->callback()); Handle receiver_check_unsupported; if (isolate->debug_execution_mode() == DebugInfo::kSideEffects && !isolate->debug()->PerformSideEffectCheckForCallback( handle(handler, isolate), receiver_check_unsupported, Debug::kNotAccessor)) { return Handle(); } VMState state(isolate); ExternalCallbackScope call_scope(isolate, FUNCTION_ADDR(f)); FunctionCallbackInfo info(values_, argv_, argc_); f(info); return GetReturnValue(isolate); } Handle PropertyCallbackArguments::CallNamedEnumerator( Handle interceptor) { DCHECK(interceptor->is_named()); LOG(isolate(), ApiObjectAccess("interceptor-named-enumerator", holder())); RuntimeCallTimerScope timer(isolate(), RuntimeCallCounterId::kNamedEnumeratorCallback); return CallPropertyEnumerator(interceptor); } Handle PropertyCallbackArguments::CallIndexedEnumerator( Handle interceptor) { DCHECK(!interceptor->is_named()); LOG(isolate(), ApiObjectAccess("interceptor-indexed-enumerator", holder())); RuntimeCallTimerScope timer(isolate(), RuntimeCallCounterId::kIndexedEnumeratorCallback); return CallPropertyEnumerator(interceptor); } Handle PropertyCallbackArguments::CallNamedGetter( Handle interceptor, Handle name) { DCHECK_NAME_COMPATIBLE(interceptor, name); Isolate* isolate = this->isolate(); RuntimeCallTimerScope timer(isolate, RuntimeCallCounterId::kNamedGetterCallback); LOG(isolate, ApiNamedPropertyAccess("interceptor-named-getter", holder(), *name)); GenericNamedPropertyGetterCallback f = ToCData(interceptor->getter()); return BasicCallNamedGetterCallback(f, name, interceptor); } Handle PropertyCallbackArguments::CallNamedDescriptor( Handle interceptor, Handle name) { DCHECK_NAME_COMPATIBLE(interceptor, name); Isolate* isolate = this->isolate(); RuntimeCallTimerScope timer(isolate, RuntimeCallCounterId::kNamedDescriptorCallback); LOG(isolate, ApiNamedPropertyAccess("interceptor-named-descriptor", holder(), *name)); GenericNamedPropertyDescriptorCallback f = ToCData( interceptor->descriptor()); return BasicCallNamedGetterCallback(f, name, interceptor); } Handle PropertyCallbackArguments::BasicCallNamedGetterCallback( GenericNamedPropertyGetterCallback f, Handle name, Handle info, Handle receiver) { DCHECK(!name->IsPrivate()); Isolate* isolate = this->isolate(); PREPARE_CALLBACK_INFO(isolate, f, Handle, v8::Value, info, receiver, Getter); f(v8::Utils::ToLocal(name), callback_info); return GetReturnValue(isolate); } Handle PropertyCallbackArguments::CallNamedSetter( Handle interceptor, Handle name, Handle value) { DCHECK_NAME_COMPATIBLE(interceptor, name); GenericNamedPropertySetterCallback f = ToCData(interceptor->setter()); Isolate* isolate = this->isolate(); RuntimeCallTimerScope timer(isolate, RuntimeCallCounterId::kNamedSetterCallback); PREPARE_CALLBACK_INFO_FAIL_SIDE_EFFECT_CHECK(isolate, f, Handle, v8::Value); LOG(isolate, ApiNamedPropertyAccess("interceptor-named-set", holder(), *name)); 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(); RuntimeCallTimerScope timer(isolate, RuntimeCallCounterId::kNamedDefinerCallback); GenericNamedPropertyDefinerCallback f = ToCData(interceptor->definer()); PREPARE_CALLBACK_INFO_FAIL_SIDE_EFFECT_CHECK(isolate, f, Handle, v8::Value); LOG(isolate, ApiNamedPropertyAccess("interceptor-named-define", holder(), *name)); f(v8::Utils::ToLocal(name), desc, callback_info); return GetReturnValue(isolate); } Handle PropertyCallbackArguments::CallIndexedSetter( Handle interceptor, uint32_t index, Handle value) { DCHECK(!interceptor->is_named()); Isolate* isolate = this->isolate(); RuntimeCallTimerScope timer(isolate, RuntimeCallCounterId::kIndexedSetterCallback); IndexedPropertySetterCallback f = ToCData(interceptor->setter()); PREPARE_CALLBACK_INFO_FAIL_SIDE_EFFECT_CHECK(isolate, f, Handle, v8::Value); LOG(isolate, ApiIndexedPropertyAccess("interceptor-indexed-set", holder(), index)); 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(); RuntimeCallTimerScope timer(isolate, RuntimeCallCounterId::kIndexedDefinerCallback); IndexedPropertyDefinerCallback f = ToCData(interceptor->definer()); PREPARE_CALLBACK_INFO_FAIL_SIDE_EFFECT_CHECK(isolate, f, Handle, v8::Value); LOG(isolate, ApiIndexedPropertyAccess("interceptor-indexed-define", holder(), index)); f(index, desc, callback_info); return GetReturnValue(isolate); } Handle PropertyCallbackArguments::CallIndexedGetter( Handle interceptor, uint32_t index) { DCHECK(!interceptor->is_named()); Isolate* isolate = this->isolate(); RuntimeCallTimerScope timer(isolate, RuntimeCallCounterId::kNamedGetterCallback); LOG(isolate, ApiIndexedPropertyAccess("interceptor-indexed-getter", holder(), index)); IndexedPropertyGetterCallback f = ToCData(interceptor->getter()); return BasicCallIndexedGetterCallback(f, index, interceptor); } Handle PropertyCallbackArguments::CallIndexedDescriptor( Handle interceptor, uint32_t index) { DCHECK(!interceptor->is_named()); Isolate* isolate = this->isolate(); RuntimeCallTimerScope timer(isolate, RuntimeCallCounterId::kIndexedDescriptorCallback); LOG(isolate, ApiIndexedPropertyAccess("interceptor-indexed-descriptor", holder(), index)); IndexedPropertyDescriptorCallback f = ToCData(interceptor->descriptor()); return BasicCallIndexedGetterCallback(f, index, interceptor); } Handle PropertyCallbackArguments::BasicCallIndexedGetterCallback( IndexedPropertyGetterCallback f, uint32_t index, Handle info) { Isolate* isolate = this->isolate(); Handle receiver_check_unsupported; PREPARE_CALLBACK_INFO(isolate, f, Handle, v8::Value, info, receiver_check_unsupported, Getter); 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(); Handle receiver_check_unsupported; PREPARE_CALLBACK_INFO(isolate, f, Handle, v8::Array, interceptor, receiver_check_unsupported, NotAccessor); f(callback_info); return GetReturnValue(isolate); } // ------------------------------------------------------------------------- // Accessors Handle PropertyCallbackArguments::CallAccessorGetter( Handle info, Handle name) { Isolate* isolate = this->isolate(); RuntimeCallTimerScope timer(isolate, RuntimeCallCounterId::kAccessorGetterCallback); LOG(isolate, ApiNamedPropertyAccess("accessor-getter", holder(), *name)); AccessorNameGetterCallback f = ToCData(info->getter()); return BasicCallNamedGetterCallback(f, name, info, handle(receiver(), isolate)); } Handle PropertyCallbackArguments::CallAccessorSetter( Handle accessor_info, Handle name, Handle value) { Isolate* isolate = this->isolate(); RuntimeCallTimerScope timer(isolate, RuntimeCallCounterId::kAccessorSetterCallback); AccessorNameSetterCallback f = ToCData(accessor_info->setter()); PREPARE_CALLBACK_INFO(isolate, f, Handle, void, accessor_info, handle(receiver(), isolate), Setter); LOG(isolate, ApiNamedPropertyAccess("accessor-setter", holder(), *name)); f(v8::Utils::ToLocal(name), v8::Utils::ToLocal(value), callback_info); return GetReturnValue(isolate); } #undef PREPARE_CALLBACK_INFO #undef PREPARE_CALLBACK_INFO_FAIL_SIDE_EFFECT_CHECK } // namespace internal } // namespace v8 #endif // V8_API_ARGUMENTS_INL_H_