Improved REPL output

This commit is contained in:
obscuren 2014-05-19 16:32:45 +02:00
parent 16421106d4
commit 017bbbb582
5 changed files with 89 additions and 12 deletions

View File

@ -15,16 +15,15 @@ import (
const Debug = true const Debug = true
// Register interrupt handlers so we can stop the ethereum func RegisterInterrupt(cb func(os.Signal)) {
func RegisterInterrupts(s *eth.Ethereum) { go func() {
// Buffered chan of one is enough // Buffered chan of one is enough
c := make(chan os.Signal, 1) c := make(chan os.Signal, 1)
// Notify about interrupts for now // Notify about interrupts for now
signal.Notify(c, os.Interrupt) signal.Notify(c, os.Interrupt)
go func() {
for sig := range c { for sig := range c {
fmt.Printf("Shutting down (%v) ... \n", sig) cb(sig)
s.Stop()
} }
}() }()
} }
@ -154,13 +153,20 @@ save these words so you can restore your account later: %s
repl := NewJSRepl(ethereum) repl := NewJSRepl(ethereum)
go repl.Start() go repl.Start()
RegisterInterrupt(func(os.Signal) {
repl.Stop()
})
} }
if StartRpc { if StartRpc {
utils.DoRpc(ethereum, RpcPort) utils.DoRpc(ethereum, RpcPort)
} }
RegisterInterrupts(ethereum) RegisterInterrupt(func(sig os.Signal) {
fmt.Printf("Shutting down (%v) ... \n", sig)
ethereum.Stop()
})
ethereum.Start(UseSeed) ethereum.Start(UseSeed)

View File

@ -11,6 +11,7 @@ import (
type Repl interface { type Repl interface {
Start() Start()
Stop()
} }
type JSRE struct { type JSRE struct {
@ -36,6 +37,9 @@ func NewJSRE(ethereum *eth.Ethereum) *JSRE {
make(map[string][]otto.Value), make(map[string][]otto.Value),
} }
// Init the JS lib
re.vm.Run(jsLib)
// We have to make sure that, whoever calls this, calls "Stop" // We have to make sure that, whoever calls this, calls "Stop"
go re.mainLoop() go re.mainLoop()
@ -113,6 +117,10 @@ func (self *JSRepl) Start() {
self.read() self.read()
} }
func (self *JSRepl) Stop() {
self.re.Stop()
}
func (self *JSRepl) parseInput(code string) { func (self *JSRepl) parseInput(code string) {
defer func() { defer func() {
if r := recover(); r != nil { if r := recover(); r != nil {
@ -126,7 +134,7 @@ func (self *JSRepl) parseInput(code string) {
return return
} }
fmt.Println(value) self.PrintValue(value)
} }
// The JSEthereum object attempts to wrap the PEthereum object and returns // The JSEthereum object attempts to wrap the PEthereum object and returns

46
ethereum/js_lib.go Normal file
View File

@ -0,0 +1,46 @@
package main
const jsLib = `
function pp(object) {
var str = "";
if(object instanceof Array) {
str += "[ ";
for(var i = 0, l = object.length; i < l; i++) {
str += pp(object[i]);
if(i < l-1) {
str += ", ";
}
}
str += " ]";
} else if(typeof(object) === "object") {
str += "{ ";
var last = Object.keys(object).sort().pop()
for(var k in object) {
str += k + ": " + pp(object[k]);
if(k !== last) {
str += ", ";
}
}
str += " }";
} else if(typeof(object) === "string") {
str += "\033[32m'" + object + "'";
} else if(typeof(object) === "undefined") {
str += "\033[1m\033[30m" + object;
} else if(typeof(object) === "number") {
str += "\033[31m" + object;
} else {
str += object;
}
str += "\033[0m";
return str;
}
function prettyPrint(object) {
console.log(pp(object))
}
`

View File

@ -8,6 +8,7 @@ package main
import "C" import "C"
import ( import (
"github.com/robertkrimen/otto"
"strings" "strings"
"unsafe" "unsafe"
) )
@ -63,18 +64,30 @@ L:
for { for {
switch result := readLine(&self.prompt); true { switch result := readLine(&self.prompt); true {
case result == nil: case result == nil:
break L //exit loop break L
case *result != "": //ignore blank lines case *result != "":
str += *result + "\n" str += *result + "\n"
self.setIndent() self.setIndent()
if indentCount <= 0 { if indentCount <= 0 {
if *result == "exit" {
self.Stop()
break L
}
addHistory(str) //allow user to recall this line addHistory(str) //allow user to recall this line
self.parseInput(str) self.parseInput(str)
str = ""
} }
} }
} }
} }
func (self *JSRepl) PrintValue(value otto.Value) {
method, _ := self.re.vm.Get("prettyPrint")
method.Call(method, value)
}

View File

@ -18,3 +18,7 @@ func (self *JSRepl) read() {
} }
} }
} }
func (self *JSRepl) PrintValue(value otto.Value) {
fmt.Println(value)
}