-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathrepl_luajit.go
670 lines (606 loc) · 15.3 KB
/
repl_luajit.go
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
package compiler
import (
"bufio"
"bytes"
"fmt"
"io"
"os"
"runtime"
"strconv"
"strings"
"time"
"github.com/gijit/gi/pkg/front"
"github.com/gijit/gi/pkg/verb"
golua "github.com/glycerine/golua/lua"
)
var p = verb.P
// Arrange that main.main runs on main thread.
// We want LuaJIT to always be on the one
// main C thread.
func init() {
runtime.LockOSThread()
}
// the single queue of work to run in main thread.
var mainQ = make(chan func())
var mainShutdown = make(chan bool)
// doMain runs f on the main thread.
func doMainWait(f func()) {
done := make(chan bool, 1)
mainQ <- func() {
f()
done <- true
}
<-done
}
func doMainAsync(f func()) {
mainQ <- func() {
f()
}
}
// Main runs the main LuaJIT service loop.
// The binary's main.main must call LuajitMain() to run this loop.
// Main does not return. If the binary needs to do other work, it
// must do it in separate goroutines.
func MainCThread() {
for {
select {
case <-mainShutdown:
return
case f := <-mainQ:
f()
}
}
}
func (cfg *GIConfig) LuajitMain() {
if reserveMainThread {
done := make(chan bool)
var r *Repl
go func() {
r = NewRepl(cfg)
// in place of defer to cleanup:
go func() {
<-done
r.lvm.Close()
close(mainShutdown)
}()
r.Loop()
done <- true
}()
// this is the C main thread, for LuaJIT
// to always and only be on... it never
// returns. Use doMain() to communicate
// with it via mainQ.
MainCThread()
} else {
r := NewRepl(cfg)
defer func() {
r.lvm.Close()
close(mainShutdown)
}()
r.Loop()
}
}
type Repl struct {
inc *IncrState
vmCfg *GIConfig
lvm *LuaVm
t0 time.Time
t1 time.Time
history []string
home string
histFn string
histFile *os.File
sessionStartAfter int
goPrompt string
goMorePrompt string
luaPrompt string
calcPrompt string
isDo bool
isSource bool
prompter *Prompter
cfg *GIConfig
prompt string
prevSrc string
prompterLine string
reader *bufio.Reader
}
func NewRepl(cfg *GIConfig) *Repl {
// give a config so it knows we are
// running under `gi` cmd. Otherwise
// tests will assume nil means they
// need to start a new goroutine for
// non main work.
lvm, err := NewLuaVmWithPrelude(cfg)
panicOn(err)
inc := NewIncrState(lvm, cfg)
r := &Repl{cfg: cfg, lvm: lvm, inc: inc}
r.home = os.Getenv("HOME")
if r.home != "" {
r.histFn = r.home + string(os.PathSeparator) + ".gijit.hist"
// open and close once to read back history
r.history, err = readHistory(r.histFn)
lh := len(r.history)
if lh > 0 {
r.sessionStartAfter = lh
}
panicOn(err)
// re-open for append new history
r.histFile, err = os.OpenFile(r.histFn,
os.O_WRONLY|os.O_CREATE|os.O_APPEND|os.O_SYNC,
0600)
panicOn(err)
}
r.reader = bufio.NewReader(os.Stdin)
r.goPrompt = "gi> "
r.calcPrompt = "calc mode> "
//r.goMorePrompt = ">>> "
r.luaPrompt = "raw luajit gi> "
r.isDo = false
r.isSource = false
if !r.cfg.NoLiner {
r.prompter = NewPrompter(r.goPrompt)
for i := range r.history {
r.prompter.prompter.AppendHistory(r.history[i])
}
}
r.setPrompt()
r.prevSrc = ""
r.prompterLine = ""
return r
}
func (r *Repl) Loop() {
for {
src, err := r.Read()
if err == io.EOF {
return
}
err = r.Eval(src)
if err == io.EOF {
return
}
}
}
func (r *Repl) Read() (src string, err error) {
var by []byte
readtop:
if r.cfg.NoLiner {
if r.prompt != "" {
fmt.Printf(r.prompt)
}
by, err = r.reader.ReadBytes('\n')
} else {
r.prompterLine, err = r.prompter.Getline(&(r.prompt))
by = []byte(r.prompterLine)
}
if err == io.EOF {
if len(by) > 0 {
fmt.Printf("\n on EOF, but len(by) = %v, by='%s'", len(by), string(by))
// process bytes first,
// return next time.
return
} else {
fmt.Printf("[EOF]\n")
return "", err
}
}
panicOn(err)
use := string(by)
src = use
cmd := bytes.TrimSpace(by)
low := string(bytes.ToLower(cmd))
if len(low) > 1 && low[0] == ':' {
if low[:2] == "::" {
// likely the start of a lua label for a goto, not a special : command.
return
}
if low[1] == '-' || (low[1] >= '0' && low[1] <= '9') {
// replay history, one command, or a range.
// check for range
num, err := getHistoryRange(low[1:], r.history)
if err != nil {
fmt.Printf("%s\n", err.Error())
return "", err
}
switch len(num) {
case 1:
fmt.Printf("replay history %03d:\n", num[0])
src = r.history[num[0]-1]
fmt.Printf("%s\n", src)
case 2:
if num[1] < num[0] {
fmt.Printf("bad history request, end before beginning.\n")
return "", nil
}
fmt.Printf("replay history %03d - %03d:\n", num[0], num[1])
src = strings.Join(r.history[num[0]-1:num[1]], "\n") + "\n"
fmt.Printf("%s\n", src)
}
}
}
if len(low) > 3 && low[:3] == ":rm" {
// remove some commands from history
var beg, end int
r.history, r.histFile, beg, end, err = removeCommands(r.history, r.histFn, r.histFile, low[3:])
if err != nil {
fmt.Printf("%s\n", err.Error())
}
if end >= 0 {
delcount := (end - beg + 1)
if end < r.sessionStartAfter {
// deleted history before our session, adjust marker
r.sessionStartAfter -= delcount
} else if beg < r.sessionStartAfter {
// delete history crosses into our session
r.sessionStartAfter = beg
}
}
return "", nil
}
switch low {
case ":ast":
r.inc.PrintAST = true
return "", nil
case ":noast":
r.inc.PrintAST = false
return "", nil
case ":q":
fmt.Printf("quiet mode\n")
verb.Verbose = false
verb.VerboseVerbose = false
return "", nil
case ":v":
fmt.Printf("verbose mode.\n")
verb.Verbose = true
verb.VerboseVerbose = false
return "", nil
case ":vv":
fmt.Printf("very verbose mode.\n")
verb.Verbose = true
verb.VerboseVerbose = true
return "", nil
case ":clear", ":reset":
r.history = r.history[:0]
if r.histFn != "" {
r.histFile, err = os.OpenFile(r.histFn,
os.O_WRONLY|os.O_CREATE|os.O_TRUNC|os.O_SYNC,
0600)
panicOn(err)
}
r.sessionStartAfter = 0
fmt.Printf("history cleared.\n")
return "", nil
case ":h":
if len(r.history) == 0 {
fmt.Printf("history: empty\n")
fmt.Printf("----- current session: -----\n")
return "", nil
}
fmt.Printf("history:\n")
newline := "\n"
if r.sessionStartAfter == 0 {
fmt.Printf("----- current session: -----\n")
}
for i, h := range r.history {
lenh := len(h)
switch {
case lenh == 0:
newline = "\n"
case h[lenh-1] == '\n':
newline = ""
default:
newline = "\n"
}
fmt.Printf("%03d: %s%s", i+1, h, newline)
if i+1 == r.sessionStartAfter {
fmt.Printf("----- current session: -----\n")
}
}
fmt.Printf("\n")
return "", nil
case ":ls":
r.displayCmd(`ls`)
goto readtop
case ":gls":
r.displayCmd(`gls`)
goto readtop
case ":glst":
r.displayCmd(`glst`)
goto readtop
case ":lst":
r.displayCmd(`lst`)
goto readtop
case ":stacks":
showLuaStacks(r.lvm.vm)
goto readtop
case ":r":
r.cfg.RawLua = true
r.cfg.CalculatorMode = false
r.prompt = r.luaPrompt
fmt.Printf("Raw LuaJIT language mode.\n")
goto readtop
case ":go", ":g", ":":
r.cfg.RawLua = false
r.cfg.CalculatorMode = false
r.prompt = r.goPrompt
fmt.Printf("Go language mode.\n")
return "", nil
case "==":
r.cfg.RawLua = false
r.cfg.CalculatorMode = true
fmt.Printf("Calculator mode.\n")
r.prompt = r.calcPrompt
return "", nil
case ":prelude", ":reload":
fmt.Printf("Reloading prelude...\n")
files, err := FetchPreludeFilenames(r.cfg.PreludePath, r.cfg.Quiet)
if err != nil {
fmt.Printf("error during prelude reload: '%v'", err)
return "", err
}
err = LuaDoPreludeFiles(r.lvm, files)
if err != nil {
fmt.Printf("error during prelude reload: '%v'", err)
}
return "", nil
case ":help", ":?":
fmt.Printf(`
======================
gijit: a go interpreter, just-in-time
https://github.com/gijit/gi
Type Go expressions or statements
directly after the prompt, or use one of
these special commands.
======================
:v Turn on verbose debug printing.
:vv Turn on very verbose printing.
:q Quiet the debug prints (default).
:r Change to raw-luajit Lua entry mode.
:g or :go Change back from raw to default Go mode.
:ast Print the Go AST prior to translation.
:noast Stop printing the Go AST.
:? Show this help (:help does the same).
:h Show command line history.
:30 Replay command number 30 from history.
:1-10 Replay commands 1 - 10 inclusive.
:reset Reset and clear history (also :clear).
:rm 3-4 Remove commands 3-4 from history.
:do <path> Run dofile(path) on a .lua file.
:source <path> Re-play Go code from a file.
:ls List all global user variables.
:gls List all global variables (include __ prefixed).
:stacks Show lua stacks for each coroutine.
= 3 + 4 Calculate the expression after the '=' (one line).
== Multiple entry calculator mode. ':' to exit.
import "fmt" Import the binary, pre-compiled package.
ctrl-d to exit History is saved in ~/.gitit.hist
`)
return "", nil
}
r.isDo = strings.HasPrefix(low, ":do")
r.isSource = strings.HasPrefix(low, ":source")
if r.isDo || r.isSource {
off := 3
action := "running dofile"
nm := "dofiles"
if r.isSource {
off = 7
action = "sourcing Go from"
nm = "source Go files"
}
files := strings.TrimSpace(low[off:])
splt := strings.Split(files, ",")
var final, show []string
for i := range splt {
tmp := strings.TrimSpace(splt[i])
home := os.Getenv("HOME")
if home != "" {
tmp = strings.Replace(tmp, "~/", home+"/", 1)
}
if len(tmp) > 0 {
final = append(final, tmp)
show = append(show, strconv.Quote(tmp))
}
}
var err error
if len(final) > 0 {
fmt.Printf("%s (%s)\n", nm, strings.Join(show, ","))
if r.isDo {
err = LuaDoUserFiles(r.lvm, final)
} else {
by, err = sourceGoFiles(final)
if err != nil {
fmt.Printf("error during %s: '%v'\n", action, err)
} else {
src = string(by)
return src, nil
}
}
if err != nil {
fmt.Printf("error during %s: '%v'\n", action, err)
}
} else {
fmt.Printf("nothing to do.\n")
}
return "", nil
}
return src, nil
}
func (r *Repl) setPrompt() {
if r.cfg.CalculatorMode {
r.prompt = r.calcPrompt
return
}
if r.cfg.RawLua {
r.prompt = r.luaPrompt
return
}
r.prompt = r.goPrompt
}
func (r *Repl) Eval(src string) error {
var use string
isContinuation := len(r.prevSrc) > 0
if !r.cfg.RawLua {
if isContinuation {
src = r.prevSrc + "\n" + src
}
//fmt.Printf("src = '%s'\n", src)
//fmt.Printf("prevSrc = '%s'\n", prevSrc)
eof, syntaxErr, empty, err := front.TopLevelParseGoSource([]byte(src))
if empty {
r.prevSrc = ""
return nil
}
//fmt.Printf("eof = %v, syntaxErr = %v\n", eof, syntaxErr)
if eof && !syntaxErr {
r.prompt = r.goMorePrompt
// get another line of input
r.prevSrc = src
return nil
}
r.prevSrc = ""
r.setPrompt()
translation, err := TranslateAndCatchPanic(r.inc, []byte(src))
if err != nil {
fmt.Printf("oops: '%v' on input '%s'\n", err, strings.TrimSpace(src))
translation = "\n"
// still write, so we get another prompt
// hmm, or maybe not
return err
} else {
p("got translation of line from Go into lua: '%s'\n", strings.TrimSpace(string(translation)))
}
use = translation
} else {
// raw mode, under :r
use = src
}
p("sending use='%v'\n", use)
// add to history as separate lines
srcLines := strings.Split(src, "\n")
//fmt.Printf("appending to history: src='%#v', srcLines='%#v'\n", src, srcLines)
lensrc := len(srcLines)
histBeg := len(r.history)
if lensrc > 1 && strings.TrimSpace(srcLines[lensrc-1]) == "" {
r.history = append(r.history, srcLines[:lensrc-1]...)
} else {
r.history = append(r.history, srcLines[:len(srcLines)]...)
}
histEnd := len(r.history)
if r.histFile != nil {
for i := histBeg; i < histEnd; i++ {
fmt.Fprintf(r.histFile, "%s\n", r.history[i])
}
r.histFile.Sync()
}
r.t0 = time.Now()
useEval := !r.cfg.RawLua
err := LuaRun(r.lvm, use, useEval)
if err != nil {
fmt.Printf("error from LuaRun: supplied lua with: '%s'\nlua stack:\n%v\n", use[:len(use)-1], err)
return nil
}
r.t1 = time.Now()
fmt.Printf("\n")
r.reader.Reset(os.Stdin)
fmt.Printf("elapsed: '%v'\n", r.t1.Sub(r.t0))
return nil
}
// :ls, :gls, :lst, :glst implementation
func (r *Repl) displayCmd(cmd string) {
err := LuaRun(r.lvm, `__`+cmd+`()`, true)
panicOn(err)
}
func showLuaStacks(vm *golua.State) {
top := vm.GetTop()
vm.GetGlobal("__all_coro")
if vm.IsNil(-1) {
panic("could not locate __all_coro in _G")
}
fmt.Printf("\n")
forEachAllCoroArrayValue(vm, -1, func(i int, name, status string) {
ignoreTopmost := 1
if i == 1 {
if name != "main" {
panic(fmt.Sprintf("name should have been main for i == 1 thread! not '%s'", name))
}
// main thread is where we are iterating from,
// so it has value, key, __all_coro, __all_coro.
// Ignore that administrivia, its not interesting.
ignoreTopmost = 5
}
thr := vm.ToThread(-1)
fmt.Printf("===================================\n")
fmt.Printf(" __all_coro %v: '%s' (%s)\n", i, name, status)
fmt.Printf("===================================\n"+
"%s\n", DumpLuaStackAsString(thr, ignoreTopmost))
})
vm.SetTop(top)
}
// Call f with each __all_coro array value in term on the top of
// the stack, the f(i, name) call will have i set to 1, 2, 3, ...
// in turn.
func forEachAllCoroArrayValue(L *golua.State, index int, f func(i int, name, status string)) {
i := 1
// Push another reference to the table on top of the stack (so we know
// where it is, and this function can work for negative, positive and
// pseudo indices.
L.PushValue(index)
// stack now contains: -1 => table
L.PushNil()
// stack now contains: -1 => nil; -2 => table
for L.Next(-2) != 0 {
// stack now contains: -1 => value; -2 => key; -3 => table
L.PushValue(-1)
// stack: value, value, key, table
// get name from __coro2notes
L.GetGlobal("__coro2notes")
// stack: __coro2notes, value, value, key, table
L.Insert(-2)
// stack: value, __coro2notes, value, key, table
L.GetTable(-2)
// stack: details, __coro2notes, value, key, table
L.PushString("__name")
// stack: "__name", details, __coro2notes, value, key, table
L.GetTable(-2)
// stack: threadName, details, __coro2notes, value, key, table
if L.IsNil(-1) {
panic("thread did not have a name?!?")
}
name := L.ToString(-1)
L.Pop(3)
// stack: value, key, table
status := getCoroutineStatus(L, -1)
// stack: value, key, table
f(i, name, status)
// pop value, leaving original key
L.Pop(1)
// stack: key, table
i++
}
// stack now contains: -1 => table (when lua_next returns 0 it pops the key
// but does not push anything.)
// Pop table
L.Pop(1)
// Stack is now the same as it was on entry to this function
return
}
func getCoroutineStatus(L *golua.State, index int) (status string) {
top := L.GetTop()
L.PushValue(index)
// stack: thread
L.GetGlobal("coroutine")
// stack: coroutine table, thread
L.PushString("status")
// stack: "status", coroutine table, thread
L.GetTable(-2)
// stack: status function, coroutine table, thread
L.Remove(-2)
// stack: status function, thread
L.Insert(-2)
// stack: thread, status function
L.Call(1, 1)
// stack: status string
status = L.ToString(-1)
L.SetTop(top)
return
}