ソースを参照

Moved JSRE to it's own package for sharing between ethere(um/al)

obscuren 11 年 前
コミット
ce8f24e57a
3 ファイル変更93 行追加18 行削除
  1. 15 15
      javascript/javascript_runtime.go
  2. 1 1
      javascript/js_lib.go
  3. 77 2
      javascript/types.go

+ 15 - 15
ethereum/repl/javascript_runtime.go → javascript/javascript_runtime.go

@@ -1,4 +1,4 @@
-package ethrepl
+package javascript
 
 import (
 	"fmt"
@@ -22,7 +22,7 @@ var jsrelogger = ethlog.NewLogger("JSRE")
 
 type JSRE struct {
 	ethereum *eth.Ethereum
-	vm       *otto.Otto
+	Vm       *otto.Otto
 	lib      *ethpub.PEthereum
 
 	blockChan  chan ethreact.Event
@@ -35,9 +35,9 @@ type JSRE struct {
 func (jsre *JSRE) LoadExtFile(path string) {
 	result, err := ioutil.ReadFile(path)
 	if err == nil {
-		jsre.vm.Run(result)
+		jsre.Vm.Run(result)
 	} else {
-		jsrelogger.Debugln("Could not load file:", path)
+		jsrelogger.Infoln("Could not load file:", path)
 	}
 }
 
@@ -58,7 +58,7 @@ func NewJSRE(ethereum *eth.Ethereum) *JSRE {
 	}
 
 	// Init the JS lib
-	re.vm.Run(jsLib)
+	re.Vm.Run(jsLib)
 
 	// Load extra javascript files
 	re.LoadIntFile("string.js")
@@ -71,7 +71,7 @@ func NewJSRE(ethereum *eth.Ethereum) *JSRE {
 	reactor := ethereum.Reactor()
 	reactor.Subscribe("newBlock", re.blockChan)
 
-	re.Bind("eth", &JSEthereum{re.lib, re.vm})
+	re.Bind("eth", &JSEthereum{re.lib, re.Vm, ethereum})
 
 	re.initStdFuncs()
 
@@ -81,11 +81,11 @@ func NewJSRE(ethereum *eth.Ethereum) *JSRE {
 }
 
 func (self *JSRE) Bind(name string, v interface{}) {
-	self.vm.Set(name, v)
+	self.Vm.Set(name, v)
 }
 
 func (self *JSRE) Run(code string) (otto.Value, error) {
-	return self.vm.Run(code)
+	return self.Vm.Run(code)
 }
 
 func (self *JSRE) Require(file string) error {
@@ -126,12 +126,12 @@ out:
 		case object := <-self.changeChan:
 			if stateObject, ok := object.Resource.(*ethstate.StateObject); ok {
 				for _, cb := range self.objectCb[ethutil.Bytes2Hex(stateObject.Address())] {
-					val, _ := self.vm.ToValue(ethpub.NewPStateObject(stateObject))
+					val, _ := self.Vm.ToValue(ethpub.NewPStateObject(stateObject))
 					cb.Call(cb, val)
 				}
 			} else if storageObject, ok := object.Resource.(*ethstate.StorageState); ok {
 				for _, cb := range self.objectCb[ethutil.Bytes2Hex(storageObject.StateAddress)+ethutil.Bytes2Hex(storageObject.Address)] {
-					val, _ := self.vm.ToValue(ethpub.NewPStorageState(storageObject))
+					val, _ := self.Vm.ToValue(ethpub.NewPStorageState(storageObject))
 					cb.Call(cb, val)
 				}
 			}
@@ -140,7 +140,7 @@ out:
 }
 
 func (self *JSRE) initStdFuncs() {
-	t, _ := self.vm.Get("eth")
+	t, _ := self.Vm.Get("eth")
 	eth := t.Object()
 	eth.Set("watch", self.watch)
 	eth.Set("addPeer", self.addPeer)
@@ -181,18 +181,18 @@ func (self *JSRE) dump(call otto.FunctionCall) otto.Value {
 		state = self.ethereum.StateManager().CurrentState()
 	}
 
-	v, _ := self.vm.ToValue(state.Dump())
+	v, _ := self.Vm.ToValue(state.Dump())
 
 	return v
 }
 
 func (self *JSRE) stopMining(call otto.FunctionCall) otto.Value {
-	v, _ := self.vm.ToValue(utils.StopMining(self.ethereum))
+	v, _ := self.Vm.ToValue(utils.StopMining(self.ethereum))
 	return v
 }
 
 func (self *JSRE) startMining(call otto.FunctionCall) otto.Value {
-	v, _ := self.vm.ToValue(utils.StartMining(self.ethereum))
+	v, _ := self.Vm.ToValue(utils.StartMining(self.ethereum))
 	return v
 }
 
@@ -245,7 +245,7 @@ func (self *JSRE) require(call otto.FunctionCall) otto.Value {
 		return otto.UndefinedValue()
 	}
 
-	t, _ := self.vm.Get("exports")
+	t, _ := self.Vm.Get("exports")
 
 	return t
 }

+ 1 - 1
ethereum/repl/js_lib.go → javascript/js_lib.go

@@ -1,4 +1,4 @@
-package ethrepl
+package javascript
 
 const jsLib = `
 function pp(object) {

+ 77 - 2
ethereum/repl/types.go → javascript/types.go

@@ -1,8 +1,12 @@
-package ethrepl
+package javascript
 
 import (
 	"fmt"
+
+	"github.com/ethereum/eth-go"
+	"github.com/ethereum/eth-go/ethchain"
 	"github.com/ethereum/eth-go/ethpub"
+	"github.com/ethereum/eth-go/ethstate"
 	"github.com/ethereum/eth-go/ethutil"
 	"github.com/obscuren/otto"
 )
@@ -34,9 +38,37 @@ func (self *JSBlock) GetTransaction(hash string) otto.Value {
 	return self.eth.toVal(self.PBlock.GetTransaction(hash))
 }
 
+type JSMessage struct {
+	To, From  string
+	Input     string
+	Output    string
+	Path      int
+	Origin    string
+	Timestamp int32
+	Coinbase  string
+	Block     string
+	Number    int32
+}
+
+func NewJSMessage(message *ethstate.Message) JSMessage {
+	return JSMessage{
+		To:        ethutil.Bytes2Hex(message.To),
+		From:      ethutil.Bytes2Hex(message.From),
+		Input:     ethutil.Bytes2Hex(message.Input),
+		Output:    ethutil.Bytes2Hex(message.Output),
+		Path:      message.Path,
+		Origin:    ethutil.Bytes2Hex(message.Origin),
+		Timestamp: int32(message.Timestamp),
+		Coinbase:  ethutil.Bytes2Hex(message.Origin),
+		Block:     ethutil.Bytes2Hex(message.Block),
+		Number:    int32(message.Number.Int64()),
+	}
+}
+
 type JSEthereum struct {
 	*ethpub.PEthereum
-	vm *otto.Otto
+	vm       *otto.Otto
+	ethereum *eth.Ethereum
 }
 
 func (self *JSEthereum) GetBlock(hash string) otto.Value {
@@ -93,3 +125,46 @@ func (self *JSEthereum) toVal(v interface{}) otto.Value {
 
 	return result
 }
+
+func (self *JSEthereum) Messages(object map[string]interface{}) otto.Value {
+	filter := ethchain.NewFilter(self.ethereum)
+
+	if object["earliest"] != nil {
+		earliest := object["earliest"]
+		if e, ok := earliest.(string); ok {
+			filter.SetEarliestBlock(ethutil.Hex2Bytes(e))
+		} else {
+			filter.SetEarliestBlock(earliest)
+		}
+	}
+	if object["latest"] != nil {
+		latest := object["latest"]
+		if l, ok := latest.(string); ok {
+			filter.SetLatestBlock(ethutil.Hex2Bytes(l))
+		} else {
+			filter.SetLatestBlock(latest)
+		}
+	}
+	if object["to"] != nil {
+		filter.SetTo(ethutil.Hex2Bytes(object["to"].(string)))
+	}
+	if object["from"] != nil {
+		filter.SetFrom(ethutil.Hex2Bytes(object["from"].(string)))
+	}
+	if object["max"] != nil {
+		filter.SetMax(object["max"].(int))
+	}
+	if object["skip"] != nil {
+		filter.SetSkip(object["skip"].(int))
+	}
+
+	messages := filter.Find()
+	var msgs []JSMessage
+	for _, m := range messages {
+		msgs = append(msgs, NewJSMessage(m))
+	}
+
+	v, _ := self.vm.ToValue(msgs)
+
+	return v
+}