build: configure --shared

Add configure flag for building a shared library that can be
embedded in other applications (like Electron). Add flags
--without-bundled-v8 and --without-v8-platform to control V8
dependencies used.

PR-URL: https://github.com/nodejs/node/pull/6994
Reviewed-By: Ben Noordhuis <[email protected]>
Reviewed-By: Fedor Indutny <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Michael Dawson <[email protected]>
diff --git a/src/node.cc b/src/node.cc
index b6fa460..cc15ac2 100644
--- a/src/node.cc
+++ b/src/node.cc
@@ -39,7 +39,9 @@
 #include "string_bytes.h"
 #include "util.h"
 #include "uv.h"
+#if NODE_USE_V8_PLATFORM
 #include "libplatform/libplatform.h"
+#endif  // NODE_USE_V8_PLATFORM
 #include "v8-debug.h"
 #include "v8-profiler.h"
 #include "zlib.h"
@@ -183,7 +185,42 @@
 
 static Mutex node_isolate_mutex;
 static v8::Isolate* node_isolate;
-static v8::Platform* default_platform;
+
+static struct {
+#if NODE_USE_V8_PLATFORM
+  void Initialize(int thread_pool_size) {
+    platform_ = v8::platform::CreateDefaultPlatform(thread_pool_size);
+    V8::InitializePlatform(platform_);
+  }
+
+  void PumpMessageLoop(Isolate* isolate) {
+    v8::platform::PumpMessageLoop(platform_, isolate);
+  }
+
+  void Dispose() {
+    delete platform_;
+    platform_ = nullptr;
+  }
+
+#if HAVE_INSPECTOR
+  void StartInspector(Environment *env, int port, bool wait) {
+    env->inspector_agent()->Start(platform_, port, wait);
+  }
+#endif  // HAVE_INSPECTOR
+
+  v8::Platform* platform_;
+#else  // !NODE_USE_V8_PLATFORM
+  void Initialize(int thread_pool_size) {}
+  void PumpMessageLoop(Isolate* isolate) {}
+  void Dispose() {}
+#if HAVE_INSPECTOR
+  void StartInspector(Environment *env, int port, bool wait) {
+    env->ThrowError("Node compiled with NODE_USE_V8_PLATFORM=0");
+  }
+#endif  // HAVE_INSPECTOR
+
+#endif  // !NODE_USE_V8_PLATFORM
+} v8_platform;
 
 #ifdef __POSIX__
 static uv_sem_t debug_semaphore;
@@ -3652,7 +3689,7 @@
   CHECK(!debugger_running);
 #if HAVE_INSPECTOR
   if (use_inspector) {
-    env->inspector_agent()->Start(default_platform, inspector_port, wait);
+    v8_platform.StartInspector(env, inspector_port, wait);
     debugger_running = true;
   } else {
 #endif
@@ -4299,11 +4336,11 @@
       SealHandleScope seal(isolate);
       bool more;
       do {
-        v8::platform::PumpMessageLoop(default_platform, isolate);
+        v8_platform.PumpMessageLoop(isolate);
         more = uv_run(env.event_loop(), UV_RUN_ONCE);
 
         if (more == false) {
-          v8::platform::PumpMessageLoop(default_platform, isolate);
+          v8_platform.PumpMessageLoop(isolate);
           EmitBeforeExit(&env);
 
           // Emit `beforeExit` if the loop became alive either after emitting
@@ -4364,8 +4401,7 @@
   V8::SetEntropySource(crypto::EntropySource);
 #endif
 
-  default_platform = v8::platform::CreateDefaultPlatform(v8_thread_pool_size);
-  V8::InitializePlatform(default_platform);
+  v8_platform.Initialize(v8_thread_pool_size);
   V8::Initialize();
 
   int exit_code = 1;
@@ -4382,8 +4418,7 @@
   }
   V8::Dispose();
 
-  delete default_platform;
-  default_platform = nullptr;
+  v8_platform.Dispose();
 
   delete[] exec_argv;
   exec_argv = nullptr;