2015-03-05 20:43:34 -06:00
|
|
|
package natspec
|
|
|
|
|
|
|
|
import (
|
2015-03-08 08:09:13 -05:00
|
|
|
"fmt"
|
2015-03-20 07:22:01 -05:00
|
|
|
"github.com/robertkrimen/otto"
|
2015-03-05 20:43:34 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
type NatSpec struct {
|
2015-03-06 11:54:26 -06:00
|
|
|
jsvm *otto.Otto
|
2015-03-05 20:43:34 -06:00
|
|
|
}
|
|
|
|
|
2015-03-08 08:09:13 -05:00
|
|
|
// TODO: should initialise with abi and userdoc jsons
|
2015-03-08 08:17:20 -05:00
|
|
|
func New() (self *NatSpec, err error) {
|
2015-03-05 20:43:34 -06:00
|
|
|
|
|
|
|
self = new(NatSpec)
|
2015-03-06 11:54:26 -06:00
|
|
|
self.jsvm = otto.New()
|
2015-03-05 20:43:34 -06:00
|
|
|
|
2015-03-08 08:09:13 -05:00
|
|
|
_, err = self.jsvm.Run(natspecJS)
|
2015-03-05 20:43:34 -06:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2015-03-06 11:54:26 -06:00
|
|
|
_, err = self.jsvm.Run("var natspec = require('natspec');")
|
2015-03-05 20:43:34 -06:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-03-08 08:09:13 -05:00
|
|
|
func (self *NatSpec) Notice(transaction, abi, method, expression string) (string, error) {
|
|
|
|
var err error
|
|
|
|
if _, err = self.jsvm.Run("var transaction = " + transaction + ";"); err != nil {
|
|
|
|
return "", fmt.Errorf("natspec.js error setting transaction: %v", err)
|
|
|
|
}
|
2015-03-05 20:43:34 -06:00
|
|
|
|
2015-03-08 08:09:13 -05:00
|
|
|
if _, err = self.jsvm.Run("var abi = " + abi + ";"); err != nil {
|
|
|
|
return "", fmt.Errorf("natspec.js error setting abi: %v", err)
|
|
|
|
}
|
2015-03-05 20:43:34 -06:00
|
|
|
|
2015-03-08 08:09:13 -05:00
|
|
|
if _, err = self.jsvm.Run("var method = '" + method + "';"); err != nil {
|
|
|
|
return "", fmt.Errorf("natspec.js error setting method: %v", err)
|
|
|
|
}
|
2015-03-05 20:43:34 -06:00
|
|
|
|
2015-03-08 08:09:13 -05:00
|
|
|
if _, err = self.jsvm.Run("var expression = \"" + expression + "\";"); err != nil {
|
|
|
|
return "", fmt.Errorf("natspec.js error setting expression: %v", err)
|
|
|
|
}
|
2015-03-05 20:43:34 -06:00
|
|
|
|
2015-03-06 11:54:26 -06:00
|
|
|
self.jsvm.Run("var call = {method: method,abi: abi,transaction: transaction};")
|
|
|
|
value, err := self.jsvm.Run("natspec.evaluateExpression(expression, call);")
|
2015-03-05 20:43:34 -06:00
|
|
|
if err != nil {
|
2015-03-08 08:09:13 -05:00
|
|
|
return "", fmt.Errorf("natspec.js error evaluating expression: %v", err)
|
|
|
|
}
|
|
|
|
evalError := "Natspec evaluation failed, wrong input params"
|
|
|
|
if value.String() == evalError {
|
|
|
|
return "", fmt.Errorf("natspec.js error evaluating expression: wrong input params in expression '%s'", expression)
|
2015-03-05 20:43:34 -06:00
|
|
|
}
|
2015-03-08 08:09:13 -05:00
|
|
|
if len(value.String()) == 0 {
|
|
|
|
return "", fmt.Errorf("natspec.js error evaluating expression")
|
|
|
|
}
|
|
|
|
|
|
|
|
return value.String(), nil
|
2015-03-05 20:43:34 -06:00
|
|
|
|
|
|
|
}
|