From 27738d8e60bc57aac5dba25041faf413caefe1f8 Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Mon, 28 Sep 2009 17:37:34 +0200 Subject: [PATCH] Clean up some things in the repl, add docs. --- bin/node-repl | 2 +- doc/api.html | 30 ++++++++++++++++++++++++++++- doc/api.txt | 32 ++++++++++++++++++++++++++++++- doc/api.xml | 22 +++++++++++++++++++++ doc/node.1 | 53 ++++++++++++++++++++++++++++++++++++++++----------- lib/repl.js | 31 +++++++++++++++++++----------- 6 files changed, 145 insertions(+), 25 deletions(-) diff --git a/bin/node-repl b/bin/node-repl index ae5ca9d6fe..20f04e89fc 100755 --- a/bin/node-repl +++ b/bin/node-repl @@ -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 diff --git a/doc/api.html b/doc/api.html index 80feec48d7..fe985109fa 100644 --- a/doc/api.html +++ b/doc/api.html @@ -1866,6 +1866,34 @@ on error: returns code, msg. code is one of the error codes li +

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. @@ -1955,7 +1983,7 @@ init (Handle<Object> target)

diff --git a/doc/api.txt b/doc/api.txt index 4a1f7fb176..38aef8e01a 100644 --- a/doc/api.txt +++ b/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. diff --git a/doc/api.xml b/doc/api.xml index 1ce13fcd45..02e0dab99b 100644 --- a/doc/api.xml +++ b/doc/api.xml @@ -1991,6 +1991,28 @@ on error: returns code, msg. code is one o + +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. diff --git a/doc/node.1 b/doc/node.1 index 836c307d62..3f5663a80a 100644 --- a/doc/node.1 +++ b/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 diff --git a/lib/repl.js b/lib/repl.js index 10a3e0faf1..e163d944aa 100644 --- a/lib/repl.js +++ b/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; } /**