1 // Copyright 2012 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.
5 #ifndef V8_ARGUMENTS_H_
6 #define V8_ARGUMENTS_H_
8 #include "src/allocation.h"
9 #include "src/isolate.h"
14 // Arguments provides access to runtime call parameters.
16 // It uses the fact that the instance fields of Arguments
17 // (length_, arguments_) are "overlayed" with the parameters
18 // (no. of parameters, and the parameter pointer) passed so
19 // that inside the C++ function, the parameters passed can
20 // be accessed conveniently:
22 // Object* Runtime_function(Arguments args) {
23 // ... use args[i] here ...
26 // Note that length_ (whose value is in the integer range) is defined
27 // as intptr_t to provide endian-neutrality on 64-bit archs.
29 class Arguments BASE_EMBEDDED {
31 Arguments(int length, Object** arguments)
32 : length_(length), arguments_(arguments) { }
34 Object*& operator[] (int index) {
35 DCHECK(0 <= index && index < length_);
36 return *(reinterpret_cast<Object**>(reinterpret_cast<intptr_t>(arguments_) -
37 index * kPointerSize));
40 template <class S> Handle<S> at(int index) {
41 Object** value = &((*this)[index]);
42 // This cast checks that the object we're accessing does indeed have the
45 return Handle<S>(reinterpret_cast<S**>(value));
48 int smi_at(int index) {
49 return Smi::cast((*this)[index])->value();
52 double number_at(int index) {
53 return (*this)[index]->Number();
56 // Get the total number of arguments including the receiver.
57 int length() const { return static_cast<int>(length_); }
59 Object** arguments() { return arguments_; }
61 Object** lowest_address() { return &this->operator[](length() - 1); }
63 Object** highest_address() { return &this->operator[](0); }
71 // For each type of callback, we have a list of arguments
72 // They are used to generate the Call() functions below
73 // These aren't included in the list as they have duplicate signatures
74 // F(GenericNamedPropertyEnumeratorCallback, ...)
75 // F(GenericNamedPropertyGetterCallback, ...)
77 #define FOR_EACH_CALLBACK_TABLE_MAPPING_0(F) \
78 F(IndexedPropertyEnumeratorCallback, v8::Array)
80 #define FOR_EACH_CALLBACK_TABLE_MAPPING_1(F) \
81 F(AccessorNameGetterCallback, v8::Value, v8::Local<v8::Name>) \
82 F(GenericNamedPropertyQueryCallback, v8::Integer, v8::Local<v8::Name>) \
83 F(GenericNamedPropertyDeleterCallback, v8::Boolean, v8::Local<v8::Name>) \
84 F(IndexedPropertyGetterCallback, v8::Value, uint32_t) \
85 F(IndexedPropertyQueryCallback, v8::Integer, uint32_t) \
86 F(IndexedPropertyDeleterCallback, v8::Boolean, uint32_t)
88 #define FOR_EACH_CALLBACK_TABLE_MAPPING_2(F) \
89 F(GenericNamedPropertySetterCallback, v8::Value, v8::Local<v8::Name>, \
90 v8::Local<v8::Value>) \
91 F(IndexedPropertySetterCallback, v8::Value, uint32_t, v8::Local<v8::Value>)
93 #define FOR_EACH_CALLBACK_TABLE_MAPPING_2_VOID_RETURN(F) \
94 F(AccessorNameSetterCallback, \
96 v8::Local<v8::Name>, \
97 v8::Local<v8::Value>) \
100 // Custom arguments replicate a small segment of stack that can be
101 // accessed through an Arguments object the same way the actual stack
103 template<int kArrayLength>
104 class CustomArgumentsBase : public Relocatable {
106 virtual inline void IterateInstance(ObjectVisitor* v) {
107 v->VisitPointers(values_, values_ + kArrayLength);
110 inline Object** begin() { return values_; }
111 explicit inline CustomArgumentsBase(Isolate* isolate)
112 : Relocatable(isolate) {}
113 Object* values_[kArrayLength];
118 class CustomArguments : public CustomArgumentsBase<T::kArgsLength> {
120 static const int kReturnValueOffset = T::kReturnValueIndex;
122 typedef CustomArgumentsBase<T::kArgsLength> Super;
124 this->begin()[kReturnValueOffset] =
125 reinterpret_cast<Object*>(kHandleZapValue);
129 explicit inline CustomArguments(Isolate* isolate) : Super(isolate) {}
131 template <typename V>
132 v8::Local<V> GetReturnValue(Isolate* isolate);
134 inline Isolate* isolate() {
135 return reinterpret_cast<Isolate*>(this->begin()[T::kIsolateIndex]);
140 class PropertyCallbackArguments
141 : public CustomArguments<PropertyCallbackInfo<Value> > {
143 typedef PropertyCallbackInfo<Value> T;
144 typedef CustomArguments<T> Super;
145 static const int kArgsLength = T::kArgsLength;
146 static const int kThisIndex = T::kThisIndex;
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;
153 PropertyCallbackArguments(Isolate* isolate,
158 Object** values = this->begin();
159 values[T::kThisIndex] = self;
160 values[T::kHolderIndex] = holder;
161 values[T::kDataIndex] = data;
162 values[T::kIsolateIndex] = reinterpret_cast<Object*>(isolate);
163 // Here the hole is set as default value.
164 // It cannot escape into js as it's remove in Call below.
165 values[T::kReturnValueDefaultValueIndex] =
166 isolate->heap()->the_hole_value();
167 values[T::kReturnValueIndex] = isolate->heap()->the_hole_value();
168 DCHECK(values[T::kHolderIndex]->IsHeapObject());
169 DCHECK(values[T::kIsolateIndex]->IsSmi());
173 * The following Call functions wrap the calling of all callbacks to handle
174 * calling either the old or the new style callbacks depending on which one
175 * has been registered.
176 * For old callbacks which return an empty handle, the ReturnValue is checked
177 * and used if it's been set to anything inside the callback.
178 * New style callbacks always use the return value.
180 #define WRITE_CALL_0(Function, ReturnValue) \
181 v8::Local<ReturnValue> Call(Function f);
183 #define WRITE_CALL_1(Function, ReturnValue, Arg1) \
184 v8::Local<ReturnValue> Call(Function f, Arg1 arg1);
186 #define WRITE_CALL_2(Function, ReturnValue, Arg1, Arg2) \
187 v8::Local<ReturnValue> Call(Function f, Arg1 arg1, Arg2 arg2);
189 #define WRITE_CALL_2_VOID(Function, ReturnValue, Arg1, Arg2) \
190 void Call(Function f, Arg1 arg1, Arg2 arg2); \
192 FOR_EACH_CALLBACK_TABLE_MAPPING_0(WRITE_CALL_0)
193 FOR_EACH_CALLBACK_TABLE_MAPPING_1(WRITE_CALL_1)
194 FOR_EACH_CALLBACK_TABLE_MAPPING_2(WRITE_CALL_2)
195 FOR_EACH_CALLBACK_TABLE_MAPPING_2_VOID_RETURN(WRITE_CALL_2_VOID)
200 #undef WRITE_CALL_2_VOID
204 class FunctionCallbackArguments
205 : public CustomArguments<FunctionCallbackInfo<Value> > {
207 typedef FunctionCallbackInfo<Value> T;
208 typedef CustomArguments<T> Super;
209 static const int kArgsLength = T::kArgsLength;
210 static const int kHolderIndex = T::kHolderIndex;
211 static const int kDataIndex = T::kDataIndex;
212 static const int kReturnValueDefaultValueIndex =
213 T::kReturnValueDefaultValueIndex;
214 static const int kIsolateIndex = T::kIsolateIndex;
215 static const int kCalleeIndex = T::kCalleeIndex;
216 static const int kContextSaveIndex = T::kContextSaveIndex;
218 FunctionCallbackArguments(internal::Isolate* isolate,
219 internal::Object* data,
220 internal::JSFunction* callee,
221 internal::Object* holder,
222 internal::Object** argv,
224 bool is_construct_call)
228 is_construct_call_(is_construct_call) {
229 Object** values = begin();
230 values[T::kDataIndex] = data;
231 values[T::kCalleeIndex] = callee;
232 values[T::kHolderIndex] = holder;
233 values[T::kContextSaveIndex] = isolate->heap()->the_hole_value();
234 values[T::kIsolateIndex] = reinterpret_cast<internal::Object*>(isolate);
235 // Here the hole is set as default value.
236 // It cannot escape into js as it's remove in Call below.
237 values[T::kReturnValueDefaultValueIndex] =
238 isolate->heap()->the_hole_value();
239 values[T::kReturnValueIndex] = isolate->heap()->the_hole_value();
240 DCHECK(values[T::kCalleeIndex]->IsJSFunction());
241 DCHECK(values[T::kHolderIndex]->IsHeapObject());
242 DCHECK(values[T::kIsolateIndex]->IsSmi());
246 * The following Call function wraps the calling of all callbacks to handle
247 * calling either the old or the new style callbacks depending on which one
248 * has been registered.
249 * For old callbacks which return an empty handle, the ReturnValue is checked
250 * and used if it's been set to anything inside the callback.
251 * New style callbacks always use the return value.
253 v8::Local<v8::Value> Call(FunctionCallback f);
256 internal::Object** argv_;
258 bool is_construct_call_;
262 double ClobberDoubleRegisters(double x1, double x2, double x3, double x4);
266 #define CLOBBER_DOUBLE_REGISTERS() ClobberDoubleRegisters(1, 2, 3, 4);
268 #define CLOBBER_DOUBLE_REGISTERS()
272 #define RUNTIME_FUNCTION_RETURNS_TYPE(Type, Name) \
273 static INLINE(Type __RT_impl_##Name(Arguments args, Isolate* isolate)); \
274 Type Name(int args_length, Object** args_object, Isolate* isolate) { \
275 CLOBBER_DOUBLE_REGISTERS(); \
276 Arguments args(args_length, args_object); \
277 return __RT_impl_##Name(args, isolate); \
279 static Type __RT_impl_##Name(Arguments args, Isolate* isolate)
282 #define RUNTIME_FUNCTION(Name) RUNTIME_FUNCTION_RETURNS_TYPE(Object*, Name)
283 #define RUNTIME_FUNCTION_RETURN_PAIR(Name) \
284 RUNTIME_FUNCTION_RETURNS_TYPE(ObjectPair, Name)
286 } } // namespace v8::internal
288 #endif // V8_ARGUMENTS_H_