-
-
Notifications
You must be signed in to change notification settings - Fork 48
/
starlark.go
164 lines (137 loc) · 3.64 KB
/
starlark.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
package main
import (
"fmt"
"io"
"io/ioutil"
"os"
"go.starlark.net/starlark"
"github.com/aarzilli/gdlv/internal/dlvclient/service/api"
"github.com/aarzilli/gdlv/internal/dlvclient/service/rpc2"
"github.com/aarzilli/gdlv/internal/starbind"
)
var StarlarkEnv = starbind.New(starlarkContext{})
type starlarkContext struct{}
func (s starlarkContext) Client() *rpc2.RPCClient {
return client
}
func (s starlarkContext) RegisterCallback(name, helpMsg string, fn func(args string) (starlark.Value, error)) {
cmdfn := func(out io.Writer, args string) error {
defer refreshState(refreshToFrameZero, clearStop, nil)
v, err := fn(args)
if err != nil {
return err
}
fmt.Fprintf(out, "%v\n", v)
return nil
}
found := false
for i := range cmds.cmds {
cmd := &cmds.cmds[i]
for _, alias := range cmd.aliases {
if alias == name {
cmd.cmdFn = cmdfn
cmd.helpMsg = helpMsg
found = true
break
}
}
if found {
break
}
}
if !found {
newcmd := command{
aliases: []string{name},
helpMsg: helpMsg,
cmdFn: cmdfn,
group: scriptCmds,
}
cmds.cmds = append(cmds.cmds, newcmd)
}
}
func (s starlarkContext) CallCommand(cmdstr string) error {
defer wnd.Changed()
out := editorWriter{true}
cmdstr, args := parseCommand(cmdstr)
return cmds.Call(cmdstr, args, &out)
}
func (s starlarkContext) Scope() api.EvalScope {
return currentEvalScope()
}
func (s starlarkContext) LoadConfig() api.LoadConfig {
return getVariableLoadConfig()
}
const defaultInitFile = `
def command_find_array(arr, pred):
"""Calls pred for each element of the array or slice 'arr' returns the index of
the first element for which pred returns true.
find_array <arr>, <pred>
Example use:
find_array "s2", lambda x: x.A == 5
"""
arrv = eval(None, arr).Variable
for i in range(0, arrv.Len):
v = arrv.Value[i]
if pred(v):
print("found", i)
return
print("not found")
def Ll(var_name, next_field_name, max_depth):
v = eval(None, var_name).Variable.Value
r = []
for i in range(0, max_depth):
r.append(v)
if v[0] == None:
break
v = v[next_field_name]
return r
def command_linked_list(args):
"""Prints the contents of a linked list.
linked_list <var_name> <next_field_name> <max_depth>
Prints up to max_depth elements of the linked list variable 'var_name' using 'next_field_name' as the name of the link field.
"""
var_name, next_field_name, max_depth = args.split(" ")
max_depth = int(max_depth)
next_name = var_name
v = Ll(var_name, next_field_name, max_depth)
for i in range(len(v)):
print(str(i)+":", v)
def command_flaky(args):
"Continues and restarts the target program repeatedly, until a breakpoint is hit"
count = 1
while True:
if dlv_command("continue") == None:
break
print("restarting", count, "...")
count = count+1
restart(Rerecord=True)
`
func executeInit() {
scrollbackOut := editorWriter{true}
initPath := configLoc() + ".star"
fmt.Fprintf(&scrollbackOut, "Loading init file %q...", initPath)
fh, err := os.Open(initPath)
if err != nil {
if !os.IsNotExist(err) {
fmt.Fprintf(&scrollbackOut, "could not read init file: %q: %v", initPath, err)
return
}
err := ioutil.WriteFile(initPath, []byte(defaultInitFile), 0660)
if err != nil {
fmt.Fprintf(&scrollbackOut, "could not create init file: %q: %v\n", initPath, err)
return
}
fh, err = os.Open(initPath)
if err != nil {
fmt.Fprintf(&scrollbackOut, "could not read init file: %q: %v\n", initPath, err)
return
}
}
fh.Close()
_, err = StarlarkEnv.Execute(&scrollbackOut, initPath, nil, "main", nil, nil)
if err != nil {
fmt.Fprintf(&scrollbackOut, "\n%v\n", err)
return
}
fmt.Fprintf(&scrollbackOut, "done\n")
}