blob: eaded13e29919793494dfe2f7f85fad7dcb125cf [file] [log] [blame]
[email protected]e87f3122013-11-12 00:41:271// 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]e87f3122013-11-12 00:41:278#include "gin/converter.h"
[email protected]48c21632013-12-12 21:32:349#include "gin/gin_export.h"
[email protected]e87f3122013-11-12 00:41:2710
11namespace gin {
12
[email protected]60531d52013-11-27 02:10:1513// 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 Roman6a1242b2019-02-04 17:51:5716//
17// If constructed instead with a v8::PropertyCallbackInfo, behaves as though a
18// function with no arguments had been called.
[email protected]48c21632013-12-12 21:32:3419class GIN_EXPORT Arguments {
[email protected]e87f3122013-11-12 00:41:2720 public:
[email protected]7618ebbb2013-11-27 03:38:2621 Arguments();
[email protected]e87f3122013-11-12 00:41:2722 explicit Arguments(const v8::FunctionCallbackInfo<v8::Value>& info);
Jeremy Roman6a1242b2019-02-04 17:51:5723 explicit Arguments(const v8::PropertyCallbackInfo<v8::Value>& info);
[email protected]e87f3122013-11-12 00:41:2724 ~Arguments();
25
Michael Lippautz5b64e892018-09-24 11:10:0026 template <typename T>
27 bool GetHolder(T* out) const {
Jeremy Roman6a1242b2019-02-04 17:51:5728 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]e87f3122013-11-12 00:41:2732 }
33
34 template<typename T>
[email protected]314cde12013-11-23 20:26:5135 bool GetData(T* out) {
Jeremy Roman6a1242b2019-02-04 17:51:5736 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]314cde12013-11-23 20:26:5139 }
40
41 template<typename T>
[email protected]e87f3122013-11-12 00:41:2742 bool GetNext(T* out) {
Jeremy Roman6a1242b2019-02-04 17:51:5743 if (is_for_property_ || next_ >= info_for_function_->Length()) {
[email protected]e87f3122013-11-12 00:41:2744 insufficient_arguments_ = true;
45 return false;
46 }
Jeremy Roman6a1242b2019-02-04 17:51:5747 v8::Local<v8::Value> val = (*info_for_function_)[next_++];
[email protected]7618ebbb2013-11-27 03:38:2648 return ConvertFromV8(isolate_, val, out);
[email protected]e87f3122013-11-12 00:41:2749 }
50
51 template<typename T>
[email protected]858eeea0a2013-11-19 05:17:1252 bool GetRemaining(std::vector<T>* out) {
Jeremy Roman6a1242b2019-02-04 17:51:5753 if (is_for_property_ || next_ >= info_for_function_->Length()) {
[email protected]858eeea0a2013-11-19 05:17:1254 insufficient_arguments_ = true;
55 return false;
56 }
Jeremy Roman6a1242b2019-02-04 17:51:5757 int remaining = info_for_function_->Length() - next_;
[email protected]858eeea0a2013-11-19 05:17:1258 out->resize(remaining);
59 for (int i = 0; i < remaining; ++i) {
Jeremy Roman6a1242b2019-02-04 17:51:5760 v8::Local<v8::Value> val = (*info_for_function_)[next_++];
[email protected]7618ebbb2013-11-27 03:38:2661 if (!ConvertFromV8(isolate_, val, &out->at(i)))
[email protected]858eeea0a2013-11-19 05:17:1262 return false;
63 }
64 return true;
65 }
66
[email protected]97f9a7952014-03-14 11:50:3367 bool Skip() {
Jeremy Roman6a1242b2019-02-04 17:51:5768 if (is_for_property_)
69 return false;
70 if (next_ >= info_for_function_->Length())
[email protected]97f9a7952014-03-14 11:50:3371 return false;
72 next_++;
73 return true;
74 }
75
76 int Length() const {
Jeremy Roman6a1242b2019-02-04 17:51:5777 return is_for_property_ ? 0 : info_for_function_->Length();
[email protected]97f9a7952014-03-14 11:50:3378 }
79
[email protected]858eeea0a2013-11-19 05:17:1280 template<typename T>
[email protected]e87f3122013-11-12 00:41:2781 void Return(T val) {
bashidbd2ef9bb2015-06-02 01:39:3282 v8::Local<v8::Value> v8_value;
83 if (!TryConvertToV8(isolate_, val, &v8_value))
84 return;
Jeremy Roman6a1242b2019-02-04 17:51:5785 (is_for_property_ ? info_for_property_->GetReturnValue()
86 : info_for_function_->GetReturnValue())
87 .Set(v8_value);
[email protected]e87f3122013-11-12 00:41:2788 }
89
rdevlin.cronind982fdf2017-03-23 22:17:4390 // Returns the creation context of the Holder.
Dan Elphick3a8863f2018-07-30 11:02:4291 v8::Local<v8::Context> GetHolderCreationContext() const;
rdevlin.cronind982fdf2017-03-23 22:17:4392
jochen87d2fee2015-07-13 08:21:3493 // Always check the return value whether the handle is empty before
94 // dereferencing the handle.
deepak.sfaaa1b62015-04-30 07:30:4895 v8::Local<v8::Value> PeekNext() const;
[email protected]97f21ca2013-11-17 17:46:0796
rdevlin.cronincd6754502017-04-19 16:14:1497 // 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]481d2492013-12-13 23:55:25102 void ThrowError() const;
103 void ThrowTypeError(const std::string& message) const;
[email protected]e87f3122013-11-12 00:41:27104
105 v8::Isolate* isolate() const { return isolate_; }
106
[email protected]744a4942014-07-18 16:46:14107 // Allows the function handler to distinguish between normal invocation
108 // and object construction.
109 bool IsConstructCall() const;
110
[email protected]e87f3122013-11-12 00:41:27111 private:
112 v8::Isolate* isolate_;
Jeremy Roman6a1242b2019-02-04 17:51:57113 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]e87f3122013-11-12 00:41:27120};
121
[email protected]e87f3122013-11-12 00:41:27122} // namespace gin
123
124#endif // GIN_ARGUMENTS_H_