Avi Drissman | 468e51b6 | 2022-09-13 20:47:01 | [diff] [blame] | 1 | // Copyright 2013 The Chromium Authors |
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 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] | 855ab43 | 2013-11-18 17:09:36 | [diff] [blame] | 7 | #include "base/strings/stringprintf.h" |
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 8 | #include "gin/converter.h" |
Dan Elphick | 05acd60 | 2021-08-30 15:22:07 | [diff] [blame] | 9 | #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] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 13 | |
| 14 | namespace gin { |
| 15 | |
[email protected] | 7618ebbb | 2013-11-27 03:38:26 | [diff] [blame] | 16 | Arguments::Arguments() |
Jeremy Roman | 6a1242b | 2019-02-04 17:51:57 | [diff] [blame] | 17 | : isolate_(nullptr), info_for_function_(nullptr), is_for_property_(false) {} |
[email protected] | 7618ebbb | 2013-11-27 03:38:26 | [diff] [blame] | 18 | |
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 19 | Arguments::Arguments(const v8::FunctionCallbackInfo<v8::Value>& info) |
| 20 | : isolate_(info.GetIsolate()), |
Jeremy Roman | 6a1242b | 2019-02-04 17:51:57 | [diff] [blame] | 21 | info_for_function_(&info), |
| 22 | is_for_property_(false) {} |
| 23 | |
| 24 | Arguments::Arguments(const v8::PropertyCallbackInfo<v8::Value>& info) |
| 25 | : isolate_(info.GetIsolate()), |
| 26 | info_for_property_(&info), |
| 27 | is_for_property_(true) {} |
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 28 | |
Chris Watkins | 756035a | 2017-12-01 03:03:27 | [diff] [blame] | 29 | Arguments::~Arguments() = default; |
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 30 | |
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 31 | v8::Local<v8::Value> Arguments::PeekNext() const { |
Jeremy Roman | 6a1242b | 2019-02-04 17:51:57 | [diff] [blame] | 32 | if (is_for_property_) |
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 33 | return v8::Local<v8::Value>(); |
Jeremy Roman | 6a1242b | 2019-02-04 17:51:57 | [diff] [blame] | 34 | if (next_ >= info_for_function_->Length()) |
| 35 | return v8::Local<v8::Value>(); |
| 36 | return (*info_for_function_)[next_]; |
[email protected] | 97f21ca | 2013-11-17 17:46:07 | [diff] [blame] | 37 | } |
| 38 | |
Nikolaos Papaspyrou | 5ce2a2f | 2023-10-19 09:09:00 | [diff] [blame] | 39 | v8::LocalVector<v8::Value> Arguments::GetAll() const { |
| 40 | v8::LocalVector<v8::Value> result(isolate_); |
Jeremy Roman | 6a1242b | 2019-02-04 17:51:57 | [diff] [blame] | 41 | if (is_for_property_) |
| 42 | return result; |
| 43 | |
| 44 | int length = info_for_function_->Length(); |
rdevlin.cronin | cd675450 | 2017-04-19 16:14:14 | [diff] [blame] | 45 | if (length == 0) |
| 46 | return result; |
| 47 | |
| 48 | result.reserve(length); |
| 49 | for (int i = 0; i < length; ++i) |
Jeremy Roman | 6a1242b | 2019-02-04 17:51:57 | [diff] [blame] | 50 | result.push_back((*info_for_function_)[i]); |
rdevlin.cronin | cd675450 | 2017-04-19 16:14:14 | [diff] [blame] | 51 | |
| 52 | return result; |
| 53 | } |
| 54 | |
Dan Elphick | 3a8863f | 2018-07-30 11:02:42 | [diff] [blame] | 55 | v8::Local<v8::Context> Arguments::GetHolderCreationContext() const { |
Igor Sheludko | ddbf0d4 | 2025-06-17 08:26:45 | [diff] [blame] | 56 | v8::Local<v8::Object> holder = is_for_property_ |
| 57 | ? info_for_property_->HolderV2() |
| 58 | : info_for_function_->This(); |
Dominik Inführ | 3af929c | 2024-10-08 05:53:19 | [diff] [blame] | 59 | return holder->GetCreationContextChecked(isolate_); |
rdevlin.cronin | d982fdf | 2017-03-23 22:17:43 | [diff] [blame] | 60 | } |
| 61 | |
Dan Elphick | 712beaa | 2018-07-24 17:37:24 | [diff] [blame] | 62 | std::string V8TypeAsString(v8::Isolate* isolate, v8::Local<v8::Value> value) { |
eseidel | 2584930f | 2014-12-15 22:06:44 | [diff] [blame] | 63 | 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 Elphick | 712beaa | 2018-07-24 17:37:24 | [diff] [blame] | 70 | if (!ConvertFromV8(isolate, value, &result)) |
eseidel | 2584930f | 2014-12-15 22:06:44 | [diff] [blame] | 71 | return std::string(); |
| 72 | return result; |
| 73 | } |
| 74 | |
[email protected] | 481d249 | 2013-12-13 23:55:25 | [diff] [blame] | 75 | void Arguments::ThrowError() const { |
Jeremy Roman | 6a1242b | 2019-02-04 17:51:57 | [diff] [blame] | 76 | if (is_for_property_) |
| 77 | return ThrowTypeError("Error processing property accessor arguments."); |
| 78 | |
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 79 | if (insufficient_arguments_) |
| 80 | return ThrowTypeError("Insufficient number of arguments."); |
| 81 | |
Jeremy Roman | 6a1242b | 2019-02-04 17:51:57 | [diff] [blame] | 82 | v8::Local<v8::Value> value = (*info_for_function_)[next_ - 1]; |
eseidel | 2584930f | 2014-12-15 22:06:44 | [diff] [blame] | 83 | return ThrowTypeError(base::StringPrintf( |
| 84 | "Error processing argument at index %d, conversion failure from %s", |
Jeremy Roman | 6a1242b | 2019-02-04 17:51:57 | [diff] [blame] | 85 | next_ - 1, V8TypeAsString(isolate_, value).c_str())); |
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 86 | } |
| 87 | |
[email protected] | 481d249 | 2013-12-13 23:55:25 | [diff] [blame] | 88 | void Arguments::ThrowTypeError(const std::string& message) const { |
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 89 | isolate_->ThrowException(v8::Exception::TypeError( |
| 90 | StringToV8(isolate_, message))); |
| 91 | } |
| 92 | |
[email protected] | 744a494 | 2014-07-18 16:46:14 | [diff] [blame] | 93 | bool Arguments::IsConstructCall() const { |
Jeremy Roman | 6a1242b | 2019-02-04 17:51:57 | [diff] [blame] | 94 | return !is_for_property_ && info_for_function_->IsConstructCall(); |
[email protected] | 744a494 | 2014-07-18 16:46:14 | [diff] [blame] | 95 | } |
| 96 | |
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 97 | } // namespace gin |