2019-09-05 00:45:25 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								// Copyright (c) 2019 GitHub, Inc. All rights reserved.
							 | 
						
					
						
							
								
									
										
										
										
											2015-08-07 18:10:19 +08:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								// Use of this source code is governed by the MIT license that can be
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								// found in the LICENSE file.
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2021-11-22 08:34:31 +01:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								#ifndef ELECTRON_SHELL_COMMON_GIN_HELPER_CALLBACK_H_
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								#define ELECTRON_SHELL_COMMON_GIN_HELPER_CALLBACK_H_
							 | 
						
					
						
							
								
									
										
										
										
											2015-08-07 19:34:00 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2024-09-18 16:23:29 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								#include <array>
							 | 
						
					
						
							
								
									
										
										
										
											2019-04-25 09:40:27 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								#include <utility>
							 | 
						
					
						
							
								
									
										
										
										
											2015-08-07 19:15:36 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2023-02-03 12:43:42 +01:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								#include "base/functional/bind.h"
							 | 
						
					
						
							
								
									
										
										
										
											2019-09-06 14:52:54 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								#include "shell/common/gin_converters/std_converter.h"
							 | 
						
					
						
							
								
									
										
										
										
											2019-09-05 00:45:25 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								#include "shell/common/gin_helper/function_template.h"
							 | 
						
					
						
							
								
									
										
										
										
											2019-10-31 16:56:00 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								#include "shell/common/gin_helper/locker.h"
							 | 
						
					
						
							
								
									
										
										
										
											2020-06-17 10:08:10 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								#include "shell/common/gin_helper/microtasks_scope.h"
							 | 
						
					
						
							
								
									
										
										
										
											2024-07-29 12:42:57 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								#include "v8/include/v8-function.h"
							 | 
						
					
						
							
								
									
										
										
										
											2021-05-31 18:42:38 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								// Implements safe conversions between JS functions and base::RepeatingCallback.
							 | 
						
					
						
							
								
									
										
										
										
											2015-08-07 18:10:19 +08:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2019-09-05 00:45:25 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								namespace gin_helper {
							 | 
						
					
						
							
								
									
										
										
										
											2015-08-07 18:10:19 +08:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2018-04-17 21:44:10 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								template <typename T>
							 | 
						
					
						
							
								
									
										
										
										
											2015-12-03 11:24:33 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								class RefCountedGlobal;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								// Manages the V8 function with RAII.
							 | 
						
					
						
							
								
									
										
										
										
											2015-11-06 20:23:41 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								class SafeV8Function {
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								 public:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  SafeV8Function(v8::Isolate* isolate, v8::Local<v8::Value> value);
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  SafeV8Function(const SafeV8Function& other);
							 | 
						
					
						
							
								
									
										
										
										
											2015-12-03 11:24:33 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								  ~SafeV8Function();
							 | 
						
					
						
							
								
									
										
										
										
											2015-11-06 20:23:41 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2015-12-03 11:24:33 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								  bool IsAlive() const;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  v8::Local<v8::Function> NewHandle(v8::Isolate* isolate) const;
							 | 
						
					
						
							
								
									
										
										
										
											2015-11-06 20:23:41 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								 private:
							 | 
						
					
						
							
								
									
										
										
										
											2015-12-03 11:24:33 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								  scoped_refptr<RefCountedGlobal<v8::Function>> v8_function_;
							 | 
						
					
						
							
								
									
										
										
										
											2015-11-06 20:23:41 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								};
							 | 
						
					
						
							
								
									
										
										
										
											2015-08-07 18:10:19 +08:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2015-10-28 17:36:01 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								// Helper to invoke a V8 function with C++ parameters.
							 | 
						
					
						
							
								
									
										
										
										
											2015-08-07 19:15:36 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								template <typename Sig>
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								struct V8FunctionInvoker {};
							 | 
						
					
						
							
								
									
										
										
										
											2015-08-07 18:10:19 +08:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2015-08-07 19:15:36 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								template <typename... ArgTypes>
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								struct V8FunctionInvoker<v8::Local<v8::Value>(ArgTypes...)> {
							 | 
						
					
						
							
								
									
										
										
										
											2015-11-06 20:23:41 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								  static v8::Local<v8::Value> Go(v8::Isolate* isolate,
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                                 const SafeV8Function& function,
							 | 
						
					
						
							
								
									
										
										
										
											2015-08-07 19:15:36 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								                                 ArgTypes... raw) {
							 | 
						
					
						
							
								
									
										
										
										
											2019-10-31 16:56:00 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    gin_helper::Locker locker(isolate);
							 | 
						
					
						
							
								
									
										
										
										
											2015-08-07 18:10:19 +08:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    v8::EscapableHandleScope handle_scope(isolate);
							 | 
						
					
						
							
								
									
										
										
										
											2015-12-03 11:24:33 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    if (!function.IsAlive())
							 | 
						
					
						
							
								
									
										
										
										
											2015-11-06 20:23:41 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      return v8::Null(isolate);
							 | 
						
					
						
							
								
									
										
										
										
											2015-12-03 11:24:33 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    v8::Local<v8::Function> holder = function.NewHandle(isolate);
							 | 
						
					
						
							
								
									
										
										
										
											2022-02-09 18:58:52 -08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    v8::Local<v8::Context> context = holder->GetCreationContextChecked();
							 | 
						
					
						
							
								
									
										
										
										
											2024-08-05 08:24:27 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    gin_helper::MicrotasksScope microtasks_scope{
							 | 
						
					
						
							
								
									
										
										
										
											2025-02-07 02:44:19 +01:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        context, true, v8::MicrotasksScope::kRunMicrotasks};
							 | 
						
					
						
							
								
									
										
										
										
											2015-08-07 18:10:19 +08:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    v8::Context::Scope context_scope(context);
							 | 
						
					
						
							
								
									
										
										
										
											2024-09-18 16:23:29 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    std::array<v8::Local<v8::Value>, sizeof...(raw)> args{
							 | 
						
					
						
							
								
									
										
										
										
											2019-09-05 00:45:25 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        gin::ConvertToV8(isolate, std::forward<ArgTypes>(raw))...};
							 | 
						
					
						
							
								
									
										
										
										
											2019-03-21 00:27:06 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    v8::MaybeLocal<v8::Value> ret = holder->Call(
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        context, holder, args.size(), args.empty() ? nullptr : &args.front());
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    if (ret.IsEmpty())
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								      return v8::Undefined(isolate);
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    else
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								      return handle_scope.Escape(ret.ToLocalChecked());
							 | 
						
					
						
							
								
									
										
										
										
											2015-08-07 18:10:19 +08:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  }
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								};
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2015-08-07 19:15:36 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								template <typename... ArgTypes>
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								struct V8FunctionInvoker<void(ArgTypes...)> {
							 | 
						
					
						
							
								
									
										
										
										
											2015-11-06 20:23:41 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								  static void Go(v8::Isolate* isolate,
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                 const SafeV8Function& function,
							 | 
						
					
						
							
								
									
										
										
										
											2015-08-07 19:15:36 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								                 ArgTypes... raw) {
							 | 
						
					
						
							
								
									
										
										
										
											2019-10-31 16:56:00 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    gin_helper::Locker locker(isolate);
							 | 
						
					
						
							
								
									
										
										
										
											2015-08-07 19:15:36 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    v8::HandleScope handle_scope(isolate);
							 | 
						
					
						
							
								
									
										
										
										
											2015-12-03 11:24:33 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    if (!function.IsAlive())
							 | 
						
					
						
							
								
									
										
										
										
											2015-11-06 20:23:41 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      return;
							 | 
						
					
						
							
								
									
										
										
										
											2015-12-03 11:24:33 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    v8::Local<v8::Function> holder = function.NewHandle(isolate);
							 | 
						
					
						
							
								
									
										
										
										
											2022-02-09 18:58:52 -08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    v8::Local<v8::Context> context = holder->GetCreationContextChecked();
							 | 
						
					
						
							
								
									
										
										
										
											2024-08-05 08:24:27 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    gin_helper::MicrotasksScope microtasks_scope{
							 | 
						
					
						
							
								
									
										
										
										
											2025-02-07 02:44:19 +01:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        context, true, v8::MicrotasksScope::kRunMicrotasks};
							 | 
						
					
						
							
								
									
										
										
										
											2015-08-07 18:10:19 +08:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    v8::Context::Scope context_scope(context);
							 | 
						
					
						
							
								
									
										
										
										
											2024-09-18 16:23:29 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    std::array<v8::Local<v8::Value>, sizeof...(raw)> args{
							 | 
						
					
						
							
								
									
										
										
										
											2019-09-05 00:45:25 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        gin::ConvertToV8(isolate, std::forward<ArgTypes>(raw))...};
							 | 
						
					
						
							
								
									
										
										
										
											2019-01-09 11:17:05 -08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    holder
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        ->Call(context, holder, args.size(),
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								               args.empty() ? nullptr : &args.front())
							 | 
						
					
						
							
								
									
										
										
										
											2019-03-21 00:27:06 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        .IsEmpty();
							 | 
						
					
						
							
								
									
										
										
										
											2015-08-07 18:10:19 +08:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  }
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								};
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2015-08-07 19:15:36 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								template <typename ReturnType, typename... ArgTypes>
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								struct V8FunctionInvoker<ReturnType(ArgTypes...)> {
							 | 
						
					
						
							
								
									
										
										
										
											2015-11-06 20:23:41 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								  static ReturnType Go(v8::Isolate* isolate,
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                       const SafeV8Function& function,
							 | 
						
					
						
							
								
									
										
										
										
											2015-08-07 19:15:36 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								                       ArgTypes... raw) {
							 | 
						
					
						
							
								
									
										
										
										
											2019-10-31 16:56:00 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    gin_helper::Locker locker(isolate);
							 | 
						
					
						
							
								
									
										
										
										
											2015-08-07 19:15:36 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    v8::HandleScope handle_scope(isolate);
							 | 
						
					
						
							
								
									
										
										
										
											2015-11-06 20:23:41 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    ReturnType ret = ReturnType();
							 | 
						
					
						
							
								
									
										
										
										
											2015-12-03 11:24:33 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    if (!function.IsAlive())
							 | 
						
					
						
							
								
									
										
										
										
											2015-11-06 20:23:41 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      return ret;
							 | 
						
					
						
							
								
									
										
										
										
											2015-12-03 11:24:33 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    v8::Local<v8::Function> holder = function.NewHandle(isolate);
							 | 
						
					
						
							
								
									
										
										
										
											2022-02-09 18:58:52 -08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    v8::Local<v8::Context> context = holder->GetCreationContextChecked();
							 | 
						
					
						
							
								
									
										
										
										
											2024-08-05 08:24:27 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    gin_helper::MicrotasksScope microtasks_scope{
							 | 
						
					
						
							
								
									
										
										
										
											2025-02-07 02:44:19 +01:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        context, true, v8::MicrotasksScope::kRunMicrotasks};
							 | 
						
					
						
							
								
									
										
										
										
											2015-08-07 18:10:19 +08:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    v8::Context::Scope context_scope(context);
							 | 
						
					
						
							
								
									
										
										
										
											2024-09-18 16:23:29 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    std::array<v8::Local<v8::Value>, sizeof...(raw)> args{
							 | 
						
					
						
							
								
									
										
										
										
											2019-09-05 00:45:25 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        gin::ConvertToV8(isolate, std::forward<ArgTypes>(raw))...};
							 | 
						
					
						
							
								
									
										
										
										
											2015-10-24 22:10:38 +05:30
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    v8::Local<v8::Value> result;
							 | 
						
					
						
							
								
									
										
										
										
											2018-04-17 21:44:10 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    auto maybe_result = holder->Call(context, holder, args.size(),
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                                     args.empty() ? nullptr : &args.front());
							 | 
						
					
						
							
								
									
										
										
										
											2015-10-24 22:10:38 +05:30
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    if (maybe_result.ToLocal(&result))
							 | 
						
					
						
							
								
									
										
										
										
											2019-09-05 00:45:25 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      gin::Converter<ReturnType>::FromV8(isolate, result, &ret);
							 | 
						
					
						
							
								
									
										
										
										
											2015-08-07 18:10:19 +08:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    return ret;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  }
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								};
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2021-05-31 18:42:38 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								// Helper to pass a C++ function to JavaScript.
							 | 
						
					
						
							
								
									
										
										
										
											2024-08-23 02:48:32 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								using Translator = base::RepeatingCallback<void(gin::Arguments* args)>;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								v8::Local<v8::Value> CreateFunctionFromTranslator(v8::Isolate* isolate,
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                                                  const Translator& translator,
							 | 
						
					
						
							
								
									
										
										
										
											2018-10-05 10:38:27 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								                                                  bool one_time);
							 | 
						
					
						
							
								
									
										
										
										
											2017-11-03 15:00:41 -03:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								v8::Local<v8::Value> BindFunctionWith(v8::Isolate* isolate,
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                                      v8::Local<v8::Context> context,
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                                      v8::Local<v8::Function> func,
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                                      v8::Local<v8::Value> arg1,
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                                      v8::Local<v8::Value> arg2);
							 | 
						
					
						
							
								
									
										
										
										
											2015-10-28 17:36:01 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								// Calls callback with Arguments.
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								template <typename Sig>
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								struct NativeFunctionInvoker {};
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								template <typename ReturnType, typename... ArgTypes>
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								struct NativeFunctionInvoker<ReturnType(ArgTypes...)> {
							 | 
						
					
						
							
								
									
										
										
										
											2021-05-06 15:01:04 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								  static void Go(base::RepeatingCallback<ReturnType(ArgTypes...)> val,
							 | 
						
					
						
							
								
									
										
										
										
											2019-09-05 00:45:25 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								                 gin::Arguments* args) {
							 | 
						
					
						
							
								
									
										
										
										
											2024-03-25 10:01:54 +01:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    using Indices = std::index_sequence_for<ArgTypes...>;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    Invoker<Indices, ArgTypes...> invoker(args,
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                                          {.holder_is_first_argument = false});
							 | 
						
					
						
							
								
									
										
										
										
											2015-10-28 17:36:01 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    if (invoker.IsOK())
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								      invoker.DispatchToCallback(val);
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  }
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								};
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2018-10-04 23:13:09 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								// Convert a callback to V8 without the call number limitation, this can easily
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								// cause memory leaks so use it with caution.
							 | 
						
					
						
							
								
									
										
										
										
											2018-10-05 10:38:27 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								template <typename Sig>
							 | 
						
					
						
							
								
									
										
										
										
											2019-09-05 00:45:25 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								v8::Local<v8::Value> CallbackToV8Leaked(
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    v8::Isolate* isolate,
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    const base::RepeatingCallback<Sig>& val) {
							 | 
						
					
						
							
								
									
										
										
										
											2024-08-23 02:48:32 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								  Translator translator =
							 | 
						
					
						
							
								
									
										
										
										
											2021-01-25 02:27:40 +01:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      base::BindRepeating(&NativeFunctionInvoker<Sig>::Go, val);
							 | 
						
					
						
							
								
									
										
										
										
											2024-08-23 02:48:32 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								  return CreateFunctionFromTranslator(isolate, translator, false);
							 | 
						
					
						
							
								
									
										
										
										
											2018-10-04 23:13:09 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								}
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2019-09-05 00:45:25 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								}  // namespace gin_helper
							 | 
						
					
						
							
								
									
										
										
										
											2015-08-07 19:34:00 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2021-11-22 08:34:31 +01:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								#endif  // ELECTRON_SHELL_COMMON_GIN_HELPER_CALLBACK_H_
							 |