blob: 104152ab908fd8beecb0c3b50e6eea9802a03a48 [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"
[email protected]48c21632013-12-12 21:32:3410#include "gin/gin_export.h"
[email protected]e87f3122013-11-12 00:41:2711
12namespace gin {
13
[email protected]60531d52013-11-27 02:10:1514// 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++.
[email protected]48c21632013-12-12 21:32:3417class GIN_EXPORT Arguments {
[email protected]e87f3122013-11-12 00:41:2718 public:
[email protected]7618ebbb2013-11-27 03:38:2619 Arguments();
[email protected]e87f3122013-11-12 00:41:2720 explicit Arguments(const v8::FunctionCallbackInfo<v8::Value>& info);
21 ~Arguments();
22
23 template<typename T>
[email protected]bf3dd3c2013-12-06 06:55:2524 bool GetHolder(T* out) {
[email protected]7618ebbb2013-11-27 03:38:2625 return ConvertFromV8(isolate_, info_->Holder(), out);
[email protected]e87f3122013-11-12 00:41:2726 }
27
28 template<typename T>
[email protected]314cde12013-11-23 20:26:5129 bool GetData(T* out) {
[email protected]7618ebbb2013-11-27 03:38:2630 return ConvertFromV8(isolate_, info_->Data(), out);
[email protected]314cde12013-11-23 20:26:5131 }
32
33 template<typename T>
[email protected]e87f3122013-11-12 00:41:2734 bool GetNext(T* out) {
[email protected]7618ebbb2013-11-27 03:38:2635 if (next_ >= info_->Length()) {
[email protected]e87f3122013-11-12 00:41:2736 insufficient_arguments_ = true;
37 return false;
38 }
[email protected]7618ebbb2013-11-27 03:38:2639 v8::Handle<v8::Value> val = (*info_)[next_++];
40 return ConvertFromV8(isolate_, val, out);
[email protected]e87f3122013-11-12 00:41:2741 }
42
43 template<typename T>
[email protected]858eeea0a2013-11-19 05:17:1244 bool GetRemaining(std::vector<T>* out) {
[email protected]7618ebbb2013-11-27 03:38:2645 if (next_ >= info_->Length()) {
[email protected]858eeea0a2013-11-19 05:17:1246 insufficient_arguments_ = true;
47 return false;
48 }
[email protected]7618ebbb2013-11-27 03:38:2649 int remaining = info_->Length() - next_;
[email protected]858eeea0a2013-11-19 05:17:1250 out->resize(remaining);
51 for (int i = 0; i < remaining; ++i) {
[email protected]7618ebbb2013-11-27 03:38:2652 v8::Handle<v8::Value> val = (*info_)[next_++];
53 if (!ConvertFromV8(isolate_, val, &out->at(i)))
[email protected]858eeea0a2013-11-19 05:17:1254 return false;
55 }
56 return true;
57 }
58
59 template<typename T>
[email protected]e87f3122013-11-12 00:41:2760 void Return(T val) {
[email protected]7618ebbb2013-11-27 03:38:2661 info_->GetReturnValue().Set(ConvertToV8(isolate_, val));
[email protected]e87f3122013-11-12 00:41:2762 }
63
[email protected]97f21ca2013-11-17 17:46:0764 v8::Handle<v8::Value> PeekNext();
65
[email protected]e87f3122013-11-12 00:41:2766 void ThrowError();
67 void ThrowTypeError(const std::string& message);
68
69 v8::Isolate* isolate() const { return isolate_; }
70
71 private:
72 v8::Isolate* isolate_;
[email protected]7618ebbb2013-11-27 03:38:2673 const v8::FunctionCallbackInfo<v8::Value>* info_;
[email protected]e87f3122013-11-12 00:41:2774 int next_;
75 bool insufficient_arguments_;
[email protected]e87f3122013-11-12 00:41:2776};
77
[email protected]e87f3122013-11-12 00:41:2778} // namespace gin
79
80#endif // GIN_ARGUMENTS_H_