From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Shelley Vohr <shelley.vohr@gmail.com>
Date: Thu, 16 Nov 2023 16:48:10 +0100
Subject: feat: optionally prevent calling V8::EnableWebAssemblyTrapHandler

V8::EnableWebAssemblyTrapHandler can be called only once or it will
hard crash. We need to prevent Node.js calling it in the event it has
already been called.

This should be upstreamed.

diff --git a/src/node.cc b/src/node.cc
index 89e0e5524c2102b86bc5506fe49aa0c6fa0f30c1..e58f28e0f0ff8d61f35ec3c5a69aa37c66c25d78 100644
--- a/src/node.cc
+++ b/src/node.cc
@@ -605,6 +605,7 @@ static void PlatformInit(ProcessInitializationFlags::Flags flags) {
 #endif
     }
 #endif  // defined(_WIN32)
+  if (!(flags & ProcessInitializationFlags::kNoEnableWasmTrapHandler))
     V8::EnableWebAssemblyTrapHandler(false);
 #endif  // NODE_USE_V8_WASM_TRAP_HANDLER
   }
diff --git a/src/node.h b/src/node.h
index 9ac0d5addcdd40d5c91d375b626099b95729548a..3ffc51783b0b6dee1c0f0a37d2f52cb1aec2fa3f 100644
--- a/src/node.h
+++ b/src/node.h
@@ -272,6 +272,10 @@ enum Flags : uint32_t {
   // cppgc::InitializeProcess() before creating a Node.js environment
   // and call cppgc::ShutdownProcess() before process shutdown.
   kNoInitializeCppgc = 1 << 13,
+  // Do not initialize the Web Assembly trap handler. This is used by
+  // embedders to account for the case where it may already have been
+  // initialized - calling it more than once will hard crash.
+  kNoEnableWasmTrapHandler = 1 << 14,
 
   // Emulate the behavior of InitializeNodeWithArgs() when passing
   // a flags argument to the InitializeOncePerProcess() replacement
