2015-03-13 00:03:16 +00:00
|
|
|
// Copyright (c) 2015 GitHub, Inc.
|
|
|
|
// Use of this source code is governed by the MIT license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
|
|
|
#include "atom/app/node_main.h"
|
|
|
|
|
2015-09-03 02:28:50 +00:00
|
|
|
#include "atom/app/uv_task_runner.h"
|
2015-03-13 00:03:16 +00:00
|
|
|
#include "atom/browser/javascript_environment.h"
|
2015-08-27 05:16:19 +00:00
|
|
|
#include "atom/browser/node_debugger.h"
|
2016-05-23 04:01:47 +00:00
|
|
|
#include "base/command_line.h"
|
|
|
|
#include "base/feature_list.h"
|
2016-07-04 06:08:55 +00:00
|
|
|
#include "base/threading/thread_task_runner_handle.h"
|
2015-03-13 00:03:16 +00:00
|
|
|
#include "gin/array_buffer.h"
|
|
|
|
#include "gin/public/isolate_holder.h"
|
2015-08-04 08:39:37 +00:00
|
|
|
#include "gin/v8_initializer.h"
|
2016-10-12 20:01:17 +00:00
|
|
|
|
|
|
|
#if defined(OS_WIN)
|
2016-10-12 20:02:36 +00:00
|
|
|
#include "atom/common/api/atom_bindings.h"
|
2016-10-12 20:01:17 +00:00
|
|
|
#include "atom/common/native_mate_converters/string16_converter.h"
|
2016-10-11 23:55:57 +00:00
|
|
|
#include "native_mate/dictionary.h"
|
2016-10-12 20:01:17 +00:00
|
|
|
#endif
|
2015-03-13 00:03:16 +00:00
|
|
|
|
2016-07-21 06:51:57 +00:00
|
|
|
#include "atom/common/node_includes.h"
|
|
|
|
|
2015-03-13 00:03:16 +00:00
|
|
|
namespace atom {
|
|
|
|
|
|
|
|
int NodeMain(int argc, char *argv[]) {
|
2015-08-27 05:16:19 +00:00
|
|
|
base::CommandLine::Init(argc, argv);
|
|
|
|
|
2015-03-13 00:03:16 +00:00
|
|
|
int exit_code = 1;
|
|
|
|
{
|
2015-09-03 02:28:50 +00:00
|
|
|
// Feed gin::PerIsolateData with a task runner.
|
2015-10-20 23:34:15 +00:00
|
|
|
argv = uv_setup_args(argc, argv);
|
2015-09-03 02:28:50 +00:00
|
|
|
uv_loop_t* loop = uv_default_loop();
|
|
|
|
scoped_refptr<UvTaskRunner> uv_task_runner(new UvTaskRunner(loop));
|
|
|
|
base::ThreadTaskRunnerHandle handle(uv_task_runner);
|
|
|
|
|
2016-05-23 04:01:47 +00:00
|
|
|
// Initialize feature list.
|
|
|
|
std::unique_ptr<base::FeatureList> feature_list(new base::FeatureList);
|
|
|
|
feature_list->InitializeFromCommandLine("", "");
|
|
|
|
base::FeatureList::SetInstance(std::move(feature_list));
|
|
|
|
|
2015-08-04 08:39:37 +00:00
|
|
|
gin::V8Initializer::LoadV8Snapshot();
|
2015-09-03 02:28:50 +00:00
|
|
|
gin::V8Initializer::LoadV8Natives();
|
2015-03-13 00:03:16 +00:00
|
|
|
JavascriptEnvironment gin_env;
|
2015-10-20 23:34:15 +00:00
|
|
|
|
|
|
|
int exec_argc;
|
|
|
|
const char** exec_argv;
|
|
|
|
node::Init(&argc, const_cast<const char**>(argv), &exec_argc, &exec_argv);
|
|
|
|
|
2015-03-13 00:03:16 +00:00
|
|
|
node::Environment* env = node::CreateEnvironment(
|
2015-09-03 02:28:50 +00:00
|
|
|
gin_env.isolate(), loop, gin_env.context(), argc, argv,
|
2015-04-20 06:10:15 +00:00
|
|
|
exec_argc, exec_argv);
|
2015-03-13 00:03:16 +00:00
|
|
|
|
2015-08-27 05:16:19 +00:00
|
|
|
// Start our custom debugger implementation.
|
|
|
|
NodeDebugger node_debugger(gin_env.isolate());
|
|
|
|
if (node_debugger.IsRunning())
|
|
|
|
env->AssignToContext(v8::Debug::GetDebugContext());
|
2015-04-14 02:15:34 +00:00
|
|
|
|
2016-10-12 20:01:17 +00:00
|
|
|
#if defined(OS_WIN)
|
2016-10-12 16:33:28 +00:00
|
|
|
mate::Dictionary process(gin_env.isolate(), env->process_object());
|
|
|
|
process.SetMethod("log", &AtomBindings::Log);
|
2016-10-12 20:01:17 +00:00
|
|
|
#endif
|
2015-04-20 06:10:15 +00:00
|
|
|
|
2016-10-12 16:33:28 +00:00
|
|
|
node::LoadEnvironment(env);
|
2016-10-11 23:55:57 +00:00
|
|
|
|
2015-03-13 00:03:16 +00:00
|
|
|
bool more;
|
|
|
|
do {
|
|
|
|
more = uv_run(env->event_loop(), UV_RUN_ONCE);
|
|
|
|
if (more == false) {
|
|
|
|
node::EmitBeforeExit(env);
|
|
|
|
|
|
|
|
// Emit `beforeExit` if the loop became alive either after emitting
|
|
|
|
// event, or after running some callbacks.
|
|
|
|
more = uv_loop_alive(env->event_loop());
|
|
|
|
if (uv_run(env->event_loop(), UV_RUN_NOWAIT) != 0)
|
|
|
|
more = true;
|
|
|
|
}
|
|
|
|
} while (more == true);
|
|
|
|
|
|
|
|
exit_code = node::EmitExit(env);
|
|
|
|
node::RunAtExit(env);
|
|
|
|
|
2016-07-21 07:43:21 +00:00
|
|
|
node::FreeEnvironment(env);
|
2015-03-13 00:03:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
v8::V8::Dispose();
|
|
|
|
|
|
|
|
return exit_code;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace atom
|