javascript_runtime.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. package javascript
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "os"
  6. "path"
  7. "path/filepath"
  8. "github.com/ethereum/go-ethereum/core"
  9. "github.com/ethereum/go-ethereum/core/types"
  10. "github.com/ethereum/go-ethereum/eth"
  11. "github.com/ethereum/go-ethereum/ethutil"
  12. "github.com/ethereum/go-ethereum/event"
  13. "github.com/ethereum/go-ethereum/logger"
  14. "github.com/ethereum/go-ethereum/state"
  15. "github.com/ethereum/go-ethereum/xeth"
  16. "github.com/obscuren/otto"
  17. )
  18. var jsrelogger = logger.NewLogger("JSRE")
  19. type JSRE struct {
  20. ethereum *eth.Ethereum
  21. Vm *otto.Otto
  22. xeth *xeth.XEth
  23. events event.Subscription
  24. objectCb map[string][]otto.Value
  25. }
  26. func (jsre *JSRE) LoadExtFile(path string) {
  27. result, err := ioutil.ReadFile(path)
  28. if err == nil {
  29. jsre.Vm.Run(result)
  30. } else {
  31. jsrelogger.Infoln("Could not load file:", path)
  32. }
  33. }
  34. func (jsre *JSRE) LoadIntFile(file string) {
  35. assetPath := path.Join(os.Getenv("GOPATH"), "src", "github.com", "ethereum", "go-ethereum", "cmd", "mist", "assets", "ext")
  36. jsre.LoadExtFile(path.Join(assetPath, file))
  37. }
  38. func NewJSRE(ethereum *eth.Ethereum) *JSRE {
  39. re := &JSRE{
  40. ethereum,
  41. otto.New(),
  42. xeth.New(ethereum),
  43. nil,
  44. make(map[string][]otto.Value),
  45. }
  46. // Init the JS lib
  47. re.Vm.Run(jsLib)
  48. // Load extra javascript files
  49. re.LoadIntFile("bignumber.min.js")
  50. // Subscribe to events
  51. mux := ethereum.EventMux()
  52. re.events = mux.Subscribe(core.NewBlockEvent{})
  53. // We have to make sure that, whoever calls this, calls "Stop"
  54. go re.mainLoop()
  55. re.Bind("eth", &JSEthereum{re.xeth, re.Vm, ethereum})
  56. re.initStdFuncs()
  57. jsrelogger.Infoln("started")
  58. return re
  59. }
  60. func (self *JSRE) Bind(name string, v interface{}) {
  61. self.Vm.Set(name, v)
  62. }
  63. func (self *JSRE) Run(code string) (otto.Value, error) {
  64. return self.Vm.Run(code)
  65. }
  66. func (self *JSRE) Require(file string) error {
  67. if len(filepath.Ext(file)) == 0 {
  68. file += ".js"
  69. }
  70. fh, err := os.Open(file)
  71. if err != nil {
  72. return err
  73. }
  74. content, _ := ioutil.ReadAll(fh)
  75. self.Run("exports = {};(function() {" + string(content) + "})();")
  76. return nil
  77. }
  78. func (self *JSRE) Stop() {
  79. self.events.Unsubscribe()
  80. jsrelogger.Infoln("stopped")
  81. }
  82. func (self *JSRE) mainLoop() {
  83. for _ = range self.events.Chan() {
  84. }
  85. }
  86. func (self *JSRE) initStdFuncs() {
  87. t, _ := self.Vm.Get("eth")
  88. eth := t.Object()
  89. eth.Set("connect", self.connect)
  90. eth.Set("require", self.require)
  91. eth.Set("stopMining", self.stopMining)
  92. eth.Set("startMining", self.startMining)
  93. eth.Set("dump", self.dump)
  94. eth.Set("export", self.export)
  95. }
  96. /*
  97. * The following methods are natively implemented javascript functions
  98. */
  99. func (self *JSRE) dump(call otto.FunctionCall) otto.Value {
  100. var block *types.Block
  101. if len(call.ArgumentList) > 0 {
  102. if call.Argument(0).IsNumber() {
  103. num, _ := call.Argument(0).ToInteger()
  104. block = self.ethereum.ChainManager().GetBlockByNumber(uint64(num))
  105. } else if call.Argument(0).IsString() {
  106. hash, _ := call.Argument(0).ToString()
  107. block = self.ethereum.ChainManager().GetBlock(ethutil.Hex2Bytes(hash))
  108. } else {
  109. fmt.Println("invalid argument for dump. Either hex string or number")
  110. }
  111. if block == nil {
  112. fmt.Println("block not found")
  113. return otto.UndefinedValue()
  114. }
  115. } else {
  116. block = self.ethereum.ChainManager().CurrentBlock()
  117. }
  118. statedb := state.New(block.Root(), self.ethereum.Db())
  119. v, _ := self.Vm.ToValue(statedb.RawDump())
  120. return v
  121. }
  122. func (self *JSRE) stopMining(call otto.FunctionCall) otto.Value {
  123. self.xeth.Miner().Stop()
  124. return otto.TrueValue()
  125. }
  126. func (self *JSRE) startMining(call otto.FunctionCall) otto.Value {
  127. self.xeth.Miner().Start()
  128. return otto.TrueValue()
  129. }
  130. func (self *JSRE) connect(call otto.FunctionCall) otto.Value {
  131. nodeURL, err := call.Argument(0).ToString()
  132. if err != nil {
  133. return otto.FalseValue()
  134. }
  135. if err := self.ethereum.SuggestPeer(nodeURL); err != nil {
  136. return otto.FalseValue()
  137. }
  138. return otto.TrueValue()
  139. }
  140. func (self *JSRE) require(call otto.FunctionCall) otto.Value {
  141. file, err := call.Argument(0).ToString()
  142. if err != nil {
  143. return otto.UndefinedValue()
  144. }
  145. if err := self.Require(file); err != nil {
  146. fmt.Println("err:", err)
  147. return otto.UndefinedValue()
  148. }
  149. t, _ := self.Vm.Get("exports")
  150. return t
  151. }
  152. func (self *JSRE) export(call otto.FunctionCall) otto.Value {
  153. if len(call.ArgumentList) == 0 {
  154. fmt.Println("err: require file name")
  155. return otto.FalseValue()
  156. }
  157. fn, err := call.Argument(0).ToString()
  158. if err != nil {
  159. fmt.Println(err)
  160. return otto.FalseValue()
  161. }
  162. data := self.ethereum.ChainManager().Export()
  163. if err := ethutil.WriteFile(fn, data); err != nil {
  164. fmt.Println(err)
  165. return otto.FalseValue()
  166. }
  167. return otto.TrueValue()
  168. }