Line data Source code
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_BUILTINS_BUILTINS_UTILS_H_
6 : #define V8_BUILTINS_BUILTINS_UTILS_H_
7 :
8 : #include "src/arguments.h"
9 : #include "src/base/logging.h"
10 : #include "src/builtins/builtins.h"
11 : #include "src/factory.h"
12 : #include "src/isolate.h"
13 :
14 : namespace v8 {
15 : namespace internal {
16 :
17 : // Arguments object passed to C++ builtins.
18 : class BuiltinArguments : public Arguments {
19 : public:
20 0 : BuiltinArguments(int length, Object** arguments)
21 : : Arguments(length, arguments) {
22 : // Check we have at least the receiver.
23 : DCHECK_LE(1, this->length());
24 0 : }
25 :
26 : Object*& operator[](int index) {
27 : DCHECK_LT(index, length());
28 22961346 : return Arguments::operator[](index);
29 : }
30 :
31 : template <class S = Object>
32 : Handle<S> at(int index) {
33 : DCHECK_LT(index, length());
34 : return Arguments::at<S>(index);
35 : }
36 :
37 : Handle<Object> atOrUndefined(Isolate* isolate, int index) {
38 7859886 : if (index >= length()) {
39 : return isolate->factory()->undefined_value();
40 : }
41 : return at<Object>(index);
42 : }
43 :
44 : Handle<Object> receiver() { return Arguments::at<Object>(0); }
45 :
46 : static const int kNewTargetOffset = 0;
47 : static const int kTargetOffset = 1;
48 : static const int kArgcOffset = 2;
49 : static const int kPaddingOffset = 3;
50 :
51 : static const int kNumExtraArgs = 4;
52 : static const int kNumExtraArgsWithReceiver = 5;
53 :
54 : Handle<JSFunction> target() {
55 16048141 : return Arguments::at<JSFunction>(Arguments::length() - 1 - kTargetOffset);
56 : }
57 : Handle<HeapObject> new_target() {
58 : return Arguments::at<HeapObject>(Arguments::length() - 1 -
59 16010703 : kNewTargetOffset);
60 : }
61 :
62 : // Gets the total number of arguments including the receiver (but
63 : // excluding extra arguments).
64 79776478 : int length() const { return Arguments::length() - kNumExtraArgs; }
65 : };
66 :
67 : // ----------------------------------------------------------------------------
68 : // Support macro for defining builtins in C++.
69 : // ----------------------------------------------------------------------------
70 : //
71 : // A builtin function is defined by writing:
72 : //
73 : // BUILTIN(name) {
74 : // ...
75 : // }
76 : //
77 : // In the body of the builtin function the arguments can be accessed
78 : // through the BuiltinArguments object args.
79 : // TODO(cbruni): add global flag to check whether any tracing events have been
80 : // enabled.
81 : #define BUILTIN(name) \
82 : MUST_USE_RESULT static Object* Builtin_Impl_##name(BuiltinArguments args, \
83 : Isolate* isolate); \
84 : \
85 : V8_NOINLINE static Object* Builtin_Impl_Stats_##name( \
86 : int args_length, Object** args_object, Isolate* isolate) { \
87 : BuiltinArguments args(args_length, args_object); \
88 : RuntimeCallTimerScope timer(isolate, &RuntimeCallStats::Builtin_##name); \
89 : TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("v8.runtime"), \
90 : "V8.Builtin_" #name); \
91 : return Builtin_Impl_##name(args, isolate); \
92 : } \
93 : \
94 : MUST_USE_RESULT Object* Builtin_##name( \
95 : int args_length, Object** args_object, Isolate* isolate) { \
96 : DCHECK(isolate->context() == nullptr || isolate->context()->IsContext()); \
97 : if (V8_UNLIKELY(FLAG_runtime_stats)) { \
98 : return Builtin_Impl_Stats_##name(args_length, args_object, isolate); \
99 : } \
100 : BuiltinArguments args(args_length, args_object); \
101 : return Builtin_Impl_##name(args, isolate); \
102 : } \
103 : \
104 : MUST_USE_RESULT static Object* Builtin_Impl_##name(BuiltinArguments args, \
105 : Isolate* isolate)
106 :
107 : // ----------------------------------------------------------------------------
108 :
109 : #define CHECK_RECEIVER(Type, name, method) \
110 : if (!args.receiver()->Is##Type()) { \
111 : THROW_NEW_ERROR_RETURN_FAILURE( \
112 : isolate, \
113 : NewTypeError(MessageTemplate::kIncompatibleMethodReceiver, \
114 : isolate->factory()->NewStringFromAsciiChecked(method), \
115 : args.receiver())); \
116 : } \
117 : Handle<Type> name = Handle<Type>::cast(args.receiver())
118 :
119 : // Throws a TypeError for {method} if the receiver is not coercible to Object,
120 : // or converts the receiver to a String otherwise and assigns it to a new var
121 : // with the given {name}.
122 : #define TO_THIS_STRING(name, method) \
123 : if (args.receiver()->IsNullOrUndefined(isolate)) { \
124 : THROW_NEW_ERROR_RETURN_FAILURE( \
125 : isolate, \
126 : NewTypeError(MessageTemplate::kCalledOnNullOrUndefined, \
127 : isolate->factory()->NewStringFromAsciiChecked(method))); \
128 : } \
129 : Handle<String> name; \
130 : ASSIGN_RETURN_FAILURE_ON_EXCEPTION( \
131 : isolate, name, Object::ToString(isolate, args.receiver()))
132 :
133 : } // namespace internal
134 : } // namespace v8
135 :
136 : #endif // V8_BUILTINS_BUILTINS_UTILS_H_
|