gin_util.h 822 B

1234567891011121314151617181920212223242526272829
  1. // Copyright (c) 2018 Slack Technologies, Inc.
  2. // Use of this source code is governed by the MIT license that can be
  3. // found in the LICENSE file.
  4. #ifndef SHELL_COMMON_GIN_UTIL_H_
  5. #define SHELL_COMMON_GIN_UTIL_H_
  6. #include "gin/converter.h"
  7. #include "gin/function_template.h"
  8. namespace gin_util {
  9. template <typename T>
  10. bool SetMethod(v8::Local<v8::Object> recv,
  11. base::StringPiece key,
  12. const T& callback) {
  13. v8::Isolate* isolate = v8::Isolate::GetCurrent();
  14. auto context = isolate->GetCurrentContext();
  15. return recv
  16. ->Set(context, gin::StringToV8(isolate, key),
  17. gin::CreateFunctionTemplate(isolate, callback)
  18. ->GetFunction(context)
  19. .ToLocalChecked())
  20. .ToChecked();
  21. }
  22. } // namespace gin_util
  23. #endif // SHELL_COMMON_GIN_UTIL_H_