go-ethereum/ethutil/natspec/natspec.go

66 lines
1.1 KiB
Go
Raw Normal View History

2015-03-05 20:43:34 -06:00
package natspec
import (
"github.com/obscuren/otto"
2015-03-05 20:43:34 -06:00
"io/ioutil"
)
type NatSpec struct {
jsvm *otto.Otto
2015-03-05 20:43:34 -06:00
}
func NewNATSpec(transaction string) (self *NatSpec, err error) {
2015-03-05 20:43:34 -06:00
self = new(NatSpec)
self.jsvm = otto.New()
2015-03-05 20:43:34 -06:00
code, err := ioutil.ReadFile("natspec.js")
if err != nil {
return
}
_, err = self.jsvm.Run(string(code))
2015-03-05 20:43:34 -06:00
if err != nil {
return
}
_, err = self.jsvm.Run("var natspec = require('natspec');")
2015-03-05 20:43:34 -06:00
if err != nil {
return
}
self.jsvm.Run("var transaction = " + transaction + ";")
2015-03-05 20:43:34 -06:00
return
}
func (self *NatSpec) SetDescription(desc string) (err error) {
_, err = self.jsvm.Run("var expression = \"" + desc + "\";")
2015-03-05 20:43:34 -06:00
return
}
func (self *NatSpec) SetABI(abi string) (err error) {
_, err = self.jsvm.Run("var abi = " + abi + ";")
2015-03-05 20:43:34 -06:00
return
}
func (self *NatSpec) SetMethod(method string) (err error) {
_, err = self.jsvm.Run("var method = '" + method + "';")
2015-03-05 20:43:34 -06:00
return
}
func (self *NatSpec) Parse() string {
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 {
return err.Error()
}
return value.String()
}