Upload upstream chromium 108.0.5359.1
[platform/framework/web/chromium-efl.git] / gin / arguments.h
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.
4
5 #ifndef GIN_ARGUMENTS_H_
6 #define GIN_ARGUMENTS_H_
7
8 #include "base/memory/raw_ptr.h"
9 #include "gin/converter.h"
10 #include "gin/gin_export.h"
11
12 namespace gin {
13
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++.
17 //
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 {
21  public:
22   Arguments();
23   explicit Arguments(const v8::FunctionCallbackInfo<v8::Value>& info);
24   explicit Arguments(const v8::PropertyCallbackInfo<v8::Value>& info);
25   ~Arguments();
26
27   template <typename T>
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);
33   }
34
35   template<typename T>
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);
40   }
41
42   template<typename T>
43   bool GetNext(T* out) {
44     if (is_for_property_ || next_ >= info_for_function_->Length()) {
45       insufficient_arguments_ = true;
46       return false;
47     }
48     v8::Local<v8::Value> val = (*info_for_function_)[next_++];
49     return ConvertFromV8(isolate_, val, out);
50   }
51
52   template<typename T>
53   bool GetRemaining(std::vector<T>* out) {
54     if (is_for_property_ || next_ >= info_for_function_->Length()) {
55       insufficient_arguments_ = true;
56       return false;
57     }
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)))
63         return false;
64     }
65     return true;
66   }
67
68   bool Skip() {
69     if (is_for_property_)
70       return false;
71     if (next_ >= info_for_function_->Length())
72       return false;
73     next_++;
74     return true;
75   }
76
77   int Length() const {
78     return is_for_property_ ? 0 : info_for_function_->Length();
79   }
80
81   template<typename T>
82   void Return(T val) {
83     v8::Local<v8::Value> v8_value;
84     if (!TryConvertToV8(isolate_, val, &v8_value))
85       return;
86     (is_for_property_ ? info_for_property_->GetReturnValue()
87                       : info_for_function_->GetReturnValue())
88         .Set(v8_value);
89   }
90
91   // Returns the creation context of the Holder.
92   v8::Local<v8::Context> GetHolderCreationContext() const;
93
94   // Always check the return value whether the handle is empty before
95   // dereferencing the handle.
96   v8::Local<v8::Value> PeekNext() const;
97
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;
102
103   void ThrowError() const;
104   void ThrowTypeError(const std::string& message) const;
105
106   v8::Isolate* isolate() const { return isolate_; }
107
108   // Allows the function handler to distinguish between normal invocation
109   // and object construction.
110   bool IsConstructCall() const;
111
112  private:
113   raw_ptr<v8::Isolate> isolate_;
114   union {
115     const v8::FunctionCallbackInfo<v8::Value>* info_for_function_;
116     const v8::PropertyCallbackInfo<v8::Value>* info_for_property_;
117   };
118   int next_ = 0;
119   bool insufficient_arguments_ = false;
120   bool is_for_property_ = false;
121 };
122
123 }  // namespace gin
124
125 #endif  // GIN_ARGUMENTS_H_