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