Skip to content

Commit

Permalink
Merge pull request #68 from hpicgs/new-refactor-start
Browse files Browse the repository at this point in the history
Replay "refactor Start() methods to use lib functions" onto new dev
  • Loading branch information
luminosuslight authored and msoechting committed Feb 21, 2018
2 parents 8c5e8e1 + 11457b0 commit 9db182d
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 21 deletions.
20 changes: 9 additions & 11 deletions lib/internal/bootstrap_node.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,17 +202,8 @@
preloadModules();
perf.markMilestone(
NODE_PERFORMANCE_MILESTONE_PRELOAD_MODULE_LOAD_END);
if (true) {
// TODO (cf): This should only happen if we run node via node::lib::Initialize().
// It should not run if we call node::lib::Start().

// Do nothing. Calling evalScript is necessary however, because it sets
// globals.require to the require function.
// Otherwise, require doesn't work.
console.log('Embedded mode');
evalScript('[eval]');
// If -i or --interactive were passed, or stdin is a TTY.
} else if (process._forceRepl || NativeModule.require('tty').isatty(0)) {
if (process._forceRepl ||
(process._allowRepl && NativeModule.require('tty').isatty(0))) {
// REPL
const cliRepl = NativeModule.require('internal/repl');
cliRepl.createInternalRepl(process.env, function(err, repl) {
Expand All @@ -234,6 +225,13 @@
// User passed '-e' or '--eval'
evalScript('[eval]');
}

} else if (!process._allowRepl &&
NativeModule.require('tty').isatty(0)) {
if (process._eval != null) {
// User passed '-e' or '--eval'
evalScript('[eval]');
}
} else {
// Read all of stdin - execute it.
process.stdin.setEncoding('utf8');
Expand Down
39 changes: 32 additions & 7 deletions src/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3279,7 +3279,7 @@ static void RawDebug(const FunctionCallbackInfo<Value>& args) {
fflush(stderr);
}

void LoadEnvironment(Environment* env) {
void LoadEnvironment(Environment* env, const bool allow_repl) {
HandleScope handle_scope(env->isolate());

TryCatch try_catch(env->isolate());
Expand Down Expand Up @@ -3342,6 +3342,20 @@ void LoadEnvironment(Environment* env) {
// like Node's I/O bindings may want to replace 'f' with their own function.
Local<Value> arg = env->process_object();

// add allow_repl parameter to JS process so we can use it in
// bootstrap_node.js.
// If adding the parameter fails, bootstrapping will fail too,
// so we can cleanup and return before we even bootstrap.
bool successfully_set_allow_repl = Object::Cast(*arg)->Set(
env->context(),
String::NewFromUtf8(env->isolate(), "_allowRepl"),
Boolean::New(env->isolate(), allow_repl)).FromJust();

if (!successfully_set_allow_repl) {
env->async_hooks()->clear_async_id_stack();
return;
}

auto ret = f->Call(env->context(), Null(env->isolate()), 1, &arg);
// If there was an error during bootstrap then it was either handled by the
// FatalException handler or it's unrecoverable (e.g. max call stack
Expand Down Expand Up @@ -4845,7 +4859,8 @@ void _ConfigureOpenSsl() {
void _StartEnv(int argc,
const char* const* argv,
int v8_argc,
const char* const* v8_argv) {
const char* const* v8_argv,
const bool allow_repl) {
std::cout << "Starting environment" << std::endl;

_environment->Start(argc, argv, v8_argc, v8_argv, v8_is_profiling);
Expand All @@ -4868,7 +4883,7 @@ void _StartEnv(int argc,
{
Environment::AsyncCallbackScope callback_scope(_environment);
_environment->async_hooks()->push_async_ids(1, 0);
LoadEnvironment(_environment);
LoadEnvironment(_environment, allow_repl);
_environment->async_hooks()->pop_async_id(1);
}

Expand All @@ -4878,12 +4893,13 @@ void _StartEnv(int argc,
} // namespace initialize

void Initialize(const std::string& program_name,
const std::vector<std::string>& node_args) {
const std::vector<std::string>& node_args,
const bool allow_repl) {
cmd_args = new CmdArgs(program_name, node_args);
Initialize(cmd_args->argc, cmd_args->argv);
Initialize(cmd_args->argc, cmd_args->argv, allow_repl);
}

void Initialize(int argc, const char** argv) {
void Initialize(int argc, const char** argv, const bool allow_repl) {
//////////
// Start 1
//////////
Expand Down Expand Up @@ -4919,7 +4935,7 @@ void Initialize(int argc, const char** argv) {
// Start environment
//////////

initialize::_StartEnv(argc, argv, exec_argc, exec_argv);
initialize::_StartEnv(argc, argv, exec_argc, exec_argv, allow_repl);
}

int Deinitialize() {
Expand Down Expand Up @@ -5143,6 +5159,15 @@ bool ProcessEvents(UvLoopBehavior behavior) {
return TickEventLoop(_environment, behavior);
}

int Start(int argc, char** argv) {
Initialize(argc, const_cast<const char**>(argv), true);
SealHandleScope seal(_environment->isolate());
PERFORMANCE_MARK(_environment, LOOP_START);
RunEventLoop([] () {}, UvLoopBehavior::RUN_DEFAULT);
PERFORMANCE_MARK(_environment, LOOP_EXIT);
return Deinitialize();
}

} // namespace node

#if !HAVE_INSPECTOR
Expand Down
13 changes: 10 additions & 3 deletions src/node_lib.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ enum class UvLoopBehavior : int {
* @return True, if the Node.js event loop is executed by `RunEventLoop`.
* False otherwise.
*/
bool EventLoopIsRunning();
bool eventLoopIsRunning();

/*********************************************************
* Start Node.js engine
Expand All @@ -80,9 +80,12 @@ bool EventLoopIsRunning();
* not called.
* @param program_name The name for the Node.js application.
* @param node_args List of arguments for the Node.js engine.
* @param allow_repl Controls whether the node.js REPL gets spawned when stdin
* is an interactive terminal.
*/
NODE_EXTERN void Initialize(const std::string& program_name = "node_lib",
const std::vector<std::string>& node_args = {});
const std::vector<std::string>& node_args = {},
const bool allow_repl = false);

/**
* @brief Starts the Node.js engine.
Expand All @@ -97,8 +100,12 @@ NODE_EXTERN void Initialize(const std::string& program_name = "node_lib",
* @param argv List of arguments for the Node.js engine,
* where the first argument needs to be the program name.
* The number of arguments must correspond to argc.
* @param allow_repl Controls whether the node.js REPL gets spawned when stdin
* is an interactive terminal.
*/
NODE_EXTERN void Initialize(int argc, const char** argv);
NODE_EXTERN void Initialize(int argc,
const char** argv,
const bool allow_repl = false);

/**
* @brief Stops the Node.js engine and destroys all current state.
Expand Down

0 comments on commit 9db182d

Please sign in to comment.