V8 API Reference, 7.2.502.16 (for Deno 0.2.4)
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/handles.h"
10 #include "src/objects.h"
11 #include "src/objects/slots.h"
12 #include "src/tracing/trace-event.h"
13 
14 namespace v8 {
15 namespace internal {
16 
17 // Arguments provides access to runtime call parameters.
18 //
19 // It uses the fact that the instance fields of Arguments
20 // (length_, arguments_) are "overlayed" with the parameters
21 // (no. of parameters, and the parameter pointer) passed so
22 // that inside the C++ function, the parameters passed can
23 // be accessed conveniently:
24 //
25 // Object* Runtime_function(Arguments args) {
26 // ... use args[i] here ...
27 // }
28 //
29 // Note that length_ (whose value is in the integer range) is defined
30 // as intptr_t to provide endian-neutrality on 64-bit archs.
31 
32 class Arguments {
33  public:
34  Arguments(int length, Address* arguments)
35  : length_(length), arguments_(arguments) {
36  DCHECK_GE(length_, 0);
37  }
38 
39  ObjectPtr operator[](int index) {
40  return ObjectPtr(*address_of_arg_at(index));
41  }
42 
43  template <class S = Object>
44  inline Handle<S> at(int index);
45 
46  inline int smi_at(int index);
47 
48  inline double number_at(int index);
49 
50  inline void set_at(int index, Object* value) {
51  *address_of_arg_at(index) = value->ptr();
52  }
53 
54  inline ObjectSlot slot_at(int index) {
55  return ObjectSlot(address_of_arg_at(index));
56  }
57 
58  inline Address* address_of_arg_at(int index) {
59  DCHECK_GE(index, 0);
60  DCHECK_LT(static_cast<uint32_t>(index), static_cast<uint32_t>(length_));
61  return reinterpret_cast<Address*>(reinterpret_cast<Address>(arguments_) -
62  index * kPointerSize);
63  }
64 
65  // Get the total number of arguments including the receiver.
66  int length() const { return static_cast<int>(length_); }
67 
68  // Arguments on the stack are in reverse order (compared to an array).
69  ObjectSlot first_slot() { return slot_at(length() - 1); }
70  ObjectSlot last_slot() { return slot_at(0); }
71 
72  private:
73  intptr_t length_;
74  Address* arguments_;
75 };
76 
77 template <>
78 inline Handle<Object> Arguments::at(int index) {
79  return Handle<Object>(address_of_arg_at(index));
80 }
81 
82 double ClobberDoubleRegisters(double x1, double x2, double x3, double x4);
83 
84 #ifdef DEBUG
85 #define CLOBBER_DOUBLE_REGISTERS() ClobberDoubleRegisters(1, 2, 3, 4);
86 #else
87 #define CLOBBER_DOUBLE_REGISTERS()
88 #endif
89 
90 // TODO(cbruni): add global flag to check whether any tracing events have been
91 // enabled.
92 #define RUNTIME_FUNCTION_RETURNS_TYPE(Type, Name) \
93  static V8_INLINE Type __RT_impl_##Name(Arguments args, Isolate* isolate); \
94  \
95  V8_NOINLINE static Type Stats_##Name(int args_length, Address* args_object, \
96  Isolate* isolate) { \
97  RuntimeCallTimerScope timer(isolate, RuntimeCallCounterId::k##Name); \
98  TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("v8.runtime"), \
99  "V8.Runtime_" #Name); \
100  Arguments args(args_length, args_object); \
101  return __RT_impl_##Name(args, isolate); \
102  } \
103  \
104  Type Name(int args_length, Address* args_object, Isolate* isolate) { \
105  DCHECK(isolate->context().is_null() || isolate->context()->IsContext()); \
106  CLOBBER_DOUBLE_REGISTERS(); \
107  if (V8_UNLIKELY(FLAG_runtime_stats)) { \
108  return Stats_##Name(args_length, args_object, isolate); \
109  } \
110  Arguments args(args_length, args_object); \
111  return __RT_impl_##Name(args, isolate); \
112  } \
113  \
114  static Type __RT_impl_##Name(Arguments args, Isolate* isolate)
115 
116 #define RUNTIME_FUNCTION(Name) RUNTIME_FUNCTION_RETURNS_TYPE(Object*, Name)
117 #define RUNTIME_FUNCTION_RETURN_PAIR(Name) \
118  RUNTIME_FUNCTION_RETURNS_TYPE(ObjectPair, Name)
119 
120 } // namespace internal
121 } // namespace v8
122 
123 #endif // V8_ARGUMENTS_H_
Definition: libplatform.h:13