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"
|
2017-09-08 11:55:17 +00:00
|
|
|
"github.com/status-im/status-go/geth/jail/internal/fetch"
|
|
|
|
"github.com/status-im/status-go/geth/jail/internal/loop"
|
2017-10-06 16:52:26 +00:00
|
|
|
"github.com/status-im/status-go/geth/jail/internal/loop/looptask"
|
2017-09-08 11:55:17 +00:00
|
|
|
"github.com/status-im/status-go/geth/jail/internal/timers"
|
|
|
|
"github.com/status-im/status-go/geth/jail/internal/vm"
|
2017-08-04 16:14:17 +00:00
|
|
|
)
|
|
|
|
|
2017-12-28 20:50:36 +00:00
|
|
|
const timeout = 5 * time.Second
|
|
|
|
|
2017-09-02 17:04:23 +00:00
|
|
|
// Cell represents a single jail cell, which is basically a JavaScript VM.
|
|
|
|
type Cell struct {
|
2017-09-08 11:55:17 +00:00
|
|
|
*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) {
|
|
|
|
vm := vm.New()
|
|
|
|
lo := loop.New(vm)
|
2017-09-08 11:55:17 +00:00
|
|
|
|
2017-11-07 17:36:42 +00:00
|
|
|
err := registerVMHandlers(vm, 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{
|
|
|
|
VM: vm,
|
|
|
|
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")
|
|
|
|
}
|
2017-10-06 16:52:26 +00:00
|
|
|
}
|