From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Shelley Vohr <shelley.vohr@gmail.com>
Date: Thu, 13 Sep 2018 08:42:22 -0700
Subject: feat: add flags for low-level hooks and exceptions

This commit adds two new exposed methods to `node.cc`, `InitGeneric` and
`LoadEnvironmentGeneric` that allow for control of which mode Node
should run, which i have called `standalone_mode` and
`upstream_node_mode`. Default behavior of `Init` and `LoadEnvironment`
remain the same.

We have 3 modes when running Node in Electron:
1. In the main process, we want to have a full Node environment, but
with signal handlers and other low level things disabled
2. In renderer process, we want Node to reuse the web page's context
3. In `ELECTRON_RUN_AS_NODE`, we want Node to run as it runs officially
by default

For modes 1 and 3, we have Node create a new V8 context with a Node
Environment on it. However, for mode 2, since the V8 context is created
by blink for web frames and web workers we make Node create the Node
Environment on the V8 context of blink, so no new V8 context is created.

As a result, a renderer process may have multiple Node Environments in it.

diff --git a/src/node.cc b/src/node.cc
index e942c108a15a3c50d92c08b344f6691050c7859d..207a95d202b4e422a39f837241f1655f7111b1e3 100644
--- a/src/node.cc
+++ b/src/node.cc
@@ -139,6 +139,8 @@ using v8::Undefined;
 using v8::V8;
 using v8::Value;
 
+bool g_upstream_node_mode = true;
+
 namespace per_process {
 
 // node_revert.h
@@ -853,7 +855,9 @@ int InitializeNodeWithArgs(std::vector<std::string>* argv,
   binding::RegisterBuiltinModules();
 
   // Make inherited handles noninheritable.
-  uv_disable_stdio_inheritance();
+  if (g_upstream_node_mode) {
+    uv_disable_stdio_inheritance();
+  }
 
   // Cache the original command line to be
   // used in diagnostic reports.
@@ -887,7 +891,8 @@ int InitializeNodeWithArgs(std::vector<std::string>* argv,
     if (exit_code != 0) return exit_code;
   }
 #endif
-
+  if (g_upstream_node_mode) {
+  // NOTE(jeremy): indentation is intentionally wrong here, to ease rebasing.
   const int exit_code = ProcessGlobalArgs(argv,
                                           exec_argv,
                                           errors,
@@ -930,7 +935,7 @@ int InitializeNodeWithArgs(std::vector<std::string>* argv,
     return 9;
   }
   per_process::metadata.versions.InitializeIntlVersions();
-
+  }  // g_upstream_node_mode
 # ifndef __POSIX__
   std::string tz;
   if (credentials::SafeGetenv("TZ", &tz) && !tz.empty()) {
diff --git a/src/node.h b/src/node.h
index 1f9afa558d0c8b7950a0f5862017e09a08118ec1..45de72bd94cf669ac2badf89d23164cb7022a5b3 100644
--- a/src/node.h
+++ b/src/node.h
@@ -213,6 +213,8 @@ namespace node {
 
 class IsolateData;
 class Environment;
+// Whether node should open some low level hooks.
+NODE_EXTERN extern bool g_upstream_node_mode;
 
 // TODO(addaleax): Officially deprecate this and replace it with something
 // better suited for a public embedder API.
