aboutsummaryrefslogtreecommitdiffstats
path: root/jsre/jsre.go
diff options
context:
space:
mode:
Diffstat (limited to 'jsre/jsre.go')
-rw-r--r--jsre/jsre.go33
1 files changed, 8 insertions, 25 deletions
diff --git a/jsre/jsre.go b/jsre/jsre.go
index d4c982897..bb0cc71ed 100644
--- a/jsre/jsre.go
+++ b/jsre/jsre.go
@@ -65,7 +65,6 @@ func New(assetPath string) *JSRE {
}
re.loopWg.Add(1)
go re.runEventLoop()
- re.Compile("pp.js", pp_js) // load prettyprint func definition
re.Set("loadScript", re.loadScript)
return re
}
@@ -255,35 +254,19 @@ func (self *JSRE) loadScript(call otto.FunctionCall) otto.Value {
return otto.TrueValue()
}
-// PrettyPrint writes v to standard output.
-func (self *JSRE) PrettyPrint(v interface{}) (val otto.Value, err error) {
- var method otto.Value
+// EvalAndPrettyPrint evaluates code and pretty prints the result to
+// standard output.
+func (self *JSRE) EvalAndPrettyPrint(code string) (err error) {
self.do(func(vm *otto.Otto) {
- val, err = vm.ToValue(v)
+ var val otto.Value
+ val, err = vm.Run(code)
if err != nil {
return
}
- method, err = vm.Get("prettyPrint")
- if err != nil {
- return
- }
- val, err = method.Call(method, val)
+ prettyPrint(vm, val)
+ fmt.Println()
})
- return val, err
-}
-
-// Eval evaluates JS function and returns result in a pretty printed string format.
-func (self *JSRE) Eval(code string) (s string, err error) {
- var val otto.Value
- val, err = self.Run(code)
- if err != nil {
- return
- }
- val, err = self.PrettyPrint(val)
- if err != nil {
- return
- }
- return fmt.Sprintf("%v", val), nil
+ return err
}
// Compile compiles and then runs a piece of JS code.