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