node_main.cc 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 "base/command_line.h"
  13. #include "base/feature_list.h"
  14. #include "base/task_scheduler/task_scheduler.h"
  15. #include "base/threading/thread_task_runner_handle.h"
  16. #include "gin/array_buffer.h"
  17. #include "gin/public/isolate_holder.h"
  18. #include "gin/v8_initializer.h"
  19. #include "native_mate/dictionary.h"
  20. #include "atom/common/node_includes.h"
  21. namespace atom {
  22. int NodeMain(int argc, char *argv[]) {
  23. base::CommandLine::Init(argc, argv);
  24. int exit_code = 1;
  25. {
  26. // Feed gin::PerIsolateData with a task runner.
  27. argv = uv_setup_args(argc, argv);
  28. uv_loop_t* loop = uv_default_loop();
  29. scoped_refptr<UvTaskRunner> uv_task_runner(new UvTaskRunner(loop));
  30. base::ThreadTaskRunnerHandle handle(uv_task_runner);
  31. // Initialize feature list.
  32. std::unique_ptr<base::FeatureList> feature_list(new base::FeatureList);
  33. feature_list->InitializeFromCommandLine("", "");
  34. base::FeatureList::SetInstance(std::move(feature_list));
  35. gin::V8Initializer::LoadV8Snapshot();
  36. gin::V8Initializer::LoadV8Natives();
  37. // V8 requires a task scheduler apparently
  38. base::TaskScheduler::CreateAndStartWithDefaultParams("Electron");
  39. // Initialize gin::IsolateHolder.
  40. JavascriptEnvironment gin_env;
  41. int exec_argc;
  42. const char** exec_argv;
  43. node::Init(&argc, const_cast<const char**>(argv), &exec_argc, &exec_argv);
  44. node::IsolateData isolate_data(gin_env.isolate(), loop);
  45. node::Environment* env = node::CreateEnvironment(
  46. &isolate_data, gin_env.context(), argc, argv,
  47. exec_argc, exec_argv);
  48. // Enable support for v8 inspector.
  49. NodeDebugger node_debugger(env);
  50. node_debugger.Start(gin_env.platform());
  51. mate::Dictionary process(gin_env.isolate(), env->process_object());
  52. #if defined(OS_WIN)
  53. process.SetMethod("log", &AtomBindings::Log);
  54. #endif
  55. process.SetMethod("crash", &AtomBindings::Crash);
  56. // Setup process.crashReporter.start in child node processes
  57. auto reporter = mate::Dictionary::CreateEmpty(gin_env.isolate());
  58. reporter.SetMethod("start", &crash_reporter::CrashReporter::StartInstance);
  59. process.Set("crashReporter", reporter);
  60. node::LoadEnvironment(env);
  61. bool more;
  62. do {
  63. more = uv_run(env->event_loop(), UV_RUN_ONCE);
  64. if (more == false) {
  65. node::EmitBeforeExit(env);
  66. // Emit `beforeExit` if the loop became alive either after emitting
  67. // event, or after running some callbacks.
  68. more = uv_loop_alive(env->event_loop());
  69. if (uv_run(env->event_loop(), UV_RUN_NOWAIT) != 0)
  70. more = true;
  71. }
  72. } while (more == true);
  73. exit_code = node::EmitExit(env);
  74. node::RunAtExit(env);
  75. node::FreeEnvironment(env);
  76. }
  77. // According to "src/gin/shell/gin_main.cc":
  78. //
  79. // gin::IsolateHolder waits for tasks running in TaskScheduler in its
  80. // destructor and thus must be destroyed before TaskScheduler starts skipping
  81. // CONTINUE_ON_SHUTDOWN tasks.
  82. base::TaskScheduler::GetInstance()->Shutdown();
  83. v8::V8::Dispose();
  84. return exit_code;
  85. }
  86. } // namespace atom
  87. #endif // ENABLE_RUN_AS_NODE