Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 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" |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 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. |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 59 | if ((*handle)->IsTheHole(isolate)) return Handle<V>(); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 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 remove 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 | |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 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); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 114 | |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 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); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 118 | |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 119 | inline Handle<Object> Call(GenericNamedPropertySetterCallback f, |
| 120 | Handle<Name> name, Handle<Object> value); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 121 | |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 122 | inline Handle<Object> Call(IndexedPropertySetterCallback f, uint32_t index, |
| 123 | Handle<Object> value); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 124 | |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 125 | inline void Call(AccessorNameSetterCallback f, Handle<Name> name, |
| 126 | Handle<Object> value); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 127 | |
| 128 | private: |
| 129 | inline JSObject* holder() { |
| 130 | return JSObject::cast(this->begin()[T::kHolderIndex]); |
| 131 | } |
| 132 | }; |
| 133 | |
| 134 | class FunctionCallbackArguments |
| 135 | : public CustomArguments<FunctionCallbackInfo<Value> > { |
| 136 | public: |
| 137 | typedef FunctionCallbackInfo<Value> T; |
| 138 | typedef CustomArguments<T> Super; |
| 139 | static const int kArgsLength = T::kArgsLength; |
| 140 | static const int kHolderIndex = T::kHolderIndex; |
| 141 | static const int kDataIndex = T::kDataIndex; |
| 142 | static const int kReturnValueDefaultValueIndex = |
| 143 | T::kReturnValueDefaultValueIndex; |
| 144 | static const int kIsolateIndex = T::kIsolateIndex; |
| 145 | static const int kCalleeIndex = T::kCalleeIndex; |
| 146 | static const int kContextSaveIndex = T::kContextSaveIndex; |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 147 | static const int kNewTargetIndex = T::kNewTargetIndex; |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 148 | |
| 149 | FunctionCallbackArguments(internal::Isolate* isolate, internal::Object* data, |
| 150 | internal::HeapObject* callee, |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 151 | internal::Object* holder, |
| 152 | internal::HeapObject* new_target, |
| 153 | internal::Object** argv, int argc) |
| 154 | : Super(isolate), argv_(argv), argc_(argc) { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 155 | Object** values = begin(); |
| 156 | values[T::kDataIndex] = data; |
| 157 | values[T::kCalleeIndex] = callee; |
| 158 | values[T::kHolderIndex] = holder; |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 159 | values[T::kNewTargetIndex] = new_target; |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 160 | values[T::kContextSaveIndex] = isolate->heap()->the_hole_value(); |
| 161 | values[T::kIsolateIndex] = reinterpret_cast<internal::Object*>(isolate); |
| 162 | // Here the hole is set as default value. |
| 163 | // It cannot escape into js as it's remove in Call below. |
| 164 | values[T::kReturnValueDefaultValueIndex] = |
| 165 | isolate->heap()->the_hole_value(); |
| 166 | values[T::kReturnValueIndex] = isolate->heap()->the_hole_value(); |
| 167 | DCHECK(values[T::kCalleeIndex]->IsJSFunction() || |
| 168 | values[T::kCalleeIndex]->IsFunctionTemplateInfo()); |
| 169 | DCHECK(values[T::kHolderIndex]->IsHeapObject()); |
| 170 | DCHECK(values[T::kIsolateIndex]->IsSmi()); |
| 171 | } |
| 172 | |
| 173 | /* |
| 174 | * The following Call function wraps the calling of all callbacks to handle |
| 175 | * calling either the old or the new style callbacks depending on which one |
| 176 | * has been registered. |
| 177 | * For old callbacks which return an empty handle, the ReturnValue is checked |
| 178 | * and used if it's been set to anything inside the callback. |
| 179 | * New style callbacks always use the return value. |
| 180 | */ |
| 181 | Handle<Object> Call(FunctionCallback f); |
| 182 | |
| 183 | private: |
| 184 | internal::Object** argv_; |
| 185 | int argc_; |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 186 | }; |
| 187 | |
| 188 | } // namespace internal |
| 189 | } // namespace v8 |
| 190 | |
| 191 | #endif // V8_API_ARGUMENTS_H_ |