[presubmit] Enable readability/namespace linter checking.
[platform/upstream/v8.git] / src / arguments.h
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.
4
5 #ifndef V8_ARGUMENTS_H_
6 #define V8_ARGUMENTS_H_
7
8 #include "src/allocation.h"
9 #include "src/isolate.h"
10
11 namespace v8 {
12 namespace internal {
13
14 // Arguments provides access to runtime call parameters.
15 //
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:
21 //
22 //   Object* Runtime_function(Arguments args) {
23 //     ... use args[i] here ...
24 //   }
25 //
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.
28
29 class Arguments BASE_EMBEDDED {
30  public:
31   Arguments(int length, Object** arguments)
32       : length_(length), arguments_(arguments) { }
33
34   Object*& operator[] (int index) {
35     DCHECK(0 <= index && index < length_);
36     return *(reinterpret_cast<Object**>(reinterpret_cast<intptr_t>(arguments_) -
37                                         index * kPointerSize));
38   }
39
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
43     // expected type.
44     S::cast(*value);
45     return Handle<S>(reinterpret_cast<S**>(value));
46   }
47
48   int smi_at(int index) {
49     return Smi::cast((*this)[index])->value();
50   }
51
52   double number_at(int index) {
53     return (*this)[index]->Number();
54   }
55
56   // Get the total number of arguments including the receiver.
57   int length() const { return static_cast<int>(length_); }
58
59   Object** arguments() { return arguments_; }
60
61   Object** lowest_address() { return &this->operator[](length() - 1); }
62
63   Object** highest_address() { return &this->operator[](0); }
64
65  private:
66   intptr_t length_;
67   Object** arguments_;
68 };
69
70
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, ...)
76
77 #define FOR_EACH_CALLBACK_TABLE_MAPPING_0(F) \
78   F(IndexedPropertyEnumeratorCallback, v8::Array)
79
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)
87
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>)
92
93 #define FOR_EACH_CALLBACK_TABLE_MAPPING_2_VOID_RETURN(F) \
94   F(AccessorNameSetterCallback, \
95     void, \
96     v8::Local<v8::Name>, \
97     v8::Local<v8::Value>) \
98
99
100 // Custom arguments replicate a small segment of stack that can be
101 // accessed through an Arguments object the same way the actual stack
102 // can.
103 template<int kArrayLength>
104 class CustomArgumentsBase : public Relocatable {
105  public:
106   virtual inline void IterateInstance(ObjectVisitor* v) {
107     v->VisitPointers(values_, values_ + kArrayLength);
108   }
109  protected:
110   inline Object** begin() { return values_; }
111   explicit inline CustomArgumentsBase(Isolate* isolate)
112       : Relocatable(isolate) {}
113   Object* values_[kArrayLength];
114 };
115
116
117 template<typename T>
118 class CustomArguments : public CustomArgumentsBase<T::kArgsLength> {
119  public:
120   static const int kReturnValueOffset = T::kReturnValueIndex;
121
122   typedef CustomArgumentsBase<T::kArgsLength> Super;
123   ~CustomArguments() {
124     this->begin()[kReturnValueOffset] =
125         reinterpret_cast<Object*>(kHandleZapValue);
126   }
127
128  protected:
129   explicit inline CustomArguments(Isolate* isolate) : Super(isolate) {}
130
131   template <typename V>
132   v8::Local<V> GetReturnValue(Isolate* isolate);
133
134   inline Isolate* isolate() {
135     return reinterpret_cast<Isolate*>(this->begin()[T::kIsolateIndex]);
136   }
137 };
138
139
140 class PropertyCallbackArguments
141     : public CustomArguments<PropertyCallbackInfo<Value> > {
142  public:
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;
152
153   PropertyCallbackArguments(Isolate* isolate,
154                             Object* data,
155                             Object* self,
156                             JSObject* holder)
157       : Super(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());
170   }
171
172   /*
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.
179    */
180 #define WRITE_CALL_0(Function, ReturnValue) \
181   v8::Local<ReturnValue> Call(Function f);
182
183 #define WRITE_CALL_1(Function, ReturnValue, Arg1) \
184   v8::Local<ReturnValue> Call(Function f, Arg1 arg1);
185
186 #define WRITE_CALL_2(Function, ReturnValue, Arg1, Arg2) \
187   v8::Local<ReturnValue> Call(Function f, Arg1 arg1, Arg2 arg2);
188
189 #define WRITE_CALL_2_VOID(Function, ReturnValue, Arg1, Arg2)                 \
190   void Call(Function f, Arg1 arg1, Arg2 arg2);                               \
191
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)
196
197 #undef WRITE_CALL_0
198 #undef WRITE_CALL_1
199 #undef WRITE_CALL_2
200 #undef WRITE_CALL_2_VOID
201 };
202
203
204 class FunctionCallbackArguments
205     : public CustomArguments<FunctionCallbackInfo<Value> > {
206  public:
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;
217
218   FunctionCallbackArguments(internal::Isolate* isolate,
219       internal::Object* data,
220       internal::JSFunction* callee,
221       internal::Object* holder,
222       internal::Object** argv,
223       int argc,
224       bool is_construct_call)
225         : Super(isolate),
226           argv_(argv),
227           argc_(argc),
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());
243   }
244
245   /*
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.
252    */
253   v8::Local<v8::Value> Call(FunctionCallback f);
254
255  private:
256   internal::Object** argv_;
257   int argc_;
258   bool is_construct_call_;
259 };
260
261
262 double ClobberDoubleRegisters(double x1, double x2, double x3, double x4);
263
264
265 #ifdef DEBUG
266 #define CLOBBER_DOUBLE_REGISTERS() ClobberDoubleRegisters(1, 2, 3, 4);
267 #else
268 #define CLOBBER_DOUBLE_REGISTERS()
269 #endif
270
271
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);                                \
278 }                                                                        \
279 static Type __RT_impl_##Name(Arguments args, Isolate* isolate)
280
281
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)
285
286 }  // namespace internal
287 }  // namespace v8
288
289 #endif  // V8_ARGUMENTS_H_