mirror of https://github.com/ethereum/go-ethereum
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
64 lines
1.7 KiB
64 lines
1.7 KiB
10 years ago
|
package natspec
|
||
|
|
||
|
import (
|
||
10 years ago
|
"fmt"
|
||
10 years ago
|
"github.com/obscuren/otto"
|
||
10 years ago
|
)
|
||
|
|
||
|
type NatSpec struct {
|
||
10 years ago
|
jsvm *otto.Otto
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
// TODO: should initialise with abi and userdoc jsons
|
||
10 years ago
|
func New() (self *NatSpec, err error) {
|
||
10 years ago
|
|
||
|
self = new(NatSpec)
|
||
10 years ago
|
self.jsvm = otto.New()
|
||
10 years ago
|
|
||
10 years ago
|
_, err = self.jsvm.Run(natspecJS)
|
||
10 years ago
|
if err != nil {
|
||
|
return
|
||
|
}
|
||
10 years ago
|
_, err = self.jsvm.Run("var natspec = require('natspec');")
|
||
10 years ago
|
if err != nil {
|
||
|
return
|
||
|
}
|
||
|
|
||
|
return
|
||
|
}
|
||
|
|
||
10 years ago
|
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)
|
||
|
}
|
||
10 years ago
|
|
||
10 years ago
|
if _, err = self.jsvm.Run("var abi = " + abi + ";"); err != nil {
|
||
|
return "", fmt.Errorf("natspec.js error setting abi: %v", err)
|
||
|
}
|
||
10 years ago
|
|
||
10 years ago
|
if _, err = self.jsvm.Run("var method = '" + method + "';"); err != nil {
|
||
|
return "", fmt.Errorf("natspec.js error setting method: %v", err)
|
||
|
}
|
||
10 years ago
|
|
||
10 years ago
|
if _, err = self.jsvm.Run("var expression = \"" + expression + "\";"); err != nil {
|
||
|
return "", fmt.Errorf("natspec.js error setting expression: %v", err)
|
||
|
}
|
||
10 years ago
|
|
||
10 years ago
|
self.jsvm.Run("var call = {method: method,abi: abi,transaction: transaction};")
|
||
|
value, err := self.jsvm.Run("natspec.evaluateExpression(expression, call);")
|
||
10 years ago
|
if err != nil {
|
||
10 years ago
|
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)
|
||
10 years ago
|
}
|
||
10 years ago
|
if len(value.String()) == 0 {
|
||
|
return "", fmt.Errorf("natspec.js error evaluating expression")
|
||
|
}
|
||
|
|
||
|
return value.String(), nil
|
||
10 years ago
|
|
||
|
}
|