Avi Drissman | 468e51b6 | 2022-09-13 20:47:01 | [diff] [blame] | 1 | // Copyright 2013 The Chromium Authors |
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 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 GIN_ARGUMENTS_H_ |
| 6 | #define GIN_ARGUMENTS_H_ |
| 7 | |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 8 | #include "base/memory/raw_ptr.h" |
Keishi Hattori | 0a5df379 | 2023-01-22 11:09:57 | [diff] [blame] | 9 | #include "base/memory/raw_ptr_exclusion.h" |
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 10 | #include "gin/converter.h" |
[email protected] | 48c2163 | 2013-12-12 21:32:34 | [diff] [blame] | 11 | #include "gin/gin_export.h" |
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 12 | |
| 13 | namespace gin { |
| 14 | |
[email protected] | 60531d5 | 2013-11-27 02:10:15 | [diff] [blame] | 15 | // Arguments is a wrapper around v8::FunctionCallbackInfo that integrates |
| 16 | // with Converter to make it easier to marshall arguments and return values |
| 17 | // between V8 and C++. |
Jeremy Roman | 6a1242b | 2019-02-04 17:51:57 | [diff] [blame] | 18 | // |
| 19 | // If constructed instead with a v8::PropertyCallbackInfo, behaves as though a |
| 20 | // function with no arguments had been called. |
[email protected] | 48c2163 | 2013-12-12 21:32:34 | [diff] [blame] | 21 | class GIN_EXPORT Arguments { |
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 22 | public: |
[email protected] | 7618ebbb | 2013-11-27 03:38:26 | [diff] [blame] | 23 | Arguments(); |
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 24 | explicit Arguments(const v8::FunctionCallbackInfo<v8::Value>& info); |
Jeremy Roman | 6a1242b | 2019-02-04 17:51:57 | [diff] [blame] | 25 | explicit Arguments(const v8::PropertyCallbackInfo<v8::Value>& info); |
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 26 | ~Arguments(); |
| 27 | |
Michael Lippautz | 5b64e89 | 2018-09-24 11:10:00 | [diff] [blame] | 28 | template <typename T> |
| 29 | bool GetHolder(T* out) const { |
Jeremy Roman | 6a1242b | 2019-02-04 17:51:57 | [diff] [blame] | 30 | v8::Local<v8::Object> holder = is_for_property_ |
| 31 | ? info_for_property_->Holder() |
Igor Sheludko | 1202f17 | 2024-04-23 08:06:53 | [diff] [blame] | 32 | : info_for_function_->This(); |
Jeremy Roman | 6a1242b | 2019-02-04 17:51:57 | [diff] [blame] | 33 | return ConvertFromV8(isolate_, holder, out); |
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | template<typename T> |
[email protected] | 314cde1 | 2013-11-23 20:26:51 | [diff] [blame] | 37 | bool GetData(T* out) { |
Jeremy Roman | 6a1242b | 2019-02-04 17:51:57 | [diff] [blame] | 38 | v8::Local<v8::Value> data = is_for_property_ ? info_for_property_->Data() |
| 39 | : info_for_function_->Data(); |
| 40 | return ConvertFromV8(isolate_, data, out); |
[email protected] | 314cde1 | 2013-11-23 20:26:51 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | template<typename T> |
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 44 | bool GetNext(T* out) { |
Jeremy Roman | 6a1242b | 2019-02-04 17:51:57 | [diff] [blame] | 45 | if (is_for_property_ || next_ >= info_for_function_->Length()) { |
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 46 | insufficient_arguments_ = true; |
| 47 | return false; |
| 48 | } |
Jeremy Roman | 6a1242b | 2019-02-04 17:51:57 | [diff] [blame] | 49 | v8::Local<v8::Value> val = (*info_for_function_)[next_++]; |
[email protected] | 7618ebbb | 2013-11-27 03:38:26 | [diff] [blame] | 50 | return ConvertFromV8(isolate_, val, out); |
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | template<typename T> |
[email protected] | 858eeea0a | 2013-11-19 05:17:12 | [diff] [blame] | 54 | bool GetRemaining(std::vector<T>* out) { |
Jeremy Roman | 6a1242b | 2019-02-04 17:51:57 | [diff] [blame] | 55 | if (is_for_property_ || next_ >= info_for_function_->Length()) { |
[email protected] | 858eeea0a | 2013-11-19 05:17:12 | [diff] [blame] | 56 | insufficient_arguments_ = true; |
| 57 | return false; |
| 58 | } |
Jeremy Roman | 6a1242b | 2019-02-04 17:51:57 | [diff] [blame] | 59 | int remaining = info_for_function_->Length() - next_; |
[email protected] | 858eeea0a | 2013-11-19 05:17:12 | [diff] [blame] | 60 | out->resize(remaining); |
| 61 | for (int i = 0; i < remaining; ++i) { |
Jeremy Roman | 6a1242b | 2019-02-04 17:51:57 | [diff] [blame] | 62 | v8::Local<v8::Value> val = (*info_for_function_)[next_++]; |
[email protected] | 7618ebbb | 2013-11-27 03:38:26 | [diff] [blame] | 63 | if (!ConvertFromV8(isolate_, val, &out->at(i))) |
[email protected] | 858eeea0a | 2013-11-19 05:17:12 | [diff] [blame] | 64 | return false; |
| 65 | } |
| 66 | return true; |
| 67 | } |
| 68 | |
[email protected] | 97f9a795 | 2014-03-14 11:50:33 | [diff] [blame] | 69 | bool Skip() { |
Jeremy Roman | 6a1242b | 2019-02-04 17:51:57 | [diff] [blame] | 70 | if (is_for_property_) |
| 71 | return false; |
| 72 | if (next_ >= info_for_function_->Length()) |
[email protected] | 97f9a795 | 2014-03-14 11:50:33 | [diff] [blame] | 73 | return false; |
| 74 | next_++; |
| 75 | return true; |
| 76 | } |
| 77 | |
| 78 | int Length() const { |
Jeremy Roman | 6a1242b | 2019-02-04 17:51:57 | [diff] [blame] | 79 | return is_for_property_ ? 0 : info_for_function_->Length(); |
[email protected] | 97f9a795 | 2014-03-14 11:50:33 | [diff] [blame] | 80 | } |
| 81 | |
Tom Sepez | d6e548a | 2024-10-11 17:26:25 | [diff] [blame] | 82 | template <typename T> |
| 83 | void Return(const T& val) { |
bashi | dbd2ef9bb | 2015-06-02 01:39:32 | [diff] [blame] | 84 | v8::Local<v8::Value> v8_value; |
Tom Sepez | d6e548a | 2024-10-11 17:26:25 | [diff] [blame] | 85 | if (!TryConvertToV8(isolate_, val, &v8_value)) { |
bashi | dbd2ef9bb | 2015-06-02 01:39:32 | [diff] [blame] | 86 | return; |
Tom Sepez | d6e548a | 2024-10-11 17:26:25 | [diff] [blame] | 87 | } |
Jeremy Roman | 6a1242b | 2019-02-04 17:51:57 | [diff] [blame] | 88 | (is_for_property_ ? info_for_property_->GetReturnValue() |
| 89 | : info_for_function_->GetReturnValue()) |
| 90 | .Set(v8_value); |
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 91 | } |
| 92 | |
rdevlin.cronin | d982fdf | 2017-03-23 22:17:43 | [diff] [blame] | 93 | // Returns the creation context of the Holder. |
Dan Elphick | 3a8863f | 2018-07-30 11:02:42 | [diff] [blame] | 94 | v8::Local<v8::Context> GetHolderCreationContext() const; |
rdevlin.cronin | d982fdf | 2017-03-23 22:17:43 | [diff] [blame] | 95 | |
jochen | 87d2fee | 2015-07-13 08:21:34 | [diff] [blame] | 96 | // Always check the return value whether the handle is empty before |
| 97 | // dereferencing the handle. |
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 98 | v8::Local<v8::Value> PeekNext() const; |
[email protected] | 97f21ca | 2013-11-17 17:46:07 | [diff] [blame] | 99 | |
rdevlin.cronin | cd675450 | 2017-04-19 16:14:14 | [diff] [blame] | 100 | // Returns all arguments. Since this doesn't require any conversion, it |
| 101 | // cannot fail. This does not rely on or modify the current position in the |
| 102 | // array used by Get/PeekNext(). |
Nikolaos Papaspyrou | 5ce2a2f | 2023-10-19 09:09:00 | [diff] [blame] | 103 | v8::LocalVector<v8::Value> GetAll() const; |
rdevlin.cronin | cd675450 | 2017-04-19 16:14:14 | [diff] [blame] | 104 | |
Katie Dektar | bcdde52f | 2022-11-04 21:29:53 | [diff] [blame] | 105 | // Returns the original v8::FunctionCallbackInfo used to construct this |
| 106 | // Arguments if it exists, or nullptr otherwise. |
| 107 | const v8::FunctionCallbackInfo<v8::Value>* GetFunctionCallbackInfo() const { |
| 108 | return info_for_function_; |
| 109 | } |
| 110 | |
[email protected] | 481d249 | 2013-12-13 23:55:25 | [diff] [blame] | 111 | void ThrowError() const; |
| 112 | void ThrowTypeError(const std::string& message) const; |
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 113 | |
| 114 | v8::Isolate* isolate() const { return isolate_; } |
| 115 | |
[email protected] | 744a494 | 2014-07-18 16:46:14 | [diff] [blame] | 116 | // Allows the function handler to distinguish between normal invocation |
| 117 | // and object construction. |
| 118 | bool IsConstructCall() const; |
| 119 | |
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 120 | private: |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 121 | raw_ptr<v8::Isolate> isolate_; |
Jeremy Roman | 6a1242b | 2019-02-04 17:51:57 | [diff] [blame] | 122 | union { |
Keishi Hattori | 0a5df379 | 2023-01-22 11:09:57 | [diff] [blame] | 123 | // This field is not a raw_ptr<> because it was filtered by the rewriter |
| 124 | // for: #union |
| 125 | RAW_PTR_EXCLUSION const v8::FunctionCallbackInfo<v8::Value>* |
| 126 | info_for_function_; |
| 127 | // This field is not a raw_ptr<> because it was filtered by the rewriter |
| 128 | // for: #union |
| 129 | RAW_PTR_EXCLUSION const v8::PropertyCallbackInfo<v8::Value>* |
| 130 | info_for_property_; |
Jeremy Roman | 6a1242b | 2019-02-04 17:51:57 | [diff] [blame] | 131 | }; |
| 132 | int next_ = 0; |
| 133 | bool insufficient_arguments_ = false; |
| 134 | bool is_for_property_ = false; |
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 135 | }; |
| 136 | |
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 137 | } // namespace gin |
| 138 | |
| 139 | #endif // GIN_ARGUMENTS_H_ |