arguments.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. #ifndef GIN_ARGUMENTS_H_
  5. #define GIN_ARGUMENTS_H_
  6. #include "base/memory/raw_ptr.h"
  7. #include "gin/converter.h"
  8. #include "gin/gin_export.h"
  9. namespace gin {
  10. // Arguments is a wrapper around v8::FunctionCallbackInfo that integrates
  11. // with Converter to make it easier to marshall arguments and return values
  12. // between V8 and C++.
  13. //
  14. // If constructed instead with a v8::PropertyCallbackInfo, behaves as though a
  15. // function with no arguments had been called.
  16. class GIN_EXPORT Arguments {
  17. public:
  18. Arguments();
  19. explicit Arguments(const v8::FunctionCallbackInfo<v8::Value>& info);
  20. explicit Arguments(const v8::PropertyCallbackInfo<v8::Value>& info);
  21. ~Arguments();
  22. template <typename T>
  23. bool GetHolder(T* out) const {
  24. v8::Local<v8::Object> holder = is_for_property_
  25. ? info_for_property_->Holder()
  26. : info_for_function_->Holder();
  27. return ConvertFromV8(isolate_, holder, out);
  28. }
  29. template<typename T>
  30. bool GetData(T* out) {
  31. v8::Local<v8::Value> data = is_for_property_ ? info_for_property_->Data()
  32. : info_for_function_->Data();
  33. return ConvertFromV8(isolate_, data, out);
  34. }
  35. template<typename T>
  36. bool GetNext(T* out) {
  37. if (is_for_property_ || next_ >= info_for_function_->Length()) {
  38. insufficient_arguments_ = true;
  39. return false;
  40. }
  41. v8::Local<v8::Value> val = (*info_for_function_)[next_++];
  42. return ConvertFromV8(isolate_, val, out);
  43. }
  44. template<typename T>
  45. bool GetRemaining(std::vector<T>* out) {
  46. if (is_for_property_ || next_ >= info_for_function_->Length()) {
  47. insufficient_arguments_ = true;
  48. return false;
  49. }
  50. int remaining = info_for_function_->Length() - next_;
  51. out->resize(remaining);
  52. for (int i = 0; i < remaining; ++i) {
  53. v8::Local<v8::Value> val = (*info_for_function_)[next_++];
  54. if (!ConvertFromV8(isolate_, val, &out->at(i)))
  55. return false;
  56. }
  57. return true;
  58. }
  59. bool Skip() {
  60. if (is_for_property_)
  61. return false;
  62. if (next_ >= info_for_function_->Length())
  63. return false;
  64. next_++;
  65. return true;
  66. }
  67. int Length() const {
  68. return is_for_property_ ? 0 : info_for_function_->Length();
  69. }
  70. template<typename T>
  71. void Return(T val) {
  72. v8::Local<v8::Value> v8_value;
  73. if (!TryConvertToV8(isolate_, val, &v8_value))
  74. return;
  75. (is_for_property_ ? info_for_property_->GetReturnValue()
  76. : info_for_function_->GetReturnValue())
  77. .Set(v8_value);
  78. }
  79. // Returns the creation context of the Holder.
  80. v8::Local<v8::Context> GetHolderCreationContext() const;
  81. // Always check the return value whether the handle is empty before
  82. // dereferencing the handle.
  83. v8::Local<v8::Value> PeekNext() const;
  84. // Returns all arguments. Since this doesn't require any conversion, it
  85. // cannot fail. This does not rely on or modify the current position in the
  86. // array used by Get/PeekNext().
  87. std::vector<v8::Local<v8::Value>> GetAll() const;
  88. void ThrowError() const;
  89. void ThrowTypeError(const std::string& message) const;
  90. v8::Isolate* isolate() const { return isolate_; }
  91. // Allows the function handler to distinguish between normal invocation
  92. // and object construction.
  93. bool IsConstructCall() const;
  94. private:
  95. raw_ptr<v8::Isolate> isolate_;
  96. union {
  97. const v8::FunctionCallbackInfo<v8::Value>* info_for_function_;
  98. const v8::PropertyCallbackInfo<v8::Value>* info_for_property_;
  99. };
  100. int next_ = 0;
  101. bool insufficient_arguments_ = false;
  102. bool is_for_property_ = false;
  103. };
  104. } // namespace gin
  105. #endif // GIN_ARGUMENTS_H_