11cb0ef41Sopenharmony_ci// Copyright 2016 the V8 project authors. All rights reserved. 21cb0ef41Sopenharmony_ci// Use of this source code is governed by a BSD-style license that can be 31cb0ef41Sopenharmony_ci// found in the LICENSE file. 41cb0ef41Sopenharmony_ci 51cb0ef41Sopenharmony_ci#ifndef V8_API_API_ARGUMENTS_H_ 61cb0ef41Sopenharmony_ci#define V8_API_API_ARGUMENTS_H_ 71cb0ef41Sopenharmony_ci 81cb0ef41Sopenharmony_ci#include "include/v8-template.h" 91cb0ef41Sopenharmony_ci#include "src/api/api.h" 101cb0ef41Sopenharmony_ci#include "src/debug/debug.h" 111cb0ef41Sopenharmony_ci#include "src/execution/isolate.h" 121cb0ef41Sopenharmony_ci#include "src/objects/slots.h" 131cb0ef41Sopenharmony_ci#include "src/objects/visitors.h" 141cb0ef41Sopenharmony_ci 151cb0ef41Sopenharmony_cinamespace v8 { 161cb0ef41Sopenharmony_cinamespace internal { 171cb0ef41Sopenharmony_ci 181cb0ef41Sopenharmony_ci// Custom arguments replicate a small segment of stack that can be 191cb0ef41Sopenharmony_ci// accessed through an Arguments object the same way the actual stack 201cb0ef41Sopenharmony_ci// can. 211cb0ef41Sopenharmony_ciclass CustomArgumentsBase : public Relocatable { 221cb0ef41Sopenharmony_ci protected: 231cb0ef41Sopenharmony_ci explicit inline CustomArgumentsBase(Isolate* isolate); 241cb0ef41Sopenharmony_ci}; 251cb0ef41Sopenharmony_ci 261cb0ef41Sopenharmony_citemplate <typename T> 271cb0ef41Sopenharmony_ciclass CustomArguments : public CustomArgumentsBase { 281cb0ef41Sopenharmony_ci public: 291cb0ef41Sopenharmony_ci static const int kReturnValueOffset = T::kReturnValueIndex; 301cb0ef41Sopenharmony_ci 311cb0ef41Sopenharmony_ci ~CustomArguments() override; 321cb0ef41Sopenharmony_ci 331cb0ef41Sopenharmony_ci inline void IterateInstance(RootVisitor* v) override { 341cb0ef41Sopenharmony_ci v->VisitRootPointers(Root::kRelocatable, nullptr, slot_at(0), 351cb0ef41Sopenharmony_ci slot_at(T::kArgsLength)); 361cb0ef41Sopenharmony_ci } 371cb0ef41Sopenharmony_ci 381cb0ef41Sopenharmony_ci protected: 391cb0ef41Sopenharmony_ci explicit inline CustomArguments(Isolate* isolate) 401cb0ef41Sopenharmony_ci : CustomArgumentsBase(isolate) {} 411cb0ef41Sopenharmony_ci 421cb0ef41Sopenharmony_ci template <typename V> 431cb0ef41Sopenharmony_ci Handle<V> GetReturnValue(Isolate* isolate); 441cb0ef41Sopenharmony_ci 451cb0ef41Sopenharmony_ci inline Isolate* isolate() { 461cb0ef41Sopenharmony_ci return reinterpret_cast<Isolate*>((*slot_at(T::kIsolateIndex)).ptr()); 471cb0ef41Sopenharmony_ci } 481cb0ef41Sopenharmony_ci 491cb0ef41Sopenharmony_ci inline FullObjectSlot slot_at(int index) { 501cb0ef41Sopenharmony_ci // This allows index == T::kArgsLength so "one past the end" slots 511cb0ef41Sopenharmony_ci // can be retrieved for iterating purposes. 521cb0ef41Sopenharmony_ci DCHECK_LE(static_cast<unsigned>(index), 531cb0ef41Sopenharmony_ci static_cast<unsigned>(T::kArgsLength)); 541cb0ef41Sopenharmony_ci return FullObjectSlot(values_ + index); 551cb0ef41Sopenharmony_ci } 561cb0ef41Sopenharmony_ci Address values_[T::kArgsLength]; 571cb0ef41Sopenharmony_ci}; 581cb0ef41Sopenharmony_ci 591cb0ef41Sopenharmony_ci// Note: Calling args.Call() sets the return value on args. For multiple 601cb0ef41Sopenharmony_ci// Call()'s, a new args should be used every time. 611cb0ef41Sopenharmony_ciclass PropertyCallbackArguments 621cb0ef41Sopenharmony_ci : public CustomArguments<PropertyCallbackInfo<Value> > { 631cb0ef41Sopenharmony_ci public: 641cb0ef41Sopenharmony_ci using T = PropertyCallbackInfo<Value>; 651cb0ef41Sopenharmony_ci using Super = CustomArguments<T>; 661cb0ef41Sopenharmony_ci static const int kArgsLength = T::kArgsLength; 671cb0ef41Sopenharmony_ci static const int kThisIndex = T::kThisIndex; 681cb0ef41Sopenharmony_ci static const int kHolderIndex = T::kHolderIndex; 691cb0ef41Sopenharmony_ci static const int kDataIndex = T::kDataIndex; 701cb0ef41Sopenharmony_ci static const int kReturnValueDefaultValueIndex = 711cb0ef41Sopenharmony_ci T::kReturnValueDefaultValueIndex; 721cb0ef41Sopenharmony_ci static const int kIsolateIndex = T::kIsolateIndex; 731cb0ef41Sopenharmony_ci static const int kShouldThrowOnErrorIndex = T::kShouldThrowOnErrorIndex; 741cb0ef41Sopenharmony_ci 751cb0ef41Sopenharmony_ci PropertyCallbackArguments(Isolate* isolate, Object data, Object self, 761cb0ef41Sopenharmony_ci JSObject holder, Maybe<ShouldThrow> should_throw); 771cb0ef41Sopenharmony_ci 781cb0ef41Sopenharmony_ci // Don't copy PropertyCallbackArguments, because they would both have the 791cb0ef41Sopenharmony_ci // same prev_ pointer. 801cb0ef41Sopenharmony_ci PropertyCallbackArguments(const PropertyCallbackArguments&) = delete; 811cb0ef41Sopenharmony_ci PropertyCallbackArguments& operator=(const PropertyCallbackArguments&) = 821cb0ef41Sopenharmony_ci delete; 831cb0ef41Sopenharmony_ci 841cb0ef41Sopenharmony_ci // ------------------------------------------------------------------------- 851cb0ef41Sopenharmony_ci // Accessor Callbacks 861cb0ef41Sopenharmony_ci // Also used for AccessorSetterCallback. 871cb0ef41Sopenharmony_ci inline Handle<Object> CallAccessorSetter(Handle<AccessorInfo> info, 881cb0ef41Sopenharmony_ci Handle<Name> name, 891cb0ef41Sopenharmony_ci Handle<Object> value); 901cb0ef41Sopenharmony_ci // Also used for AccessorGetterCallback, AccessorNameGetterCallback. 911cb0ef41Sopenharmony_ci inline Handle<Object> CallAccessorGetter(Handle<AccessorInfo> info, 921cb0ef41Sopenharmony_ci Handle<Name> name); 931cb0ef41Sopenharmony_ci 941cb0ef41Sopenharmony_ci // ------------------------------------------------------------------------- 951cb0ef41Sopenharmony_ci // Named Interceptor Callbacks 961cb0ef41Sopenharmony_ci inline Handle<Object> CallNamedQuery(Handle<InterceptorInfo> interceptor, 971cb0ef41Sopenharmony_ci Handle<Name> name); 981cb0ef41Sopenharmony_ci inline Handle<Object> CallNamedGetter(Handle<InterceptorInfo> interceptor, 991cb0ef41Sopenharmony_ci Handle<Name> name); 1001cb0ef41Sopenharmony_ci inline Handle<Object> CallNamedSetter(Handle<InterceptorInfo> interceptor, 1011cb0ef41Sopenharmony_ci Handle<Name> name, 1021cb0ef41Sopenharmony_ci Handle<Object> value); 1031cb0ef41Sopenharmony_ci inline Handle<Object> CallNamedDefiner(Handle<InterceptorInfo> interceptor, 1041cb0ef41Sopenharmony_ci Handle<Name> name, 1051cb0ef41Sopenharmony_ci const v8::PropertyDescriptor& desc); 1061cb0ef41Sopenharmony_ci inline Handle<Object> CallNamedDeleter(Handle<InterceptorInfo> interceptor, 1071cb0ef41Sopenharmony_ci Handle<Name> name); 1081cb0ef41Sopenharmony_ci inline Handle<Object> CallNamedDescriptor(Handle<InterceptorInfo> interceptor, 1091cb0ef41Sopenharmony_ci Handle<Name> name); 1101cb0ef41Sopenharmony_ci inline Handle<JSObject> CallNamedEnumerator( 1111cb0ef41Sopenharmony_ci Handle<InterceptorInfo> interceptor); 1121cb0ef41Sopenharmony_ci 1131cb0ef41Sopenharmony_ci // ------------------------------------------------------------------------- 1141cb0ef41Sopenharmony_ci // Indexed Interceptor Callbacks 1151cb0ef41Sopenharmony_ci inline Handle<Object> CallIndexedQuery(Handle<InterceptorInfo> interceptor, 1161cb0ef41Sopenharmony_ci uint32_t index); 1171cb0ef41Sopenharmony_ci inline Handle<Object> CallIndexedGetter(Handle<InterceptorInfo> interceptor, 1181cb0ef41Sopenharmony_ci uint32_t index); 1191cb0ef41Sopenharmony_ci inline Handle<Object> CallIndexedSetter(Handle<InterceptorInfo> interceptor, 1201cb0ef41Sopenharmony_ci uint32_t index, Handle<Object> value); 1211cb0ef41Sopenharmony_ci inline Handle<Object> CallIndexedDefiner(Handle<InterceptorInfo> interceptor, 1221cb0ef41Sopenharmony_ci uint32_t index, 1231cb0ef41Sopenharmony_ci const v8::PropertyDescriptor& desc); 1241cb0ef41Sopenharmony_ci inline Handle<Object> CallIndexedDeleter(Handle<InterceptorInfo> interceptor, 1251cb0ef41Sopenharmony_ci uint32_t index); 1261cb0ef41Sopenharmony_ci inline Handle<Object> CallIndexedDescriptor( 1271cb0ef41Sopenharmony_ci Handle<InterceptorInfo> interceptor, uint32_t index); 1281cb0ef41Sopenharmony_ci inline Handle<JSObject> CallIndexedEnumerator( 1291cb0ef41Sopenharmony_ci Handle<InterceptorInfo> interceptor); 1301cb0ef41Sopenharmony_ci 1311cb0ef41Sopenharmony_ci private: 1321cb0ef41Sopenharmony_ci /* 1331cb0ef41Sopenharmony_ci * The following Call functions wrap the calling of all callbacks to handle 1341cb0ef41Sopenharmony_ci * calling either the old or the new style callbacks depending on which one 1351cb0ef41Sopenharmony_ci * has been registered. 1361cb0ef41Sopenharmony_ci * For old callbacks which return an empty handle, the ReturnValue is checked 1371cb0ef41Sopenharmony_ci * and used if it's been set to anything inside the callback. 1381cb0ef41Sopenharmony_ci * New style callbacks always use the return value. 1391cb0ef41Sopenharmony_ci */ 1401cb0ef41Sopenharmony_ci inline Handle<JSObject> CallPropertyEnumerator( 1411cb0ef41Sopenharmony_ci Handle<InterceptorInfo> interceptor); 1421cb0ef41Sopenharmony_ci 1431cb0ef41Sopenharmony_ci inline Handle<Object> BasicCallIndexedGetterCallback( 1441cb0ef41Sopenharmony_ci IndexedPropertyGetterCallback f, uint32_t index, Handle<Object> info); 1451cb0ef41Sopenharmony_ci inline Handle<Object> BasicCallNamedGetterCallback( 1461cb0ef41Sopenharmony_ci GenericNamedPropertyGetterCallback f, Handle<Name> name, 1471cb0ef41Sopenharmony_ci Handle<Object> info, Handle<Object> receiver = Handle<Object>()); 1481cb0ef41Sopenharmony_ci 1491cb0ef41Sopenharmony_ci inline JSObject holder(); 1501cb0ef41Sopenharmony_ci inline Object receiver(); 1511cb0ef41Sopenharmony_ci}; 1521cb0ef41Sopenharmony_ci 1531cb0ef41Sopenharmony_ciclass FunctionCallbackArguments 1541cb0ef41Sopenharmony_ci : public CustomArguments<FunctionCallbackInfo<Value> > { 1551cb0ef41Sopenharmony_ci public: 1561cb0ef41Sopenharmony_ci using T = FunctionCallbackInfo<Value>; 1571cb0ef41Sopenharmony_ci using Super = CustomArguments<T>; 1581cb0ef41Sopenharmony_ci static const int kArgsLength = T::kArgsLength; 1591cb0ef41Sopenharmony_ci static const int kHolderIndex = T::kHolderIndex; 1601cb0ef41Sopenharmony_ci static const int kDataIndex = T::kDataIndex; 1611cb0ef41Sopenharmony_ci static const int kReturnValueDefaultValueIndex = 1621cb0ef41Sopenharmony_ci T::kReturnValueDefaultValueIndex; 1631cb0ef41Sopenharmony_ci static const int kIsolateIndex = T::kIsolateIndex; 1641cb0ef41Sopenharmony_ci static const int kNewTargetIndex = T::kNewTargetIndex; 1651cb0ef41Sopenharmony_ci 1661cb0ef41Sopenharmony_ci FunctionCallbackArguments(Isolate* isolate, Object data, HeapObject callee, 1671cb0ef41Sopenharmony_ci Object holder, HeapObject new_target, Address* argv, 1681cb0ef41Sopenharmony_ci int argc); 1691cb0ef41Sopenharmony_ci 1701cb0ef41Sopenharmony_ci /* 1711cb0ef41Sopenharmony_ci * The following Call function wraps the calling of all callbacks to handle 1721cb0ef41Sopenharmony_ci * calling either the old or the new style callbacks depending on which one 1731cb0ef41Sopenharmony_ci * has been registered. 1741cb0ef41Sopenharmony_ci * For old callbacks which return an empty handle, the ReturnValue is checked 1751cb0ef41Sopenharmony_ci * and used if it's been set to anything inside the callback. 1761cb0ef41Sopenharmony_ci * New style callbacks always use the return value. 1771cb0ef41Sopenharmony_ci */ 1781cb0ef41Sopenharmony_ci inline Handle<Object> Call(CallHandlerInfo handler); 1791cb0ef41Sopenharmony_ci 1801cb0ef41Sopenharmony_ci private: 1811cb0ef41Sopenharmony_ci inline JSReceiver holder(); 1821cb0ef41Sopenharmony_ci 1831cb0ef41Sopenharmony_ci internal::Address* argv_; 1841cb0ef41Sopenharmony_ci int argc_; 1851cb0ef41Sopenharmony_ci}; 1861cb0ef41Sopenharmony_ci 1871cb0ef41Sopenharmony_ci} // namespace internal 1881cb0ef41Sopenharmony_ci} // namespace v8 1891cb0ef41Sopenharmony_ci 1901cb0ef41Sopenharmony_ci#endif // V8_API_API_ARGUMENTS_H_ 191