diff options
author | obscuren <geffobscura@gmail.com> | 2015-03-18 04:27:34 +0800 |
---|---|---|
committer | obscuren <geffobscura@gmail.com> | 2015-03-18 04:27:34 +0800 |
commit | 917050dc30d8717d7e0bba1257165c1aec44887f (patch) | |
tree | 712f8823391286438a0e5c3d1f53c4682254650e /javascript/javascript_runtime.go | |
parent | fe819f3b9f7a2d8d842c53b7269ccceace533569 (diff) | |
parent | 53104b09fa823cb5457960b8518b9650a5b083da (diff) | |
download | dexon-917050dc30d8717d7e0bba1257165c1aec44887f.tar dexon-917050dc30d8717d7e0bba1257165c1aec44887f.tar.gz dexon-917050dc30d8717d7e0bba1257165c1aec44887f.tar.bz2 dexon-917050dc30d8717d7e0bba1257165c1aec44887f.tar.lz dexon-917050dc30d8717d7e0bba1257165c1aec44887f.tar.xz dexon-917050dc30d8717d7e0bba1257165c1aec44887f.tar.zst dexon-917050dc30d8717d7e0bba1257165c1aec44887f.zip |
Merge branch 'develop' into rpcfrontier
Diffstat (limited to 'javascript/javascript_runtime.go')
-rw-r--r-- | javascript/javascript_runtime.go | 103 |
1 files changed, 0 insertions, 103 deletions
diff --git a/javascript/javascript_runtime.go b/javascript/javascript_runtime.go deleted file mode 100644 index 0a137f72a..000000000 --- a/javascript/javascript_runtime.go +++ /dev/null @@ -1,103 +0,0 @@ -package javascript - -import ( - "fmt" - "io/ioutil" - "os" - "path" - "path/filepath" - - "github.com/ethereum/go-ethereum/logger" - "github.com/ethereum/go-ethereum/xeth" - "github.com/obscuren/otto" -) - -var jsrelogger = logger.NewLogger("JSRE") - -type JSRE struct { - Vm *otto.Otto - xeth *xeth.XEth - - objectCb map[string][]otto.Value -} - -func (jsre *JSRE) LoadExtFile(path string) { - result, err := ioutil.ReadFile(path) - if err == nil { - jsre.Vm.Run(result) - } else { - jsrelogger.Infoln("Could not load file:", path) - } -} - -func (jsre *JSRE) LoadIntFile(file string) { - assetPath := path.Join(os.Getenv("GOPATH"), "src", "github.com", "ethereum", "go-ethereum", "cmd", "mist", "assets", "ext") - jsre.LoadExtFile(path.Join(assetPath, file)) -} - -func NewJSRE(xeth *xeth.XEth) *JSRE { - re := &JSRE{ - otto.New(), - xeth, - make(map[string][]otto.Value), - } - - // Init the JS lib - re.Vm.Run(jsLib) - - // Load extra javascript files - re.LoadIntFile("bignumber.min.js") - - re.Bind("eth", &JSEthereum{re.xeth, re.Vm}) - - re.initStdFuncs() - - jsrelogger.Infoln("started") - - return re -} - -func (self *JSRE) Bind(name string, v interface{}) { - self.Vm.Set(name, v) -} - -func (self *JSRE) Run(code string) (otto.Value, error) { - return self.Vm.Run(code) -} - -func (self *JSRE) initStdFuncs() { - t, _ := self.Vm.Get("eth") - eth := t.Object() - eth.Set("require", self.require) -} - -func (self *JSRE) Require(file string) error { - if len(filepath.Ext(file)) == 0 { - file += ".js" - } - - fh, err := os.Open(file) - if err != nil { - return err - } - - content, _ := ioutil.ReadAll(fh) - self.Run("exports = {};(function() {" + string(content) + "})();") - - return nil -} - -func (self *JSRE) require(call otto.FunctionCall) otto.Value { - file, err := call.Argument(0).ToString() - if err != nil { - return otto.UndefinedValue() - } - if err := self.Require(file); err != nil { - fmt.Println("err:", err) - return otto.UndefinedValue() - } - - t, _ := self.Vm.Get("exports") - - return t -} |