Revert "[M120 Migration]Fix for crash during chrome exit"
[platform/framework/web/chromium-efl.git] / gin / arguments.cc
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.
4
5 #include "gin/arguments.h"
6
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"
13
14 namespace gin {
15
16 Arguments::Arguments()
17     : isolate_(nullptr), info_for_function_(nullptr), is_for_property_(false) {}
18
19 Arguments::Arguments(const v8::FunctionCallbackInfo<v8::Value>& info)
20     : isolate_(info.GetIsolate()),
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) {}
28
29 Arguments::~Arguments() = default;
30
31 v8::Local<v8::Value> Arguments::PeekNext() const {
32   if (is_for_property_)
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_];
37 }
38
39 v8::LocalVector<v8::Value> Arguments::GetAll() const {
40   v8::LocalVector<v8::Value> result(isolate_);
41   if (is_for_property_)
42     return result;
43
44   int length = info_for_function_->Length();
45   if (length == 0)
46     return result;
47
48   result.reserve(length);
49   for (int i = 0; i < length; ++i)
50     result.push_back((*info_for_function_)[i]);
51
52   return result;
53 }
54
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();
60 }
61
62 std::string V8TypeAsString(v8::Isolate* isolate, v8::Local<v8::Value> value) {
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;
70   if (!ConvertFromV8(isolate, value, &result))
71     return std::string();
72   return result;
73 }
74
75 void Arguments::ThrowError() const {
76   if (is_for_property_)
77     return ThrowTypeError("Error processing property accessor arguments.");
78
79   if (insufficient_arguments_)
80     return ThrowTypeError("Insufficient number of arguments.");
81
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()));
86 }
87
88 void Arguments::ThrowTypeError(const std::string& message) const {
89   isolate_->ThrowException(v8::Exception::TypeError(
90       StringToV8(isolate_, message)));
91 }
92
93 bool Arguments::IsConstructCall() const {
94   return !is_for_property_ && info_for_function_->IsConstructCall();
95 }
96
97 }  // namespace gin