diff options
author | zelig <viktor.tron@gmail.com> | 2015-03-08 21:09:13 +0800 |
---|---|---|
committer | zelig <viktor.tron@gmail.com> | 2015-03-08 21:09:13 +0800 |
commit | 97dc4edb4569225f4b819717a2fabf0209d312e0 (patch) | |
tree | 2097e453c15fc9f4d725165927018598e42e1358 /ethutil/natspec/natspec.go | |
parent | 07955b30419a26b9b213f71955a02a49995dc0e3 (diff) | |
download | dexon-97dc4edb4569225f4b819717a2fabf0209d312e0.tar dexon-97dc4edb4569225f4b819717a2fabf0209d312e0.tar.gz dexon-97dc4edb4569225f4b819717a2fabf0209d312e0.tar.bz2 dexon-97dc4edb4569225f4b819717a2fabf0209d312e0.tar.lz dexon-97dc4edb4569225f4b819717a2fabf0209d312e0.tar.xz dexon-97dc4edb4569225f4b819717a2fabf0209d312e0.tar.zst dexon-97dc4edb4569225f4b819717a2fabf0209d312e0.zip |
natspec for poc-9
- put natspec.js in a heredoc string (to help binary)
- constructor takes no args, just sets up js vm
- API: only exports Notice(transaction, abi, method, expression string) (string, error)
- effort to turn natspec.js error strings to proper go errors
- test errors
Diffstat (limited to 'ethutil/natspec/natspec.go')
-rw-r--r-- | ethutil/natspec/natspec.go | 58 |
1 files changed, 28 insertions, 30 deletions
diff --git a/ethutil/natspec/natspec.go b/ethutil/natspec/natspec.go index 00e6f8720..ebc2658da 100644 --- a/ethutil/natspec/natspec.go +++ b/ethutil/natspec/natspec.go @@ -1,24 +1,21 @@ package natspec import ( + "fmt" "github.com/obscuren/otto" - "io/ioutil" ) type NatSpec struct { jsvm *otto.Otto } -func NewNATSpec(transaction string) (self *NatSpec, err error) { +// TODO: should initialise with abi and userdoc jsons +func NewNATSpec() (self *NatSpec, err error) { self = new(NatSpec) self.jsvm = otto.New() - code, err := ioutil.ReadFile("natspec.js") - if err != nil { - return - } - _, err = self.jsvm.Run(string(code)) + _, err = self.jsvm.Run(natspecJS) if err != nil { return } @@ -27,39 +24,40 @@ func NewNATSpec(transaction string) (self *NatSpec, err error) { return } - self.jsvm.Run("var transaction = " + transaction + ";") - - return -} - -func (self *NatSpec) SetDescription(desc string) (err error) { - - _, err = self.jsvm.Run("var expression = \"" + desc + "\";") return - } -func (self *NatSpec) SetABI(abi string) (err error) { - - _, err = self.jsvm.Run("var abi = " + abi + ";") - return - -} - -func (self *NatSpec) SetMethod(method string) (err error) { +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) + } - _, err = self.jsvm.Run("var method = '" + method + "';") - return + if _, err = self.jsvm.Run("var abi = " + abi + ";"); err != nil { + return "", fmt.Errorf("natspec.js error setting abi: %v", err) + } -} + if _, err = self.jsvm.Run("var method = '" + method + "';"); err != nil { + return "", fmt.Errorf("natspec.js error setting method: %v", err) + } -func (self *NatSpec) Parse() string { + if _, err = self.jsvm.Run("var expression = \"" + expression + "\";"); err != nil { + return "", fmt.Errorf("natspec.js error setting expression: %v", err) + } self.jsvm.Run("var call = {method: method,abi: abi,transaction: transaction};") value, err := self.jsvm.Run("natspec.evaluateExpression(expression, call);") if err != nil { - return err.Error() + 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) } - return value.String() + if len(value.String()) == 0 { + return "", fmt.Errorf("natspec.js error evaluating expression") + } + + return value.String(), nil } |