node_main.cc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // Copyright (c) 2015 GitHub, Inc.
  2. // Use of this source code is governed by the MIT license that can be
  3. // found in the LICENSE file.
  4. #ifdef ENABLE_RUN_AS_NODE
  5. #include "atom/app/node_main.h"
  6. #include "atom/app/uv_task_runner.h"
  7. #include "atom/browser/javascript_environment.h"
  8. #include "atom/browser/node_debugger.h"
  9. #include "atom/common/api/atom_bindings.h"
  10. #include "atom/common/crash_reporter/crash_reporter.h"
  11. #include "atom/common/native_mate_converters/string16_converter.h"
  12. #include "atom/common/node_bindings.h"
  13. #include "base/command_line.h"
  14. #include "base/feature_list.h"
  15. #include "base/task_scheduler/task_scheduler.h"
  16. #include "base/threading/thread_task_runner_handle.h"
  17. #include "gin/array_buffer.h"
  18. #include "gin/public/isolate_holder.h"
  19. #include "gin/v8_initializer.h"
  20. #include "native_mate/dictionary.h"
  21. #include "atom/common/node_includes.h"
  22. namespace atom {
  23. int NodeMain(int argc, char* argv[]) {
  24. base::CommandLine::Init(argc, argv);
  25. int exit_code = 1;
  26. {
  27. // Feed gin::PerIsolateData with a task runner.
  28. argv = uv_setup_args(argc, argv);
  29. uv_loop_t* loop = uv_default_loop();
  30. scoped_refptr<UvTaskRunner> uv_task_runner(new UvTaskRunner(loop));
  31. base::ThreadTaskRunnerHandle handle(uv_task_runner);
  32. // Initialize feature list.
  33. auto feature_list = std::make_unique<base::FeatureList>();
  34. feature_list->InitializeFromCommandLine("", "");
  35. base::FeatureList::SetInstance(std::move(feature_list));
  36. gin::V8Initializer::LoadV8Snapshot(
  37. gin::V8Initializer::V8SnapshotFileType::kWithAdditionalContext);
  38. gin::V8Initializer::LoadV8Natives();
  39. // V8 requires a task scheduler apparently
  40. base::TaskScheduler::CreateAndStartWithDefaultParams("Electron");
  41. // Initialize gin::IsolateHolder.
  42. JavascriptEnvironment gin_env;
  43. // Explicitly register electron's builtin modules.
  44. NodeBindings::RegisterBuiltinModules();
  45. int exec_argc;
  46. const char** exec_argv;
  47. node::Init(&argc, const_cast<const char**>(argv), &exec_argc, &exec_argv);
  48. node::Environment* env = node::CreateEnvironment(
  49. node::CreateIsolateData(gin_env.isolate(), loop, gin_env.platform()),
  50. gin_env.context(), argc, argv, exec_argc, exec_argv);
  51. // Enable support for v8 inspector.
  52. NodeDebugger node_debugger(env);
  53. node_debugger.Start(gin_env.platform());
  54. mate::Dictionary process(gin_env.isolate(), env->process_object());
  55. #if defined(OS_WIN)
  56. process.SetMethod("log", &AtomBindings::Log);
  57. #endif
  58. process.SetMethod("crash", &AtomBindings::Crash);
  59. // Setup process.crashReporter.start in child node processes
  60. auto reporter = mate::Dictionary::CreateEmpty(gin_env.isolate());
  61. reporter.SetMethod("start", &crash_reporter::CrashReporter::StartInstance);
  62. process.Set("crashReporter", reporter);
  63. node::LoadEnvironment(env);
  64. bool more;
  65. do {
  66. more = uv_run(env->event_loop(), UV_RUN_ONCE);
  67. gin_env.platform()->DrainBackgroundTasks(env->isolate());
  68. if (more == false) {
  69. node::EmitBeforeExit(env);
  70. // Emit `beforeExit` if the loop became alive either after emitting
  71. // event, or after running some callbacks.
  72. more = uv_loop_alive(env->event_loop());
  73. if (uv_run(env->event_loop(), UV_RUN_NOWAIT) != 0)
  74. more = true;
  75. }
  76. } while (more == true);
  77. exit_code = node::EmitExit(env);
  78. node::RunAtExit(env);
  79. gin_env.platform()->DrainBackgroundTasks(env->isolate());
  80. gin_env.platform()->CancelPendingDelayedTasks(env->isolate());
  81. node::FreeEnvironment(env);
  82. }
  83. // According to "src/gin/shell/gin_main.cc":
  84. //
  85. // gin::IsolateHolder waits for tasks running in TaskScheduler in its
  86. // destructor and thus must be destroyed before TaskScheduler starts skipping
  87. // CONTINUE_ON_SHUTDOWN tasks.
  88. base::TaskScheduler::GetInstance()->Shutdown();
  89. v8::V8::Dispose();
  90. return exit_code;
  91. }
  92. } // namespace atom
  93. #endif // ENABLE_RUN_AS_NODE