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 56233125 : 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 18720097 : 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 kNumExtraArgs = 3;
50 : static const int kNumExtraArgsWithReceiver = 4;
51 :
52 : Handle<JSFunction> target() {
53 54049891 : return Arguments::at<JSFunction>(Arguments::length() - 1 - kTargetOffset);
54 : }
55 : Handle<HeapObject> new_target() {
56 : return Arguments::at<HeapObject>(Arguments::length() - 1 -
57 54042465 : kNewTargetOffset);
58 : }
59 :
60 : // Gets the total number of arguments including the receiver (but
61 : // excluding extra arguments).
62 73161529 : int length() const { return Arguments::length() - kNumExtraArgs; }
63 : };
64 :
65 : // ----------------------------------------------------------------------------
66 : // Support macro for defining builtins in C++.
67 : // ----------------------------------------------------------------------------
68 : //
69 : // A builtin function is defined by writing:
70 : //
71 : // BUILTIN(name) {
72 : // ...
73 : // }
74 : //
75 : // In the body of the builtin function the arguments can be accessed
76 : // through the BuiltinArguments object args.
77 : // TODO(cbruni): add global flag to check whether any tracing events have been
78 : // enabled.
79 : #define BUILTIN(name) \
80 : MUST_USE_RESULT static Object* Builtin_Impl_##name(BuiltinArguments args, \
81 : Isolate* isolate); \
82 : \
83 : V8_NOINLINE static Object* Builtin_Impl_Stats_##name( \
84 : int args_length, Object** args_object, Isolate* isolate) { \
85 : BuiltinArguments args(args_length, args_object); \
86 : RuntimeCallTimerScope timer(isolate, &RuntimeCallStats::Builtin_##name); \
87 : TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("v8.runtime"), \
88 : "V8.Builtin_" #name); \
89 : return Builtin_Impl_##name(args, isolate); \
90 : } \
91 : \
92 : MUST_USE_RESULT Object* Builtin_##name( \
93 : int args_length, Object** args_object, Isolate* isolate) { \
94 : DCHECK(isolate->context() == nullptr || isolate->context()->IsContext()); \
95 : if (V8_UNLIKELY(FLAG_runtime_stats)) { \
96 : return Builtin_Impl_Stats_##name(args_length, args_object, isolate); \
97 : } \
98 : BuiltinArguments args(args_length, args_object); \
99 : return Builtin_Impl_##name(args, isolate); \
100 : } \
101 : \
102 : MUST_USE_RESULT static Object* Builtin_Impl_##name(BuiltinArguments args, \
103 : Isolate* isolate)
104 :
105 : // ----------------------------------------------------------------------------
106 :
107 : #define CHECK_RECEIVER(Type, name, method) \
108 : if (!args.receiver()->Is##Type()) { \
109 : THROW_NEW_ERROR_RETURN_FAILURE( \
110 : isolate, \
111 : NewTypeError(MessageTemplate::kIncompatibleMethodReceiver, \
112 : isolate->factory()->NewStringFromAsciiChecked(method), \
113 : args.receiver())); \
114 : } \
115 : Handle<Type> name = Handle<Type>::cast(args.receiver())
116 :
117 : // Throws a TypeError for {method} if the receiver is not coercible to Object,
118 : // or converts the receiver to a String otherwise and assigns it to a new var
119 : // with the given {name}.
120 : #define TO_THIS_STRING(name, method) \
121 : if (args.receiver()->IsNullOrUndefined(isolate)) { \
122 : THROW_NEW_ERROR_RETURN_FAILURE( \
123 : isolate, \
124 : NewTypeError(MessageTemplate::kCalledOnNullOrUndefined, \
125 : isolate->factory()->NewStringFromAsciiChecked(method))); \
126 : } \
127 : Handle<String> name; \
128 : ASSIGN_RETURN_FAILURE_ON_EXCEPTION( \
129 : isolate, name, Object::ToString(isolate, args.receiver()))
130 :
131 : } // namespace internal
132 : } // namespace v8
133 :
134 : #endif // V8_BUILTINS_BUILTINS_UTILS_H_
|