OLD | NEW |
(Empty) | |
| 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/code-stub-assembler.h" |
| 12 |
| 13 namespace v8 { |
| 14 namespace internal { |
| 15 |
| 16 // Arguments object passed to C++ builtins. |
| 17 class BuiltinArguments : public Arguments { |
| 18 public: |
| 19 BuiltinArguments(int length, Object** arguments) |
| 20 : Arguments(length, arguments) { |
| 21 // Check we have at least the receiver. |
| 22 DCHECK_LE(1, this->length()); |
| 23 } |
| 24 |
| 25 Object*& operator[](int index) { |
| 26 DCHECK_LT(index, length()); |
| 27 return Arguments::operator[](index); |
| 28 } |
| 29 |
| 30 template <class S> |
| 31 Handle<S> at(int index) { |
| 32 DCHECK_LT(index, length()); |
| 33 return Arguments::at<S>(index); |
| 34 } |
| 35 |
| 36 Handle<Object> atOrUndefined(Isolate* isolate, int index) { |
| 37 if (index >= length()) { |
| 38 return isolate->factory()->undefined_value(); |
| 39 } |
| 40 return at<Object>(index); |
| 41 } |
| 42 |
| 43 Handle<Object> receiver() { return Arguments::at<Object>(0); } |
| 44 |
| 45 static const int kNewTargetOffset = 0; |
| 46 static const int kTargetOffset = 1; |
| 47 static const int kArgcOffset = 2; |
| 48 static const int kNumExtraArgs = 3; |
| 49 static const int kNumExtraArgsWithReceiver = 4; |
| 50 |
| 51 template <class S> |
| 52 Handle<S> target() { |
| 53 return Arguments::at<S>(Arguments::length() - 1 - kTargetOffset); |
| 54 } |
| 55 Handle<HeapObject> new_target() { |
| 56 return Arguments::at<HeapObject>(Arguments::length() - 1 - |
| 57 kNewTargetOffset); |
| 58 } |
| 59 |
| 60 // Gets the total number of arguments including the receiver (but |
| 61 // excluding extra arguments). |
| 62 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 (FLAG_runtime_call_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()->IsNull(isolate) || \ |
| 122 args.receiver()->IsUndefined(isolate)) { \ |
| 123 THROW_NEW_ERROR_RETURN_FAILURE( \ |
| 124 isolate, \ |
| 125 NewTypeError(MessageTemplate::kCalledOnNullOrUndefined, \ |
| 126 isolate->factory()->NewStringFromAsciiChecked(method))); \ |
| 127 } \ |
| 128 Handle<String> name; \ |
| 129 ASSIGN_RETURN_FAILURE_ON_EXCEPTION( \ |
| 130 isolate, name, Object::ToString(isolate, args.receiver())) |
| 131 |
| 132 } // namespace internal |
| 133 } // namespace v8 |
| 134 |
| 135 #endif // V8_BUILTINS_BUILTINS_UTILS_H_ |
OLD | NEW |