2018-02-03 14:12:13 +08:00
|
|
|
package eval
|
|
|
|
|
|
|
|
import (
|
|
|
|
"runtime"
|
|
|
|
|
2021-01-27 09:28:38 +08:00
|
|
|
"src.elv.sh/pkg/logutil"
|
|
|
|
"src.elv.sh/pkg/parse"
|
2018-02-03 14:12:13 +08:00
|
|
|
)
|
|
|
|
|
2020-08-17 01:46:29 +08:00
|
|
|
func init() {
|
2022-03-20 23:50:25 +08:00
|
|
|
addBuiltinFns(map[string]any{
|
2020-08-17 01:46:29 +08:00
|
|
|
"src": src,
|
|
|
|
"-gc": _gc,
|
|
|
|
"-stack": _stack,
|
|
|
|
"-log": _log,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func src(fm *Frame) parse.Source {
|
|
|
|
return fm.srcMeta
|
|
|
|
}
|
|
|
|
|
|
|
|
func _gc() {
|
|
|
|
runtime.GC()
|
|
|
|
}
|
|
|
|
|
2021-06-12 05:20:27 +08:00
|
|
|
func _stack(fm *Frame) error {
|
2020-08-09 11:42:32 +08:00
|
|
|
// TODO(xiaq): Dup with main.go.
|
2018-02-03 14:12:13 +08:00
|
|
|
buf := make([]byte, 1024)
|
|
|
|
for runtime.Stack(buf, true) == cap(buf) {
|
|
|
|
buf = make([]byte, cap(buf)*2)
|
|
|
|
}
|
2021-06-12 05:20:27 +08:00
|
|
|
_, err := fm.ByteOutput().Write(buf)
|
|
|
|
return err
|
2018-02-03 14:12:13 +08:00
|
|
|
}
|
|
|
|
|
2018-02-04 14:30:36 +08:00
|
|
|
func _log(fname string) error {
|
2020-09-03 12:27:18 +08:00
|
|
|
return logutil.SetOutputFile(fname)
|
2018-02-03 14:12:13 +08:00
|
|
|
}
|