blob: fe4c6aa3cffe91e18cfdbfa7d65c8b10058423fc [file] [log] [blame]
[email protected]e87f3122013-11-12 00:41:271// 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
11namespace gin {
12
[email protected]60531d52013-11-27 02:10:1513// 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]e87f3122013-11-12 00:41:2716class Arguments {
17 public:
18 explicit Arguments(const v8::FunctionCallbackInfo<v8::Value>& info);
19 ~Arguments();
20
21 template<typename T>
[email protected]314cde12013-11-23 20:26:5122 // TODO(aa): Rename GetHolder().
[email protected]e87f3122013-11-12 00:41:2723 bool Holder(T* out) {
24 return ConvertFromV8(info_.Holder(), out);
25 }
26
27 template<typename T>
[email protected]314cde12013-11-23 20:26:5128 bool GetData(T* out) {
29 return ConvertFromV8(info_.Data(), out);
30 }
31
32 template<typename T>
[email protected]e87f3122013-11-12 00:41:2733 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]858eeea0a2013-11-19 05:17:1243 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]e87f3122013-11-12 00:41:2759 void Return(T val) {
60 info_.GetReturnValue().Set(ConvertToV8(isolate_, val));
61 }
62
[email protected]97f21ca2013-11-17 17:46:0763 v8::Handle<v8::Value> PeekNext();
64
[email protected]e87f3122013-11-12 00:41:2765 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_