1 // Copyright 2013 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "gin/arguments.h"
7 #include "base/strings/stringprintf.h"
8 #include "gin/converter.h"
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"
16 Arguments::Arguments()
17 : isolate_(nullptr), info_for_function_(nullptr), is_for_property_(false) {}
19 Arguments::Arguments(const v8::FunctionCallbackInfo<v8::Value>& info)
20 : isolate_(info.GetIsolate()),
21 info_for_function_(&info),
22 is_for_property_(false) {}
24 Arguments::Arguments(const v8::PropertyCallbackInfo<v8::Value>& info)
25 : isolate_(info.GetIsolate()),
26 info_for_property_(&info),
27 is_for_property_(true) {}
29 Arguments::~Arguments() = default;
31 v8::Local<v8::Value> Arguments::PeekNext() const {
33 return v8::Local<v8::Value>();
34 if (next_ >= info_for_function_->Length())
35 return v8::Local<v8::Value>();
36 return (*info_for_function_)[next_];
39 v8::LocalVector<v8::Value> Arguments::GetAll() const {
40 v8::LocalVector<v8::Value> result(isolate_);
44 int length = info_for_function_->Length();
48 result.reserve(length);
49 for (int i = 0; i < length; ++i)
50 result.push_back((*info_for_function_)[i]);
55 v8::Local<v8::Context> Arguments::GetHolderCreationContext() const {
56 v8::Local<v8::Object> holder = is_for_property_
57 ? info_for_property_->Holder()
58 : info_for_function_->Holder();
59 return holder->GetCreationContextChecked();
62 std::string V8TypeAsString(v8::Isolate* isolate, v8::Local<v8::Value> value) {
64 return "<empty handle>";
65 if (value->IsUndefined())
70 if (!ConvertFromV8(isolate, value, &result))
75 void Arguments::ThrowError() const {
77 return ThrowTypeError("Error processing property accessor arguments.");
79 if (insufficient_arguments_)
80 return ThrowTypeError("Insufficient number of arguments.");
82 v8::Local<v8::Value> value = (*info_for_function_)[next_ - 1];
83 return ThrowTypeError(base::StringPrintf(
84 "Error processing argument at index %d, conversion failure from %s",
85 next_ - 1, V8TypeAsString(isolate_, value).c_str()));
88 void Arguments::ThrowTypeError(const std::string& message) const {
89 isolate_->ThrowException(v8::Exception::TypeError(
90 StringToV8(isolate_, message)));
93 bool Arguments::IsConstructCall() const {
94 return !is_for_property_ && info_for_function_->IsConstructCall();