2019-09-05 00:45:25 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								// Copyright (c) 2019 GitHub, Inc. All rights reserved.
							 | 
						
					
						
							
								
									
										
										
										
											2015-10-28 17:36:01 +08:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								// Use of this source code is governed by the MIT license that can be
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								// found in the LICENSE file.
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2019-09-05 00:45:25 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								#include "shell/common/gin_helper/callback.h"
							 | 
						
					
						
							
								
									
										
										
										
											2015-11-06 20:23:41 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2019-09-05 00:45:25 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								#include "content/public/browser/browser_thread.h"
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								#include "gin/dictionary.h"
							 | 
						
					
						
							
								
									
										
										
										
											2023-04-20 09:27:02 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								#include "shell/common/process_util.h"
							 | 
						
					
						
							
								
									
										
										
										
											2015-10-28 17:36:01 +08:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2019-09-05 00:45:25 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								namespace gin_helper {
							 | 
						
					
						
							
								
									
										
										
										
											2015-10-28 17:36:01 +08:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								namespace {
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2024-08-23 02:48:32 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								struct TranslatorHolder {
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  explicit TranslatorHolder(v8::Isolate* isolate)
							 | 
						
					
						
							
								
									
										
										
										
											2018-10-04 23:13:09 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      : handle(isolate, v8::External::New(isolate, this)) {
							 | 
						
					
						
							
								
									
										
										
										
											2022-05-17 12:48:40 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    handle.SetWeak(this, &GC, v8::WeakCallbackType::kParameter);
							 | 
						
					
						
							
								
									
										
										
										
											2018-10-04 23:13:09 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								  }
							 | 
						
					
						
							
								
									
										
										
										
											2024-08-23 02:48:32 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								  ~TranslatorHolder() {
							 | 
						
					
						
							
								
									
										
										
										
											2018-10-04 23:13:09 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    if (!handle.IsEmpty()) {
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								      handle.ClearWeak();
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								      handle.Reset();
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    }
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  }
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2024-08-23 02:48:32 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								  static void GC(const v8::WeakCallbackInfo<TranslatorHolder>& data) {
							 | 
						
					
						
							
								
									
										
										
										
											2018-10-04 23:13:09 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    delete data.GetParameter();
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  }
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  v8::Global<v8::External> handle;
							 | 
						
					
						
							
								
									
										
										
										
											2024-08-23 02:48:32 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								  Translator translator;
							 | 
						
					
						
							
								
									
										
										
										
											2015-10-28 17:36:01 +08:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								};
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2024-08-23 02:48:32 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								// Cached JavaScript version of |CallTranslator|.
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								v8::Persistent<v8::FunctionTemplate> g_call_translator;
							 | 
						
					
						
							
								
									
										
										
										
											2015-10-28 17:36:01 +08:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2024-08-23 02:48:32 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								void CallTranslator(v8::Local<v8::External> external,
							 | 
						
					
						
							
								
									
										
										
										
											2015-10-28 20:44:46 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								                    v8::Local<v8::Object> state,
							 | 
						
					
						
							
								
									
										
										
										
											2019-09-05 00:45:25 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								                    gin::Arguments* args) {
							 | 
						
					
						
							
								
									
										
										
										
											2020-03-11 18:07:54 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								  // Whether the callback should only be called once.
							 | 
						
					
						
							
								
									
										
										
										
											2015-10-28 20:44:46 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								  v8::Isolate* isolate = args->isolate();
							 | 
						
					
						
							
								
									
										
										
										
											2019-01-09 11:17:05 -08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								  auto context = isolate->GetCurrentContext();
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  bool one_time =
							 | 
						
					
						
							
								
									
										
										
										
											2019-09-06 14:52:54 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      state->Has(context, gin::StringToSymbol(isolate, "oneTime")).ToChecked();
							 | 
						
					
						
							
								
									
										
										
										
											2015-10-28 20:44:46 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  // Check if the callback has already been called.
							 | 
						
					
						
							
								
									
										
										
										
											2018-10-04 23:13:09 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								  if (one_time) {
							 | 
						
					
						
							
								
									
										
										
										
											2019-09-06 14:52:54 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    auto called_symbol = gin::StringToSymbol(isolate, "called");
							 | 
						
					
						
							
								
									
										
										
										
											2019-01-09 11:17:05 -08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    if (state->Has(context, called_symbol).ToChecked()) {
							 | 
						
					
						
							
								
									
										
										
										
											2020-03-11 18:07:54 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      args->ThrowTypeError("One-time callback was called more than once");
							 | 
						
					
						
							
								
									
										
										
										
											2018-10-04 23:13:09 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      return;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    } else {
							 | 
						
					
						
							
								
									
										
										
										
											2019-01-09 11:17:05 -08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      state->Set(context, called_symbol, v8::Boolean::New(isolate, true))
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								          .ToChecked();
							 | 
						
					
						
							
								
									
										
										
										
											2018-10-04 23:13:09 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    }
							 | 
						
					
						
							
								
									
										
										
										
											2015-10-28 20:44:46 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								  }
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2024-08-23 02:48:32 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								  auto* holder = static_cast<TranslatorHolder*>(external->Value());
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  holder->translator.Run(args);
							 | 
						
					
						
							
								
									
										
										
										
											2018-10-04 23:13:09 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  // Free immediately for one-time callback.
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  if (one_time)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    delete holder;
							 | 
						
					
						
							
								
									
										
										
										
											2015-10-28 17:36:01 +08:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								}
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								}  // namespace
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2015-12-03 11:24:33 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								// Destroy the class on UI thread when possible.
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								struct DeleteOnUIThread {
							 | 
						
					
						
							
								
									
										
										
										
											2018-04-17 21:55:30 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								  template <typename T>
							 | 
						
					
						
							
								
									
										
										
										
											2015-12-03 11:24:33 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								  static void Destruct(const T* x) {
							 | 
						
					
						
							
								
									
										
										
										
											2023-04-20 09:27:02 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    if (electron::IsBrowserProcess() &&
							 | 
						
					
						
							
								
									
										
										
										
											2019-09-05 00:45:25 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        !content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)) {
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								      content::BrowserThread::DeleteSoon(content::BrowserThread::UI, FROM_HERE,
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                                         x);
							 | 
						
					
						
							
								
									
										
										
										
											2015-12-03 11:24:33 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    } else {
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								      delete x;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    }
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  }
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								};
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								// Like v8::Global, but ref-counted.
							 | 
						
					
						
							
								
									
										
										
										
											2018-04-17 21:55:30 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								template <typename T>
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								class RefCountedGlobal
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    : public base::RefCountedThreadSafe<RefCountedGlobal<T>, DeleteOnUIThread> {
							 | 
						
					
						
							
								
									
										
										
										
											2015-12-03 11:24:33 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								 public:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  RefCountedGlobal(v8::Isolate* isolate, v8::Local<v8::Value> value)
							 | 
						
					
						
							
								
									
										
										
										
											2021-05-12 09:38:21 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      : handle_(isolate, value.As<T>()) {}
							 | 
						
					
						
							
								
									
										
										
										
											2015-12-03 11:24:33 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2018-04-17 21:55:30 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								  bool IsAlive() const { return !handle_.IsEmpty(); }
							 | 
						
					
						
							
								
									
										
										
										
											2015-12-03 11:24:33 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  v8::Local<T> NewHandle(v8::Isolate* isolate) const {
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    return v8::Local<T>::New(isolate, handle_);
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  }
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								 private:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  v8::Global<T> handle_;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								};
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2015-11-06 20:23:41 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								SafeV8Function::SafeV8Function(v8::Isolate* isolate, v8::Local<v8::Value> value)
							 | 
						
					
						
							
								
									
										
										
										
											2018-04-17 21:55:30 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    : v8_function_(new RefCountedGlobal<v8::Function>(isolate, value)) {}
							 | 
						
					
						
							
								
									
										
										
										
											2015-11-06 20:23:41 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2019-09-16 18:12:00 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								SafeV8Function::SafeV8Function(const SafeV8Function& other) = default;
							 | 
						
					
						
							
								
									
										
										
										
											2015-11-06 20:23:41 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2019-09-16 18:12:00 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								SafeV8Function::~SafeV8Function() = default;
							 | 
						
					
						
							
								
									
										
										
										
											2015-11-06 20:23:41 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2015-12-03 11:24:33 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								bool SafeV8Function::IsAlive() const {
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  return v8_function_.get() && v8_function_->IsAlive();
							 | 
						
					
						
							
								
									
										
										
										
											2015-11-06 20:23:41 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								}
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2015-12-03 11:24:33 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								v8::Local<v8::Function> SafeV8Function::NewHandle(v8::Isolate* isolate) const {
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  return v8_function_->NewHandle(isolate);
							 | 
						
					
						
							
								
									
										
										
										
											2015-11-06 20:23:41 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								}
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2024-08-23 02:48:32 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								v8::Local<v8::Value> CreateFunctionFromTranslator(v8::Isolate* isolate,
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                                                  const Translator& translator,
							 | 
						
					
						
							
								
									
										
										
										
											2018-10-05 10:38:27 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								                                                  bool one_time) {
							 | 
						
					
						
							
								
									
										
										
										
											2015-10-28 17:36:01 +08:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  // The FunctionTemplate is cached.
							 | 
						
					
						
							
								
									
										
										
										
											2024-08-23 02:48:32 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								  if (g_call_translator.IsEmpty())
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    g_call_translator.Reset(
							 | 
						
					
						
							
								
									
										
										
										
											2019-09-05 00:45:25 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        isolate,
							 | 
						
					
						
							
								
									
										
										
										
											2024-08-23 02:48:32 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        CreateFunctionTemplate(isolate, base::BindRepeating(&CallTranslator)));
							 | 
						
					
						
							
								
									
										
										
										
											2015-10-28 17:36:01 +08:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2024-08-23 02:48:32 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								  v8::Local<v8::FunctionTemplate> call_translator =
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								      v8::Local<v8::FunctionTemplate>::New(isolate, g_call_translator);
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  auto* holder = new TranslatorHolder(isolate);
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  holder->translator = translator;
							 | 
						
					
						
							
								
									
										
										
										
											2023-08-21 03:43:41 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								  auto state = gin::Dictionary::CreateEmpty(isolate);
							 | 
						
					
						
							
								
									
										
										
										
											2018-10-04 23:13:09 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								  if (one_time)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    state.Set("oneTime", true);
							 | 
						
					
						
							
								
									
										
										
										
											2019-01-09 11:17:05 -08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								  auto context = isolate->GetCurrentContext();
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  return BindFunctionWith(
							 | 
						
					
						
							
								
									
										
										
										
											2024-08-23 02:48:32 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      isolate, context, call_translator->GetFunction(context).ToLocalChecked(),
							 | 
						
					
						
							
								
									
										
										
										
											2019-09-05 00:45:25 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      holder->handle.Get(isolate), gin::ConvertToV8(isolate, state));
							 | 
						
					
						
							
								
									
										
										
										
											2015-10-28 17:36:01 +08:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								}
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2017-11-03 15:00:41 -03:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								// func.bind(func, arg1).
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								// NB(zcbenz): Using C++11 version crashes VS.
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								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) {
							 | 
						
					
						
							
								
									
										
										
										
											2019-04-30 20:18:22 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								  v8::MaybeLocal<v8::Value> bind =
							 | 
						
					
						
							
								
									
										
										
										
											2019-09-06 14:52:54 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      func->Get(context, gin::StringToV8(isolate, "bind"));
							 | 
						
					
						
							
								
									
										
										
										
											2017-11-03 15:00:41 -03:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								  CHECK(!bind.IsEmpty());
							 | 
						
					
						
							
								
									
										
										
										
											2021-05-12 09:38:21 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								  v8::Local<v8::Function> bind_func = bind.ToLocalChecked().As<v8::Function>();
							 | 
						
					
						
							
								
									
										
										
										
											2017-11-03 15:00:41 -03:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								  v8::Local<v8::Value> converted[] = {func, arg1, arg2};
							 | 
						
					
						
							
								
									
										
										
										
											2022-03-24 21:39:03 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								  return bind_func->Call(context, func, std::size(converted), converted)
							 | 
						
					
						
							
								
									
										
										
										
											2017-11-03 15:00:41 -03:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      .ToLocalChecked();
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								}
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2019-09-05 00:45:25 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								}  // namespace gin_helper
							 |