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#include "gin/arguments.h"
6
7#include "base/strings/stringprintf.h"
8#include "gin/converter.h"
9
10namespace gin {
11
12Arguments::Arguments()
13    : isolate_(NULL),
14      info_(NULL),
15      next_(0),
16      insufficient_arguments_(false) {
17}
18
19Arguments::Arguments(const v8::FunctionCallbackInfo<v8::Value>& info)
20    : isolate_(info.GetIsolate()),
21      info_(&info),
22      next_(0),
23      insufficient_arguments_(false) {
24}
25
26Arguments::~Arguments() {
27}
28
29v8::Handle<v8::Value> Arguments::PeekNext() const {
30  if (next_ >= info_->Length())
31    return v8::Handle<v8::Value>();
32  return (*info_)[next_];
33}
34
35void Arguments::ThrowError() const {
36  if (insufficient_arguments_)
37    return ThrowTypeError("Insufficient number of arguments.");
38
39  ThrowTypeError(base::StringPrintf(
40      "Error processing argument %d.", next_ - 1));
41}
42
43void Arguments::ThrowTypeError(const std::string& message) const {
44  isolate_->ThrowException(v8::Exception::TypeError(
45      StringToV8(isolate_, message)));
46}
47
48bool Arguments::IsConstructCall() const {
49  return info_->IsConstructCall();
50}
51
52}  // namespace gin
53