aboutsummaryrefslogtreecommitdiffstats
path: root/ethereum/repl/repl.go
diff options
context:
space:
mode:
authorzelig <viktor.tron@gmail.com>2014-07-21 20:30:37 +0800
committerzelig <viktor.tron@gmail.com>2014-07-21 20:30:37 +0800
commit4d5a890b46a75fb644277bbf2a8c034cf2a13424 (patch)
tree7d37fddbcdbd218fe6e0e216aa3554380229d133 /ethereum/repl/repl.go
parent75a7a4c97c350e911f4d721e940a59c0740ae967 (diff)
parentf702e27485981562ed7b88ecd3f8485af4c61b62 (diff)
downloaddexon-4d5a890b46a75fb644277bbf2a8c034cf2a13424.tar
dexon-4d5a890b46a75fb644277bbf2a8c034cf2a13424.tar.gz
dexon-4d5a890b46a75fb644277bbf2a8c034cf2a13424.tar.bz2
dexon-4d5a890b46a75fb644277bbf2a8c034cf2a13424.tar.lz
dexon-4d5a890b46a75fb644277bbf2a8c034cf2a13424.tar.xz
dexon-4d5a890b46a75fb644277bbf2a8c034cf2a13424.tar.zst
dexon-4d5a890b46a75fb644277bbf2a8c034cf2a13424.zip
merge upstream
Diffstat (limited to 'ethereum/repl/repl.go')
-rw-r--r--ethereum/repl/repl.go83
1 files changed, 83 insertions, 0 deletions
diff --git a/ethereum/repl/repl.go b/ethereum/repl/repl.go
new file mode 100644
index 000000000..92d4ad86a
--- /dev/null
+++ b/ethereum/repl/repl.go
@@ -0,0 +1,83 @@
+package ethrepl
+
+import (
+ "bufio"
+ "fmt"
+ "github.com/ethereum/eth-go"
+ "github.com/ethereum/eth-go/ethlog"
+ "github.com/ethereum/eth-go/ethutil"
+ "io"
+ "os"
+ "path"
+)
+
+var logger = ethlog.NewLogger("REPL")
+
+type Repl interface {
+ Start()
+ Stop()
+}
+
+type JSRepl struct {
+ re *JSRE
+
+ prompt string
+
+ history *os.File
+
+ running bool
+}
+
+func NewJSRepl(ethereum *eth.Ethereum) *JSRepl {
+ hist, err := os.OpenFile(path.Join(ethutil.Config.ExecPath, "history"), os.O_RDWR|os.O_CREATE, os.ModePerm)
+ if err != nil {
+ panic(err)
+ }
+
+ return &JSRepl{re: NewJSRE(ethereum), prompt: "> ", history: hist}
+}
+
+func (self *JSRepl) Start() {
+ if !self.running {
+ self.running = true
+ logger.Infoln("init JS Console")
+ reader := bufio.NewReader(self.history)
+ for {
+ line, err := reader.ReadString('\n')
+ if err != nil && err == io.EOF {
+ break
+ } else if err != nil {
+ fmt.Println("error reading history", err)
+ break
+ }
+
+ addHistory(line[:len(line)-1])
+ }
+ self.read()
+ }
+}
+
+func (self *JSRepl) Stop() {
+ if self.running {
+ self.running = false
+ self.re.Stop()
+ logger.Infoln("exit JS Console")
+ self.history.Close()
+ }
+}
+
+func (self *JSRepl) parseInput(code string) {
+ defer func() {
+ if r := recover(); r != nil {
+ fmt.Println("[native] error", r)
+ }
+ }()
+
+ value, err := self.re.Run(code)
+ if err != nil {
+ fmt.Println(err)
+ return
+ }
+
+ self.PrintValue(value)
+}