File: repl.html

package info (click to toggle)
coffeescript 2.7.0%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 9,360 kB
  • sloc: makefile: 20; xml: 9; sh: 6; javascript: 5
file content (770 lines) | stat: -rw-r--r-- 33,921 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
<!DOCTYPE html>

<html>
<head>
  <title>repl.coffee</title>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <meta name="viewport" content="width=device-width, target-densitydpi=160dpi, initial-scale=1.0; maximum-scale=1.0; user-scalable=0;">
  <link rel="stylesheet" media="all" href="docco.css" />
</head>
<body>
  <div id="container">
    <div id="background"></div>
    
      <ul id="jump_to">
        <li>
          <a class="large" href="javascript:void(0);">Jump To &hellip;</a>
          <a class="small" href="javascript:void(0);">+</a>
          <div id="jump_wrapper">
          <div id="jump_page_wrapper">
            <div id="jump_page">
              
                
                <a class="source" href="browser.html">
                  browser.coffee
                </a>
              
                
                <a class="source" href="cake.html">
                  cake.coffee
                </a>
              
                
                <a class="source" href="coffeescript.html">
                  coffeescript.coffee
                </a>
              
                
                <a class="source" href="command.html">
                  command.coffee
                </a>
              
                
                <a class="source" href="grammar.html">
                  grammar.coffee
                </a>
              
                
                <a class="source" href="helpers.html">
                  helpers.coffee
                </a>
              
                
                <a class="source" href="index.html">
                  index.coffee
                </a>
              
                
                <a class="source" href="lexer.html">
                  lexer.coffee
                </a>
              
                
                <a class="source" href="nodes.html">
                  nodes.coffee
                </a>
              
                
                <a class="source" href="optparse.html">
                  optparse.coffee
                </a>
              
                
                <a class="source" href="register.html">
                  register.coffee
                </a>
              
                
                <a class="source" href="repl.html">
                  repl.coffee
                </a>
              
                
                <a class="source" href="rewriter.html">
                  rewriter.coffee
                </a>
              
                
                <a class="source" href="scope.html">
                  scope.litcoffee
                </a>
              
                
                <a class="source" href="sourcemap.html">
                  sourcemap.litcoffee
                </a>
              
            </div>
          </div>
        </li>
      </ul>
    
    <ul class="sections">
        
          <li id="title">
              <div class="annotation">
                  <h1>repl.coffee</h1>
              </div>
          </li>
        
        
        
        <li id="section-1">
            <div class="annotation">
              
              <div class="sswrap ">
                <a class="ss" href="#section-1">&#x00a7;</a>
              </div>
              
            </div>
            
            <div class="content"><div class='highlight'><pre>fs = <span class="hljs-built_in">require</span> <span class="hljs-string">&#x27;fs&#x27;</span>
path = <span class="hljs-built_in">require</span> <span class="hljs-string">&#x27;path&#x27;</span>
vm = <span class="hljs-built_in">require</span> <span class="hljs-string">&#x27;vm&#x27;</span>
nodeREPL = <span class="hljs-built_in">require</span> <span class="hljs-string">&#x27;repl&#x27;</span>
CoffeeScript = <span class="hljs-built_in">require</span> <span class="hljs-string">&#x27;./&#x27;</span>
{merge, updateSyntaxError} = <span class="hljs-built_in">require</span> <span class="hljs-string">&#x27;./helpers&#x27;</span>

sawSIGINT = <span class="hljs-literal">no</span>
transpile = <span class="hljs-literal">no</span>

replDefaults =
  prompt: <span class="hljs-string">&#x27;coffee&gt; &#x27;</span>,
  historyFile: <span class="hljs-keyword">do</span> -&gt;
    historyPath = process.env.XDG_CACHE_HOME <span class="hljs-keyword">or</span> process.env.HOME
    path.join historyPath, <span class="hljs-string">&#x27;.coffee_history&#x27;</span> <span class="hljs-keyword">if</span> historyPath
  historyMaxInputSize: <span class="hljs-number">10240</span>
  eval: <span class="hljs-function"><span class="hljs-params">(input, context, filename, cb)</span> -&gt;</span></pre></div></div>
            
        </li>
        
        
        <li id="section-2">
            <div class="annotation">
              
              <div class="sswrap ">
                <a class="ss" href="#section-2">&#x00a7;</a>
              </div>
              <p>XXX: multiline hack.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    input = input.replace <span class="hljs-regexp">/\uFF00/g</span>, <span class="hljs-string">&#x27;\n&#x27;</span></pre></div></div>
            
        </li>
        
        
        <li id="section-3">
            <div class="annotation">
              
              <div class="sswrap ">
                <a class="ss" href="#section-3">&#x00a7;</a>
              </div>
              <p>Node’s REPL sends the input ending with a newline and then wrapped in
parens. Unwrap all that.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    input = input.replace <span class="hljs-regexp">/^\(([\s\S]*)\n\)$/m</span>, <span class="hljs-string">&#x27;$1&#x27;</span></pre></div></div>
            
        </li>
        
        
        <li id="section-4">
            <div class="annotation">
              
              <div class="sswrap ">
                <a class="ss" href="#section-4">&#x00a7;</a>
              </div>
              <p>Node’s REPL v6.9.1+ sends the input wrapped in a try/catch statement.
Unwrap that too.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    input = input.replace <span class="hljs-regexp">/^\s*try\s*{([\s\S]*)}\s*catch.*$/m</span>, <span class="hljs-string">&#x27;$1&#x27;</span></pre></div></div>
            
        </li>
        
        
        <li id="section-5">
            <div class="annotation">
              
              <div class="sswrap ">
                <a class="ss" href="#section-5">&#x00a7;</a>
              </div>
              <p>Require AST nodes to do some AST manipulation.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    {Block, Assign, Value, Literal, Call, Code, Root} = <span class="hljs-built_in">require</span> <span class="hljs-string">&#x27;./nodes&#x27;</span>

    <span class="hljs-keyword">try</span></pre></div></div>
            
        </li>
        
        
        <li id="section-6">
            <div class="annotation">
              
              <div class="sswrap ">
                <a class="ss" href="#section-6">&#x00a7;</a>
              </div>
              <p>Tokenize the clean input.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>      tokens = CoffeeScript.tokens input</pre></div></div>
            
        </li>
        
        
        <li id="section-7">
            <div class="annotation">
              
              <div class="sswrap ">
                <a class="ss" href="#section-7">&#x00a7;</a>
              </div>
              <p>Filter out tokens generated just to hold comments.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>      <span class="hljs-keyword">if</span> tokens.length &gt;= <span class="hljs-number">2</span> <span class="hljs-keyword">and</span> tokens[<span class="hljs-number">0</span>].generated <span class="hljs-keyword">and</span>
         tokens[<span class="hljs-number">0</span>].comments?.length <span class="hljs-keyword">isnt</span> <span class="hljs-number">0</span> <span class="hljs-keyword">and</span> <span class="hljs-string">&quot;<span class="hljs-subst">#{tokens[<span class="hljs-number">0</span>][<span class="hljs-number">1</span>]}</span>&quot;</span> <span class="hljs-keyword">is</span> <span class="hljs-string">&#x27;&#x27;</span> <span class="hljs-keyword">and</span>
         tokens[<span class="hljs-number">1</span>][<span class="hljs-number">0</span>] <span class="hljs-keyword">is</span> <span class="hljs-string">&#x27;TERMINATOR&#x27;</span>
        tokens = tokens[<span class="hljs-number">2.</span>..]
      <span class="hljs-keyword">if</span> tokens.length &gt;= <span class="hljs-number">1</span> <span class="hljs-keyword">and</span> tokens[tokens.length - <span class="hljs-number">1</span>].generated <span class="hljs-keyword">and</span>
         tokens[tokens.length - <span class="hljs-number">1</span>].comments?.length <span class="hljs-keyword">isnt</span> <span class="hljs-number">0</span> <span class="hljs-keyword">and</span> <span class="hljs-string">&quot;<span class="hljs-subst">#{tokens[tokens.length - <span class="hljs-number">1</span>][<span class="hljs-number">1</span>]}</span>&quot;</span> <span class="hljs-keyword">is</span> <span class="hljs-string">&#x27;&#x27;</span>
        tokens.pop()</pre></div></div>
            
        </li>
        
        
        <li id="section-8">
            <div class="annotation">
              
              <div class="sswrap ">
                <a class="ss" href="#section-8">&#x00a7;</a>
              </div>
              <p>Collect referenced variable names just like in <code>CoffeeScript.compile</code>.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>      referencedVars = (token[<span class="hljs-number">1</span>] <span class="hljs-keyword">for</span> token <span class="hljs-keyword">in</span> tokens <span class="hljs-keyword">when</span> token[<span class="hljs-number">0</span>] <span class="hljs-keyword">is</span> <span class="hljs-string">&#x27;IDENTIFIER&#x27;</span>)</pre></div></div>
            
        </li>
        
        
        <li id="section-9">
            <div class="annotation">
              
              <div class="sswrap ">
                <a class="ss" href="#section-9">&#x00a7;</a>
              </div>
              <p>Generate the AST of the tokens.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>      ast = CoffeeScript.nodes(tokens).body</pre></div></div>
            
        </li>
        
        
        <li id="section-10">
            <div class="annotation">
              
              <div class="sswrap ">
                <a class="ss" href="#section-10">&#x00a7;</a>
              </div>
              <p>Add assignment to <code>__</code> variable to force the input to be an expression.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>      ast = <span class="hljs-keyword">new</span> Block [<span class="hljs-keyword">new</span> Assign (<span class="hljs-keyword">new</span> Value <span class="hljs-keyword">new</span> Literal <span class="hljs-string">&#x27;__&#x27;</span>), ast, <span class="hljs-string">&#x27;=&#x27;</span>]</pre></div></div>
            
        </li>
        
        
        <li id="section-11">
            <div class="annotation">
              
              <div class="sswrap ">
                <a class="ss" href="#section-11">&#x00a7;</a>
              </div>
              <p>Wrap the expression in a closure to support top-level <code>await</code>.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>      ast     = <span class="hljs-keyword">new</span> Code [], ast
      isAsync = ast.isAsync</pre></div></div>
            
        </li>
        
        
        <li id="section-12">
            <div class="annotation">
              
              <div class="sswrap ">
                <a class="ss" href="#section-12">&#x00a7;</a>
              </div>
              <p>Invoke the wrapping closure.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>      ast    = <span class="hljs-keyword">new</span> Root <span class="hljs-keyword">new</span> Block [<span class="hljs-keyword">new</span> Call ast]
      js     = ast.compile {bare: <span class="hljs-literal">yes</span>, locals: <span class="hljs-built_in">Object</span>.keys(context), referencedVars, sharedScope: <span class="hljs-literal">yes</span>}
      <span class="hljs-keyword">if</span> transpile
        js = transpile.transpile(js, transpile.options).code</pre></div></div>
            
        </li>
        
        
        <li id="section-13">
            <div class="annotation">
              
              <div class="sswrap ">
                <a class="ss" href="#section-13">&#x00a7;</a>
              </div>
              <p>Strip <code>&quot;use strict&quot;</code>, to avoid an exception on assigning to
undeclared variable <code>__</code>.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>        js = js.replace <span class="hljs-regexp">/^&quot;use strict&quot;|^&#x27;use strict&#x27;/</span>, <span class="hljs-string">&#x27;&#x27;</span>
      result = runInContext js, context, filename</pre></div></div>
            
        </li>
        
        
        <li id="section-14">
            <div class="annotation">
              
              <div class="sswrap ">
                <a class="ss" href="#section-14">&#x00a7;</a>
              </div>
              <p>Await an async result, if necessary.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>      <span class="hljs-keyword">if</span> isAsync
        result.<span class="hljs-keyword">then</span> (resolvedResult) -&gt;
          cb <span class="hljs-literal">null</span>, resolvedResult <span class="hljs-keyword">unless</span> sawSIGINT
        sawSIGINT = <span class="hljs-literal">no</span>
      <span class="hljs-keyword">else</span>
        cb <span class="hljs-literal">null</span>, result
    <span class="hljs-keyword">catch</span> err</pre></div></div>
            
        </li>
        
        
        <li id="section-15">
            <div class="annotation">
              
              <div class="sswrap ">
                <a class="ss" href="#section-15">&#x00a7;</a>
              </div>
              <p>AST’s <code>compile</code> does not add source code information to syntax errors.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>      updateSyntaxError err, input
      cb err
<span class="hljs-function">
<span class="hljs-title">runInContext</span> = <span class="hljs-params">(js, context, filename)</span> -&gt;</span>
  <span class="hljs-keyword">if</span> context <span class="hljs-keyword">is</span> global
    vm.runInThisContext js, filename
  <span class="hljs-keyword">else</span>
    vm.runInContext js, context, filename
<span class="hljs-function">
<span class="hljs-title">addMultilineHandler</span> = <span class="hljs-params">(repl)</span> -&gt;</span>
  {inputStream, outputStream} = repl</pre></div></div>
            
        </li>
        
        
        <li id="section-16">
            <div class="annotation">
              
              <div class="sswrap ">
                <a class="ss" href="#section-16">&#x00a7;</a>
              </div>
              <p>Node 0.11.12 changed API, prompt is now _prompt.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>  origPrompt = repl._prompt ? repl.prompt

  multiline =
    enabled: <span class="hljs-literal">off</span>
    initialPrompt: origPrompt.replace <span class="hljs-regexp">/^[^&gt; ]*/</span>, <span class="hljs-function"><span class="hljs-params">(x)</span> -&gt;</span> x.replace <span class="hljs-regexp">/./g</span>, <span class="hljs-string">&#x27;-&#x27;</span>
    prompt: origPrompt.replace <span class="hljs-regexp">/^[^&gt; ]*&gt;?/</span>, <span class="hljs-function"><span class="hljs-params">(x)</span> -&gt;</span> x.replace <span class="hljs-regexp">/./g</span>, <span class="hljs-string">&#x27;.&#x27;</span>
    buffer: <span class="hljs-string">&#x27;&#x27;</span></pre></div></div>
            
        </li>
        
        
        <li id="section-17">
            <div class="annotation">
              
              <div class="sswrap ">
                <a class="ss" href="#section-17">&#x00a7;</a>
              </div>
              <p>Proxy node’s line listener</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>  nodeLineListener = repl.listeners(<span class="hljs-string">&#x27;line&#x27;</span>)[<span class="hljs-number">0</span>]
  repl.removeListener <span class="hljs-string">&#x27;line&#x27;</span>, nodeLineListener
  repl.<span class="hljs-literal">on</span> <span class="hljs-string">&#x27;line&#x27;</span>, <span class="hljs-function"><span class="hljs-params">(cmd)</span> -&gt;</span>
    <span class="hljs-keyword">if</span> multiline.enabled
      multiline.buffer += <span class="hljs-string">&quot;<span class="hljs-subst">#{cmd}</span>\n&quot;</span>
      repl.setPrompt multiline.prompt
      repl.prompt <span class="hljs-literal">true</span>
    <span class="hljs-keyword">else</span>
      repl.setPrompt origPrompt
      nodeLineListener cmd
    <span class="hljs-keyword">return</span></pre></div></div>
            
        </li>
        
        
        <li id="section-18">
            <div class="annotation">
              
              <div class="sswrap ">
                <a class="ss" href="#section-18">&#x00a7;</a>
              </div>
              <p>Handle Ctrl-v</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>  inputStream.<span class="hljs-literal">on</span> <span class="hljs-string">&#x27;keypress&#x27;</span>, <span class="hljs-function"><span class="hljs-params">(char, key)</span> -&gt;</span>
    <span class="hljs-keyword">return</span> <span class="hljs-keyword">unless</span> key <span class="hljs-keyword">and</span> key.ctrl <span class="hljs-keyword">and</span> <span class="hljs-keyword">not</span> key.meta <span class="hljs-keyword">and</span> <span class="hljs-keyword">not</span> key.shift <span class="hljs-keyword">and</span> key.name <span class="hljs-keyword">is</span> <span class="hljs-string">&#x27;v&#x27;</span>
    <span class="hljs-keyword">if</span> multiline.enabled</pre></div></div>
            
        </li>
        
        
        <li id="section-19">
            <div class="annotation">
              
              <div class="sswrap ">
                <a class="ss" href="#section-19">&#x00a7;</a>
              </div>
              <p>allow arbitrarily switching between modes any time before multiple lines are entered</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>      <span class="hljs-keyword">unless</span> multiline.buffer.match <span class="hljs-regexp">/\n/</span>
        multiline.enabled = <span class="hljs-keyword">not</span> multiline.enabled
        repl.setPrompt origPrompt
        repl.prompt <span class="hljs-literal">true</span>
        <span class="hljs-keyword">return</span></pre></div></div>
            
        </li>
        
        
        <li id="section-20">
            <div class="annotation">
              
              <div class="sswrap ">
                <a class="ss" href="#section-20">&#x00a7;</a>
              </div>
              <p>no-op unless the current line is empty</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>      <span class="hljs-keyword">return</span> <span class="hljs-keyword">if</span> repl.line? <span class="hljs-keyword">and</span> <span class="hljs-keyword">not</span> repl.line.match <span class="hljs-regexp">/^\s*$/</span></pre></div></div>
            
        </li>
        
        
        <li id="section-21">
            <div class="annotation">
              
              <div class="sswrap ">
                <a class="ss" href="#section-21">&#x00a7;</a>
              </div>
              <p>eval, print, loop</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>      multiline.enabled = <span class="hljs-keyword">not</span> multiline.enabled
      repl.line = <span class="hljs-string">&#x27;&#x27;</span>
      repl.cursor = <span class="hljs-number">0</span>
      repl.output.cursorTo <span class="hljs-number">0</span>
      repl.output.clearLine <span class="hljs-number">1</span></pre></div></div>
            
        </li>
        
        
        <li id="section-22">
            <div class="annotation">
              
              <div class="sswrap ">
                <a class="ss" href="#section-22">&#x00a7;</a>
              </div>
              <p>XXX: multiline hack</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>      multiline.buffer = multiline.buffer.replace <span class="hljs-regexp">/\n/g</span>, <span class="hljs-string">&#x27;\uFF00&#x27;</span>
      repl.emit <span class="hljs-string">&#x27;line&#x27;</span>, multiline.buffer
      multiline.buffer = <span class="hljs-string">&#x27;&#x27;</span>
    <span class="hljs-keyword">else</span>
      multiline.enabled = <span class="hljs-keyword">not</span> multiline.enabled
      repl.setPrompt multiline.initialPrompt
      repl.prompt <span class="hljs-literal">true</span>
    <span class="hljs-keyword">return</span></pre></div></div>
            
        </li>
        
        
        <li id="section-23">
            <div class="annotation">
              
              <div class="sswrap ">
                <a class="ss" href="#section-23">&#x00a7;</a>
              </div>
              <p>Store and load command history from a file</p>

            </div>
            
            <div class="content"><div class='highlight'><pre><span class="hljs-function"><span class="hljs-title">addHistory</span> = <span class="hljs-params">(repl, filename, maxSize)</span> -&gt;</span>
  lastLine = <span class="hljs-literal">null</span>
  <span class="hljs-keyword">try</span></pre></div></div>
            
        </li>
        
        
        <li id="section-24">
            <div class="annotation">
              
              <div class="sswrap ">
                <a class="ss" href="#section-24">&#x00a7;</a>
              </div>
              <p>Get file info and at most maxSize of command history</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    stat = fs.statSync filename
    size = <span class="hljs-built_in">Math</span>.min maxSize, stat.size</pre></div></div>
            
        </li>
        
        
        <li id="section-25">
            <div class="annotation">
              
              <div class="sswrap ">
                <a class="ss" href="#section-25">&#x00a7;</a>
              </div>
              <p>Read last <code>size</code> bytes from the file</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    readFd = fs.openSync filename, <span class="hljs-string">&#x27;r&#x27;</span>
    buffer = Buffer.alloc size
    fs.readSync readFd, buffer, <span class="hljs-number">0</span>, size, stat.size - size
    fs.closeSync readFd</pre></div></div>
            
        </li>
        
        
        <li id="section-26">
            <div class="annotation">
              
              <div class="sswrap ">
                <a class="ss" href="#section-26">&#x00a7;</a>
              </div>
              <p>Set the history on the interpreter</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    repl.history = buffer.toString().split(<span class="hljs-string">&#x27;\n&#x27;</span>).reverse()</pre></div></div>
            
        </li>
        
        
        <li id="section-27">
            <div class="annotation">
              
              <div class="sswrap ">
                <a class="ss" href="#section-27">&#x00a7;</a>
              </div>
              <p>If the history file was truncated we should pop off a potential partial line</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    repl.history.pop() <span class="hljs-keyword">if</span> stat.size &gt; maxSize</pre></div></div>
            
        </li>
        
        
        <li id="section-28">
            <div class="annotation">
              
              <div class="sswrap ">
                <a class="ss" href="#section-28">&#x00a7;</a>
              </div>
              <p>Shift off the final blank newline</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    repl.history.shift() <span class="hljs-keyword">if</span> repl.history[<span class="hljs-number">0</span>] <span class="hljs-keyword">is</span> <span class="hljs-string">&#x27;&#x27;</span>
    repl.historyIndex = <span class="hljs-number">-1</span>
    lastLine = repl.history[<span class="hljs-number">0</span>]

  fd = fs.openSync filename, <span class="hljs-string">&#x27;a&#x27;</span>

  repl.addListener <span class="hljs-string">&#x27;line&#x27;</span>, <span class="hljs-function"><span class="hljs-params">(code)</span> -&gt;</span>
    <span class="hljs-keyword">if</span> code <span class="hljs-keyword">and</span> code.length <span class="hljs-keyword">and</span> code <span class="hljs-keyword">isnt</span> <span class="hljs-string">&#x27;.history&#x27;</span> <span class="hljs-keyword">and</span> code <span class="hljs-keyword">isnt</span> <span class="hljs-string">&#x27;.exit&#x27;</span> <span class="hljs-keyword">and</span> lastLine <span class="hljs-keyword">isnt</span> code</pre></div></div>
            
        </li>
        
        
        <li id="section-29">
            <div class="annotation">
              
              <div class="sswrap ">
                <a class="ss" href="#section-29">&#x00a7;</a>
              </div>
              <p>Save the latest command in the file</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>      fs.writeSync fd, <span class="hljs-string">&quot;<span class="hljs-subst">#{code}</span>\n&quot;</span>
      lastLine = code</pre></div></div>
            
        </li>
        
        
        <li id="section-30">
            <div class="annotation">
              
              <div class="sswrap ">
                <a class="ss" href="#section-30">&#x00a7;</a>
              </div>
              <p>XXX: The SIGINT event from REPLServer is undocumented, so this is a bit fragile</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>  repl.<span class="hljs-literal">on</span> <span class="hljs-string">&#x27;SIGINT&#x27;</span>, <span class="hljs-function">-&gt;</span> sawSIGINT = <span class="hljs-literal">yes</span>
  repl.<span class="hljs-literal">on</span> <span class="hljs-string">&#x27;exit&#x27;</span>, <span class="hljs-function">-&gt;</span> fs.closeSync fd</pre></div></div>
            
        </li>
        
        
        <li id="section-31">
            <div class="annotation">
              
              <div class="sswrap ">
                <a class="ss" href="#section-31">&#x00a7;</a>
              </div>
              <p>Add a command to show the history stack</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>  repl.commands[getCommandId(repl, <span class="hljs-string">&#x27;history&#x27;</span>)] =
    help: <span class="hljs-string">&#x27;Show command history&#x27;</span>
    action: <span class="hljs-function">-&gt;</span>
      repl.outputStream.write <span class="hljs-string">&quot;<span class="hljs-subst">#{repl.history[..].reverse().join <span class="hljs-string">&#x27;\n&#x27;</span>}</span>\n&quot;</span>
      repl.displayPrompt()
<span class="hljs-function">
<span class="hljs-title">getCommandId</span> = <span class="hljs-params">(repl, commandName)</span> -&gt;</span></pre></div></div>
            
        </li>
        
        
        <li id="section-32">
            <div class="annotation">
              
              <div class="sswrap ">
                <a class="ss" href="#section-32">&#x00a7;</a>
              </div>
              <p>Node 0.11 changed API, a command such as ‘.help’ is now stored as ‘help’</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>  commandsHaveLeadingDot = repl.commands[<span class="hljs-string">&#x27;.help&#x27;</span>]?
  <span class="hljs-keyword">if</span> commandsHaveLeadingDot <span class="hljs-keyword">then</span> <span class="hljs-string">&quot;.<span class="hljs-subst">#{commandName}</span>&quot;</span> <span class="hljs-keyword">else</span> commandName

module.<span class="hljs-built_in">exports</span> =
  start: <span class="hljs-function"><span class="hljs-params">(opts = {})</span> -&gt;</span>
    [major, minor, build] = process.versions.node.split(<span class="hljs-string">&#x27;.&#x27;</span>).map (n) -&gt; <span class="hljs-built_in">parseInt</span>(n, <span class="hljs-number">10</span>)

    <span class="hljs-keyword">if</span> major &lt; <span class="hljs-number">6</span>
      console.warn <span class="hljs-string">&quot;Node 6+ required for CoffeeScript REPL&quot;</span>
      process.exit <span class="hljs-number">1</span>

    CoffeeScript.register()
    process.argv = [<span class="hljs-string">&#x27;coffee&#x27;</span>].concat process.argv[<span class="hljs-number">2.</span>.]
    <span class="hljs-keyword">if</span> opts.transpile
      transpile = {}
      <span class="hljs-keyword">try</span>
        transpile.transpile = <span class="hljs-built_in">require</span>(<span class="hljs-string">&#x27;@babel/core&#x27;</span>).transform
      <span class="hljs-keyword">catch</span>
        <span class="hljs-keyword">try</span>
          transpile.transpile = <span class="hljs-built_in">require</span>(<span class="hljs-string">&#x27;babel-core&#x27;</span>).transform
        <span class="hljs-keyword">catch</span>
          console.error <span class="hljs-string">&#x27;&#x27;&#x27;
            To use --transpile with an interactive REPL, @babel/core must be installed either in the current folder or globally:
              npm install --save-dev @babel/core
            or
              npm install --global @babel/core
            And you must save options to configure Babel in one of the places it looks to find its options.
            See https://coffeescript.org/#transpilation
          &#x27;&#x27;&#x27;</span>
          process.exit <span class="hljs-number">1</span>
      transpile.options =
        filename: path.resolve process.cwd(), <span class="hljs-string">&#x27;&lt;repl&gt;&#x27;</span></pre></div></div>
            
        </li>
        
        
        <li id="section-33">
            <div class="annotation">
              
              <div class="sswrap ">
                <a class="ss" href="#section-33">&#x00a7;</a>
              </div>
              <p>Since the REPL compilation path is unique (in <code>eval</code> above), we need
another way to get the <code>options</code> object attached to a module so that
it knows later on whether it needs to be transpiled. In the case of
the REPL, the only applicable option is <code>transpile</code>.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>      Module = <span class="hljs-built_in">require</span> <span class="hljs-string">&#x27;module&#x27;</span>
      originalModuleLoad = Module::load
      Module::load = <span class="hljs-function"><span class="hljs-params">(filename)</span> -&gt;</span>
        @options = transpile: transpile.options
        originalModuleLoad.call @, filename
    opts = merge replDefaults, opts
    repl = nodeREPL.start opts
    runInContext opts.prelude, repl.context, <span class="hljs-string">&#x27;prelude&#x27;</span> <span class="hljs-keyword">if</span> opts.prelude
    repl.<span class="hljs-literal">on</span> <span class="hljs-string">&#x27;exit&#x27;</span>, <span class="hljs-function">-&gt;</span> repl.outputStream.write <span class="hljs-string">&#x27;\n&#x27;</span> <span class="hljs-keyword">if</span> <span class="hljs-keyword">not</span> repl.closed
    addMultilineHandler repl
    addHistory repl, opts.historyFile, opts.historyMaxInputSize <span class="hljs-keyword">if</span> opts.historyFile</pre></div></div>
            
        </li>
        
        
        <li id="section-34">
            <div class="annotation">
              
              <div class="sswrap ">
                <a class="ss" href="#section-34">&#x00a7;</a>
              </div>
              <p>Adapt help inherited from the node REPL</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    repl.commands[getCommandId(repl, <span class="hljs-string">&#x27;load&#x27;</span>)].help = <span class="hljs-string">&#x27;Load code from a file into this REPL session&#x27;</span>
    repl</pre></div></div>
            
        </li>
        
    </ul>
  </div>
</body>
</html>