@@ -303,7 +303,20 @@ When a new [`repl.REPLServer`][] is created, a custom evaluation function may be
303
303
provided. This can be used, for instance, to implement fully customized REPL
304
304
applications.
305
305
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:
307
320
308
321
``` mjs
309
322
import repl from ' node:repl' ;
@@ -312,8 +325,12 @@ function byThePowerOfTwo(number) {
312
325
return number * number;
313
326
}
314
327
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
+ }
317
334
}
318
335
319
336
repl .start ({ prompt: ' Enter a number: ' , eval: myEval });
@@ -326,8 +343,12 @@ function byThePowerOfTwo(number) {
326
343
return number * number;
327
344
}
328
345
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
+ }
331
352
}
332
353
333
354
repl .start ({ prompt: ' Enter a number: ' , eval: myEval });
@@ -691,7 +712,8 @@ changes:
691
712
* ` eval ` {Function} The function to be used when evaluating each given line
692
713
of input. ** Default:** an async wrapper for the JavaScript ` eval() `
693
714
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.
695
717
* ` useColors ` {boolean} If ` true ` , specifies that the default ` writer `
696
718
function should include ANSI color styling to REPL output. If a custom
697
719
` writer ` function is provided then this has no effect. ** Default:** checking
@@ -914,4 +936,5 @@ avoiding open network interfaces.
914
936
[ `repl.start()` ] : #replstartoptions
915
937
[ `reverse-i-search` ] : #reverse-i-search
916
938
[ `util.inspect()` ] : util.md#utilinspectobject-options
939
+ [ custom evaluation functions ] : #custom-evaluation-functions
917
940
[ stream ] : stream.md
0 commit comments