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