summaryrefslogtreecommitdiffstats
path: root/gin/arguments.h
blob: 8a372eadfdb3113b2da925c996cbaa35f8d53989 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef GIN_ARGUMENTS_H_
#define GIN_ARGUMENTS_H_

#include "base/basictypes.h"
#include "gin/converter.h"

namespace gin {

class Arguments {
 public:
  explicit Arguments(const v8::FunctionCallbackInfo<v8::Value>& info);
  ~Arguments();

  template<typename T>
  // TODO(aa): Rename GetHolder().
  bool Holder(T* out) {
    return ConvertFromV8(info_.Holder(), out);
  }

  template<typename T>
  bool GetData(T* out) {
    return ConvertFromV8(info_.Data(), out);
  }

  template<typename T>
  bool GetNext(T* out) {
    if (next_ >= info_.Length()) {
      insufficient_arguments_ = true;
      return false;
    }
    v8::Handle<v8::Value> val = info_[next_++];
    return ConvertFromV8(val, out);
  }

  template<typename T>
  bool GetRemaining(std::vector<T>* out) {
    if (next_ >= info_.Length()) {
      insufficient_arguments_ = true;
      return false;
    }
    int remaining = info_.Length() - next_;
    out->resize(remaining);
    for (int i = 0; i < remaining; ++i) {
      v8::Handle<v8::Value> val = info_[next_++];
      if (!ConvertFromV8(val, &out->at(i)))
        return false;
    }
    return true;
  }

  template<typename T>
  void Return(T val) {
    info_.GetReturnValue().Set(ConvertToV8(isolate_, val));
  }

  v8::Handle<v8::Value> PeekNext();

  void ThrowError();
  void ThrowTypeError(const std::string& message);

  v8::Isolate* isolate() const { return isolate_; }

 private:
  v8::Isolate* isolate_;
  const v8::FunctionCallbackInfo<v8::Value>& info_;
  int next_;
  bool insufficient_arguments_;

  DISALLOW_COPY_AND_ASSIGN(Arguments);
};

}  // namespace gin

#endif  // GIN_ARGUMENTS_H_