-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
514 lines (462 loc) · 13.8 KB
/
main.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
package main
import (
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
"os/signal"
"path/filepath"
"sync"
"text/template"
"github.com/davidthomas426/goconsole/interp"
"github.com/peterh/liner"
_ "golang.org/x/tools/go/gcimporter"
"golang.org/x/tools/go/types"
"golang.org/x/tools/go/types/typeutil"
)
type Import struct {
LocalName string
Path string
}
type Package struct {
Path string
Name string
Types []Type
Objects []Object
typeMap *typeutil.Map
}
type Type struct {
CheckerType string
UseReflectString bool
TypeString string
ReflectString string
}
type Object struct {
Name string
Qualified string
}
type Interp struct {
Imports []Import
Packages []Package
}
func visitedType(typ types.Type) bool {
return typeMap.At(typ) != nil
}
func (p *Package) AddType(typ types.Type, t Type) {
typeMap.Set(typ, struct{}{})
p.Types = append(p.Types, t)
}
var typeMap = new(typeutil.Map)
func main() {
var cmdError error
defer func() {
fmt.Println()
if cmdError != nil {
os.Exit(1)
}
}()
importSet := map[Import]bool{
Import{Path: "os"}: true,
Import{Path: "fmt"}: true,
Import{Path: "github.com/davidthomas426/goconsole/interp"}: true,
Import{Path: "github.com/peterh/liner"}: true,
Import{LocalName: "_", Path: "golang.org/x/tools/go/gcimporter"}: true,
Import{Path: "golang.org/x/tools/go/types"}: true,
Import{Path: "golang.org/x/tools/go/types/typeutil"}: true,
}
if len(os.Args) >= 2 {
// At least one package to import provided on command line
importSet[Import{Path: "log"}] = true
importSet[Import{Path: "reflect"}] = true
}
pkgNames := make(map[string]bool)
pkgMap := map[string]*types.Package{}
var pkgs []Package
var tpkgs []*types.Package
for _, path := range os.Args[1:] {
var tpkg *types.Package
var err error
if path == "unsafe" {
tpkg = types.Unsafe
} else {
tpkg, err = types.DefaultImport(pkgMap, path)
if err != nil {
log.Fatal(err)
}
}
tpkgs = append(tpkgs, tpkg)
imp := Import{Path: path}
importSet[imp] = true
pkg := Package{
Path: path,
Name: tpkg.Name(),
}
pkgs = append(pkgs, pkg)
// Store the local package name in pkgNames
pkgNames[pkg.Name] = true
}
for i, tpkg := range tpkgs {
for _, name := range tpkg.Scope().Names() {
obj := tpkg.Scope().Lookup(name)
if obj.Exported() {
processObj(&pkgs[i], obj, pkgNames)
}
}
}
imports := make([]Import, 0, len(importSet))
for imp := range importSet {
imports = append(imports, imp)
}
interp := &Interp{
Imports: imports,
Packages: pkgs,
}
workDir, err := ioutil.TempDir("", "goconsole")
if err != nil {
log.Fatal(err)
}
defer os.RemoveAll(workDir)
fn := filepath.Join(workDir, "goconsole.go")
srcFile, err := os.Create(fn)
if err != nil {
log.Panic(err)
}
defer srcFile.Close()
err = interpTmpl.Execute(srcFile, interp)
if err != nil {
log.Panic(err)
}
srcFile.Close()
// Grab the terminal mode and reset it on exit interrupt signal, just in case
mode, err := liner.TerminalMode()
if err != nil {
log.Panic(err)
}
var once sync.Once
resetTerminal := func() {
mode.ApplyMode()
}
go func() {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
<-c
once.Do(resetTerminal)
}()
defer once.Do(resetTerminal)
cmd := exec.Command("go", "run", fn)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Stdin = os.Stdin
cmdError = cmd.Run()
}
func processObj(pkg *Package, obj types.Object, pkgNames map[string]bool) {
switch obj := obj.(type) {
case *types.TypeName:
if obj.Exported() {
cts := fmt.Sprintf("scope.Lookup(%q).Type()", obj.Name())
processType(pkg, obj.Type(), cts, "", false, pkgNames)
}
case *types.Func, *types.Var:
processVar(pkg, obj, pkgNames)
}
}
func addType(pkg *Package, typ types.Type, checkerTypeStr string, reflectTypeStr string, useReflectStr bool) {
if visitedType(typ) {
return
}
t := Type{
CheckerType: checkerTypeStr,
TypeString: interp.TypeString(typ),
ReflectString: reflectTypeStr,
UseReflectString: useReflectStr,
}
pkg.AddType(typ, t)
}
func processType(pkg *Package, typ types.Type, checkerTypeStr string, reflectTypeStr string,
useReflectString bool, pkgNames map[string]bool) {
if visitedType(typ) {
return
}
index := len(pkg.Types)
// First, add the type itself
addType(pkg, typ, checkerTypeStr, reflectTypeStr, useReflectString)
// If it's a (nameless) channel type, add the other two directions
switch typ := typ.(type) {
case *types.Chan:
// Add the other two directions of this channel type
tBoth := types.NewChan(types.SendRecv, typ.Elem())
tSend := types.NewChan(types.SendOnly, typ.Elem())
tRecv := types.NewChan(types.RecvOnly, typ.Elem())
chanTypes := []*types.Chan{tBoth, tSend, tRecv}
typesDirs := []string{"types.SendRecv", "types.SendOnly", "types.RecvOnly"}
reflectDirs := []string{"reflect.BothDir", "reflect.SendDir", "reflect.RecvDir"}
for i, t := range chanTypes {
if types.Identical(t, typ) {
continue
}
tdir := typesDirs[i]
rdir := reflectDirs[i]
cts := fmt.Sprintf("types.NewChan(%s, t%d.(*types.Chan).Elem())", tdir, index)
rts := fmt.Sprintf("reflect.ChanOf(%s, rt%d.Elem())", rdir, index)
addType(pkg, t, cts, rts, true)
}
}
// Recursively process components of the type
undTyp := typ.Underlying()
switch undTyp := undTyp.(type) {
case *types.Array:
// Process element type
et := undTyp.Elem()
ects := fmt.Sprintf("t%d.Underlying().(*types.Array).Elem()", index)
erts := fmt.Sprintf("rt%d.Elem()", index)
processType(pkg, et, ects, erts, true, pkgNames)
case *types.Basic:
// Nothing to do
case *types.Chan:
// Process element type
et := undTyp.Elem()
ects := fmt.Sprintf("t%d.Underlying().(*types.Chan).Elem()", index)
erts := fmt.Sprintf("rt%d.Elem()", index)
processType(pkg, et, ects, erts, true, pkgNames)
case *types.Interface:
// Nothing to do
case *types.Map:
// Process key and element types
et := undTyp.Elem()
ects := fmt.Sprintf("t%d.Underlying().(*types.Map).Elem()", index)
erts := fmt.Sprintf("rt%d.Elem()", index)
processType(pkg, et, ects, erts, true, pkgNames)
kt := undTyp.Key()
kcts := fmt.Sprintf("t%d.Underlying().(*types.Map).Key()", index)
krts := fmt.Sprintf("rt%d.Key()", index)
processType(pkg, kt, kcts, krts, true, pkgNames)
case *types.Named:
log.Fatal("what kind of type has an underlying type that's a *types.Named?!")
case *types.Pointer:
// Process element type
et := undTyp.Elem()
ects := fmt.Sprintf("t%d.Underlying().(*types.Pointer).Elem()", index)
erts := fmt.Sprintf("rt%d.Elem()", index)
processType(pkg, et, ects, erts, true, pkgNames)
case *types.Signature:
// Process parameter types and result types
for i := 0; i < undTyp.Params().Len(); i++ {
pt := undTyp.Params().At(i).Type()
pcts := fmt.Sprintf("t%d.Underlying().(*types.Signature).Params().At(%d).Type()", index, i)
prts := fmt.Sprintf("rt%d.In(%d)", index, i)
processType(pkg, pt, pcts, prts, true, pkgNames)
}
for i := 0; i < undTyp.Results().Len(); i++ {
rt := undTyp.Results().At(i).Type()
rcts := fmt.Sprintf("t%d.Underlying().(*types.Signature).Results().At(%d).Type()", index, i)
rrts := fmt.Sprintf("rt%d.Out(%d)", index, i)
processType(pkg, rt, rcts, rrts, true, pkgNames)
}
case *types.Slice:
// Process element type
et := undTyp.Elem()
ects := fmt.Sprintf("t%d.Underlying().(*types.Slice).Elem()", index)
erts := fmt.Sprintf("rt%d.Elem()", index)
processType(pkg, et, ects, erts, true, pkgNames)
case *types.Struct:
// Process exported field types
for i := 0; i < undTyp.NumFields(); i++ {
f := undTyp.Field(i)
if f.Exported() {
ft := undTyp.Field(i).Type()
fcts := fmt.Sprintf("t%d.Underlying().(*types.Struct).Field(%d).Type()", index, i)
frts := fmt.Sprintf("rt%d.Field(%d).Type", index, i) // TODO: FIX THIS LINE
processType(pkg, ft, fcts, frts, true, pkgNames)
}
}
}
// TODO: Add (or process?) the method type for each method in the type's method set
// What does "the method type" mean?
// Suppose we have the following definitions:
//
// type MyInt int
//
// func (n MyInt) Foo(x MyInt) MyInt {
// return n + x
// }
//
// var n = MyInt(17)
//
// Then we can obtain a function type representing this method in a few ways:
// 1) MyInt.Foo
// -> func(MyInt, MyInt) MyInt
// 2) (*MyInt).Foo
// -> func(*MyInt, MyInt) MyInt
// 3) n.Foo
// -> func(MyInt) MyInt
// 4) (&n).Foo (ditto)
// -> func(MyInt) MyInt
//
// Method expressions (1) and (2) are obtainable because the type is writable.
// Method values (3) and (4) are obtainable because the type is obtainable.
// Also note that (3) and (4) have the same type, so processing both in this way is redundant
// but not harmful.
//
// The reflect package gives us a way to get most of these easily.
// reflect.Type has "Method" and "MethodByName" methods that return (1) (or (2) if called on the ptr type).
// -> This is true unless typ is an interface type. If it's an interface type, then these
// methods on reflect.Type give the same type as (3) and (4). In that case, we can't get
// our hands on the method type as a reflect.Type to put in the typemap, but if we don't
// pick it up somewhere else (externally obtainable), we can simulate it easily with a closure.
//
// reflect.Value has "Method" and "MethodByName" methods that return (3) or (4).
//
// If underlying type is writable, add it, too
// (we don't need to process it recursively because it has the same components as the current type,
// and either no methods for non-interface types or the same methods for interface types)
if isWritable(undTyp, pkgNames) {
// Since it's writable, we don't need to pass a reflectTypeStr
cts := checkerTypeStr + ".Underlying()"
addType(pkg, undTyp, cts, "", false)
}
}
func processVar(pkg *Package, obj types.Object, pkgNames map[string]bool) {
if !obj.Exported() {
return
}
// Add an Object to pkg.Objects
o := Object{
Name: obj.Name(),
Qualified: pkg.Name + "." + obj.Name(),
}
pkg.Objects = append(pkg.Objects, o)
if typ := obj.Type(); !visitedType(typ) {
cts := fmt.Sprintf("scope.Lookup(%q).Type()", o.Name)
rts := fmt.Sprintf("reflect.TypeOf(%s)", o.Qualified)
processType(pkg, obj.Type(), cts, rts, true, pkgNames)
}
}
func isWritable(typ types.Type, pkgNames map[string]bool) bool {
return !hasUnexportedType(typ, pkgNames)
}
func hasUnexportedType(typ types.Type, pkgNames map[string]bool) bool {
switch typ := typ.(type) {
case *types.Array:
return hasUnexportedType(typ.Elem(), pkgNames)
case *types.Basic:
return false
case *types.Chan:
return hasUnexportedType(typ.Elem(), pkgNames)
case *types.Interface:
// If I can write the embedded interfaces and the explicit methods, I can write the interface
for i := 0; i < typ.NumEmbeddeds(); i++ {
if hasUnexportedType(typ.Embedded(i), pkgNames) {
return true
}
}
for i := 0; i < typ.NumExplicitMethods(); i++ {
if hasUnexportedType(typ.ExplicitMethod(i).Type(), pkgNames) {
return true
}
}
return false
case *types.Map:
return hasUnexportedType(typ.Key(), pkgNames) || hasUnexportedType(typ.Elem(), pkgNames)
case *types.Named:
pkg := typ.Obj().Pkg()
return pkg != nil && (!pkgNames[pkg.Name()] || !typ.Obj().Exported())
case *types.Pointer:
return hasUnexportedType(typ.Elem(), pkgNames)
case *types.Signature:
// If I can write the parameters and the results, I can write the signature
for i := 0; i < typ.Params().Len(); i++ {
if hasUnexportedType(typ.Params().At(i).Type(), pkgNames) {
return true
}
}
for i := 0; i < typ.Results().Len(); i++ {
if hasUnexportedType(typ.Results().At(i).Type(), pkgNames) {
return true
}
}
return false
case *types.Slice:
return hasUnexportedType(typ.Elem(), pkgNames)
case *types.Struct:
// If I can write the fields, I can write the struct
for i := 0; i < typ.NumFields(); i++ {
if hasUnexportedType(typ.Field(i).Type(), pkgNames) {
return true
}
}
return false
}
return false
}
var interpTmpl = template.Must(template.New("interp").Parse(interpStr))
var interpStr = `
package main
import ({{range .Imports}}
{{with .LocalName}}{{.}} {{end}}{{printf "%q" .Path}}{{end}}
)
func main() {
// Silence errors about not using reflect package
{{if .Packages}}_ = reflect.ValueOf{{end}}
pkgMap := map[string]*types.Package{}
typeMap := new(typeutil.Map)
pkgs := []*interp.Package{}
{{range .Packages}}
{
{{if (eq "unsafe" .Path)}}tpkg := types.Unsafe
_ = log.Fatal
{{else}}tpkg, err := types.DefaultImport(pkgMap, {{printf "%q" .Path}})
if err != nil {
log.Fatal(err)
}
{{end}}scope := tpkg.Scope()
_ = scope
pkg := &interp.Package{
Name: {{printf "%q" .Name}},
Pkg: tpkg,
Objs: map[string]interp.Object{},
}
pkgs = append(pkgs, pkg)
{{range $index, $typ := .Types}}
t{{$index}} := {{$typ.CheckerType}}
{{if (not $typ.UseReflectString)}}var pv{{$index}} *{{$typ.TypeString}}
rt{{$index}} := reflect.TypeOf(pv{{$index}}).Elem()
{{else}}rt{{$index}} := {{$typ.ReflectString}}
{{end}}typeMap.Set(t{{$index}}, rt{{$index}})
{{end}}
{{range .Objects}}
pkg.Objs[{{printf "%q" .Name}}] = interp.Object{
Value: reflect.ValueOf({{.Qualified}}),
Typ: scope.Lookup({{printf "%q" .Name}}).Type(),
}
{{end}}
}
{{end}}
interp := interp.NewInterpreter(pkgs, pkgMap, typeMap)
var lerr error
defer func() {
if lerr == liner.ErrPromptAborted {
os.Exit(2)
}
}()
line := liner.NewLiner()
defer line.Close()
line.SetCtrlCAborts(true)
src, lerr := line.Prompt(">>> ")
for lerr == nil {
incomplete, err := interp.Run(src)
if err != nil {
fmt.Println(err)
break
}
if src != "" {
line.AppendHistory(src)
}
if incomplete {
src, lerr = line.Prompt("... ")
} else {
src, lerr = line.Prompt(">>> ")
}
}
}
`