@@ -303,7 +303,20 @@ When a new [`repl.REPLServer`][] is created, a custom evaluation function may be
303303provided. This can be used, for instance, to implement fully customized REPL
304304applications.
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 (` REPL<n> ` , where ` n ` is the 1-based index of the current command).
313+ This can be useful for debugging purposes.
314+ * ` callback ` {Function} A function to invoke once the code evaluation is complete. The callback takes two parameters:
315+ * An error object to provide if an error occurred during evaluation, or ` null ` /` undefined ` if no error occurred.
316+ * The result of the code evaluation (this is not relevant if an error is provided).
317+
318+ The following illustrates an example of a REPL that squares a given number, an error is instead printed
319+ if the provided input is not actually a number:
307320
308321``` mjs
309322import repl from ' node:repl' ;
@@ -312,8 +325,12 @@ function byThePowerOfTwo(number) {
312325 return number * number;
313326}
314327
315- function myEval (cmd , context , filename , callback ) {
316- callback (null , byThePowerOfTwo (cmd));
328+ function myEval (code , context , replResourceName , callback ) {
329+ if (isNaN (code)) {
330+ callback (new Error (` ${ code .trim ()} is not a number` ));
331+ } else {
332+ callback (null , byThePowerOfTwo (code));
333+ }
317334}
318335
319336repl .start ({ prompt: ' Enter a number: ' , eval: myEval });
@@ -326,8 +343,12 @@ function byThePowerOfTwo(number) {
326343 return number * number;
327344}
328345
329- function myEval (cmd , context , filename , callback ) {
330- callback (null , byThePowerOfTwo (cmd));
346+ function myEval (code , context , replResourceName , callback ) {
347+ if (isNaN (code)) {
348+ callback (new Error (` ${ code .trim ()} is not a number` ));
349+ } else {
350+ callback (null , byThePowerOfTwo (code));
351+ }
331352}
332353
333354repl .start ({ prompt: ' Enter a number: ' , eval: myEval });
@@ -691,7 +712,8 @@ changes:
691712 * ` eval ` {Function} The function to be used when evaluating each given line
692713 of input. ** Default:** an async wrapper for the JavaScript ` eval() `
693714 function. An ` eval ` function can error with ` repl.Recoverable ` to indicate
694- the input was incomplete and prompt for additional lines.
715+ the input was incomplete and prompt for additional lines. See the
716+ [ custom evaluation functions] [ ] section for more details.
695717 * ` useColors ` {boolean} If ` true ` , specifies that the default ` writer `
696718 function should include ANSI color styling to REPL output. If a custom
697719 ` writer ` function is provided then this has no effect. ** Default:** checking
@@ -914,4 +936,5 @@ avoiding open network interfaces.
914936[ `repl.start()` ] : #replstartoptions
915937[ `reverse-i-search` ] : #reverse-i-search
916938[ `util.inspect()` ] : util.md#utilinspectobject-options
939+ [ custom evaluation functions ] : #custom-evaluation-functions
917940[ stream ] : stream.md
0 commit comments