Move debug builtins to separate file.

This commit is contained in:
Qi Xiao 2018-02-02 22:12:13 -08:00
parent cc861528de
commit 33584b00f5
2 changed files with 53 additions and 43 deletions

View File

@ -8,7 +8,6 @@ import (
"math/rand"
"net"
"path/filepath"
"runtime"
"time"
"unsafe"
@ -75,12 +74,6 @@ func init() {
{"esleep", sleep},
{"-time", _time},
// Debugging
{"src", src},
{"-gc", _gc},
{"-stack", _stack},
{"-log", _log},
{"-ifaddrs", _ifaddrs},
})
// For rand and randint.
@ -157,42 +150,6 @@ func _time(ec *Frame, args []interface{}, opts map[string]interface{}) {
fmt.Fprintln(ec.ports[1].File, dt)
}
func src(ec *Frame, args []interface{}, opts map[string]interface{}) {
TakeNoArg(args)
TakeNoOpt(opts)
ec.OutputChan() <- ec.srcMeta
}
func _gc(ec *Frame, args []interface{}, opts map[string]interface{}) {
TakeNoArg(args)
TakeNoOpt(opts)
runtime.GC()
}
func _stack(ec *Frame, args []interface{}, opts map[string]interface{}) {
TakeNoArg(args)
TakeNoOpt(opts)
out := ec.ports[1].File
// XXX dup with main.go
buf := make([]byte, 1024)
for runtime.Stack(buf, true) == cap(buf) {
buf = make([]byte, cap(buf)*2)
}
out.Write(buf)
}
func _log(ec *Frame, args []interface{}, opts map[string]interface{}) {
var fnamev string
ScanArgs(args, &fnamev)
fname := fnamev
TakeNoOpt(opts)
maybeThrow(util.SetOutputFile(fname))
}
func _ifaddrs(ec *Frame, args []interface{}, opts map[string]interface{}) {
TakeNoArg(args)
TakeNoOpt(opts)

53
eval/builtin_fn_debug.go Normal file
View File

@ -0,0 +1,53 @@
package eval
import (
"runtime"
"github.com/elves/elvish/util"
)
func init() {
addToBuiltinFns([]*BuiltinFn{
// Debugging
{"src", src},
{"-gc", _gc},
{"-stack", _stack},
{"-log", _log},
})
}
func src(ec *Frame, args []interface{}, opts map[string]interface{}) {
TakeNoArg(args)
TakeNoOpt(opts)
ec.OutputChan() <- ec.srcMeta
}
func _gc(ec *Frame, args []interface{}, opts map[string]interface{}) {
TakeNoArg(args)
TakeNoOpt(opts)
runtime.GC()
}
func _stack(ec *Frame, args []interface{}, opts map[string]interface{}) {
TakeNoArg(args)
TakeNoOpt(opts)
out := ec.ports[1].File
// XXX dup with main.go
buf := make([]byte, 1024)
for runtime.Stack(buf, true) == cap(buf) {
buf = make([]byte, cap(buf)*2)
}
out.Write(buf)
}
func _log(ec *Frame, args []interface{}, opts map[string]interface{}) {
var fnamev string
ScanArgs(args, &fnamev)
fname := fnamev
TakeNoOpt(opts)
maybeThrow(util.SetOutputFile(fname))
}