javascript_runtime.go 5.3 KB

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