Skip to content

Commit 310ccb5

Browse files
dario-piotrowiczRafaelGSS
authored andcommitted
doc: document REPL custom eval arguments
PR-URL: #57690 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 0f7b565 commit 310ccb5

File tree

1 file changed

+28
-6
lines changed

1 file changed

+28
-6
lines changed

doc/api/repl.md

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,19 @@ When a new [`repl.REPLServer`][] is created, a custom evaluation function may be
303303
provided. This can be used, for instance, to implement fully customized REPL
304304
applications.
305305

306-
The following illustrates an example of a REPL that squares a given number:
306+
An evaluation function accepts the following four arguments:
307+
308+
* `code` {string} The code to be executed (e.g. `1 + 1`).
309+
* `context` {Object} The context in which the code is executed. This can either be the JavaScript `global`
310+
context or a context specific to the REPL instance, depending on the `useGlobal` option.
311+
* `replResourceName` {string} An identifier for the REPL resource associated with the current code
312+
evaluation. This can be useful for debugging purposes.
313+
* `callback` {Function} A function to invoke once the code evaluation is complete. The callback takes two parameters:
314+
* An error object to provide if an error occurred during evaluation, or `null`/`undefined` if no error occurred.
315+
* The result of the code evaluation (this is not relevant if an error is provided).
316+
317+
The following illustrates an example of a REPL that squares a given number, an error is instead printed
318+
if the provided input is not actually a number:
307319

308320
```mjs
309321
import repl from 'node:repl';
@@ -312,8 +324,12 @@ function byThePowerOfTwo(number) {
312324
return number * number;
313325
}
314326

315-
function myEval(cmd, context, filename, callback) {
316-
callback(null, byThePowerOfTwo(cmd));
327+
function myEval(code, context, replResourceName, callback) {
328+
if (isNaN(code)) {
329+
callback(new Error(`${code.trim()} is not a number`));
330+
} else {
331+
callback(null, byThePowerOfTwo(code));
332+
}
317333
}
318334

319335
repl.start({ prompt: 'Enter a number: ', eval: myEval });
@@ -326,8 +342,12 @@ function byThePowerOfTwo(number) {
326342
return number * number;
327343
}
328344

329-
function myEval(cmd, context, filename, callback) {
330-
callback(null, byThePowerOfTwo(cmd));
345+
function myEval(code, context, replResourceName, callback) {
346+
if (isNaN(code)) {
347+
callback(new Error(`${code.trim()} is not a number`));
348+
} else {
349+
callback(null, byThePowerOfTwo(code));
350+
}
331351
}
332352

333353
repl.start({ prompt: 'Enter a number: ', eval: myEval });
@@ -691,7 +711,8 @@ changes:
691711
* `eval` {Function} The function to be used when evaluating each given line
692712
of input. **Default:** an async wrapper for the JavaScript `eval()`
693713
function. An `eval` function can error with `repl.Recoverable` to indicate
694-
the input was incomplete and prompt for additional lines.
714+
the input was incomplete and prompt for additional lines. See the
715+
[custom evaluation functions][] section for more details.
695716
* `useColors` {boolean} If `true`, specifies that the default `writer`
696717
function should include ANSI color styling to REPL output. If a custom
697718
`writer` function is provided then this has no effect. **Default:** checking
@@ -914,4 +935,5 @@ avoiding open network interfaces.
914935
[`repl.start()`]: #replstartoptions
915936
[`reverse-i-search`]: #reverse-i-search
916937
[`util.inspect()`]: util.md#utilinspectobject-options
938+
[custom evaluation functions]: #custom-evaluation-functions
917939
[stream]: stream.md

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy