From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Samuel Attard <marshallofsound@electronjs.org>
Date: Wed, 8 Mar 2023 13:04:21 -0800
Subject: refactor: expose HostImportModuleDynamically and
 HostGetImportMetaProperties to embedders

This is so that Electron can blend Blink's and Node's implementations of these isolate handlers.

diff --git a/third_party/blink/renderer/bindings/core/v8/v8_initializer.cc b/third_party/blink/renderer/bindings/core/v8/v8_initializer.cc
index 0d6d15ca0abf91df9b7ac9f1630a150b7afcd6d3..8ffd5a65d6b7d0f61372c7dbe3d1ca3d9397d6d3 100644
--- a/third_party/blink/renderer/bindings/core/v8/v8_initializer.cc
+++ b/third_party/blink/renderer/bindings/core/v8/v8_initializer.cc
@@ -738,8 +738,9 @@ bool WasmCustomDescriptorsEnabledCallback(v8::Local<v8::Context> context) {
   return RuntimeEnabledFeatures::WebAssemblyCustomDescriptorsEnabled(
       execution_context);
 }
+}  // namespace
 
-v8::MaybeLocal<v8::Promise> HostImportModuleWithPhaseDynamically(
+v8::MaybeLocal<v8::Promise> V8Initializer::HostImportModuleWithPhaseDynamically(
     v8::Local<v8::Context> context,
     v8::Local<v8::Data> v8_host_defined_options,
     v8::Local<v8::Value> v8_referrer_resource_url,
@@ -817,20 +818,23 @@ v8::MaybeLocal<v8::Promise> HostImportModuleWithPhaseDynamically(
 
   return resolver->Promise().V8Promise();
 }
+// namespace
 
+namespace {
 v8::MaybeLocal<v8::Promise> HostImportModuleDynamically(
     v8::Local<v8::Context> context,
     v8::Local<v8::Data> v8_host_defined_options,
     v8::Local<v8::Value> v8_referrer_resource_url,
     v8::Local<v8::String> v8_specifier,
     v8::Local<v8::FixedArray> v8_import_attributes) {
-  return HostImportModuleWithPhaseDynamically(
+  return V8Initializer::HostImportModuleWithPhaseDynamically(
       context, v8_host_defined_options, v8_referrer_resource_url, v8_specifier,
       v8::ModuleImportPhase::kEvaluation, v8_import_attributes);
 }
+}  // namespace
 
 // https://html.spec.whatwg.org/C/#hostgetimportmetaproperties
-void HostGetImportMetaProperties(v8::Local<v8::Context> context,
+void V8Initializer::HostGetImportMetaProperties(v8::Local<v8::Context> context,
                                  v8::Local<v8::Module> module,
                                  v8::Local<v8::Object> meta) {
   v8::Isolate* isolate = v8::Isolate::GetCurrent();
@@ -857,6 +861,7 @@ void HostGetImportMetaProperties(v8::Local<v8::Context> context,
   meta->CreateDataProperty(context, resolve_key, resolve_value).ToChecked();
 }
 
+namespace {
 bool IsDOMExceptionWrapper(v8::Isolate* isolate, v8::Local<v8::Object> object) {
   return V8DOMException::HasInstance(isolate, object);
 }
@@ -887,7 +892,6 @@ void EmitDevToolsEvent(v8::Isolate* isolate) {
 
 }  // namespace
 
-// static
 void V8Initializer::InitializeV8Common(v8::Isolate* isolate) {
   // Set up garbage collection before setting up anything else as V8 may trigger
   // GCs during Blink setup.
@@ -903,9 +907,9 @@ void V8Initializer::InitializeV8Common(v8::Isolate* isolate) {
       SharedArrayBufferConstructorEnabledCallback);
   isolate->SetHostImportModuleDynamicallyCallback(HostImportModuleDynamically);
   isolate->SetHostImportModuleWithPhaseDynamicallyCallback(
-      HostImportModuleWithPhaseDynamically);
+     V8Initializer::HostImportModuleWithPhaseDynamically);
   isolate->SetHostInitializeImportMetaObjectCallback(
-      HostGetImportMetaProperties);
+     V8Initializer::HostGetImportMetaProperties);
   isolate->SetIsJSApiWrapperNativeErrorCallback(IsDOMExceptionWrapper);
   isolate->SetMetricsRecorder(std::make_shared<V8MetricsRecorder>(isolate));
 
diff --git a/third_party/blink/renderer/bindings/core/v8/v8_initializer.h b/third_party/blink/renderer/bindings/core/v8/v8_initializer.h
index ba688857b996f49ee7fb5e071bdbb55f1b667b27..f87eeddb6da48bf6a554c35e45e7f39def14938e 100644
--- a/third_party/blink/renderer/bindings/core/v8/v8_initializer.h
+++ b/third_party/blink/renderer/bindings/core/v8/v8_initializer.h
@@ -84,6 +84,18 @@ class CORE_EXPORT V8Initializer {
   static void PromiseRejectHandlerInMainThread(v8::PromiseRejectMessage data);
   static void ExceptionPropagationCallback(v8::ExceptionPropagationMessage);
 
+  static v8::MaybeLocal<v8::Promise> HostImportModuleWithPhaseDynamically(
+    v8::Local<v8::Context> context,
+    v8::Local<v8::Data> v8_host_defined_options,
+    v8::Local<v8::Value> v8_referrer_resource_url,
+    v8::Local<v8::String> v8_specifier,
+    v8::ModuleImportPhase import_phase,
+    v8::Local<v8::FixedArray> v8_import_assertions);
+
+  static void HostGetImportMetaProperties(v8::Local<v8::Context> context,
+                                          v8::Local<v8::Module> module,
+                                          v8::Local<v8::Object> meta);
+
   static void WasmAsyncResolvePromiseCallback(
       v8::Isolate* isolate,
       v8::Local<v8::Context> context,
