Clean up some things in the repl, add docs.
This commit is contained in:
parent
835554c92b
commit
27738d8e60
@ -6,6 +6,6 @@ puts("Enter ECMAScript at the prompt.");
|
||||
puts("Tip 1: Use 'rlwrap node-repl' for a better interface");
|
||||
puts("Tip 2: Type Control-D to exit.");
|
||||
|
||||
include("/repl.js");
|
||||
require("/repl.js").start();
|
||||
|
||||
// vim:ft=javascript
|
||||
|
30
doc/api.html
30
doc/api.html
@ -1866,6 +1866,34 @@ on error: returns <tt>code, msg</tt>. <tt>code</tt> is one of the error codes li
|
||||
</li>
|
||||
</ul></div>
|
||||
</div>
|
||||
<h2 id="_repl">REPL</h2>
|
||||
<div class="sectionbody">
|
||||
<div class="paragraph"><p>A Read-Eval-Print-Loop is available both as a standalone program and easily
|
||||
includable in other programs.</p></div>
|
||||
<div class="paragraph"><p>The standalone REPL is called <tt>node-repl</tt> and is installed at
|
||||
<tt>$PREFIX/bin/node-repl</tt>. It’s recommended to use it with the program
|
||||
<tt>rlwrap</tt> for a better user interface. I set</p></div>
|
||||
<div class="listingblock">
|
||||
<div class="content">
|
||||
<pre><tt>alias node-repl="rlwrap node-repl"</tt></pre>
|
||||
</div></div>
|
||||
<div class="paragraph"><p>in my zsh configuration.</p></div>
|
||||
<div class="paragraph"><p>Inside the REPL, Control+D will exit. The special variable <tt>_</tt> (underscore) contains the
|
||||
result of the last expression.</p></div>
|
||||
<div class="paragraph"><p>The library is called <tt>/repl.js</tt> and it can be used like this:</p></div>
|
||||
<div class="listingblock">
|
||||
<div class="content">
|
||||
<pre><tt>include("/utils.js");
|
||||
include("/tcp.js");
|
||||
nconnections = 0;
|
||||
createServer(function (c) {
|
||||
error("Connection!");
|
||||
nconnections += 1;
|
||||
c.close();
|
||||
}).listen(5000);
|
||||
require("/repl.js").start("simple tcp server> ");</tt></pre>
|
||||
</div></div>
|
||||
</div>
|
||||
<h2 id="_extension_api">Extension API</h2>
|
||||
<div class="sectionbody">
|
||||
<div class="paragraph"><p>External modules can be compiled and dynamically linked into Node.
|
||||
@ -1955,7 +1983,7 @@ init (Handle<Object> target)
|
||||
<div id="footer">
|
||||
<div id="footer-text">
|
||||
Version 0.1.12<br />
|
||||
Last updated 2009-09-28 16:33:21 CEST
|
||||
Last updated 2009-09-28 17:35:44 CEST
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
|
32
doc/api.txt
32
doc/api.txt
@ -35,7 +35,6 @@ Server running at http://127.0.0.1:8000/
|
||||
----------------------------------------
|
||||
|
||||
|
||||
|
||||
== API
|
||||
|
||||
Node supports 3 string encodings. UTF-8 (+"utf8"+), ASCII (+"ascii"+), and
|
||||
@ -1160,6 +1159,37 @@ Each DNS query can return an error code.
|
||||
|
||||
|
||||
|
||||
== REPL
|
||||
|
||||
A Read-Eval-Print-Loop is available both as a standalone program and easily
|
||||
includable in other programs.
|
||||
|
||||
The standalone REPL is called +node-repl+ and is installed at
|
||||
+$PREFIX/bin/node-repl+. It's recommended to use it with the program
|
||||
+rlwrap+ for a better user interface. I set
|
||||
------------------------------------
|
||||
alias node-repl="rlwrap node-repl"
|
||||
------------------------------------
|
||||
in my zsh configuration.
|
||||
|
||||
Inside the REPL, Control+D will exit. The special variable +_+ (underscore) contains the
|
||||
result of the last expression.
|
||||
|
||||
The library is called +/repl.js+ and it can be used like this:
|
||||
------------------------------------
|
||||
include("/utils.js");
|
||||
include("/tcp.js");
|
||||
nconnections = 0;
|
||||
createServer(function (c) {
|
||||
error("Connection!");
|
||||
nconnections += 1;
|
||||
c.close();
|
||||
}).listen(5000);
|
||||
require("/repl.js").start("simple tcp server> ");
|
||||
------------------------------------
|
||||
|
||||
|
||||
|
||||
== Extension API
|
||||
|
||||
External modules can be compiled and dynamically linked into Node.
|
||||
|
22
doc/api.xml
22
doc/api.xml
@ -1991,6 +1991,28 @@ on error: returns <literal>code, msg</literal>. <literal>code</literal> is one o
|
||||
</itemizedlist>
|
||||
</refsect2>
|
||||
</refsect1>
|
||||
<refsect1 id="_repl">
|
||||
<title>REPL</title>
|
||||
<simpara>A Read-Eval-Print-Loop is available both as a standalone program and easily
|
||||
includable in other programs.</simpara>
|
||||
<simpara>The standalone REPL is called <literal>node-repl</literal> and is installed at
|
||||
<literal>$PREFIX/bin/node-repl</literal>. It’s recommended to use it with the program
|
||||
<literal>rlwrap</literal> for a better user interface. I set</simpara>
|
||||
<screen>alias node-repl="rlwrap node-repl"</screen>
|
||||
<simpara>in my zsh configuration.</simpara>
|
||||
<simpara>Inside the REPL, Control+D will exit. The special variable <literal>_</literal> (underscore) contains the
|
||||
result of the last expression.</simpara>
|
||||
<simpara>The library is called <literal>/repl.js</literal> and it can be used like this:</simpara>
|
||||
<screen>include("/utils.js");
|
||||
include("/tcp.js");
|
||||
nconnections = 0;
|
||||
createServer(function (c) {
|
||||
error("Connection!");
|
||||
nconnections += 1;
|
||||
c.close();
|
||||
}).listen(5000);
|
||||
require("/repl.js").start("simple tcp server> ");</screen>
|
||||
</refsect1>
|
||||
<refsect1 id="_extension_api">
|
||||
<title>Extension API</title>
|
||||
<simpara>External modules can be compiled and dynamically linked into Node.
|
||||
|
53
doc/node.1
53
doc/node.1
@ -59,7 +59,7 @@ process is the equivalent of window in browser\-side javascript\. It is the glob
|
||||
.sp
|
||||
.TS
|
||||
allbox tab(:);
|
||||
ltB ltB ltBx.
|
||||
ltB ltB ltB.
|
||||
T{
|
||||
Event
|
||||
T}:T{
|
||||
@ -165,7 +165,7 @@ All EventEmitters emit the event "newListener" when new listeners are added\.
|
||||
.sp
|
||||
.TS
|
||||
allbox tab(:);
|
||||
ltB ltB ltBx.
|
||||
ltB ltB ltB.
|
||||
T{
|
||||
Event
|
||||
T}:T{
|
||||
@ -221,7 +221,7 @@ node\.Promise inherits from node\.eventEmitter\. A promise emits one of two even
|
||||
.sp
|
||||
.TS
|
||||
allbox tab(:);
|
||||
ltB ltB ltBx.
|
||||
ltB ltB ltB.
|
||||
T{
|
||||
Event
|
||||
T}:T{
|
||||
@ -320,7 +320,7 @@ Standard I/O is handled through a special object node\.stdio\. stdout and stdin
|
||||
.sp
|
||||
.TS
|
||||
allbox tab(:);
|
||||
ltB ltB ltBx.
|
||||
ltB ltB ltB.
|
||||
T{
|
||||
Event
|
||||
T}:T{
|
||||
@ -476,7 +476,7 @@ node.ChildProcess
|
||||
.RS
|
||||
.TS
|
||||
allbox tab(:);
|
||||
ltB ltB ltBx.
|
||||
ltB ltB ltB.
|
||||
T{
|
||||
Event
|
||||
T}:T{
|
||||
@ -834,7 +834,7 @@ http.Server
|
||||
.RS
|
||||
.TS
|
||||
allbox tab(:);
|
||||
ltB ltB ltBx.
|
||||
ltB ltB ltB.
|
||||
T{
|
||||
Event
|
||||
T}:T{
|
||||
@ -918,7 +918,7 @@ This object is created internally by a HTTP server\(emnot by the user\(emand pas
|
||||
.sp
|
||||
.TS
|
||||
allbox tab(:);
|
||||
ltB ltB ltBx.
|
||||
ltB ltB ltB.
|
||||
T{
|
||||
Event
|
||||
T}:T{
|
||||
@ -1175,7 +1175,7 @@ This object is created internally and returned from the request methods of a htt
|
||||
.sp
|
||||
.TS
|
||||
allbox tab(:);
|
||||
ltB ltB ltBx.
|
||||
ltB ltB ltB.
|
||||
T{
|
||||
Event
|
||||
T}:T{
|
||||
@ -1273,7 +1273,7 @@ This object is created internally and passed to the "response" event\.
|
||||
.sp
|
||||
.TS
|
||||
allbox tab(:);
|
||||
ltB ltB ltBx.
|
||||
ltB ltB ltB.
|
||||
T{
|
||||
Event
|
||||
T}:T{
|
||||
@ -1384,7 +1384,7 @@ server\.listen(7000, "localhost");
|
||||
.RE
|
||||
.TS
|
||||
allbox tab(:);
|
||||
ltB ltB ltBx.
|
||||
ltB ltB ltB.
|
||||
T{
|
||||
Event
|
||||
T}:T{
|
||||
@ -1466,7 +1466,7 @@ This object is used as a TCP client and also as a server\-side socket for tcp\.S
|
||||
.sp
|
||||
.TS
|
||||
allbox tab(:);
|
||||
ltB ltB ltBx.
|
||||
ltB ltB ltB.
|
||||
T{
|
||||
Event
|
||||
T}:T{
|
||||
@ -1762,6 +1762,37 @@ node\.dns\.NOMEM: out of memory while processing\.
|
||||
\h'-04'\(bu\h'+03'
|
||||
node\.dns\.BADQUERY: the query is malformed\.
|
||||
.RE
|
||||
.SH "REPL"
|
||||
A Read\-Eval\-Print\-Loop is available both as a standalone program and easily includable in other programs\.
|
||||
.sp
|
||||
The standalone REPL is called node\-repl and is installed at $PREFIX/bin/node\-repl\. It\(cqs recommended to use it with the program rlwrap for a better user interface\. I set
|
||||
.sp
|
||||
.sp
|
||||
.RS 4
|
||||
.nf
|
||||
alias node\-repl="rlwrap node\-repl"
|
||||
.fi
|
||||
.RE
|
||||
in my zsh configuration\.
|
||||
.sp
|
||||
Inside the REPL, Control+D will exit\. The special variable _ (underscore) contains the result of the last expression\.
|
||||
.sp
|
||||
The library is called /repl\.js and it can be used like this:
|
||||
.sp
|
||||
.sp
|
||||
.RS 4
|
||||
.nf
|
||||
include("/utils\.js");
|
||||
include("/tcp\.js");
|
||||
nconnections = 0;
|
||||
createServer(function (c) {
|
||||
error("Connection!");
|
||||
nconnections += 1;
|
||||
c\.close();
|
||||
})\.listen(5000);
|
||||
require("/repl\.js")\.start("simple tcp server> ");
|
||||
.fi
|
||||
.RE
|
||||
.SH "EXTENSION API"
|
||||
External modules can be compiled and dynamically linked into Node\. Node is more or less glue between several C and C++ libraries:
|
||||
.sp
|
||||
|
31
lib/repl.js
31
lib/repl.js
@ -5,19 +5,23 @@ var utils = require("utils.js");
|
||||
|
||||
utils.puts("Type '.help' for options.");
|
||||
|
||||
node.stdio.open();
|
||||
node.stdio.addListener("data", readline);
|
||||
|
||||
var buffered_cmd = '';
|
||||
var trimmer = /^\s*(.+)\s*$/m;
|
||||
var scopedVar = /^\s*var\s*([_\w\$]+)(.*)$/m;
|
||||
var scopeFunc = /^\s*function\s*([_\w\$]+)/;
|
||||
|
||||
exports.prompt = "node> ";
|
||||
exports.scope = {};
|
||||
exports.prompt = "node> ";
|
||||
exports.start = function (prompt) {
|
||||
if (prompt !== undefined) {
|
||||
exports.prompt = prompt;
|
||||
}
|
||||
|
||||
displayPrompt();
|
||||
|
||||
node.stdio.open();
|
||||
node.stdio.addListener("data", readline);
|
||||
displayPrompt();
|
||||
}
|
||||
|
||||
/**
|
||||
* The main REPL function. This is called everytime the user enters
|
||||
@ -43,18 +47,23 @@ function readline (cmd) {
|
||||
// Scope the readline with exports.scope to provide "local" vars
|
||||
with (exports.scope) {
|
||||
var ret = eval(buffered_cmd);
|
||||
exports.scope['_'] = ret;
|
||||
utils.p(ret);
|
||||
if (ret !== undefined) {
|
||||
exports.scope['_'] = ret;
|
||||
utils.p(ret);
|
||||
}
|
||||
}
|
||||
|
||||
buffered_cmd = '';
|
||||
} catch (e) {
|
||||
if (!(e instanceof SyntaxError))
|
||||
throw e;
|
||||
if (!(e instanceof SyntaxError)) throw e;
|
||||
}
|
||||
} catch (e) {
|
||||
// On error: Print the error and clear the buffer
|
||||
utils.puts('caught an exception: ' + e);
|
||||
if (e.stack) {
|
||||
utils.puts(e.stack);
|
||||
} else {
|
||||
utils.puts(e.toString());
|
||||
}
|
||||
buffered_cmd = '';
|
||||
}
|
||||
|
||||
@ -98,7 +107,7 @@ function parseREPLKeyword (cmd) {
|
||||
displayPrompt();
|
||||
return true;
|
||||
}
|
||||
return 0;
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
x
Reference in New Issue
Block a user