2017-08-04 16:14:17 +00:00
|
|
|
package console
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/robertkrimen/otto"
|
2017-09-25 18:22:57 +00:00
|
|
|
"github.com/status-im/status-go/geth/signal"
|
2017-08-04 16:14:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Write provides the base function to write data to the underline writer
|
|
|
|
// for the underline otto vm.
|
|
|
|
func Write(fn otto.FunctionCall, w io.Writer, consoleEventName string) otto.Value {
|
2017-09-25 18:22:57 +00:00
|
|
|
signal.Send(signal.Envelope{
|
2017-08-04 16:14:17 +00:00
|
|
|
Type: consoleEventName,
|
|
|
|
Event: convertArgs(fn.ArgumentList),
|
|
|
|
})
|
|
|
|
|
|
|
|
// Next print out the giving values.
|
|
|
|
fmt.Fprintf(w, "%s: %s", consoleEventName, formatForConsole(fn.ArgumentList))
|
|
|
|
|
|
|
|
return otto.UndefinedValue()
|
|
|
|
}
|
|
|
|
|
|
|
|
// formatForConsole handles conversion of giving otto.Values into
|
|
|
|
// string counter part.
|
|
|
|
func formatForConsole(argumentList []otto.Value) string {
|
|
|
|
output := []string{}
|
|
|
|
for _, argument := range argumentList {
|
|
|
|
output = append(output, fmt.Sprintf("%v", argument))
|
|
|
|
}
|
|
|
|
return strings.Join(output, " ")
|
|
|
|
}
|
|
|
|
|
|
|
|
// convertArgs attempts to convert otto.Values into proper go types else
|
|
|
|
// uses original.
|
|
|
|
func convertArgs(argumentList []otto.Value) []interface{} {
|
|
|
|
var items []interface{}
|
|
|
|
|
|
|
|
for _, arg := range argumentList {
|
|
|
|
realArg, err := arg.Export()
|
|
|
|
if err != nil {
|
|
|
|
items = append(items, arg)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
items = append(items, realArg)
|
|
|
|
}
|
|
|
|
|
|
|
|
return items
|
|
|
|
}
|