~ubuntu-branches/ubuntu/saucy/nodejs/saucy-proposed

« back to all changes in this revision

Viewing changes to doc/api/repl.markdown

  • Committer: Package Import Robot
  • Author(s): Jérémy Lal
  • Date: 2013-08-14 00:16:46 UTC
  • mfrom: (7.1.40 sid)
  • Revision ID: package-import@ubuntu.com-20130814001646-bzlysfh8sd6mukbo
Tags: 0.10.15~dfsg1-4
* Update 2005 patch, adding a handful of tests that can fail on
  slow platforms.
* Add 1004 patch to fix test failures when writing NaN to buffer
  on mipsel.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
# REPL
2
2
 
3
 
A Read-Eval-Print-Loop (REPL) is available both as a standalone program and easily
4
 
includable in other programs.  REPL provides a way to interactively run
5
 
JavaScript and see the results.  It can be used for debugging, testing, or
 
3
A Read-Eval-Print-Loop (REPL) is available both as a standalone program and
 
4
easily includable in other programs. The REPL provides a way to interactively
 
5
run JavaScript and see the results.  It can be used for debugging, testing, or
6
6
just trying things out.
7
7
 
8
8
By executing `node` without any arguments from the command-line you will be
19
19
    2
20
20
    3
21
21
 
22
 
For advanced line-editors, start node with the environmental variable `NODE_NO_READLINE=1`.
23
 
This will start the REPL in canonical terminal settings which will allow you to use with `rlwrap`.
 
22
For advanced line-editors, start node with the environmental variable
 
23
`NODE_NO_READLINE=1`. This will start the main and debugger REPL in canonical
 
24
terminal settings which will allow you to use with `rlwrap`.
24
25
 
25
26
For example, you could add this to your bashrc file:
26
27
 
27
28
    alias node="env NODE_NO_READLINE=1 rlwrap node"
28
29
 
29
30
 
30
 
## repl.start([prompt], [stream], [eval], [useGlobal], [ignoreUndefined])
31
 
 
32
 
Starts a REPL with `prompt` as the prompt and `stream` for all I/O.  `prompt`
33
 
is optional and defaults to `> `.  `stream` is optional and defaults to
34
 
`process.stdin`. `eval` is optional too and defaults to async wrapper for
35
 
`eval()`.
36
 
 
37
 
If `useGlobal` is set to true, then the repl will use the global object,
38
 
instead of running scripts in a separate context. Defaults to `false`.
39
 
 
40
 
If `ignoreUndefined` is set to true, then the repl will not output return value
41
 
of command if it's `undefined`. Defaults to `false`.
 
31
## repl.start(options)
 
32
 
 
33
Returns and starts a `REPLServer` instance. Accepts an "options" Object that
 
34
takes the following values:
 
35
 
 
36
 - `prompt` - the prompt and `stream` for all I/O. Defaults to `> `.
 
37
 
 
38
 - `input` - the readable stream to listen to. Defaults to `process.stdin`.
 
39
 
 
40
 - `output` - the writable stream to write readline data to. Defaults to
 
41
   `process.stdout`.
 
42
 
 
43
 - `terminal` - pass `true` if the `stream` should be treated like a TTY, and
 
44
   have ANSI/VT100 escape codes written to it. Defaults to checking `isTTY`
 
45
   on the `output` stream upon instantiation.
 
46
 
 
47
 - `eval` - function that will be used to eval each given line. Defaults to
 
48
   an async wrapper for `eval()`. See below for an example of a custom `eval`.
 
49
 
 
50
 - `useColors` - a boolean which specifies whether or not the `writer` function
 
51
   should output colors. If a different `writer` function is set then this does
 
52
   nothing. Defaults to the repl's `terminal` value.
 
53
 
 
54
 - `useGlobal` - if set to `true`, then the repl will use the `global` object,
 
55
   instead of running scripts in a separate context. Defaults to `false`.
 
56
 
 
57
 - `ignoreUndefined` - if set to `true`, then the repl will not output the
 
58
   return value of command if it's `undefined`. Defaults to `false`.
 
59
 
 
60
 - `writer` - the function to invoke for each command that gets evaluated which
 
61
   returns the formatting (including coloring) to display. Defaults to
 
62
   `util.inspect`.
42
63
 
43
64
You can use your own `eval` function if it has following signature:
44
65
 
45
 
    function eval(cmd, callback) {
 
66
    function eval(cmd, context, filename, callback) {
46
67
      callback(null, result);
47
68
    }
48
69
 
56
77
 
57
78
    connections = 0;
58
79
 
59
 
    repl.start("node via stdin> ");
 
80
    repl.start({
 
81
      prompt: "node via stdin> ",
 
82
      input: process.stdin,
 
83
      output: process.stdout
 
84
    });
60
85
 
61
86
    net.createServer(function (socket) {
62
87
      connections += 1;
63
 
      repl.start("node via Unix socket> ", socket);
 
88
      repl.start({
 
89
        prompt: "node via Unix socket> ",
 
90
        input: socket,
 
91
        output: socket
 
92
      }).on('exit', function() {
 
93
        socket.end();
 
94
      })
64
95
    }).listen("/tmp/node-repl-sock");
65
96
 
66
97
    net.createServer(function (socket) {
67
98
      connections += 1;
68
 
      repl.start("node via TCP socket> ", socket);
 
99
      repl.start({
 
100
        prompt: "node via TCP socket> ",
 
101
        input: socket,
 
102
        output: socket
 
103
      }).on('exit', function() {
 
104
        socket.end();
 
105
      });
69
106
    }).listen(5001);
70
107
 
71
108
Running this program from the command line will start a REPL on stdin.  Other
76
113
By starting a REPL from a Unix socket-based server instead of stdin, you can
77
114
connect to a long-running node process without restarting it.
78
115
 
 
116
For an example of running a "full-featured" (`terminal`) REPL over
 
117
a `net.Server` and `net.Socket` instance, see: https://gist.github.com/2209310
 
118
 
 
119
For an example of running a REPL instance over `curl(1)`,
 
120
see: https://gist.github.com/2053342
 
121
 
 
122
### Event: 'exit'
 
123
 
 
124
`function () {}`
 
125
 
 
126
Emitted when the user exits the REPL in any of the defined ways. Namely, typing
 
127
`.exit` at the repl, pressing Ctrl+C twice to signal SIGINT, or pressing Ctrl+D
 
128
to signal "end" on the `input` stream.
 
129
 
 
130
Example of listening for `exit`:
 
131
 
 
132
    r.on('exit', function () {
 
133
      console.log('Got "exit" event from repl!');
 
134
      process.exit();
 
135
    });
 
136
 
79
137
 
80
138
## REPL Features
81
139
 
101
159
    var repl = require("repl"),
102
160
        msg = "message";
103
161
 
104
 
    repl.start().context.m = msg;
 
162
    repl.start("> ").context.m = msg;
105
163
 
106
164
Things in the `context` object appear as local within the REPL:
107
165