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 "gin/converter.h"
10 #include "gin/gin_export.h"
14 // Arguments is a wrapper around v8::FunctionCallbackInfo that integrates
15 // with Converter to make it easier to marshall arguments and return values
16 // between V8 and C++.
18 // If constructed instead with a v8::PropertyCallbackInfo, behaves as though a
19 // function with no arguments had been called.
20 class GIN_EXPORT Arguments {
23 explicit Arguments(const v8::FunctionCallbackInfo<v8::Value>& info);
24 explicit Arguments(const v8::PropertyCallbackInfo<v8::Value>& info);
28 bool GetHolder(T* out) const {
29 v8::Local<v8::Object> holder = is_for_property_
30 ? info_for_property_->Holder()
31 : info_for_function_->Holder();
32 return ConvertFromV8(isolate_, holder, out);
36 bool GetData(T* out) {
37 v8::Local<v8::Value> data = is_for_property_ ? info_for_property_->Data()
38 : info_for_function_->Data();
39 return ConvertFromV8(isolate_, data, out);
43 bool GetNext(T* out) {
44 if (is_for_property_ || next_ >= info_for_function_->Length()) {
45 insufficient_arguments_ = true;
48 v8::Local<v8::Value> val = (*info_for_function_)[next_++];
49 return ConvertFromV8(isolate_, val, out);
53 bool GetRemaining(std::vector<T>* out) {
54 if (is_for_property_ || next_ >= info_for_function_->Length()) {
55 insufficient_arguments_ = true;
58 int remaining = info_for_function_->Length() - next_;
59 out->resize(remaining);
60 for (int i = 0; i < remaining; ++i) {
61 v8::Local<v8::Value> val = (*info_for_function_)[next_++];
62 if (!ConvertFromV8(isolate_, val, &out->at(i)))
71 if (next_ >= info_for_function_->Length())
78 return is_for_property_ ? 0 : info_for_function_->Length();
83 v8::Local<v8::Value> v8_value;
84 if (!TryConvertToV8(isolate_, val, &v8_value))
86 (is_for_property_ ? info_for_property_->GetReturnValue()
87 : info_for_function_->GetReturnValue())
91 // Returns the creation context of the Holder.
92 v8::Local<v8::Context> GetHolderCreationContext() const;
94 // Always check the return value whether the handle is empty before
95 // dereferencing the handle.
96 v8::Local<v8::Value> PeekNext() const;
98 // Returns all arguments. Since this doesn't require any conversion, it
99 // cannot fail. This does not rely on or modify the current position in the
100 // array used by Get/PeekNext().
101 std::vector<v8::Local<v8::Value>> GetAll() const;
103 void ThrowError() const;
104 void ThrowTypeError(const std::string& message) const;
106 v8::Isolate* isolate() const { return isolate_; }
108 // Allows the function handler to distinguish between normal invocation
109 // and object construction.
110 bool IsConstructCall() const;
113 raw_ptr<v8::Isolate> isolate_;
115 const v8::FunctionCallbackInfo<v8::Value>* info_for_function_;
116 const v8::PropertyCallbackInfo<v8::Value>* info_for_property_;
119 bool insufficient_arguments_ = false;
120 bool is_for_property_ = false;
125 #endif // GIN_ARGUMENTS_H_