|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +constcommon=require('../common.js'); |
| 4 | +constrepl=require('node:repl'); |
| 5 | +const{ PassThrough, Writable }=require('node:stream'); |
| 6 | + |
| 7 | +constbench=common.createBenchmark(main,{ |
| 8 | +n: [1e4], |
| 9 | +code: [ |
| 10 | +'1 + 1\n', |
| 11 | +'Promise.resolve(42)\n', |
| 12 | +], |
| 13 | +mode: ['sloppy','strict'], |
| 14 | +terminal: [0,1], |
| 15 | +useGlobal: [0,1], |
| 16 | +}); |
| 17 | + |
| 18 | +functionmain({ n,code: inputCode, mode, terminal, useGlobal }){ |
| 19 | +constinput=newPassThrough(); |
| 20 | +constoutput=newWritable({write(c,e,cb){cb();}}); |
| 21 | +constserver=newrepl.REPLServer({ |
| 22 | + input, |
| 23 | + output, |
| 24 | +replMode: mode==='strict' ? |
| 25 | +repl.REPL_MODE_STRICT : |
| 26 | +repl.REPL_MODE_SLOPPY, |
| 27 | +terminal: !!terminal, |
| 28 | +useGlobal: !!useGlobal, |
| 29 | +}); |
| 30 | +constoriginalEval=server.eval; |
| 31 | +// TTY input dispatch can briefly have no other active event loop handles. |
| 32 | +constkeepAlive=setInterval(()=>{},0x7fffffff); |
| 33 | +letremaining=n; |
| 34 | + |
| 35 | +// eslint-disable-next-line node-core/func-name-matching |
| 36 | +server.eval=functionREPLEval(code,context,file,callback){ |
| 37 | +originalEval(code,context,file,functiononEvaluate(){ |
| 38 | +constresult=Reflect.apply(callback,this,arguments); |
| 39 | +if(--remaining===0){ |
| 40 | +bench.end(n); |
| 41 | +clearInterval(keepAlive); |
| 42 | +server.close(); |
| 43 | +}else{ |
| 44 | +setImmediate(()=>input.write(inputCode)); |
| 45 | +} |
| 46 | +returnresult; |
| 47 | +}); |
| 48 | +}; |
| 49 | + |
| 50 | +bench.start(); |
| 51 | +input.write(inputCode); |
| 52 | +} |
0 commit comments