blob: fadb1d2dca490fe37d942d4f58e765dbf8fbcde9 [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"
Dan Elphick05acd602021-08-30 15:22:079#include "v8/include/v8-exception.h"
10#include "v8/include/v8-isolate.h"
11#include "v8/include/v8-object.h"
12#include "v8/include/v8-template.h"
[email protected]e87f3122013-11-12 00:41:2713
14namespace gin {
15
[email protected]7618ebbb2013-11-27 03:38:2616Arguments::Arguments()
Jeremy Roman6a1242b2019-02-04 17:51:5717 : isolate_(nullptr), info_for_function_(nullptr), is_for_property_(false) {}
[email protected]7618ebbb2013-11-27 03:38:2618
[email protected]e87f3122013-11-12 00:41:2719Arguments::Arguments(const v8::FunctionCallbackInfo<v8::Value>& info)
20 : isolate_(info.GetIsolate()),
Jeremy Roman6a1242b2019-02-04 17:51:5721 info_for_function_(&info),
22 is_for_property_(false) {}
23
24Arguments::Arguments(const v8::PropertyCallbackInfo<v8::Value>& info)
25 : isolate_(info.GetIsolate()),
26 info_for_property_(&info),
27 is_for_property_(true) {}
[email protected]e87f3122013-11-12 00:41:2728
Chris Watkins756035a2017-12-01 03:03:2729Arguments::~Arguments() = default;
[email protected]e87f3122013-11-12 00:41:2730
deepak.sfaaa1b62015-04-30 07:30:4831v8::Local<v8::Value> Arguments::PeekNext() const {
Jeremy Roman6a1242b2019-02-04 17:51:5732 if (is_for_property_)
deepak.sfaaa1b62015-04-30 07:30:4833 return v8::Local<v8::Value>();
Jeremy Roman6a1242b2019-02-04 17:51:5734 if (next_ >= info_for_function_->Length())
35 return v8::Local<v8::Value>();
36 return (*info_for_function_)[next_];
[email protected]97f21ca2013-11-17 17:46:0737}
38
rdevlin.cronincd6754502017-04-19 16:14:1439std::vector<v8::Local<v8::Value>> Arguments::GetAll() const {
40 std::vector<v8::Local<v8::Value>> result;
Jeremy Roman6a1242b2019-02-04 17:51:5741 if (is_for_property_)
42 return result;
43
44 int length = info_for_function_->Length();
rdevlin.cronincd6754502017-04-19 16:14:1445 if (length == 0)
46 return result;
47
48 result.reserve(length);
49 for (int i = 0; i < length; ++i)
Jeremy Roman6a1242b2019-02-04 17:51:5750 result.push_back((*info_for_function_)[i]);
rdevlin.cronincd6754502017-04-19 16:14:1451
52 return result;
53}
54
Dan Elphick3a8863f2018-07-30 11:02:4255v8::Local<v8::Context> Arguments::GetHolderCreationContext() const {
Jeremy Roman6a1242b2019-02-04 17:51:5756 v8::Local<v8::Object> holder = is_for_property_
57 ? info_for_property_->Holder()
58 : info_for_function_->Holder();
Camillo Bruni1109e282021-12-14 15:53:1359 return holder->GetCreationContextChecked();
rdevlin.cronind982fdf2017-03-23 22:17:4360}
61
Dan Elphick712beaa2018-07-24 17:37:2462std::string V8TypeAsString(v8::Isolate* isolate, v8::Local<v8::Value> value) {
eseidel2584930f2014-12-15 22:06:4463 if (value.IsEmpty())
64 return "<empty handle>";
65 if (value->IsUndefined())
66 return "undefined";
67 if (value->IsNull())
68 return "null";
69 std::string result;
Dan Elphick712beaa2018-07-24 17:37:2470 if (!ConvertFromV8(isolate, value, &result))
eseidel2584930f2014-12-15 22:06:4471 return std::string();
72 return result;
73}
74
[email protected]481d2492013-12-13 23:55:2575void Arguments::ThrowError() const {
Jeremy Roman6a1242b2019-02-04 17:51:5776 if (is_for_property_)
77 return ThrowTypeError("Error processing property accessor arguments.");
78
[email protected]e87f3122013-11-12 00:41:2779 if (insufficient_arguments_)
80 return ThrowTypeError("Insufficient number of arguments.");
81
Jeremy Roman6a1242b2019-02-04 17:51:5782 v8::Local<v8::Value> value = (*info_for_function_)[next_ - 1];
eseidel2584930f2014-12-15 22:06:4483 return ThrowTypeError(base::StringPrintf(
84 "Error processing argument at index %d, conversion failure from %s",
Jeremy Roman6a1242b2019-02-04 17:51:5785 next_ - 1, V8TypeAsString(isolate_, value).c_str()));
[email protected]e87f3122013-11-12 00:41:2786}
87
[email protected]481d2492013-12-13 23:55:2588void Arguments::ThrowTypeError(const std::string& message) const {
[email protected]e87f3122013-11-12 00:41:2789 isolate_->ThrowException(v8::Exception::TypeError(
90 StringToV8(isolate_, message)));
91}
92
[email protected]744a4942014-07-18 16:46:1493bool Arguments::IsConstructCall() const {
Jeremy Roman6a1242b2019-02-04 17:51:5794 return !is_for_property_ && info_for_function_->IsConstructCall();
[email protected]744a4942014-07-18 16:46:1495}
96
[email protected]e87f3122013-11-12 00:41:2797} // namespace gin