blob: 07335c7cfe2326f32e907a0b3cdbec3ac1688cce [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()
Jeremy Roman6a1242b2019-02-04 17:51:5713 : isolate_(nullptr), info_for_function_(nullptr), is_for_property_(false) {}
[email protected]7618ebbb2013-11-27 03:38:2614
[email protected]e87f3122013-11-12 00:41:2715Arguments::Arguments(const v8::FunctionCallbackInfo<v8::Value>& info)
16 : isolate_(info.GetIsolate()),
Jeremy Roman6a1242b2019-02-04 17:51:5717 info_for_function_(&info),
18 is_for_property_(false) {}
19
20Arguments::Arguments(const v8::PropertyCallbackInfo<v8::Value>& info)
21 : isolate_(info.GetIsolate()),
22 info_for_property_(&info),
23 is_for_property_(true) {}
[email protected]e87f3122013-11-12 00:41:2724
Chris Watkins756035a2017-12-01 03:03:2725Arguments::~Arguments() = default;
[email protected]e87f3122013-11-12 00:41:2726
deepak.sfaaa1b62015-04-30 07:30:4827v8::Local<v8::Value> Arguments::PeekNext() const {
Jeremy Roman6a1242b2019-02-04 17:51:5728 if (is_for_property_)
deepak.sfaaa1b62015-04-30 07:30:4829 return v8::Local<v8::Value>();
Jeremy Roman6a1242b2019-02-04 17:51:5730 if (next_ >= info_for_function_->Length())
31 return v8::Local<v8::Value>();
32 return (*info_for_function_)[next_];
[email protected]97f21ca2013-11-17 17:46:0733}
34
rdevlin.cronincd6754502017-04-19 16:14:1435std::vector<v8::Local<v8::Value>> Arguments::GetAll() const {
36 std::vector<v8::Local<v8::Value>> result;
Jeremy Roman6a1242b2019-02-04 17:51:5737 if (is_for_property_)
38 return result;
39
40 int length = info_for_function_->Length();
rdevlin.cronincd6754502017-04-19 16:14:1441 if (length == 0)
42 return result;
43
44 result.reserve(length);
45 for (int i = 0; i < length; ++i)
Jeremy Roman6a1242b2019-02-04 17:51:5746 result.push_back((*info_for_function_)[i]);
rdevlin.cronincd6754502017-04-19 16:14:1447
48 return result;
49}
50
Dan Elphick3a8863f2018-07-30 11:02:4251v8::Local<v8::Context> Arguments::GetHolderCreationContext() const {
Jeremy Roman6a1242b2019-02-04 17:51:5752 v8::Local<v8::Object> holder = is_for_property_
53 ? info_for_property_->Holder()
54 : info_for_function_->Holder();
55 return holder->CreationContext();
rdevlin.cronind982fdf2017-03-23 22:17:4356}
57
Dan Elphick712beaa2018-07-24 17:37:2458std::string V8TypeAsString(v8::Isolate* isolate, v8::Local<v8::Value> value) {
eseidel2584930f2014-12-15 22:06:4459 if (value.IsEmpty())
60 return "<empty handle>";
61 if (value->IsUndefined())
62 return "undefined";
63 if (value->IsNull())
64 return "null";
65 std::string result;
Dan Elphick712beaa2018-07-24 17:37:2466 if (!ConvertFromV8(isolate, value, &result))
eseidel2584930f2014-12-15 22:06:4467 return std::string();
68 return result;
69}
70
[email protected]481d2492013-12-13 23:55:2571void Arguments::ThrowError() const {
Jeremy Roman6a1242b2019-02-04 17:51:5772 if (is_for_property_)
73 return ThrowTypeError("Error processing property accessor arguments.");
74
[email protected]e87f3122013-11-12 00:41:2775 if (insufficient_arguments_)
76 return ThrowTypeError("Insufficient number of arguments.");
77
Jeremy Roman6a1242b2019-02-04 17:51:5778 v8::Local<v8::Value> value = (*info_for_function_)[next_ - 1];
eseidel2584930f2014-12-15 22:06:4479 return ThrowTypeError(base::StringPrintf(
80 "Error processing argument at index %d, conversion failure from %s",
Jeremy Roman6a1242b2019-02-04 17:51:5781 next_ - 1, V8TypeAsString(isolate_, value).c_str()));
[email protected]e87f3122013-11-12 00:41:2782}
83
[email protected]481d2492013-12-13 23:55:2584void Arguments::ThrowTypeError(const std::string& message) const {
[email protected]e87f3122013-11-12 00:41:2785 isolate_->ThrowException(v8::Exception::TypeError(
86 StringToV8(isolate_, message)));
87}
88
[email protected]744a4942014-07-18 16:46:1489bool Arguments::IsConstructCall() const {
Jeremy Roman6a1242b2019-02-04 17:51:5790 return !is_for_property_ && info_for_function_->IsConstructCall();
[email protected]744a4942014-07-18 16:46:1491}
92
[email protected]e87f3122013-11-12 00:41:2793} // namespace gin