| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287 |
- // Copyright 2015 The go-ethereum Authors
- // This file is part of the go-ethereum library.
- //
- // The go-ethereum library is free software: you can redistribute it and/or modify
- // it under the terms of the GNU Lesser General Public License as published by
- // the Free Software Foundation, either version 3 of the License, or
- // (at your option) any later version.
- //
- // The go-ethereum library is distributed in the hope that it will be useful,
- // but WITHOUT ANY WARRANTY; without even the implied warranty of
- // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- // GNU Lesser General Public License for more details.
- //
- // You should have received a copy of the GNU Lesser General Public License
- // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
- // Package jsre provides execution environment for JavaScript.
- package jsre
- import (
- "fmt"
- "io/ioutil"
- "sync"
- "time"
- "github.com/ethereum/go-ethereum/common"
- "github.com/robertkrimen/otto"
- )
- /*
- JSRE is a generic JS runtime environment embedding the otto JS interpreter.
- It provides some helper functions to
- - load code from files
- - run code snippets
- - require libraries
- - bind native go objects
- */
- type JSRE struct {
- assetPath string
- evalQueue chan *evalReq
- stopEventLoop chan bool
- loopWg sync.WaitGroup
- }
- // jsTimer is a single timer instance with a callback function
- type jsTimer struct {
- timer *time.Timer
- duration time.Duration
- interval bool
- call otto.FunctionCall
- }
- // evalReq is a serialized vm execution request processed by runEventLoop.
- type evalReq struct {
- fn func(vm *otto.Otto)
- done chan bool
- }
- // runtime must be stopped with Stop() after use and cannot be used after stopping
- func New(assetPath string) *JSRE {
- re := &JSRE{
- assetPath: assetPath,
- evalQueue: make(chan *evalReq),
- stopEventLoop: make(chan bool),
- }
- re.loopWg.Add(1)
- go re.runEventLoop()
- re.Set("loadScript", re.loadScript)
- re.Set("inspect", prettyPrintJS)
- return re
- }
- // This function runs the main event loop from a goroutine that is started
- // when JSRE is created. Use Stop() before exiting to properly stop it.
- // The event loop processes vm access requests from the evalQueue in a
- // serialized way and calls timer callback functions at the appropriate time.
- // Exported functions always access the vm through the event queue. You can
- // call the functions of the otto vm directly to circumvent the queue. These
- // functions should be used if and only if running a routine that was already
- // called from JS through an RPC call.
- func (self *JSRE) runEventLoop() {
- vm := otto.New()
- registry := map[*jsTimer]*jsTimer{}
- ready := make(chan *jsTimer)
- newTimer := func(call otto.FunctionCall, interval bool) (*jsTimer, otto.Value) {
- delay, _ := call.Argument(1).ToInteger()
- if 0 >= delay {
- delay = 1
- }
- timer := &jsTimer{
- duration: time.Duration(delay) * time.Millisecond,
- call: call,
- interval: interval,
- }
- registry[timer] = timer
- timer.timer = time.AfterFunc(timer.duration, func() {
- ready <- timer
- })
- value, err := call.Otto.ToValue(timer)
- if err != nil {
- panic(err)
- }
- return timer, value
- }
- setTimeout := func(call otto.FunctionCall) otto.Value {
- _, value := newTimer(call, false)
- return value
- }
- setInterval := func(call otto.FunctionCall) otto.Value {
- _, value := newTimer(call, true)
- return value
- }
- clearTimeout := func(call otto.FunctionCall) otto.Value {
- timer, _ := call.Argument(0).Export()
- if timer, ok := timer.(*jsTimer); ok {
- timer.timer.Stop()
- delete(registry, timer)
- }
- return otto.UndefinedValue()
- }
- vm.Set("setTimeout", setTimeout)
- vm.Set("setInterval", setInterval)
- vm.Set("clearTimeout", clearTimeout)
- vm.Set("clearInterval", clearTimeout)
- var waitForCallbacks bool
- loop:
- for {
- select {
- case timer := <-ready:
- // execute callback, remove/reschedule the timer
- var arguments []interface{}
- if len(timer.call.ArgumentList) > 2 {
- tmp := timer.call.ArgumentList[2:]
- arguments = make([]interface{}, 2+len(tmp))
- for i, value := range tmp {
- arguments[i+2] = value
- }
- } else {
- arguments = make([]interface{}, 1)
- }
- arguments[0] = timer.call.ArgumentList[0]
- _, err := vm.Call(`Function.call.call`, nil, arguments...)
- if err != nil {
- fmt.Println("js error:", err, arguments)
- }
-
- _, inreg := registry[timer] // when clearInterval is called from within the callback don't reset it
- if timer.interval && inreg {
- timer.timer.Reset(timer.duration)
- } else {
- delete(registry, timer)
- if waitForCallbacks && (len(registry) == 0) {
- break loop
- }
- }
- case req := <-self.evalQueue:
- // run the code, send the result back
- req.fn(vm)
- close(req.done)
- if waitForCallbacks && (len(registry) == 0) {
- break loop
- }
- case waitForCallbacks = <-self.stopEventLoop:
- if !waitForCallbacks || (len(registry) == 0) {
- break loop
- }
- }
- }
- for _, timer := range registry {
- timer.timer.Stop()
- delete(registry, timer)
- }
- self.loopWg.Done()
- }
- // do schedules the given function on the event loop.
- func (self *JSRE) do(fn func(*otto.Otto)) {
- done := make(chan bool)
- req := &evalReq{fn, done}
- self.evalQueue <- req
- <-done
- }
- // stops the event loop before exit, optionally waits for all timers to expire
- func (self *JSRE) Stop(waitForCallbacks bool) {
- self.stopEventLoop <- waitForCallbacks
- self.loopWg.Wait()
- }
- // Exec(file) loads and runs the contents of a file
- // if a relative path is given, the jsre's assetPath is used
- func (self *JSRE) Exec(file string) error {
- code, err := ioutil.ReadFile(common.AbsolutePath(self.assetPath, file))
- if err != nil {
- return err
- }
- self.do(func(vm *otto.Otto) { _, err = vm.Run(code) })
- return err
- }
- // Bind assigns value v to a variable in the JS environment
- // This method is deprecated, use Set.
- func (self *JSRE) Bind(name string, v interface{}) error {
- return self.Set(name, v)
- }
- // Run runs a piece of JS code.
- func (self *JSRE) Run(code string) (v otto.Value, err error) {
- self.do(func(vm *otto.Otto) { v, err = vm.Run(code) })
- return v, err
- }
- // Get returns the value of a variable in the JS environment.
- func (self *JSRE) Get(ns string) (v otto.Value, err error) {
- self.do(func(vm *otto.Otto) { v, err = vm.Get(ns) })
- return v, err
- }
- // Set assigns value v to a variable in the JS environment.
- func (self *JSRE) Set(ns string, v interface{}) (err error) {
- self.do(func(vm *otto.Otto) { err = vm.Set(ns, v) })
- return err
- }
- // loadScript executes a JS script from inside the currently executing JS code.
- func (self *JSRE) loadScript(call otto.FunctionCall) otto.Value {
- file, err := call.Argument(0).ToString()
- if err != nil {
- // TODO: throw exception
- return otto.FalseValue()
- }
- file = common.AbsolutePath(self.assetPath, file)
- source, err := ioutil.ReadFile(file)
- if err != nil {
- // TODO: throw exception
- return otto.FalseValue()
- }
- if _, err := compileAndRun(call.Otto, file, source); err != nil {
- // TODO: throw exception
- fmt.Println("err:", err)
- return otto.FalseValue()
- }
- // TODO: return evaluation result
- return otto.TrueValue()
- }
- // EvalAndPrettyPrint evaluates code and pretty prints the result to
- // standard output.
- func (self *JSRE) EvalAndPrettyPrint(code string) (err error) {
- self.do(func(vm *otto.Otto) {
- var val otto.Value
- val, err = vm.Run(code)
- if err != nil {
- return
- }
- prettyPrint(vm, val)
- fmt.Println()
- })
- return err
- }
- // Compile compiles and then runs a piece of JS code.
- func (self *JSRE) Compile(filename string, src interface{}) (err error) {
- self.do(func(vm *otto.Otto) { _, err = compileAndRun(vm, filename, src) })
- return err
- }
- func compileAndRun(vm *otto.Otto, filename string, src interface{}) (otto.Value, error) {
- script, err := vm.Compile(filename, src)
- if err != nil {
- return otto.Value{}, err
- }
- return vm.Run(script)
- }
|