console: run initConsole before initWeb3

This commit is contained in:
Felix Lange 2020-01-22 02:34:53 +01:00
parent 87e0aa707f
commit 77b900767c

View file

@ -108,14 +108,15 @@ func New(config Config) (*Console, error) {
// init retrieves the available APIs from the remote RPC provider and initializes // init retrieves the available APIs from the remote RPC provider and initializes
// the console's JavaScript namespaces based on the exposed modules. // the console's JavaScript namespaces based on the exposed modules.
func (c *Console) init(preload []string) error { func (c *Console) init(preload []string) error {
c.initConsoleObject()
// Initialize the JavaScript <-> Go RPC bridge. // Initialize the JavaScript <-> Go RPC bridge.
bridge := newBridge(c.client, c.prompter, c.printer) bridge := newBridge(c.client, c.prompter, c.printer)
if err := c.initWeb3(bridge); err != nil { if err := c.initWeb3(bridge); err != nil {
return err return err
} }
c.jsre.Do(func(vm *goja.Runtime) { c.initConsoleObject(vm) })
// Load the supported APIs into the JavaScript runtime environment // Load the supported APIs.
apis, err := c.client.SupportedModules() apis, err := c.client.SupportedModules()
if err != nil { if err != nil {
return fmt.Errorf("api modules: %v", err) return fmt.Errorf("api modules: %v", err)
@ -140,12 +141,13 @@ func (c *Console) init(preload []string) error {
return fmt.Errorf("namespace flattening: %v", err) return fmt.Errorf("namespace flattening: %v", err)
} }
// Add bridge overrides for web3.js functionality.
c.jsre.Do(func(vm *goja.Runtime) { c.jsre.Do(func(vm *goja.Runtime) {
c.initAdmin(vm, bridge) c.initAdmin(vm, bridge)
c.initPersonal(vm, bridge) c.initPersonal(vm, bridge)
}) })
// Preload any JavaScript files before starting the console // Preload JavaScript files.
for _, path := range preload { for _, path := range preload {
if err := c.jsre.Exec(path); err != nil { if err := c.jsre.Exec(path); err != nil {
failure := err.Error() failure := err.Error()
@ -156,7 +158,7 @@ func (c *Console) init(preload []string) error {
} }
} }
// Configure the console's input prompter for history and tab completion. // Configure the input prompter for history and tab completion.
if c.prompter != nil { if c.prompter != nil {
if content, err := ioutil.ReadFile(c.histPath); err != nil { if content, err := ioutil.ReadFile(c.histPath); err != nil {
c.prompter.SetHistory(nil) c.prompter.SetHistory(nil)
@ -169,6 +171,15 @@ func (c *Console) init(preload []string) error {
return nil return nil
} }
func (c *Console) initConsoleObject() {
c.jsre.Do(func(vm *goja.Runtime) {
console := vm.NewObject()
console.Set("log", c.consoleOutput)
console.Set("error", c.consoleOutput)
vm.Set("console", console)
})
}
func (c *Console) initWeb3(bridge *bridge) error { func (c *Console) initWeb3(bridge *bridge) error {
if err := c.jsre.Compile("bignumber.js", string(jsre.BignumberJs)); err != nil { if err := c.jsre.Compile("bignumber.js", string(jsre.BignumberJs)); err != nil {
return fmt.Errorf("bignumber.js: %v", err) return fmt.Errorf("bignumber.js: %v", err)
@ -190,13 +201,6 @@ func (c *Console) initWeb3(bridge *bridge) error {
return err return err
} }
func (c *Console) initConsoleObject(vm *goja.Runtime) {
console := vm.NewObject()
console.Set("log", c.consoleOutput)
console.Set("error", c.consoleOutput)
vm.Set("console", console)
}
// initAdmin creates additional admin APIs implemented by the bridge. // initAdmin creates additional admin APIs implemented by the bridge.
func (c *Console) initAdmin(vm *goja.Runtime, bridge *bridge) { func (c *Console) initAdmin(vm *goja.Runtime, bridge *bridge) {
if admin := getObject(vm, "admin"); admin != nil { if admin := getObject(vm, "admin"); admin != nil {