javascript_runtime.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. package javascript
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "os"
  6. "path"
  7. "path/filepath"
  8. "github.com/ethereum/go-ethereum"
  9. "github.com/ethereum/go-ethereum/cmd/utils"
  10. "github.com/ethereum/go-ethereum/core"
  11. "github.com/ethereum/go-ethereum/core/types"
  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. pipe *xeth.JSXEth
  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.NewJSXEth(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("string.js")
  51. re.LoadIntFile("big.js")
  52. // Subscribe to events
  53. mux := ethereum.EventMux()
  54. re.events = mux.Subscribe(core.NewBlockEvent{})
  55. // We have to make sure that, whoever calls this, calls "Stop"
  56. go re.mainLoop()
  57. re.Bind("eth", &JSEthereum{re.pipe, re.Vm, ethereum})
  58. re.initStdFuncs()
  59. jsrelogger.Infoln("started")
  60. return re
  61. }
  62. func (self *JSRE) Bind(name string, v interface{}) {
  63. self.Vm.Set(name, v)
  64. }
  65. func (self *JSRE) Run(code string) (otto.Value, error) {
  66. return self.Vm.Run(code)
  67. }
  68. func (self *JSRE) Require(file string) error {
  69. if len(filepath.Ext(file)) == 0 {
  70. file += ".js"
  71. }
  72. fh, err := os.Open(file)
  73. if err != nil {
  74. return err
  75. }
  76. content, _ := ioutil.ReadAll(fh)
  77. self.Run("exports = {};(function() {" + string(content) + "})();")
  78. return nil
  79. }
  80. func (self *JSRE) Stop() {
  81. self.events.Unsubscribe()
  82. jsrelogger.Infoln("stopped")
  83. }
  84. func (self *JSRE) mainLoop() {
  85. for _ = range self.events.Chan() {
  86. }
  87. }
  88. func (self *JSRE) initStdFuncs() {
  89. t, _ := self.Vm.Get("eth")
  90. eth := t.Object()
  91. eth.Set("watch", self.watch)
  92. eth.Set("addPeer", self.addPeer)
  93. eth.Set("require", self.require)
  94. eth.Set("stopMining", self.stopMining)
  95. eth.Set("startMining", self.startMining)
  96. eth.Set("execBlock", self.execBlock)
  97. eth.Set("dump", self.dump)
  98. }
  99. /*
  100. * The following methods are natively implemented javascript functions
  101. */
  102. func (self *JSRE) dump(call otto.FunctionCall) otto.Value {
  103. var state *state.State
  104. if len(call.ArgumentList) > 0 {
  105. var block *types.Block
  106. if call.Argument(0).IsNumber() {
  107. num, _ := call.Argument(0).ToInteger()
  108. block = self.ethereum.ChainManager().GetBlockByNumber(uint64(num))
  109. } else if call.Argument(0).IsString() {
  110. hash, _ := call.Argument(0).ToString()
  111. block = self.ethereum.ChainManager().GetBlock(ethutil.Hex2Bytes(hash))
  112. } else {
  113. fmt.Println("invalid argument for dump. Either hex string or number")
  114. }
  115. if block == nil {
  116. fmt.Println("block not found")
  117. return otto.UndefinedValue()
  118. }
  119. state = block.State()
  120. } else {
  121. state = self.ethereum.BlockManager().CurrentState()
  122. }
  123. v, _ := self.Vm.ToValue(state.Dump())
  124. return v
  125. }
  126. func (self *JSRE) stopMining(call otto.FunctionCall) otto.Value {
  127. v, _ := self.Vm.ToValue(utils.StopMining(self.ethereum))
  128. return v
  129. }
  130. func (self *JSRE) startMining(call otto.FunctionCall) otto.Value {
  131. v, _ := self.Vm.ToValue(utils.StartMining(self.ethereum))
  132. return v
  133. }
  134. // eth.watch
  135. func (self *JSRE) watch(call otto.FunctionCall) otto.Value {
  136. addr, _ := call.Argument(0).ToString()
  137. var storageAddr string
  138. var cb otto.Value
  139. var storageCallback bool
  140. if len(call.ArgumentList) > 2 {
  141. storageCallback = true
  142. storageAddr, _ = call.Argument(1).ToString()
  143. cb = call.Argument(2)
  144. } else {
  145. cb = call.Argument(1)
  146. }
  147. if storageCallback {
  148. self.objectCb[addr+storageAddr] = append(self.objectCb[addr+storageAddr], cb)
  149. // event := "storage:" + string(ethutil.Hex2Bytes(addr)) + ":" + string(ethutil.Hex2Bytes(storageAddr))
  150. // self.ethereum.EventMux().Subscribe(event, self.changeChan)
  151. } else {
  152. self.objectCb[addr] = append(self.objectCb[addr], cb)
  153. // event := "object:" + string(ethutil.Hex2Bytes(addr))
  154. // self.ethereum.EventMux().Subscribe(event, self.changeChan)
  155. }
  156. return otto.UndefinedValue()
  157. }
  158. func (self *JSRE) addPeer(call otto.FunctionCall) otto.Value {
  159. host, err := call.Argument(0).ToString()
  160. if err != nil {
  161. return otto.FalseValue()
  162. }
  163. self.ethereum.ConnectToPeer(host)
  164. return otto.TrueValue()
  165. }
  166. func (self *JSRE) require(call otto.FunctionCall) otto.Value {
  167. file, err := call.Argument(0).ToString()
  168. if err != nil {
  169. return otto.UndefinedValue()
  170. }
  171. if err := self.Require(file); err != nil {
  172. fmt.Println("err:", err)
  173. return otto.UndefinedValue()
  174. }
  175. t, _ := self.Vm.Get("exports")
  176. return t
  177. }
  178. func (self *JSRE) execBlock(call otto.FunctionCall) otto.Value {
  179. hash, err := call.Argument(0).ToString()
  180. if err != nil {
  181. return otto.UndefinedValue()
  182. }
  183. err = utils.BlockDo(self.ethereum, ethutil.Hex2Bytes(hash))
  184. if err != nil {
  185. fmt.Println(err)
  186. return otto.FalseValue()
  187. }
  188. return otto.TrueValue()
  189. }