Skip to content

Commit 9481bc1

Browse files
tjholowaychukry
authored andcommitted
Added -e, --eval
1 parent 5986a58 commit 9481bc1

2 files changed

Lines changed: 20 additions & 2 deletions

File tree

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;
@@ -1608,6 +1610,11 @@ static void Load(int argc, char *argv[]) {
16081610

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

1613+
// -e, --eval
1614+
if (eval_string) {
1615+
process->Set(String::NewSymbol("_eval"), String::New(eval_string));
1616+
}
1617+
16111618
size_t size = 2*PATH_MAX;
16121619
char execPath[size];
16131620
if (OS::GetExecutablePath(execPath, &size) != 0) {
@@ -1751,6 +1758,13 @@ static void ParseArgs(int *argc, char **argv) {
17511758
} else if (strcmp(arg, "--help") == 0 || strcmp(arg, "-h") == 0) {
17521759
PrintHelp();
17531760
exit(0);
1761+
} else if (strcmp(arg, "--eval") == 0 || strcmp(arg, "-e") == 0) {
1762+
if (*argc <= i + 1) {
1763+
fprintf(stderr, "Error: --eval requires an argument\n");
1764+
exit(1);
1765+
}
1766+
argv[i] = const_cast<char*>("");
1767+
eval_string = argv[++i];
17541768
} else if (strcmp(arg, "--v8-options") == 0) {
17551769
argv[i] = const_cast<char*>("--help");
17561770
} else if (argv[i][0] != '-') {

src/node.js

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

579579
if (process.argv[1]) {
580+
// Load module
580581
if (process.argv[1].charAt(0) != "/" && !(/^http:\/\//).exec(process.argv[1])) {
581582
process.argv[1] = path.join(cwd, process.argv[1]);
582583
}
583-
584584
// REMOVEME: nextTick should not be necessary. This hack to get
585585
// test/simple/test-exception-handler2.js working.
586586
process.nextTick(function() {
587587
module.runMain();
588588
});
589+
590+
} else if (process._eval) {
591+
// -e, --eval
592+
if (process._eval) console.log(eval(process._eval));
589593
} else {
590-
// No arguments, run the repl
594+
// REPL
591595
module.requireNative('repl').start();
592596
}
593597

0 commit comments

Comments
 (0)