javascript_runtime.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. package main
  2. import (
  3. "fmt"
  4. "github.com/ethereum/eth-go"
  5. "github.com/ethereum/eth-go/ethchain"
  6. "github.com/ethereum/eth-go/ethlog"
  7. "github.com/ethereum/eth-go/ethpub"
  8. "github.com/ethereum/eth-go/ethutil"
  9. "github.com/ethereum/go-ethereum/utils"
  10. "github.com/obscuren/otto"
  11. "io/ioutil"
  12. "os"
  13. "path"
  14. "path/filepath"
  15. )
  16. var jsrelogger = ethlog.NewLogger("JSRE")
  17. type JSRE struct {
  18. ethereum *eth.Ethereum
  19. vm *otto.Otto
  20. lib *ethpub.PEthereum
  21. blockChan chan ethutil.React
  22. changeChan chan ethutil.React
  23. quitChan chan bool
  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.Debugln("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", "ethereal", "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. ethpub.NewPEthereum(ethereum),
  43. make(chan ethutil.React, 1),
  44. make(chan ethutil.React, 1),
  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. re.Bind("eth", &JSEthereum{re.lib, re.vm})
  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. // Kill the main loop
  80. self.quitChan <- true
  81. close(self.blockChan)
  82. close(self.quitChan)
  83. close(self.changeChan)
  84. jsrelogger.Infoln("stopped")
  85. }
  86. func (self *JSRE) mainLoop() {
  87. // Subscribe to events
  88. reactor := self.ethereum.Reactor()
  89. reactor.Subscribe("newBlock", self.blockChan)
  90. out:
  91. for {
  92. select {
  93. case <-self.quitChan:
  94. break out
  95. case block := <-self.blockChan:
  96. if _, ok := block.Resource.(*ethchain.Block); ok {
  97. }
  98. case object := <-self.changeChan:
  99. if stateObject, ok := object.Resource.(*ethchain.StateObject); ok {
  100. for _, cb := range self.objectCb[ethutil.Hex(stateObject.Address())] {
  101. val, _ := self.vm.ToValue(ethpub.NewPStateObject(stateObject))
  102. cb.Call(cb, val)
  103. }
  104. } else if storageObject, ok := object.Resource.(*ethchain.StorageState); ok {
  105. for _, cb := range self.objectCb[ethutil.Hex(storageObject.StateAddress)+ethutil.Hex(storageObject.Address)] {
  106. val, _ := self.vm.ToValue(ethpub.NewPStorageState(storageObject))
  107. cb.Call(cb, val)
  108. }
  109. }
  110. }
  111. }
  112. }
  113. func (self *JSRE) initStdFuncs() {
  114. t, _ := self.vm.Get("eth")
  115. eth := t.Object()
  116. eth.Set("watch", self.watch)
  117. eth.Set("addPeer", self.addPeer)
  118. eth.Set("require", self.require)
  119. eth.Set("stopMining", self.stopMining)
  120. eth.Set("startMining", self.startMining)
  121. eth.Set("execBlock", self.execBlock)
  122. }
  123. /*
  124. * The following methods are natively implemented javascript functions
  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.FromHex(addr)) + ":" + string(ethutil.FromHex(storageAddr))
  150. self.ethereum.Reactor().Subscribe(event, self.changeChan)
  151. } else {
  152. self.objectCb[addr] = append(self.objectCb[addr], cb)
  153. event := "object:" + string(ethutil.FromHex(addr))
  154. self.ethereum.Reactor().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.FromHex(hash))
  184. if err != nil {
  185. fmt.Println(err)
  186. return otto.FalseValue()
  187. }
  188. return otto.TrueValue()
  189. }