1 // Copyright 2013 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef GIN_ARGUMENTS_H_
6 #define GIN_ARGUMENTS_H_
8 #include "base/memory/raw_ptr.h"
9 #include "base/memory/raw_ptr_exclusion.h"
10 #include "gin/converter.h"
11 #include "gin/gin_export.h"
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++.
19 // If constructed instead with a v8::PropertyCallbackInfo, behaves as though a
20 // function with no arguments had been called.
21 class GIN_EXPORT Arguments {
24 explicit Arguments(const v8::FunctionCallbackInfo<v8::Value>& info);
25 explicit Arguments(const v8::PropertyCallbackInfo<v8::Value>& info);
29 bool GetHolder(T* out) const {
30 v8::Local<v8::Object> holder = is_for_property_
31 ? info_for_property_->Holder()
32 : info_for_function_->Holder();
33 return ConvertFromV8(isolate_, holder, out);
37 bool GetData(T* out) {
38 v8::Local<v8::Value> data = is_for_property_ ? info_for_property_->Data()
39 : info_for_function_->Data();
40 return ConvertFromV8(isolate_, data, out);
44 bool GetNext(T* out) {
45 if (is_for_property_ || next_ >= info_for_function_->Length()) {
46 insufficient_arguments_ = true;
49 v8::Local<v8::Value> val = (*info_for_function_)[next_++];
50 return ConvertFromV8(isolate_, val, out);
54 bool GetRemaining(std::vector<T>* out) {
55 if (is_for_property_ || next_ >= info_for_function_->Length()) {
56 insufficient_arguments_ = true;
59 int remaining = info_for_function_->Length() - next_;
60 out->resize(remaining);
61 for (int i = 0; i < remaining; ++i) {
62 v8::Local<v8::Value> val = (*info_for_function_)[next_++];
63 if (!ConvertFromV8(isolate_, val, &out->at(i)))
72 if (next_ >= info_for_function_->Length())
79 return is_for_property_ ? 0 : info_for_function_->Length();
84 v8::Local<v8::Value> v8_value;
85 if (!TryConvertToV8(isolate_, val, &v8_value))
87 (is_for_property_ ? info_for_property_->GetReturnValue()
88 : info_for_function_->GetReturnValue())
92 // Returns the creation context of the Holder.
93 v8::Local<v8::Context> GetHolderCreationContext() const;
95 // Always check the return value whether the handle is empty before
96 // dereferencing the handle.
97 v8::Local<v8::Value> PeekNext() const;
99 // Returns all arguments. Since this doesn't require any conversion, it
100 // cannot fail. This does not rely on or modify the current position in the
101 // array used by Get/PeekNext().
102 v8::LocalVector<v8::Value> GetAll() const;
104 // Returns the original v8::FunctionCallbackInfo used to construct this
105 // Arguments if it exists, or nullptr otherwise.
106 const v8::FunctionCallbackInfo<v8::Value>* GetFunctionCallbackInfo() const {
107 return info_for_function_;
110 void ThrowError() const;
111 void ThrowTypeError(const std::string& message) const;
113 v8::Isolate* isolate() const { return isolate_; }
115 // Allows the function handler to distinguish between normal invocation
116 // and object construction.
117 bool IsConstructCall() const;
120 raw_ptr<v8::Isolate> isolate_;
122 // This field is not a raw_ptr<> because it was filtered by the rewriter
124 RAW_PTR_EXCLUSION const v8::FunctionCallbackInfo<v8::Value>*
126 // This field is not a raw_ptr<> because it was filtered by the rewriter
128 RAW_PTR_EXCLUSION const v8::PropertyCallbackInfo<v8::Value>*
132 bool insufficient_arguments_ = false;
133 bool is_for_property_ = false;
138 #endif // GIN_ARGUMENTS_H_