2017-08-04 16:14:17 +00:00
|
|
|
package jail
|
|
|
|
|
|
|
|
import (
|
|
|
|
"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"
|
|
|
|
"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-08-04 16:14:17 +00:00
|
|
|
id string
|
2017-09-08 11:55:17 +00:00
|
|
|
*vm.VM
|
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-09-08 11:55:17 +00:00
|
|
|
// start loop in a goroutine
|
2017-09-02 17:04:23 +00:00
|
|
|
// Cell is currently immortal, so the loop
|
2017-09-01 20:17:34 +00:00
|
|
|
go lo.Run()
|
|
|
|
|
2017-09-02 17:04:23 +00:00
|
|
|
return &Cell{
|
2017-08-04 16:14:17 +00:00
|
|
|
id: id,
|
2017-09-08 11:55:17 +00:00
|
|
|
VM: cellVM,
|
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
|
|
|
}
|