object_template_builder.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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_OBJECT_TEMPLATE_BUILDER_H_
  5. #define GIN_OBJECT_TEMPLATE_BUILDER_H_
  6. #include <type_traits>
  7. #include <utility>
  8. #include "base/bind.h"
  9. #include "base/callback.h"
  10. #include "base/memory/raw_ptr.h"
  11. #include "base/strings/string_piece.h"
  12. #include "gin/converter.h"
  13. #include "gin/function_template.h"
  14. #include "gin/gin_export.h"
  15. #include "v8/include/v8-forward.h"
  16. namespace gin {
  17. namespace internal {
  18. template <typename T>
  19. v8::Local<v8::FunctionTemplate> CreateFunctionTemplate(v8::Isolate* isolate,
  20. T callback,
  21. const char* type_name) {
  22. // We need to handle member function pointers case specially because the first
  23. // parameter for callbacks to MFP should typically come from the the
  24. // JavaScript "this" object the function was called on, not from the first
  25. // normal parameter.
  26. InvokerOptions options;
  27. if (std::is_member_function_pointer<T>::value) {
  28. options.holder_is_first_argument = true;
  29. options.holder_type = type_name;
  30. }
  31. return ::gin::CreateFunctionTemplate(
  32. isolate, base::BindRepeating(std::move(callback)), std::move(options));
  33. }
  34. } // namespace internal
  35. // ObjectTemplateBuilder provides a handy interface to creating
  36. // v8::ObjectTemplate instances with various sorts of properties.
  37. class GIN_EXPORT ObjectTemplateBuilder {
  38. public:
  39. explicit ObjectTemplateBuilder(v8::Isolate* isolate);
  40. ObjectTemplateBuilder(v8::Isolate* isolate, const char* type_name);
  41. ObjectTemplateBuilder(v8::Isolate* isolate,
  42. const char* type_name,
  43. v8::Local<v8::ObjectTemplate> tmpl);
  44. ObjectTemplateBuilder(const ObjectTemplateBuilder& other);
  45. ~ObjectTemplateBuilder();
  46. // It's against Google C++ style to return a non-const ref, but we take some
  47. // poetic license here in order that all calls to Set() can be via the '.'
  48. // operator and line up nicely.
  49. template<typename T>
  50. ObjectTemplateBuilder& SetValue(const base::StringPiece& name, T val) {
  51. return SetImpl(name, ConvertToV8(isolate_, val));
  52. }
  53. // In the following methods, T and U can be function pointer, member function
  54. // pointer, base::RepeatingCallback, or v8::FunctionTemplate. Most clients
  55. // will want to use one of the first two options. Also see
  56. // gin::CreateFunctionTemplate() for creating raw function templates.
  57. template<typename T>
  58. ObjectTemplateBuilder& SetMethod(const base::StringPiece& name,
  59. const T& callback) {
  60. return SetImpl(
  61. name, internal::CreateFunctionTemplate(isolate_, callback, type_name_));
  62. }
  63. template <typename T>
  64. ObjectTemplateBuilder& SetMethod(v8::Local<v8::Name> name,
  65. const T& callback) {
  66. return SetImpl(
  67. name, internal::CreateFunctionTemplate(isolate_, callback, type_name_));
  68. }
  69. template<typename T>
  70. ObjectTemplateBuilder& SetProperty(const base::StringPiece& name,
  71. const T& getter) {
  72. return SetPropertyImpl(
  73. name, internal::CreateFunctionTemplate(isolate_, getter, type_name_),
  74. v8::Local<v8::FunctionTemplate>());
  75. }
  76. template<typename T, typename U>
  77. ObjectTemplateBuilder& SetProperty(const base::StringPiece& name,
  78. const T& getter, const U& setter) {
  79. return SetPropertyImpl(
  80. name, internal::CreateFunctionTemplate(isolate_, getter, type_name_),
  81. internal::CreateFunctionTemplate(isolate_, setter, type_name_));
  82. }
  83. // Whereas SetProperty creates an accessor property, this creates what appears
  84. // to be a data property but whose value is lazily computed the first time the
  85. // [[Get]] operation occurs.
  86. template <typename T>
  87. ObjectTemplateBuilder& SetLazyDataProperty(const base::StringPiece& name,
  88. const T& getter) {
  89. InvokerOptions options;
  90. if (std::is_member_function_pointer<T>::value) {
  91. options.holder_is_first_argument = true;
  92. options.holder_type = type_name_;
  93. }
  94. auto [callback, data] = CreateDataPropertyCallback(
  95. isolate_, base::BindRepeating(getter), std::move(options));
  96. return SetLazyDataPropertyImpl(name, callback, data);
  97. }
  98. ObjectTemplateBuilder& AddNamedPropertyInterceptor();
  99. ObjectTemplateBuilder& AddIndexedPropertyInterceptor();
  100. v8::Local<v8::ObjectTemplate> Build();
  101. private:
  102. ObjectTemplateBuilder& SetImpl(const base::StringPiece& name,
  103. v8::Local<v8::Data> val);
  104. ObjectTemplateBuilder& SetImpl(v8::Local<v8::Name> name,
  105. v8::Local<v8::Data> val);
  106. ObjectTemplateBuilder& SetPropertyImpl(
  107. const base::StringPiece& name, v8::Local<v8::FunctionTemplate> getter,
  108. v8::Local<v8::FunctionTemplate> setter);
  109. ObjectTemplateBuilder& SetLazyDataPropertyImpl(
  110. const base::StringPiece& name,
  111. v8::AccessorNameGetterCallback callback,
  112. v8::Local<v8::Value> data);
  113. raw_ptr<v8::Isolate> isolate_;
  114. // If provided, |type_name_| will be used to give a user-friendly error
  115. // message if a member function is invoked on the wrong type of object.
  116. const char* type_name_ = nullptr;
  117. // ObjectTemplateBuilder should only be used on the stack.
  118. v8::Local<v8::ObjectTemplate> template_;
  119. };
  120. } // namespace gin
  121. #endif // GIN_OBJECT_TEMPLATE_BUILDER_H_