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 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
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(IndexedPropertySetterCallback f, uint32_t index,
123 Handle<Object> value);
124
125 inline void Call(AccessorNameSetterCallback f, Handle<Name> name,
126 Handle<Object> value);
127
128 private:
holder()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;
147 static const int kNewTargetIndex = T::kNewTargetIndex;
148
FunctionCallbackArguments(internal::Isolate * isolate,internal::Object * data,internal::HeapObject * callee,internal::Object * holder,internal::HeapObject * new_target,internal::Object ** argv,int argc)149 FunctionCallbackArguments(internal::Isolate* isolate, internal::Object* data,
150 internal::HeapObject* callee,
151 internal::Object* holder,
152 internal::HeapObject* new_target,
153 internal::Object** argv, int argc)
154 : Super(isolate), argv_(argv), argc_(argc) {
155 Object** values = begin();
156 values[T::kDataIndex] = data;
157 values[T::kCalleeIndex] = callee;
158 values[T::kHolderIndex] = holder;
159 values[T::kNewTargetIndex] = new_target;
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_;
186 };
187
188 } // namespace internal
189 } // namespace v8
190
191 #endif // V8_API_ARGUMENTS_H_
192