arguments.cc 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. #include "gin/arguments.h"
  5. #include "base/strings/stringprintf.h"
  6. #include "gin/converter.h"
  7. #include "v8/include/v8-exception.h"
  8. #include "v8/include/v8-isolate.h"
  9. #include "v8/include/v8-object.h"
  10. #include "v8/include/v8-template.h"
  11. namespace gin {
  12. Arguments::Arguments()
  13. : isolate_(nullptr), info_for_function_(nullptr), is_for_property_(false) {}
  14. Arguments::Arguments(const v8::FunctionCallbackInfo<v8::Value>& info)
  15. : isolate_(info.GetIsolate()),
  16. info_for_function_(&info),
  17. is_for_property_(false) {}
  18. Arguments::Arguments(const v8::PropertyCallbackInfo<v8::Value>& info)
  19. : isolate_(info.GetIsolate()),
  20. info_for_property_(&info),
  21. is_for_property_(true) {}
  22. Arguments::~Arguments() = default;
  23. v8::Local<v8::Value> Arguments::PeekNext() const {
  24. if (is_for_property_)
  25. return v8::Local<v8::Value>();
  26. if (next_ >= info_for_function_->Length())
  27. return v8::Local<v8::Value>();
  28. return (*info_for_function_)[next_];
  29. }
  30. std::vector<v8::Local<v8::Value>> Arguments::GetAll() const {
  31. std::vector<v8::Local<v8::Value>> result;
  32. if (is_for_property_)
  33. return result;
  34. int length = info_for_function_->Length();
  35. if (length == 0)
  36. return result;
  37. result.reserve(length);
  38. for (int i = 0; i < length; ++i)
  39. result.push_back((*info_for_function_)[i]);
  40. return result;
  41. }
  42. v8::Local<v8::Context> Arguments::GetHolderCreationContext() const {
  43. v8::Local<v8::Object> holder = is_for_property_
  44. ? info_for_property_->Holder()
  45. : info_for_function_->Holder();
  46. return holder->GetCreationContextChecked();
  47. }
  48. std::string V8TypeAsString(v8::Isolate* isolate, v8::Local<v8::Value> value) {
  49. if (value.IsEmpty())
  50. return "<empty handle>";
  51. if (value->IsUndefined())
  52. return "undefined";
  53. if (value->IsNull())
  54. return "null";
  55. std::string result;
  56. if (!ConvertFromV8(isolate, value, &result))
  57. return std::string();
  58. return result;
  59. }
  60. void Arguments::ThrowError() const {
  61. if (is_for_property_)
  62. return ThrowTypeError("Error processing property accessor arguments.");
  63. if (insufficient_arguments_)
  64. return ThrowTypeError("Insufficient number of arguments.");
  65. v8::Local<v8::Value> value = (*info_for_function_)[next_ - 1];
  66. return ThrowTypeError(base::StringPrintf(
  67. "Error processing argument at index %d, conversion failure from %s",
  68. next_ - 1, V8TypeAsString(isolate_, value).c_str()));
  69. }
  70. void Arguments::ThrowTypeError(const std::string& message) const {
  71. isolate_->ThrowException(v8::Exception::TypeError(
  72. StringToV8(isolate_, message)));
  73. }
  74. bool Arguments::IsConstructCall() const {
  75. return !is_for_property_ && info_for_function_->IsConstructCall();
  76. }
  77. } // namespace gin