2015-07-06 19:54:22 -05:00
|
|
|
// Copyright 2015 The go-ethereum Authors
|
2015-07-22 11:48:40 -05:00
|
|
|
// This file is part of the go-ethereum library.
|
2015-07-06 19:54:22 -05:00
|
|
|
//
|
2015-07-23 11:35:11 -05:00
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
2015-07-06 19:54:22 -05:00
|
|
|
// 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.
|
|
|
|
//
|
2015-07-22 11:48:40 -05:00
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
2015-07-06 19:54:22 -05:00
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2015-07-22 11:48:40 -05:00
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2015-07-06 19:54:22 -05:00
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
2015-07-22 11:48:40 -05:00
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
2015-07-06 19:54:22 -05:00
|
|
|
|
2015-07-06 22:08:16 -05:00
|
|
|
// Package jsre provides execution environment for JavaScript.
|
2015-03-15 01:13:39 -05:00
|
|
|
package jsre
|
|
|
|
|
|
|
|
import (
|
2016-02-19 05:06:58 -06:00
|
|
|
crand "crypto/rand"
|
|
|
|
"encoding/binary"
|
2015-03-15 01:13:39 -05:00
|
|
|
"fmt"
|
2016-05-06 04:40:23 -05:00
|
|
|
"io"
|
2015-03-15 01:13:39 -05:00
|
|
|
"io/ioutil"
|
2016-02-19 05:06:58 -06:00
|
|
|
"math/rand"
|
2015-04-21 19:31:59 -05:00
|
|
|
"time"
|
2015-03-24 11:49:28 -05:00
|
|
|
|
2015-03-16 10:48:54 -05:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2017-01-12 14:28:35 -06:00
|
|
|
"github.com/ethereum/go-ethereum/internal/jsre/deps"
|
2015-04-21 19:31:59 -05:00
|
|
|
"github.com/robertkrimen/otto"
|
2015-03-15 01:13:39 -05:00
|
|
|
)
|
|
|
|
|
2017-01-12 14:28:35 -06:00
|
|
|
var (
|
|
|
|
BigNumber_JS = deps.MustAsset("bignumber.js")
|
|
|
|
Web3_JS = deps.MustAsset("web3.js")
|
|
|
|
)
|
|
|
|
|
2015-03-15 01:13:39 -05:00
|
|
|
/*
|
|
|
|
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 {
|
2015-05-24 19:27:37 -05:00
|
|
|
assetPath string
|
2016-05-06 04:40:23 -05:00
|
|
|
output io.Writer
|
2015-04-21 19:31:59 -05:00
|
|
|
evalQueue chan *evalReq
|
|
|
|
stopEventLoop chan bool
|
2016-06-02 14:20:32 -05:00
|
|
|
closed chan struct{}
|
2015-04-21 19:31:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// jsTimer is a single timer instance with a callback function
|
|
|
|
type jsTimer struct {
|
|
|
|
timer *time.Timer
|
|
|
|
duration time.Duration
|
|
|
|
interval bool
|
|
|
|
call otto.FunctionCall
|
2015-03-15 01:13:39 -05:00
|
|
|
}
|
|
|
|
|
2015-05-24 19:27:37 -05:00
|
|
|
// evalReq is a serialized vm execution request processed by runEventLoop.
|
2015-04-21 19:31:59 -05:00
|
|
|
type evalReq struct {
|
2015-05-24 19:27:37 -05:00
|
|
|
fn func(vm *otto.Otto)
|
2015-04-21 19:31:59 -05:00
|
|
|
done chan bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// runtime must be stopped with Stop() after use and cannot be used after stopping
|
2016-05-06 04:40:23 -05:00
|
|
|
func New(assetPath string, output io.Writer) *JSRE {
|
2015-03-15 01:13:39 -05:00
|
|
|
re := &JSRE{
|
2015-05-24 19:27:37 -05:00
|
|
|
assetPath: assetPath,
|
2016-05-06 04:40:23 -05:00
|
|
|
output: output,
|
2016-06-02 14:20:32 -05:00
|
|
|
closed: make(chan struct{}),
|
2015-05-24 19:27:37 -05:00
|
|
|
evalQueue: make(chan *evalReq),
|
|
|
|
stopEventLoop: make(chan bool),
|
2015-03-15 01:13:39 -05:00
|
|
|
}
|
2015-04-21 19:31:59 -05:00
|
|
|
go re.runEventLoop()
|
2015-05-24 19:27:37 -05:00
|
|
|
re.Set("loadScript", re.loadScript)
|
2016-12-10 18:53:34 -06:00
|
|
|
re.Set("inspect", re.prettyPrintJS)
|
2015-03-15 01:13:39 -05:00
|
|
|
return re
|
|
|
|
}
|
|
|
|
|
2016-02-19 05:06:58 -06:00
|
|
|
// randomSource returns a pseudo random value generator.
|
|
|
|
func randomSource() *rand.Rand {
|
|
|
|
bytes := make([]byte, 8)
|
|
|
|
seed := time.Now().UnixNano()
|
|
|
|
if _, err := crand.Read(bytes); err == nil {
|
|
|
|
seed = int64(binary.LittleEndian.Uint64(bytes))
|
|
|
|
}
|
|
|
|
|
|
|
|
src := rand.NewSource(seed)
|
|
|
|
return rand.New(src)
|
|
|
|
}
|
|
|
|
|
2015-05-24 19:27:37 -05:00
|
|
|
// 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.
|
2015-04-21 19:31:59 -05:00
|
|
|
|
2015-05-24 19:27:37 -05:00
|
|
|
// 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.
|
2015-04-21 19:31:59 -05:00
|
|
|
func (self *JSRE) runEventLoop() {
|
2016-06-02 14:20:32 -05:00
|
|
|
defer close(self.closed)
|
|
|
|
|
2015-05-24 19:27:37 -05:00
|
|
|
vm := otto.New()
|
2016-02-19 05:06:58 -06:00
|
|
|
r := randomSource()
|
|
|
|
vm.SetRandomSource(r.Float64)
|
|
|
|
|
2015-04-21 19:31:59 -05:00
|
|
|
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()
|
|
|
|
}
|
2015-11-23 05:03:31 -06:00
|
|
|
vm.Set("_setTimeout", setTimeout)
|
|
|
|
vm.Set("_setInterval", setInterval)
|
|
|
|
vm.Run(`var setTimeout = function(args) {
|
|
|
|
if (arguments.length < 1) {
|
|
|
|
throw TypeError("Failed to execute 'setTimeout': 1 argument required, but only 0 present.");
|
|
|
|
}
|
|
|
|
return _setTimeout.apply(this, arguments);
|
|
|
|
}`)
|
|
|
|
vm.Run(`var setInterval = function(args) {
|
|
|
|
if (arguments.length < 1) {
|
|
|
|
throw TypeError("Failed to execute 'setInterval': 1 argument required, but only 0 present.");
|
|
|
|
}
|
|
|
|
return _setInterval.apply(this, arguments);
|
|
|
|
}`)
|
2015-05-24 19:27:37 -05:00
|
|
|
vm.Set("clearTimeout", clearTimeout)
|
|
|
|
vm.Set("clearInterval", clearTimeout)
|
2015-04-21 19:31:59 -05:00
|
|
|
|
|
|
|
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]
|
2015-05-24 19:27:37 -05:00
|
|
|
_, err := vm.Call(`Function.call.call`, nil, arguments...)
|
2015-04-21 19:31:59 -05:00
|
|
|
if err != nil {
|
2015-05-07 12:56:44 -05:00
|
|
|
fmt.Println("js error:", err, arguments)
|
2015-04-21 19:31:59 -05:00
|
|
|
}
|
2015-11-23 05:03:31 -06:00
|
|
|
|
2015-09-16 04:57:33 -05:00
|
|
|
_, inreg := registry[timer] // when clearInterval is called from within the callback don't reset it
|
|
|
|
if timer.interval && inreg {
|
2015-04-21 19:31:59 -05:00
|
|
|
timer.timer.Reset(timer.duration)
|
|
|
|
} else {
|
|
|
|
delete(registry, timer)
|
|
|
|
if waitForCallbacks && (len(registry) == 0) {
|
|
|
|
break loop
|
|
|
|
}
|
|
|
|
}
|
2015-05-24 19:27:37 -05:00
|
|
|
case req := <-self.evalQueue:
|
2015-04-21 19:31:59 -05:00
|
|
|
// run the code, send the result back
|
2015-05-24 19:27:37 -05:00
|
|
|
req.fn(vm)
|
|
|
|
close(req.done)
|
2015-04-21 19:31:59 -05:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-20 07:28:34 -06:00
|
|
|
// Do executes the given function on the JS event loop.
|
|
|
|
func (self *JSRE) Do(fn func(*otto.Otto)) {
|
2015-05-24 19:27:37 -05:00
|
|
|
done := make(chan bool)
|
|
|
|
req := &evalReq{fn, done}
|
|
|
|
self.evalQueue <- req
|
|
|
|
<-done
|
|
|
|
}
|
|
|
|
|
2015-04-21 19:31:59 -05:00
|
|
|
// stops the event loop before exit, optionally waits for all timers to expire
|
|
|
|
func (self *JSRE) Stop(waitForCallbacks bool) {
|
2016-06-02 14:20:32 -05:00
|
|
|
select {
|
|
|
|
case <-self.closed:
|
|
|
|
case self.stopEventLoop <- waitForCallbacks:
|
|
|
|
<-self.closed
|
|
|
|
}
|
2015-04-21 19:31:59 -05:00
|
|
|
}
|
|
|
|
|
2015-03-15 01:13:39 -05:00
|
|
|
// 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 {
|
2015-05-24 19:27:37 -05:00
|
|
|
code, err := ioutil.ReadFile(common.AbsolutePath(self.assetPath, file))
|
2015-03-15 01:13:39 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-04-20 16:17:00 -05:00
|
|
|
var script *otto.Script
|
|
|
|
self.Do(func(vm *otto.Otto) {
|
|
|
|
script, err = vm.Compile(file, code)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
_, err = vm.Run(script)
|
|
|
|
})
|
2015-03-15 01:13:39 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-05-24 19:27:37 -05:00
|
|
|
// 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)
|
2015-03-15 01:13:39 -05:00
|
|
|
}
|
|
|
|
|
2015-05-24 19:27:37 -05:00
|
|
|
// Run runs a piece of JS code.
|
|
|
|
func (self *JSRE) Run(code string) (v otto.Value, err error) {
|
2016-02-20 07:28:34 -06:00
|
|
|
self.Do(func(vm *otto.Otto) { v, err = vm.Run(code) })
|
2015-05-24 19:27:37 -05:00
|
|
|
return v, err
|
2015-03-15 01:13:39 -05:00
|
|
|
}
|
|
|
|
|
2015-05-24 19:27:37 -05:00
|
|
|
// Get returns the value of a variable in the JS environment.
|
|
|
|
func (self *JSRE) Get(ns string) (v otto.Value, err error) {
|
2016-02-20 07:28:34 -06:00
|
|
|
self.Do(func(vm *otto.Otto) { v, err = vm.Get(ns) })
|
2015-05-24 19:27:37 -05:00
|
|
|
return v, err
|
2015-03-15 01:13:39 -05:00
|
|
|
}
|
|
|
|
|
2015-05-24 19:27:37 -05:00
|
|
|
// Set assigns value v to a variable in the JS environment.
|
|
|
|
func (self *JSRE) Set(ns string, v interface{}) (err error) {
|
2016-02-20 07:28:34 -06:00
|
|
|
self.Do(func(vm *otto.Otto) { err = vm.Set(ns, v) })
|
2015-05-24 19:27:37 -05:00
|
|
|
return err
|
2015-03-15 01:13:39 -05:00
|
|
|
}
|
|
|
|
|
2015-05-24 19:27:37 -05:00
|
|
|
// loadScript executes a JS script from inside the currently executing JS code.
|
2015-03-15 01:13:39 -05:00
|
|
|
func (self *JSRE) loadScript(call otto.FunctionCall) otto.Value {
|
|
|
|
file, err := call.Argument(0).ToString()
|
|
|
|
if err != nil {
|
2015-05-24 19:27:37 -05:00
|
|
|
// TODO: throw exception
|
|
|
|
return otto.FalseValue()
|
|
|
|
}
|
|
|
|
file = common.AbsolutePath(self.assetPath, file)
|
|
|
|
source, err := ioutil.ReadFile(file)
|
|
|
|
if err != nil {
|
|
|
|
// TODO: throw exception
|
2015-03-15 01:13:39 -05:00
|
|
|
return otto.FalseValue()
|
|
|
|
}
|
2015-05-24 19:27:37 -05:00
|
|
|
if _, err := compileAndRun(call.Otto, file, source); err != nil {
|
|
|
|
// TODO: throw exception
|
2015-03-15 01:13:39 -05:00
|
|
|
fmt.Println("err:", err)
|
|
|
|
return otto.FalseValue()
|
|
|
|
}
|
2015-05-24 19:27:37 -05:00
|
|
|
// TODO: return evaluation result
|
2015-03-15 01:13:39 -05:00
|
|
|
return otto.TrueValue()
|
|
|
|
}
|
|
|
|
|
2016-05-06 04:40:23 -05:00
|
|
|
// Evaluate executes code and pretty prints the result to the specified output
|
|
|
|
// stream.
|
|
|
|
func (self *JSRE) Evaluate(code string, w io.Writer) error {
|
|
|
|
var fail error
|
|
|
|
|
2016-02-20 07:28:34 -06:00
|
|
|
self.Do(func(vm *otto.Otto) {
|
2016-05-06 04:40:23 -05:00
|
|
|
val, err := vm.Run(code)
|
2015-05-24 19:27:37 -05:00
|
|
|
if err != nil {
|
2016-05-11 09:28:29 -05:00
|
|
|
prettyError(vm, err, w)
|
2016-05-06 04:40:23 -05:00
|
|
|
} else {
|
|
|
|
prettyPrint(vm, val, w)
|
2015-05-24 19:27:37 -05:00
|
|
|
}
|
2016-05-11 09:28:29 -05:00
|
|
|
fmt.Fprintln(w)
|
2015-05-24 19:27:37 -05:00
|
|
|
})
|
2016-05-06 04:40:23 -05:00
|
|
|
return fail
|
2015-03-15 01:13:39 -05:00
|
|
|
}
|
2015-03-24 11:49:28 -05:00
|
|
|
|
2015-05-24 19:27:37 -05:00
|
|
|
// Compile compiles and then runs a piece of JS code.
|
|
|
|
func (self *JSRE) Compile(filename string, src interface{}) (err error) {
|
2016-02-20 07:28:34 -06:00
|
|
|
self.Do(func(vm *otto.Otto) { _, err = compileAndRun(vm, filename, src) })
|
2015-05-24 19:27:37 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func compileAndRun(vm *otto.Otto, filename string, src interface{}) (otto.Value, error) {
|
|
|
|
script, err := vm.Compile(filename, src)
|
2015-03-24 11:49:28 -05:00
|
|
|
if err != nil {
|
2015-05-24 19:27:37 -05:00
|
|
|
return otto.Value{}, err
|
2015-03-24 11:49:28 -05:00
|
|
|
}
|
2015-05-24 19:27:37 -05:00
|
|
|
return vm.Run(script)
|
2015-03-24 11:49:28 -05:00
|
|
|
}
|