[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 | |
| 13 | class Arguments { |
| 14 | public: |
| 15 | explicit Arguments(const v8::FunctionCallbackInfo<v8::Value>& info); |
| 16 | ~Arguments(); |
| 17 | |
| 18 | template<typename T> |
| 19 | bool Holder(T* out) { |
| 20 | return ConvertFromV8(info_.Holder(), out); |
| 21 | } |
| 22 | |
| 23 | template<typename T> |
| 24 | bool GetNext(T* out) { |
| 25 | if (next_ >= info_.Length()) { |
| 26 | insufficient_arguments_ = true; |
| 27 | return false; |
| 28 | } |
| 29 | v8::Handle<v8::Value> val = info_[next_++]; |
| 30 | return ConvertFromV8(val, out); |
| 31 | } |
| 32 | |
| 33 | template<typename T> |
| 34 | void Return(T val) { |
| 35 | info_.GetReturnValue().Set(ConvertToV8(isolate_, val)); |
| 36 | } |
| 37 | |
| 38 | void ThrowError(); |
| 39 | void ThrowTypeError(const std::string& message); |
| 40 | |
| 41 | v8::Isolate* isolate() const { return isolate_; } |
| 42 | |
| 43 | private: |
| 44 | v8::Isolate* isolate_; |
| 45 | const v8::FunctionCallbackInfo<v8::Value>& info_; |
| 46 | int next_; |
| 47 | bool insufficient_arguments_; |
| 48 | |
| 49 | DISALLOW_COPY_AND_ASSIGN(Arguments); |
| 50 | }; |
| 51 | |
| 52 | } // namespace gin |
| 53 | |
| 54 | #endif // GIN_ARGUMENTS_H_ |