Skip to content

Commit

Permalink
make nw run as node
Browse files Browse the repository at this point in the history
  • Loading branch information
rogerwang committed Aug 25, 2014
1 parent d54520d commit 3d94950
Show file tree
Hide file tree
Showing 3 changed files with 156 additions and 44 deletions.
8 changes: 5 additions & 3 deletions lib/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -295,9 +295,11 @@ Module._load = function(request, parent, isMain) {
if (isMain) {
process.mainModule = module;
module.id = '.';
// require() in DOM needs this module as parent
module._compile('global.module = module;\n' +
'global.require = require;\n', 'nw-emulate-node');
if (process.__node_webkit) {
// require() in DOM needs this module as parent
module._compile('global.module = module;\n' +
'global.require = require;\n', 'nw-emulate-node');
}
}

Module._cache[filename] = module;
Expand Down
31 changes: 19 additions & 12 deletions src/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2605,7 +2605,8 @@ void SetupProcessObject(Environment* env,
int argc,
const char* const* argv,
int exec_argc,
const char* const* exec_argv) {
const char* const* exec_argv,
bool node_webkit) {
HandleScope scope(env->isolate());

Local<Object> process = env->process_object();
Expand All @@ -2614,6 +2615,9 @@ void SetupProcessObject(Environment* env,
ProcessTitleGetter,
ProcessTitleSetter);

if (node_webkit) {
READONLY_PROPERTY(process, "__node_webkit", Integer::New(env->isolate(), 1));
}
// process.version
READONLY_PROPERTY(process,
"version",
Expand Down Expand Up @@ -3378,11 +3382,12 @@ static void DebugEnd(const FunctionCallbackInfo<Value>& args) {
void Init(int* argc,
const char** argv,
int* exec_argc,
const char*** exec_argv) {
const char*** exec_argv,
bool node_webkit) {
// Initialize prog_start_time to get relative uptime.
uv_uptime(&prog_start_time);

#if 0
if (!node_webkit) {
// Make inherited handles noninheritable.
uv_disable_stdio_inheritance();

Expand Down Expand Up @@ -3435,7 +3440,7 @@ void Init(int* argc,
const char expose_debug_as[] = "--expose_debug_as=v8debug";
V8::SetFlagsFromString(expose_debug_as, sizeof(expose_debug_as) - 1);
}
#endif
} //node-webkit

// V8::SetArrayBufferAllocator(&ArrayBufferAllocator::the_singleton);

Expand Down Expand Up @@ -3473,7 +3478,8 @@ void Init(int* argc,
#endif // __POSIX__

V8::SetFatalErrorHandler(node::OnFatalError);
#if 0

if (!node_webkit) {
V8::AddMessageListener(OnMessage);

// If the --debug flag was specified then initialize the debug thread.
Expand All @@ -3482,7 +3488,7 @@ void Init(int* argc,
} else {
RegisterDebugSignalHandler();
}
#endif
}
}


Expand Down Expand Up @@ -3557,7 +3563,8 @@ Environment* CreateEnvironment(Isolate* isolate,
int argc,
const char* const* argv,
int exec_argc,
const char* const* exec_argv) {
const char* const* exec_argv,
bool node_webkit) {
HandleScope handle_scope(isolate);

Local<Context> context = Context::New(isolate);
Expand Down Expand Up @@ -3593,7 +3600,7 @@ Environment* CreateEnvironment(Isolate* isolate,
Local<Object> process_object = process_template->GetFunction()->NewInstance();
env->set_process_object(process_object);

SetupProcessObject(env, argc, argv, exec_argc, exec_argv);
SetupProcessObject(env, argc, argv, exec_argc, exec_argv, node_webkit);
Load(env);

return env;
Expand All @@ -3615,7 +3622,7 @@ int Start(int argc, char** argv) {
// optional, in case you're wondering.
int exec_argc;
const char** exec_argv;
Init(&argc, const_cast<const char**>(argv), &exec_argc, &exec_argv);
Init(&argc, const_cast<const char**>(argv), &exec_argc, &exec_argv, false);

#if HAVE_OPENSSL
// V8 on Windows doesn't have a good source of entropy. Seed it from
Expand All @@ -3628,7 +3635,7 @@ int Start(int argc, char** argv) {
{
Locker locker(node_isolate);
Environment* env =
CreateEnvironment(node_isolate, argc, argv, exec_argc, exec_argv);
CreateEnvironment(node_isolate, argc, argv, exec_argc, exec_argv, false);
// This Context::Scope is here so EnableDebug() can look up the current
// environment with Environment::GetCurrentChecked().
// TODO(bnoordhuis) Reorder the debugger initialization logic so it can
Expand Down Expand Up @@ -3682,7 +3689,7 @@ void SetupUv(int argc, char** argv) {
// optional, in case you're wondering.
int exec_argc;
const char** exec_argv;
Init(&argc, const_cast<const char**>(argv), &exec_argc, &exec_argv);
Init(&argc, const_cast<const char**>(argv), &exec_argc, &exec_argv, true);

#if HAVE_OPENSSL
// V8 on Windows doesn't have a good source of entropy. Seed it from
Expand Down Expand Up @@ -3728,7 +3735,7 @@ void SetupContext(int argc, char *argv[], v8::Handle<v8::Context> context) {
Local<Object> process_object = process_template->GetFunction()->NewInstance();
env->set_process_object(process_object);

SetupProcessObject(env, argc, argv, argc, argv);
SetupProcessObject(env, argc, argv, 0, NULL, true);
Load(env);

g_env = env;
Expand Down
161 changes: 132 additions & 29 deletions src/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,17 @@

startup.globalVariables();
startup.globalTimeouts();
//startup.globalConsole();
// nw: we try to keep the uniform behaviour (logging to devtools) here.
// FIXME: should have a better solution for full console support
global.console = {
log: function() {},
error: function(){},
warn: function() {}
}; // Will be override when WebKit is started.
if (process.__node_webkit) {
// nw: we try to keep the uniform behaviour (logging to devtools) here.
// FIXME: should have a better solution for full console support
global.console = {
log: function() {},
error: function(){},
warn: function() {}
}; // Will be override when WebKit is started.
} else {
startup.globalConsole();
}

startup.processAssert();
startup.processConfig();
Expand All @@ -69,33 +72,133 @@

startup.resolveArgv0();

startup.initNw();
if (process.__node_webkit) {
startup.initNw();
var Module = NativeModule.require('module');

var Module = NativeModule.require('module');
// Execute the main script
if (process.argv[1]) {
// make process.argv[1] into a full path
var path = NativeModule.require('path');
process.argv[1] = path.resolve(process.argv[1]);

// Execute the main script
if (process.argv[1]) {
// make process.argv[1] into a full path
var path = NativeModule.require('path');
process.argv[1] = path.resolve(process.argv[1]);
// If this is a worker in cluster mode, start up the communication
// channel.
if (process.env.NODE_UNIQUE_ID) {
var cluster = NativeModule.require('cluster');
cluster._setupWorker();

// Make sure it's not accidentally inherited by child processes.
delete process.env.NODE_UNIQUE_ID;
}
// Main entry point into most programs:
process.nextTick(Module.runMain);
}
// Emulate node.js script's execution everionment
var module = new Module('.', null);
global.process.mainModule = module;
module._compile('global.module = module;\n' +
'global.require = global.__nw_require = require;\n', 'nw-emulate-node');
} else { // upstream node startup
// There are various modes that Node can run in. The most common two
// are running from a script and running the REPL - but there are a few
// others like the debugger or running --eval arguments. Here we decide
// which mode we run in.

if (NativeModule.exists('_third_party_main')) {
// To allow people to extend Node in different ways, this hook allows
// one to drop a file lib/_third_party_main.js into the build
// directory which will be executed instead of Node's normal loading.
process.nextTick(function() {
NativeModule.require('_third_party_main');
});

} else if (process.argv[1] == 'debug') {
// Start the debugger agent
var d = NativeModule.require('_debugger');
d.start();

} else if (process._eval != null) {
// User passed '-e' or '--eval' arguments to Node.
evalScript('[eval]');
} else if (process.argv[1]) {
// make process.argv[1] into a full path
var path = NativeModule.require('path');
process.argv[1] = path.resolve(process.argv[1]);

// If this is a worker in cluster mode, start up the communication
// channel.
if (process.env.NODE_UNIQUE_ID) {
var cluster = NativeModule.require('cluster');
cluster._setupWorker();

// Make sure it's not accidentally inherited by child processes.
delete process.env.NODE_UNIQUE_ID;
}

var Module = NativeModule.require('module');

if (global.v8debug &&
process.execArgv.some(function(arg) {
return arg.match(/^--debug-brk(=[0-9]*)?$/);
})) {

// XXX Fix this terrible hack!
//
// Give the client program a few ticks to connect.
// Otherwise, there's a race condition where `node debug foo.js`
// will not be able to connect in time to catch the first
// breakpoint message on line 1.
//
// A better fix would be to somehow get a message from the
// global.v8debug object about a connection, and runMain when
// that occurs. --isaacs

var debugTimeout = +process.env.NODE_DEBUG_TIMEOUT || 50;
setTimeout(Module.runMain, debugTimeout);

// If this is a worker in cluster mode, start up the communication
// channel.
if (process.env.NODE_UNIQUE_ID) {
var cluster = NativeModule.require('cluster');
cluster._setupWorker();
} else {
// Main entry point into most programs:
Module.runMain();
}

} else {
var Module = NativeModule.require('module');

// If -i or --interactive were passed, or stdin is a TTY.
if (process._forceRepl || NativeModule.require('tty').isatty(0)) {
// REPL
var opts = {
useGlobal: true,
ignoreUndefined: false
};
if (parseInt(process.env['NODE_NO_READLINE'], 10)) {
opts.terminal = false;
}
if (parseInt(process.env['NODE_DISABLE_COLORS'], 10)) {
opts.useColors = false;
}
var repl = Module.requireRepl().start(opts);
repl.on('exit', function() {
process.exit();
});

// Make sure it's not accidentally inherited by child processes.
delete process.env.NODE_UNIQUE_ID;
} else {
// Read all of stdin - execute it.
process.stdin.setEncoding('utf8');

var code = '';
process.stdin.on('data', function(d) {
code += d;
});

process.stdin.on('end', function() {
process._eval = code;
evalScript('[stdin]');
});
}
}
// Main entry point into most programs:
process.nextTick(Module.runMain);
}
// Emulate node.js script's execution everionment
var module = new Module('.', null);
global.process.mainModule = module;
module._compile('global.module = module;\n' +
'global.require = global.__nw_require = require;\n', 'nw-emulate-node');
}

startup.globalVariables = function() {
Expand Down

0 comments on commit 3d94950

Please sign in to comment.