Upload upstream chromium 71.0.3578.0
[platform/framework/web/chromium-efl.git] / gin / arguments.h
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
8 #include "gin/converter.h"
9 #include "gin/gin_export.h"
10
11 namespace gin {
12
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++.
16 class GIN_EXPORT Arguments {
17  public:
18   Arguments();
19   explicit Arguments(const v8::FunctionCallbackInfo<v8::Value>& info);
20   ~Arguments();
21
22   template <typename T>
23   bool GetHolder(T* out) const {
24     return ConvertFromV8(isolate_, info_->Holder(), out);
25   }
26
27   template<typename T>
28   bool GetData(T* out) {
29     return ConvertFromV8(isolate_, info_->Data(), out);
30   }
31
32   template<typename T>
33   bool GetNext(T* out) {
34     if (next_ >= info_->Length()) {
35       insufficient_arguments_ = true;
36       return false;
37     }
38     v8::Local<v8::Value> val = (*info_)[next_++];
39     return ConvertFromV8(isolate_, val, out);
40   }
41
42   template<typename T>
43   bool GetRemaining(std::vector<T>* out) {
44     if (next_ >= info_->Length()) {
45       insufficient_arguments_ = true;
46       return false;
47     }
48     int remaining = info_->Length() - next_;
49     out->resize(remaining);
50     for (int i = 0; i < remaining; ++i) {
51       v8::Local<v8::Value> val = (*info_)[next_++];
52       if (!ConvertFromV8(isolate_, val, &out->at(i)))
53         return false;
54     }
55     return true;
56   }
57
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
69   template<typename T>
70   void Return(T val) {
71     v8::Local<v8::Value> v8_value;
72     if (!TryConvertToV8(isolate_, val, &v8_value))
73       return;
74     info_->GetReturnValue().Set(v8_value);
75   }
76
77   // Returns the creation context of the Holder.
78   v8::Local<v8::Context> GetHolderCreationContext() const;
79
80   // Always check the return value whether the handle is empty before
81   // dereferencing the handle.
82   v8::Local<v8::Value> PeekNext() const;
83
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
89   void ThrowError() const;
90   void ThrowTypeError(const std::string& message) const;
91
92   v8::Isolate* isolate() const { return isolate_; }
93
94   // Allows the function handler to distinguish between normal invocation
95   // and object construction.
96   bool IsConstructCall() const;
97
98  private:
99   v8::Isolate* isolate_;
100   const v8::FunctionCallbackInfo<v8::Value>* info_;
101   int next_;
102   bool insufficient_arguments_;
103 };
104
105 }  // namespace gin
106
107 #endif  // GIN_ARGUMENTS_H_