[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 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 "base/basictypes.h" | ||||
9 | #include "gin/converter.h" | ||||
10 | |||||
11 | namespace gin { | ||||
12 | |||||
[email protected] | 60531d5 | 2013-11-27 02:10:15 | [diff] [blame^] | 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++. | ||||
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 16 | class Arguments { |
17 | public: | ||||
18 | explicit Arguments(const v8::FunctionCallbackInfo<v8::Value>& info); | ||||
19 | ~Arguments(); | ||||
20 | |||||
21 | template<typename T> | ||||
[email protected] | 314cde1 | 2013-11-23 20:26:51 | [diff] [blame] | 22 | // TODO(aa): Rename GetHolder(). |
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 23 | bool Holder(T* out) { |
24 | return ConvertFromV8(info_.Holder(), out); | ||||
25 | } | ||||
26 | |||||
27 | template<typename T> | ||||
[email protected] | 314cde1 | 2013-11-23 20:26:51 | [diff] [blame] | 28 | bool GetData(T* out) { |
29 | return ConvertFromV8(info_.Data(), out); | ||||
30 | } | ||||
31 | |||||
32 | template<typename T> | ||||
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 33 | bool GetNext(T* out) { |
34 | if (next_ >= info_.Length()) { | ||||
35 | insufficient_arguments_ = true; | ||||
36 | return false; | ||||
37 | } | ||||
38 | v8::Handle<v8::Value> val = info_[next_++]; | ||||
39 | return ConvertFromV8(val, out); | ||||
40 | } | ||||
41 | |||||
42 | template<typename T> | ||||
[email protected] | 858eeea0a | 2013-11-19 05:17:12 | [diff] [blame] | 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::Handle<v8::Value> val = info_[next_++]; | ||||
52 | if (!ConvertFromV8(val, &out->at(i))) | ||||
53 | return false; | ||||
54 | } | ||||
55 | return true; | ||||
56 | } | ||||
57 | |||||
58 | template<typename T> | ||||
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 59 | void Return(T val) { |
60 | info_.GetReturnValue().Set(ConvertToV8(isolate_, val)); | ||||
61 | } | ||||
62 | |||||
[email protected] | 97f21ca | 2013-11-17 17:46:07 | [diff] [blame] | 63 | v8::Handle<v8::Value> PeekNext(); |
64 | |||||
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 65 | void ThrowError(); |
66 | void ThrowTypeError(const std::string& message); | ||||
67 | |||||
68 | v8::Isolate* isolate() const { return isolate_; } | ||||
69 | |||||
70 | private: | ||||
71 | v8::Isolate* isolate_; | ||||
72 | const v8::FunctionCallbackInfo<v8::Value>& info_; | ||||
73 | int next_; | ||||
74 | bool insufficient_arguments_; | ||||
75 | |||||
76 | DISALLOW_COPY_AND_ASSIGN(Arguments); | ||||
77 | }; | ||||
78 | |||||
79 | } // namespace gin | ||||
80 | |||||
81 | #endif // GIN_ARGUMENTS_H_ |