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:
IterateInstance(ObjectVisitor * v)20 virtual inline void IterateInstance(ObjectVisitor* v) {
21 v->VisitPointers(values_, values_ + kArrayLength);
22 }
23
24 protected:
begin()25 inline Object** begin() { return values_; }
CustomArgumentsBase(Isolate * isolate)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;
~CustomArguments()37 ~CustomArguments() {
38 this->begin()[kReturnValueOffset] =
39 reinterpret_cast<Object*>(kHandleZapValue);
40 }
41
42 protected:
CustomArguments(Isolate * isolate)43 explicit inline CustomArguments(Isolate* isolate) : Super(isolate) {}
44
45 template <typename V>
46 Handle<V> GetReturnValue(Isolate* isolate);
47
isolate()48 inline Isolate* isolate() {
49 return reinterpret_cast<Isolate*>(this->begin()[T::kIsolateIndex]);
50 }
51 };
52
53 template <typename T>
54 template <typename V>
GetReturnValue(Isolate * isolate)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
PropertyCallbackArguments(Isolate * isolate,Object * data,Object * self,JSObject * holder,Object::ShouldThrow should_throw)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:
holder()136 inline JSObject* holder() {
137 return JSObject::cast(this->begin()[T::kHolderIndex]);
138 }
139
140 bool PerformSideEffectCheck(Isolate* isolate, Address function);
141 };
142
143 class FunctionCallbackArguments
144 : public CustomArguments<FunctionCallbackInfo<Value> > {
145 public:
146 typedef FunctionCallbackInfo<Value> T;
147 typedef CustomArguments<T> Super;
148 static const int kArgsLength = T::kArgsLength;
149 static const int kHolderIndex = T::kHolderIndex;
150 static const int kDataIndex = T::kDataIndex;
151 static const int kReturnValueDefaultValueIndex =
152 T::kReturnValueDefaultValueIndex;
153 static const int kIsolateIndex = T::kIsolateIndex;
154 static const int kCalleeIndex = T::kCalleeIndex;
155 static const int kContextSaveIndex = T::kContextSaveIndex;
156 static const int kNewTargetIndex = T::kNewTargetIndex;
157
FunctionCallbackArguments(internal::Isolate * isolate,internal::Object * data,internal::HeapObject * callee,internal::Object * holder,internal::HeapObject * new_target,internal::Object ** argv,int argc)158 FunctionCallbackArguments(internal::Isolate* isolate, internal::Object* data,
159 internal::HeapObject* callee,
160 internal::Object* holder,
161 internal::HeapObject* new_target,
162 internal::Object** argv, int argc)
163 : Super(isolate), argv_(argv), argc_(argc) {
164 Object** values = begin();
165 values[T::kDataIndex] = data;
166 values[T::kCalleeIndex] = callee;
167 values[T::kHolderIndex] = holder;
168 values[T::kNewTargetIndex] = new_target;
169 values[T::kContextSaveIndex] = isolate->heap()->the_hole_value();
170 values[T::kIsolateIndex] = reinterpret_cast<internal::Object*>(isolate);
171 // Here the hole is set as default value.
172 // It cannot escape into js as it's remove in Call below.
173 values[T::kReturnValueDefaultValueIndex] =
174 isolate->heap()->the_hole_value();
175 values[T::kReturnValueIndex] = isolate->heap()->the_hole_value();
176 DCHECK(values[T::kCalleeIndex]->IsJSFunction() ||
177 values[T::kCalleeIndex]->IsFunctionTemplateInfo());
178 DCHECK(values[T::kHolderIndex]->IsHeapObject());
179 DCHECK(values[T::kIsolateIndex]->IsSmi());
180 }
181
182 /*
183 * The following Call function wraps the calling of all callbacks to handle
184 * calling either the old or the new style callbacks depending on which one
185 * has been registered.
186 * For old callbacks which return an empty handle, the ReturnValue is checked
187 * and used if it's been set to anything inside the callback.
188 * New style callbacks always use the return value.
189 */
190 Handle<Object> Call(FunctionCallback f);
191
192 private:
193 internal::Object** argv_;
194 int argc_;
195 };
196
197 } // namespace internal
198 } // namespace v8
199
200 #endif // V8_API_ARGUMENTS_H_
201