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 1: Use 'rlwrap node-repl' for a better interface");
|
||||||
puts("Tip 2: Type Control-D to exit.");
|
puts("Tip 2: Type Control-D to exit.");
|
||||||
|
|
||||||
include("/repl.js");
|
require("/repl.js").start();
|
||||||
|
|
||||||
// vim:ft=javascript
|
// 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>
|
</li>
|
||||||
</ul></div>
|
</ul></div>
|
||||||
</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>
|
<h2 id="_extension_api">Extension API</h2>
|
||||||
<div class="sectionbody">
|
<div class="sectionbody">
|
||||||
<div class="paragraph"><p>External modules can be compiled and dynamically linked into Node.
|
<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">
|
||||||
<div id="footer-text">
|
<div id="footer-text">
|
||||||
Version 0.1.12<br />
|
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>
|
||||||
</div>
|
</div>
|
||||||
</body>
|
</body>
|
||||||
|
32
doc/api.txt
32
doc/api.txt
@ -35,7 +35,6 @@ Server running at http://127.0.0.1:8000/
|
|||||||
----------------------------------------
|
----------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
== API
|
== API
|
||||||
|
|
||||||
Node supports 3 string encodings. UTF-8 (+"utf8"+), ASCII (+"ascii"+), and
|
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
|
== Extension API
|
||||||
|
|
||||||
External modules can be compiled and dynamically linked into Node.
|
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>
|
</itemizedlist>
|
||||||
</refsect2>
|
</refsect2>
|
||||||
</refsect1>
|
</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">
|
<refsect1 id="_extension_api">
|
||||||
<title>Extension API</title>
|
<title>Extension API</title>
|
||||||
<simpara>External modules can be compiled and dynamically linked into Node.
|
<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
|
.sp
|
||||||
.TS
|
.TS
|
||||||
allbox tab(:);
|
allbox tab(:);
|
||||||
ltB ltB ltBx.
|
ltB ltB ltB.
|
||||||
T{
|
T{
|
||||||
Event
|
Event
|
||||||
T}:T{
|
T}:T{
|
||||||
@ -165,7 +165,7 @@ All EventEmitters emit the event "newListener" when new listeners are added\.
|
|||||||
.sp
|
.sp
|
||||||
.TS
|
.TS
|
||||||
allbox tab(:);
|
allbox tab(:);
|
||||||
ltB ltB ltBx.
|
ltB ltB ltB.
|
||||||
T{
|
T{
|
||||||
Event
|
Event
|
||||||
T}:T{
|
T}:T{
|
||||||
@ -221,7 +221,7 @@ node\.Promise inherits from node\.eventEmitter\. A promise emits one of two even
|
|||||||
.sp
|
.sp
|
||||||
.TS
|
.TS
|
||||||
allbox tab(:);
|
allbox tab(:);
|
||||||
ltB ltB ltBx.
|
ltB ltB ltB.
|
||||||
T{
|
T{
|
||||||
Event
|
Event
|
||||||
T}:T{
|
T}:T{
|
||||||
@ -320,7 +320,7 @@ Standard I/O is handled through a special object node\.stdio\. stdout and stdin
|
|||||||
.sp
|
.sp
|
||||||
.TS
|
.TS
|
||||||
allbox tab(:);
|
allbox tab(:);
|
||||||
ltB ltB ltBx.
|
ltB ltB ltB.
|
||||||
T{
|
T{
|
||||||
Event
|
Event
|
||||||
T}:T{
|
T}:T{
|
||||||
@ -476,7 +476,7 @@ node.ChildProcess
|
|||||||
.RS
|
.RS
|
||||||
.TS
|
.TS
|
||||||
allbox tab(:);
|
allbox tab(:);
|
||||||
ltB ltB ltBx.
|
ltB ltB ltB.
|
||||||
T{
|
T{
|
||||||
Event
|
Event
|
||||||
T}:T{
|
T}:T{
|
||||||
@ -834,7 +834,7 @@ http.Server
|
|||||||
.RS
|
.RS
|
||||||
.TS
|
.TS
|
||||||
allbox tab(:);
|
allbox tab(:);
|
||||||
ltB ltB ltBx.
|
ltB ltB ltB.
|
||||||
T{
|
T{
|
||||||
Event
|
Event
|
||||||
T}:T{
|
T}:T{
|
||||||
@ -918,7 +918,7 @@ This object is created internally by a HTTP server\(emnot by the user\(emand pas
|
|||||||
.sp
|
.sp
|
||||||
.TS
|
.TS
|
||||||
allbox tab(:);
|
allbox tab(:);
|
||||||
ltB ltB ltBx.
|
ltB ltB ltB.
|
||||||
T{
|
T{
|
||||||
Event
|
Event
|
||||||
T}:T{
|
T}:T{
|
||||||
@ -1175,7 +1175,7 @@ This object is created internally and returned from the request methods of a htt
|
|||||||
.sp
|
.sp
|
||||||
.TS
|
.TS
|
||||||
allbox tab(:);
|
allbox tab(:);
|
||||||
ltB ltB ltBx.
|
ltB ltB ltB.
|
||||||
T{
|
T{
|
||||||
Event
|
Event
|
||||||
T}:T{
|
T}:T{
|
||||||
@ -1273,7 +1273,7 @@ This object is created internally and passed to the "response" event\.
|
|||||||
.sp
|
.sp
|
||||||
.TS
|
.TS
|
||||||
allbox tab(:);
|
allbox tab(:);
|
||||||
ltB ltB ltBx.
|
ltB ltB ltB.
|
||||||
T{
|
T{
|
||||||
Event
|
Event
|
||||||
T}:T{
|
T}:T{
|
||||||
@ -1384,7 +1384,7 @@ server\.listen(7000, "localhost");
|
|||||||
.RE
|
.RE
|
||||||
.TS
|
.TS
|
||||||
allbox tab(:);
|
allbox tab(:);
|
||||||
ltB ltB ltBx.
|
ltB ltB ltB.
|
||||||
T{
|
T{
|
||||||
Event
|
Event
|
||||||
T}:T{
|
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
|
.sp
|
||||||
.TS
|
.TS
|
||||||
allbox tab(:);
|
allbox tab(:);
|
||||||
ltB ltB ltBx.
|
ltB ltB ltB.
|
||||||
T{
|
T{
|
||||||
Event
|
Event
|
||||||
T}:T{
|
T}:T{
|
||||||
@ -1762,6 +1762,37 @@ node\.dns\.NOMEM: out of memory while processing\.
|
|||||||
\h'-04'\(bu\h'+03'
|
\h'-04'\(bu\h'+03'
|
||||||
node\.dns\.BADQUERY: the query is malformed\.
|
node\.dns\.BADQUERY: the query is malformed\.
|
||||||
.RE
|
.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"
|
.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:
|
External modules can be compiled and dynamically linked into Node\. Node is more or less glue between several C and C++ libraries:
|
||||||
.sp
|
.sp
|
||||||
|
31
lib/repl.js
31
lib/repl.js
@ -5,19 +5,23 @@ var utils = require("utils.js");
|
|||||||
|
|
||||||
utils.puts("Type '.help' for options.");
|
utils.puts("Type '.help' for options.");
|
||||||
|
|
||||||
node.stdio.open();
|
|
||||||
node.stdio.addListener("data", readline);
|
|
||||||
|
|
||||||
var buffered_cmd = '';
|
var buffered_cmd = '';
|
||||||
var trimmer = /^\s*(.+)\s*$/m;
|
var trimmer = /^\s*(.+)\s*$/m;
|
||||||
var scopedVar = /^\s*var\s*([_\w\$]+)(.*)$/m;
|
var scopedVar = /^\s*var\s*([_\w\$]+)(.*)$/m;
|
||||||
var scopeFunc = /^\s*function\s*([_\w\$]+)/;
|
var scopeFunc = /^\s*function\s*([_\w\$]+)/;
|
||||||
|
|
||||||
exports.prompt = "node> ";
|
|
||||||
exports.scope = {};
|
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
|
* 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
|
// Scope the readline with exports.scope to provide "local" vars
|
||||||
with (exports.scope) {
|
with (exports.scope) {
|
||||||
var ret = eval(buffered_cmd);
|
var ret = eval(buffered_cmd);
|
||||||
exports.scope['_'] = ret;
|
if (ret !== undefined) {
|
||||||
utils.p(ret);
|
exports.scope['_'] = ret;
|
||||||
|
utils.p(ret);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
buffered_cmd = '';
|
buffered_cmd = '';
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
if (!(e instanceof SyntaxError))
|
if (!(e instanceof SyntaxError)) throw e;
|
||||||
throw e;
|
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
// On error: Print the error and clear the buffer
|
// 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 = '';
|
buffered_cmd = '';
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -98,7 +107,7 @@ function parseREPLKeyword (cmd) {
|
|||||||
displayPrompt();
|
displayPrompt();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return 0;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
x
Reference in New Issue
Block a user