1 // Copyright 2016 the V8 project authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef V8_API_ARGUMENTS_H_ 6 #define V8_API_ARGUMENTS_H_ 7 8 #include "src/api.h" 9 #include "src/isolate.h" 10 11 namespace v8 { 12 namespace internal { 13 14 // Custom arguments replicate a small segment of stack that can be 15 // accessed through an Arguments object the same way the actual stack 16 // can. 17 template <int kArrayLength> 18 class CustomArgumentsBase : public Relocatable { 19 public: 20 virtual inline void IterateInstance(ObjectVisitor* v) { 21 v->VisitPointers(values_, values_ + kArrayLength); 22 } 23 24 protected: 25 inline Object** begin() { return values_; } 26 explicit inline CustomArgumentsBase(Isolate* isolate) 27 : Relocatable(isolate) {} 28 Object* values_[kArrayLength]; 29 }; 30 31 template <typename T> 32 class CustomArguments : public CustomArgumentsBase<T::kArgsLength> { 33 public: 34 static const int kReturnValueOffset = T::kReturnValueIndex; 35 36 typedef CustomArgumentsBase<T::kArgsLength> Super; 37 ~CustomArguments() { 38 this->begin()[kReturnValueOffset] = 39 reinterpret_cast<Object*>(kHandleZapValue); 40 } 41 42 protected: 43 explicit inline CustomArguments(Isolate* isolate) : Super(isolate) {} 44 45 template <typename V> 46 Handle<V> GetReturnValue(Isolate* isolate); 47 48 inline Isolate* isolate() { 49 return reinterpret_cast<Isolate*>(this->begin()[T::kIsolateIndex]); 50 } 51 }; 52 53 template <typename T> 54 template <typename V> 55 Handle<V> CustomArguments<T>::GetReturnValue(Isolate* isolate) { 56 // Check the ReturnValue. 57 Object** handle = &this->begin()[kReturnValueOffset]; 58 // Nothing was set, return empty handle as per previous behaviour. 59 if ((*handle)->IsTheHole(isolate)) return Handle<V>(); 60 Handle<V> result = Handle<V>::cast(Handle<Object>(handle)); 61 result->VerifyApiCallResultType(); 62 return result; 63 } 64 65 class PropertyCallbackArguments 66 : public CustomArguments<PropertyCallbackInfo<Value> > { 67 public: 68 typedef PropertyCallbackInfo<Value> T; 69 typedef CustomArguments<T> Super; 70 static const int kArgsLength = T::kArgsLength; 71 static const int kThisIndex = T::kThisIndex; 72 static const int kHolderIndex = T::kHolderIndex; 73 static const int kDataIndex = T::kDataIndex; 74 static const int kReturnValueDefaultValueIndex = 75 T::kReturnValueDefaultValueIndex; 76 static const int kIsolateIndex = T::kIsolateIndex; 77 static const int kShouldThrowOnErrorIndex = T::kShouldThrowOnErrorIndex; 78 79 PropertyCallbackArguments(Isolate* isolate, Object* data, Object* self, 80 JSObject* holder, Object::ShouldThrow should_throw) 81 : Super(isolate) { 82 Object** values = this->begin(); 83 values[T::kThisIndex] = self; 84 values[T::kHolderIndex] = holder; 85 values[T::kDataIndex] = data; 86 values[T::kIsolateIndex] = reinterpret_cast<Object*>(isolate); 87 values[T::kShouldThrowOnErrorIndex] = 88 Smi::FromInt(should_throw == Object::THROW_ON_ERROR ? 1 : 0); 89 90 // Here the hole is set as default value. 91 // It cannot escape into js as it's removed in Call below. 92 values[T::kReturnValueDefaultValueIndex] = 93 isolate->heap()->the_hole_value(); 94 values[T::kReturnValueIndex] = isolate->heap()->the_hole_value(); 95 DCHECK(values[T::kHolderIndex]->IsHeapObject()); 96 DCHECK(values[T::kIsolateIndex]->IsSmi()); 97 } 98 99 /* 100 * The following Call functions wrap the calling of all callbacks to handle 101 * calling either the old or the new style callbacks depending on which one 102 * has been registered. 103 * For old callbacks which return an empty handle, the ReturnValue is checked 104 * and used if it's been set to anything inside the callback. 105 * New style callbacks always use the return value. 106 */ 107 Handle<JSObject> Call(IndexedPropertyEnumeratorCallback f); 108 109 inline Handle<Object> Call(AccessorNameGetterCallback f, Handle<Name> name); 110 inline Handle<Object> Call(GenericNamedPropertyQueryCallback f, 111 Handle<Name> name); 112 inline Handle<Object> Call(GenericNamedPropertyDeleterCallback f, 113 Handle<Name> name); 114 115 inline Handle<Object> Call(IndexedPropertyGetterCallback f, uint32_t index); 116 inline Handle<Object> Call(IndexedPropertyQueryCallback f, uint32_t index); 117 inline Handle<Object> Call(IndexedPropertyDeleterCallback f, uint32_t index); 118 119 inline Handle<Object> Call(GenericNamedPropertySetterCallback f, 120 Handle<Name> name, Handle<Object> value); 121 122 inline Handle<Object> Call(GenericNamedPropertyDefinerCallback f, 123 Handle<Name> name, 124 const v8::PropertyDescriptor& desc); 125 126 inline Handle<Object> Call(IndexedPropertySetterCallback f, uint32_t index, 127 Handle<Object> value); 128 129 inline Handle<Object> Call(IndexedPropertyDefinerCallback f, uint32_t index, 130 const v8::PropertyDescriptor& desc); 131 132 inline void Call(AccessorNameSetterCallback f, Handle<Name> name, 133 Handle<Object> value); 134 135 private: 136 inline JSObject* holder() { 137 return JSObject::cast(this->begin()[T::kHolderIndex]); 138 } 139 }; 140 141 class FunctionCallbackArguments 142 : public CustomArguments<FunctionCallbackInfo<Value> > { 143 public: 144 typedef FunctionCallbackInfo<Value> T; 145 typedef CustomArguments<T> Super; 146 static const int kArgsLength = T::kArgsLength; 147 static const int kHolderIndex = T::kHolderIndex; 148 static const int kDataIndex = T::kDataIndex; 149 static const int kReturnValueDefaultValueIndex = 150 T::kReturnValueDefaultValueIndex; 151 static const int kIsolateIndex = T::kIsolateIndex; 152 static const int kCalleeIndex = T::kCalleeIndex; 153 static const int kContextSaveIndex = T::kContextSaveIndex; 154 static const int kNewTargetIndex = T::kNewTargetIndex; 155 156 FunctionCallbackArguments(internal::Isolate* isolate, internal::Object* data, 157 internal::HeapObject* callee, 158 internal::Object* holder, 159 internal::HeapObject* new_target, 160 internal::Object** argv, int argc) 161 : Super(isolate), argv_(argv), argc_(argc) { 162 Object** values = begin(); 163 values[T::kDataIndex] = data; 164 values[T::kCalleeIndex] = callee; 165 values[T::kHolderIndex] = holder; 166 values[T::kNewTargetIndex] = new_target; 167 values[T::kContextSaveIndex] = isolate->heap()->the_hole_value(); 168 values[T::kIsolateIndex] = reinterpret_cast<internal::Object*>(isolate); 169 // Here the hole is set as default value. 170 // It cannot escape into js as it's remove in Call below. 171 values[T::kReturnValueDefaultValueIndex] = 172 isolate->heap()->the_hole_value(); 173 values[T::kReturnValueIndex] = isolate->heap()->the_hole_value(); 174 DCHECK(values[T::kCalleeIndex]->IsJSFunction() || 175 values[T::kCalleeIndex]->IsFunctionTemplateInfo()); 176 DCHECK(values[T::kHolderIndex]->IsHeapObject()); 177 DCHECK(values[T::kIsolateIndex]->IsSmi()); 178 } 179 180 /* 181 * The following Call function wraps the calling of all callbacks to handle 182 * calling either the old or the new style callbacks depending on which one 183 * has been registered. 184 * For old callbacks which return an empty handle, the ReturnValue is checked 185 * and used if it's been set to anything inside the callback. 186 * New style callbacks always use the return value. 187 */ 188 Handle<Object> Call(FunctionCallback f); 189 190 private: 191 internal::Object** argv_; 192 int argc_; 193 }; 194 195 } // namespace internal 196 } // namespace v8 197 198 #endif // V8_API_ARGUMENTS_H_ 199