blob: 46552a5022ac27d22ece4839115ab8c73c127945 [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#include "gin/arguments.h"
6
[email protected]855ab432013-11-18 17:09:367#include "base/strings/stringprintf.h"
[email protected]e87f3122013-11-12 00:41:278#include "gin/converter.h"
9
10namespace gin {
11
[email protected]7618ebbb2013-11-27 03:38:2612Arguments::Arguments()
13 : isolate_(NULL),
14 info_(NULL),
15 next_(0),
16 insufficient_arguments_(false) {
17}
18
[email protected]e87f3122013-11-12 00:41:2719Arguments::Arguments(const v8::FunctionCallbackInfo<v8::Value>& info)
20 : isolate_(info.GetIsolate()),
[email protected]7618ebbb2013-11-27 03:38:2621 info_(&info),
[email protected]e87f3122013-11-12 00:41:2722 next_(0),
23 insufficient_arguments_(false) {
24}
25
Chris Watkins756035a2017-12-01 03:03:2726Arguments::~Arguments() = default;
[email protected]e87f3122013-11-12 00:41:2727
deepak.sfaaa1b62015-04-30 07:30:4828v8::Local<v8::Value> Arguments::PeekNext() const {
[email protected]7618ebbb2013-11-27 03:38:2629 if (next_ >= info_->Length())
deepak.sfaaa1b62015-04-30 07:30:4830 return v8::Local<v8::Value>();
[email protected]7618ebbb2013-11-27 03:38:2631 return (*info_)[next_];
[email protected]97f21ca2013-11-17 17:46:0732}
33
rdevlin.cronincd6754502017-04-19 16:14:1434std::vector<v8::Local<v8::Value>> Arguments::GetAll() const {
35 std::vector<v8::Local<v8::Value>> result;
36 int length = info_->Length();
37 if (length == 0)
38 return result;
39
40 result.reserve(length);
41 for (int i = 0; i < length; ++i)
42 result.push_back((*info_)[i]);
43
44 return result;
45}
46
Dan Elphick3a8863f2018-07-30 11:02:4247v8::Local<v8::Context> Arguments::GetHolderCreationContext() const {
rdevlin.cronind982fdf2017-03-23 22:17:4348 return info_->Holder()->CreationContext();
49}
50
Dan Elphick712beaa2018-07-24 17:37:2451std::string V8TypeAsString(v8::Isolate* isolate, v8::Local<v8::Value> value) {
eseidel2584930f2014-12-15 22:06:4452 if (value.IsEmpty())
53 return "<empty handle>";
54 if (value->IsUndefined())
55 return "undefined";
56 if (value->IsNull())
57 return "null";
58 std::string result;
Dan Elphick712beaa2018-07-24 17:37:2459 if (!ConvertFromV8(isolate, value, &result))
eseidel2584930f2014-12-15 22:06:4460 return std::string();
61 return result;
62}
63
[email protected]481d2492013-12-13 23:55:2564void Arguments::ThrowError() const {
[email protected]e87f3122013-11-12 00:41:2765 if (insufficient_arguments_)
66 return ThrowTypeError("Insufficient number of arguments.");
67
eseidel2584930f2014-12-15 22:06:4468 return ThrowTypeError(base::StringPrintf(
69 "Error processing argument at index %d, conversion failure from %s",
Dan Elphick712beaa2018-07-24 17:37:2470 next_ - 1, V8TypeAsString(isolate_, (*info_)[next_ - 1]).c_str()));
[email protected]e87f3122013-11-12 00:41:2771}
72
[email protected]481d2492013-12-13 23:55:2573void Arguments::ThrowTypeError(const std::string& message) const {
[email protected]e87f3122013-11-12 00:41:2774 isolate_->ThrowException(v8::Exception::TypeError(
75 StringToV8(isolate_, message)));
76}
77
[email protected]744a4942014-07-18 16:46:1478bool Arguments::IsConstructCall() const {
79 return info_->IsConstructCall();
80}
81
[email protected]e87f3122013-11-12 00:41:2782} // namespace gin