[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++. |
[email protected] | 48c2163 | 2013-12-12 21:32:34 | [diff] [blame] | 16 | class GIN_EXPORT Arguments { |
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 17 | public: |
[email protected] | 7618ebbb | 2013-11-27 03:38:26 | [diff] [blame] | 18 | Arguments(); |
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 19 | explicit Arguments(const v8::FunctionCallbackInfo<v8::Value>& info); |
| 20 | ~Arguments(); |
| 21 | |
| 22 | template<typename T> |
[email protected] | bf3dd3c | 2013-12-06 06:55:25 | [diff] [blame] | 23 | bool GetHolder(T* out) { |
[email protected] | 7618ebbb | 2013-11-27 03:38:26 | [diff] [blame] | 24 | return ConvertFromV8(isolate_, info_->Holder(), out); |
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 25 | } |
| 26 | |
| 27 | template<typename T> |
[email protected] | 314cde1 | 2013-11-23 20:26:51 | [diff] [blame] | 28 | bool GetData(T* out) { |
[email protected] | 7618ebbb | 2013-11-27 03:38:26 | [diff] [blame] | 29 | return ConvertFromV8(isolate_, info_->Data(), out); |
[email protected] | 314cde1 | 2013-11-23 20:26:51 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | template<typename T> |
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 33 | bool GetNext(T* out) { |
[email protected] | 7618ebbb | 2013-11-27 03:38:26 | [diff] [blame] | 34 | if (next_ >= info_->Length()) { |
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 35 | insufficient_arguments_ = true; |
| 36 | return false; |
| 37 | } |
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 38 | v8::Local<v8::Value> val = (*info_)[next_++]; |
[email protected] | 7618ebbb | 2013-11-27 03:38:26 | [diff] [blame] | 39 | return ConvertFromV8(isolate_, val, out); |
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | template<typename T> |
[email protected] | 858eeea0a | 2013-11-19 05:17:12 | [diff] [blame] | 43 | bool GetRemaining(std::vector<T>* out) { |
[email protected] | 7618ebbb | 2013-11-27 03:38:26 | [diff] [blame] | 44 | if (next_ >= info_->Length()) { |
[email protected] | 858eeea0a | 2013-11-19 05:17:12 | [diff] [blame] | 45 | insufficient_arguments_ = true; |
| 46 | return false; |
| 47 | } |
[email protected] | 7618ebbb | 2013-11-27 03:38:26 | [diff] [blame] | 48 | int remaining = info_->Length() - next_; |
[email protected] | 858eeea0a | 2013-11-19 05:17:12 | [diff] [blame] | 49 | out->resize(remaining); |
| 50 | for (int i = 0; i < remaining; ++i) { |
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 51 | v8::Local<v8::Value> val = (*info_)[next_++]; |
[email protected] | 7618ebbb | 2013-11-27 03:38:26 | [diff] [blame] | 52 | if (!ConvertFromV8(isolate_, val, &out->at(i))) |
[email protected] | 858eeea0a | 2013-11-19 05:17:12 | [diff] [blame] | 53 | return false; |
| 54 | } |
| 55 | return true; |
| 56 | } |
| 57 | |
[email protected] | 97f9a795 | 2014-03-14 11:50:33 | [diff] [blame] | 58 | bool Skip() { |
| 59 | if (next_ >= info_->Length()) |
| 60 | return false; |
| 61 | next_++; |
| 62 | return true; |
| 63 | } |
| 64 | |
| 65 | int Length() const { |
| 66 | return info_->Length(); |
| 67 | } |
| 68 | |
[email protected] | 858eeea0a | 2013-11-19 05:17:12 | [diff] [blame] | 69 | template<typename T> |
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 70 | void Return(T val) { |
bashi | dbd2ef9bb | 2015-06-02 01:39:32 | [diff] [blame] | 71 | v8::Local<v8::Value> v8_value; |
| 72 | if (!TryConvertToV8(isolate_, val, &v8_value)) |
| 73 | return; |
| 74 | info_->GetReturnValue().Set(v8_value); |
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 75 | } |
| 76 | |
rdevlin.cronin | d982fdf | 2017-03-23 22:17:43 | [diff] [blame] | 77 | // Returns the creation context of the Holder. |
| 78 | v8::Local<v8::Context> GetHolderCreationContext(); |
| 79 | |
jochen | 87d2fee | 2015-07-13 08:21:34 | [diff] [blame] | 80 | // Always check the return value whether the handle is empty before |
| 81 | // dereferencing the handle. |
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 82 | v8::Local<v8::Value> PeekNext() const; |
[email protected] | 97f21ca | 2013-11-17 17:46:07 | [diff] [blame] | 83 | |
rdevlin.cronin | cd675450 | 2017-04-19 16:14:14 | [diff] [blame^] | 84 | // Returns all arguments. Since this doesn't require any conversion, it |
| 85 | // cannot fail. This does not rely on or modify the current position in the |
| 86 | // array used by Get/PeekNext(). |
| 87 | std::vector<v8::Local<v8::Value>> GetAll() const; |
| 88 | |
[email protected] | 481d249 | 2013-12-13 23:55:25 | [diff] [blame] | 89 | void ThrowError() const; |
| 90 | void ThrowTypeError(const std::string& message) const; |
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 91 | |
| 92 | v8::Isolate* isolate() const { return isolate_; } |
| 93 | |
[email protected] | 744a494 | 2014-07-18 16:46:14 | [diff] [blame] | 94 | // Allows the function handler to distinguish between normal invocation |
| 95 | // and object construction. |
| 96 | bool IsConstructCall() const; |
| 97 | |
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 98 | private: |
| 99 | v8::Isolate* isolate_; |
[email protected] | 7618ebbb | 2013-11-27 03:38:26 | [diff] [blame] | 100 | const v8::FunctionCallbackInfo<v8::Value>* info_; |
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 101 | int next_; |
| 102 | bool insufficient_arguments_; |
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 103 | }; |
| 104 | |
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 105 | } // namespace gin |
| 106 | |
| 107 | #endif // GIN_ARGUMENTS_H_ |