2017-08-04 16:14:17 +00:00
|
|
|
package jail
|
|
|
|
|
|
|
|
import (
|
2017-10-06 16:52:26 +00:00
|
|
|
"context"
|
|
|
|
|
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-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
|
|
|
|
lo *loop.Loop
|
2017-08-04 16:14:17 +00:00
|
|
|
}
|
|
|
|
|
2017-09-02 17:04:23 +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-09-08 11:55:17 +00:00
|
|
|
func newCell(id string, ottoVM *otto.Otto) (*Cell, error) {
|
|
|
|
cellVM := vm.New(ottoVM)
|
2017-09-01 20:17:34 +00:00
|
|
|
|
2017-09-08 11:55:17 +00:00
|
|
|
lo := loop.New(cellVM)
|
|
|
|
|
|
|
|
registerVMHandlers(cellVM, lo)
|
2017-08-04 16:14:17 +00:00
|
|
|
|
2017-10-06 16:52:26 +00:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
|
|
|
|
// start event loop in background
|
|
|
|
go lo.Run(ctx)
|
2017-09-01 20:17:34 +00:00
|
|
|
|
2017-09-02 17:04:23 +00:00
|
|
|
return &Cell{
|
2017-10-06 16:52:26 +00:00
|
|
|
VM: cellVM,
|
|
|
|
id: id,
|
|
|
|
cancel: cancel,
|
|
|
|
lo: lo,
|
2017-08-04 16:14:17 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
func registerVMHandlers(v *vm.VM, lo *loop.Loop) error {
|
|
|
|
// setTimeout/setInterval functions
|
|
|
|
if err := timers.Define(v, lo); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-08-04 16:14:17 +00:00
|
|
|
|
2017-09-08 11:55:17 +00:00
|
|
|
// FetchAPI functions
|
|
|
|
if err := fetch.Define(v, lo); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-08-04 16:14:17 +00:00
|
|
|
|
2017-09-08 11:55:17 +00:00
|
|
|
return nil
|
2017-08-04 16:14:17 +00:00
|
|
|
}
|
2017-10-06 16:52:26 +00:00
|
|
|
|
|
|
|
// Stop halts event loop associated with cell.
|
|
|
|
func (c *Cell) Stop() {
|
|
|
|
c.cancel()
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
func (c *Cell) CallAsync(fn otto.Value, args ...interface{}) {
|
|
|
|
task := looptask.NewCallTask(fn, args...)
|
|
|
|
c.lo.Add(task)
|
|
|
|
// TODO(divan): review API of `loop` package, it's contrintuitive
|
|
|
|
go c.lo.Ready(task)
|
|
|
|
}
|