2017-08-04 16:14:17 +00:00
|
|
|
package jail
|
|
|
|
|
|
|
|
import (
|
2017-10-06 16:52:26 +00:00
|
|
|
"context"
|
2017-11-07 17:36:42 +00:00
|
|
|
"errors"
|
|
|
|
"time"
|
2017-10-06 16:52:26 +00:00
|
|
|
|
2017-08-04 16:14:17 +00:00
|
|
|
"github.com/robertkrimen/otto"
|
2018-06-08 11:29:50 +00:00
|
|
|
"github.com/status-im/status-go/jail/internal/fetch"
|
|
|
|
"github.com/status-im/status-go/jail/internal/loop"
|
|
|
|
"github.com/status-im/status-go/jail/internal/loop/looptask"
|
|
|
|
"github.com/status-im/status-go/jail/internal/timers"
|
|
|
|
"github.com/status-im/status-go/jail/internal/vm"
|
2017-08-04 16:14:17 +00:00
|
|
|
)
|
|
|
|
|
2017-12-28 20:50:36 +00:00
|
|
|
const timeout = 5 * time.Second
|
|
|
|
|
2018-03-01 16:48:30 +00:00
|
|
|
// Manager defines methods for managing jailed environments
|
|
|
|
type Manager interface {
|
|
|
|
// Call executes given JavaScript function w/i a jail cell context identified by the chatID.
|
|
|
|
Call(chatID, this, args string) string
|
|
|
|
|
|
|
|
// CreateCell creates a new jail cell.
|
|
|
|
CreateCell(chatID string) (JSCell, error)
|
|
|
|
|
|
|
|
// Parse creates a new jail cell context, with the given chatID as identifier.
|
|
|
|
// New context executes provided JavaScript code, right after the initialization.
|
|
|
|
// DEPRECATED in favour of CreateAndInitCell.
|
|
|
|
Parse(chatID, js string) string
|
|
|
|
|
|
|
|
// CreateAndInitCell creates a new jail cell and initialize it
|
|
|
|
// with web3 and other handlers.
|
|
|
|
CreateAndInitCell(chatID string, code ...string) string
|
|
|
|
|
|
|
|
// Cell returns an existing instance of JSCell.
|
|
|
|
Cell(chatID string) (JSCell, error)
|
|
|
|
|
|
|
|
// Execute allows to run arbitrary JS code within a cell.
|
|
|
|
Execute(chatID, code string) string
|
|
|
|
|
|
|
|
// SetBaseJS allows to setup initial JavaScript to be loaded on each jail.CreateAndInitCell().
|
|
|
|
SetBaseJS(js string)
|
|
|
|
|
|
|
|
// Stop stops all background activity of jail
|
|
|
|
Stop()
|
|
|
|
}
|
|
|
|
|
|
|
|
// JSValue is a wrapper around an otto.Value.
|
|
|
|
type JSValue struct {
|
|
|
|
value otto.Value
|
|
|
|
}
|
|
|
|
|
|
|
|
// Value returns the underlying otto.Value from a JSValue. This value IS NOT THREADSAFE.
|
|
|
|
func (v *JSValue) Value() otto.Value {
|
|
|
|
return v.value
|
|
|
|
}
|
|
|
|
|
|
|
|
// JSCell represents single jail cell, which is basically a JavaScript VM.
|
|
|
|
// It's designed to be a transparent wrapper around otto.VM's methods.
|
|
|
|
type JSCell interface {
|
|
|
|
// Set a value inside VM.
|
|
|
|
Set(string, interface{}) error
|
|
|
|
// Get a value from VM.
|
|
|
|
Get(string) (JSValue, error)
|
|
|
|
// Run an arbitrary JS code. Input maybe string or otto.Script.
|
|
|
|
Run(interface{}) (JSValue, error)
|
|
|
|
// Call an arbitrary JS function by name and args.
|
|
|
|
Call(item string, this interface{}, args ...interface{}) (JSValue, error)
|
|
|
|
// Stop stops background execution of cell.
|
|
|
|
Stop() error
|
|
|
|
}
|
|
|
|
|
2017-09-02 17:04:23 +00:00
|
|
|
// Cell represents a single jail cell, which is basically a JavaScript VM.
|
|
|
|
type Cell struct {
|
2018-03-01 16:48:30 +00:00
|
|
|
jsvm *vm.VM
|
2017-10-06 16:52:26 +00:00
|
|
|
id string
|
|
|
|
cancel context.CancelFunc
|
2017-11-07 17:36:42 +00:00
|
|
|
|
|
|
|
loop *loop.Loop
|
|
|
|
loopStopped chan struct{}
|
|
|
|
loopErr error
|
2017-08-04 16:14:17 +00:00
|
|
|
}
|
|
|
|
|
2017-11-07 17:36:42 +00:00
|
|
|
// NewCell encapsulates what we need to create a new jailCell from the
|
2017-08-04 16:14:17 +00:00
|
|
|
// provided vm and eventloop instance.
|
2017-11-07 17:36:42 +00:00
|
|
|
func NewCell(id string) (*Cell, error) {
|
2018-06-12 16:50:25 +00:00
|
|
|
newVM := vm.New()
|
|
|
|
lo := loop.New(newVM)
|
2017-09-08 11:55:17 +00:00
|
|
|
|
2018-06-12 16:50:25 +00:00
|
|
|
err := registerVMHandlers(newVM, lo)
|
2017-10-20 09:06:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-08-04 16:14:17 +00:00
|
|
|
|
2017-10-06 16:52:26 +00:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
2017-11-07 17:36:42 +00:00
|
|
|
loopStopped := make(chan struct{})
|
|
|
|
cell := Cell{
|
2018-06-12 16:50:25 +00:00
|
|
|
jsvm: newVM,
|
2017-11-07 17:36:42 +00:00
|
|
|
id: id,
|
|
|
|
cancel: cancel,
|
|
|
|
loop: lo,
|
|
|
|
loopStopped: loopStopped,
|
|
|
|
}
|
2017-10-06 16:52:26 +00:00
|
|
|
|
2017-11-07 17:36:42 +00:00
|
|
|
// Start event loop in the background.
|
|
|
|
go func() {
|
|
|
|
err := lo.Run(ctx)
|
|
|
|
if err != context.Canceled {
|
|
|
|
cell.loopErr = err
|
|
|
|
}
|
2017-09-01 20:17:34 +00:00
|
|
|
|
2017-11-07 17:36:42 +00:00
|
|
|
close(loopStopped)
|
|
|
|
}()
|
|
|
|
|
|
|
|
return &cell, nil
|
2017-08-04 16:14:17 +00:00
|
|
|
}
|
|
|
|
|
2017-09-08 11:55:17 +00:00
|
|
|
// registerHandlers register variuous functions and handlers
|
|
|
|
// to the Otto VM, such as Fetch API callbacks or promises.
|
2017-11-07 17:36:42 +00:00
|
|
|
func registerVMHandlers(vm *vm.VM, lo *loop.Loop) error {
|
2017-09-08 11:55:17 +00:00
|
|
|
// setTimeout/setInterval functions
|
2017-11-07 17:36:42 +00:00
|
|
|
if err := timers.Define(vm, lo); err != nil {
|
2017-09-08 11:55:17 +00:00
|
|
|
return err
|
|
|
|
}
|
2017-08-04 16:14:17 +00:00
|
|
|
|
2017-09-08 11:55:17 +00:00
|
|
|
// FetchAPI functions
|
2017-11-07 17:36:42 +00:00
|
|
|
return fetch.Define(vm, lo)
|
2017-08-04 16:14:17 +00:00
|
|
|
}
|
2017-10-06 16:52:26 +00:00
|
|
|
|
|
|
|
// Stop halts event loop associated with cell.
|
2017-11-07 17:36:42 +00:00
|
|
|
func (c *Cell) Stop() error {
|
2017-10-06 16:52:26 +00:00
|
|
|
c.cancel()
|
2017-11-07 17:36:42 +00:00
|
|
|
|
|
|
|
select {
|
|
|
|
case <-c.loopStopped:
|
|
|
|
return c.loopErr
|
|
|
|
case <-time.After(time.Second):
|
|
|
|
return errors.New("stopping the cell timed out")
|
|
|
|
}
|
2017-10-06 16:52:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// CallAsync puts otto's function with given args into
|
|
|
|
// event queue loop and schedules for immediate execution.
|
|
|
|
// Intended to be used by any cell user that want's to run
|
|
|
|
// async call, like callback.
|
2017-12-28 20:50:36 +00:00
|
|
|
func (c *Cell) CallAsync(fn otto.Value, args ...interface{}) error {
|
2017-10-06 16:52:26 +00:00
|
|
|
task := looptask.NewCallTask(fn, args...)
|
2017-12-28 20:50:36 +00:00
|
|
|
errChan := make(chan error)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
defer close(errChan)
|
|
|
|
err := c.loop.AddAndExecute(task)
|
|
|
|
if err != nil {
|
|
|
|
errChan <- err
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
timer := time.NewTimer(timeout)
|
|
|
|
defer timer.Stop()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case err := <-errChan:
|
|
|
|
return err
|
|
|
|
|
|
|
|
case <-timer.C:
|
|
|
|
return errors.New("Timeout")
|
|
|
|
}
|
2018-03-01 16:48:30 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set calls Set on the underlying JavaScript VM.
|
|
|
|
func (c *Cell) Set(key string, val interface{}) error {
|
|
|
|
return c.jsvm.Set(key, val)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get calls Get on the underlying JavaScript VM and returns
|
|
|
|
// a wrapper around the otto.Value.
|
|
|
|
func (c *Cell) Get(key string) (JSValue, error) {
|
|
|
|
v, err := c.jsvm.Get(key)
|
|
|
|
if err != nil {
|
|
|
|
return JSValue{}, err
|
|
|
|
}
|
2018-06-12 16:50:25 +00:00
|
|
|
value := JSValue{value: v}
|
|
|
|
return value, nil
|
2018-03-01 16:48:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetObjectValue calls GetObjectValue on the underlying JavaScript VM and returns
|
|
|
|
// a wrapper around the otto.Value.
|
|
|
|
func (c *Cell) GetObjectValue(v otto.Value, name string) (JSValue, error) {
|
|
|
|
v, err := c.jsvm.GetObjectValue(v, name)
|
|
|
|
if err != nil {
|
|
|
|
return JSValue{}, err
|
|
|
|
}
|
2018-06-12 16:50:25 +00:00
|
|
|
value := JSValue{value: v}
|
|
|
|
return value, nil
|
2018-03-01 16:48:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Run calls Run on the underlying JavaScript VM and returns
|
|
|
|
// a wrapper around the otto.Value.
|
|
|
|
func (c *Cell) Run(src interface{}) (JSValue, error) {
|
|
|
|
v, err := c.jsvm.Run(src)
|
|
|
|
if err != nil {
|
|
|
|
return JSValue{}, err
|
|
|
|
}
|
2018-06-12 16:50:25 +00:00
|
|
|
value := JSValue{value: v}
|
|
|
|
return value, nil
|
2018-03-01 16:48:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Call calls Call on the underlying JavaScript VM and returns
|
|
|
|
// a wrapper around the otto.Value.
|
|
|
|
func (c *Cell) Call(item string, this interface{}, args ...interface{}) (JSValue, error) {
|
|
|
|
v, err := c.jsvm.Call(item, this, args...)
|
|
|
|
if err != nil {
|
|
|
|
return JSValue{}, err
|
|
|
|
}
|
2018-06-12 16:50:25 +00:00
|
|
|
value := JSValue{value: v}
|
|
|
|
return value, nil
|
2017-10-06 16:52:26 +00:00
|
|
|
}
|