Skip to content

Commit 08f1bf4

Browse files
tjholowaychukry
authored andcommitted
Added -e, --eval
1 parent 18ff6db commit 08f1bf4

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

src/node.cc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ static Persistent<String> listeners_symbol;
6868
static Persistent<String> uncaught_exception_symbol;
6969
static Persistent<String> emit_symbol;
7070

71+
72+
static char *eval_string = NULL;
7173
static int option_end_index = 0;
7274
static bool use_debug_agent = false;
7375
static bool debug_wait_connect = false;
@@ -1612,6 +1614,11 @@ static void Load(int argc, char *argv[]) {
16121614

16131615
process->Set(String::NewSymbol("pid"), Integer::New(getpid()));
16141616

1617+
// -e, --eval
1618+
if (eval_string) {
1619+
process->Set(String::NewSymbol("_eval"), String::New(eval_string));
1620+
}
1621+
16151622
size_t size = 2*PATH_MAX;
16161623
char execPath[size];
16171624
if (OS::GetExecutablePath(execPath, &size) != 0) {
@@ -1764,6 +1771,13 @@ static void ParseArgs(int *argc, char **argv) {
17641771
} else if (strcmp(arg, "--help") == 0 || strcmp(arg, "-h") == 0) {
17651772
PrintHelp();
17661773
exit(0);
1774+
} else if (strcmp(arg, "--eval") == 0 || strcmp(arg, "-e") == 0) {
1775+
if (*argc <= i + 1) {
1776+
fprintf(stderr, "Error: --eval requires an argument\n");
1777+
exit(1);
1778+
}
1779+
argv[i] = const_cast<char*>("");
1780+
eval_string = argv[++i];
17671781
} else if (strcmp(arg, "--v8-options") == 0) {
17681782
argv[i] = const_cast<char*>("--help");
17691783
} else if (argv[i][0] != '-') {

src/node.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -746,17 +746,21 @@ if (process.argv[0].indexOf('/') > 0) {
746746
}
747747

748748
if (process.argv[1]) {
749+
// Load module
749750
if (process.argv[1].charAt(0) != "/" && !(/^http:\/\//).exec(process.argv[1])) {
750751
process.argv[1] = path.join(cwd, process.argv[1]);
751752
}
752-
753753
// REMOVEME: nextTick should not be necessary. This hack to get
754754
// test/simple/test-exception-handler2.js working.
755755
process.nextTick(function() {
756756
module.runMain();
757757
});
758+
759+
} else if (process._eval) {
760+
// -e, --eval
761+
if (process._eval) console.log(eval(process._eval));
758762
} else {
759-
// No arguments, run the repl
763+
// REPL
760764
module.requireNative('repl').start();
761765
}
762766

0 commit comments

Comments
 (0)