aboutsummaryrefslogtreecommitdiffstats
path: root/ethutil/natspec/natspec.go
blob: 00e6f8720fd73ca692fdcc18b40ab6e6063841bd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package natspec

import (
    "github.com/obscuren/otto"
    "io/ioutil"
)

type NatSpec struct {
    jsvm *otto.Otto
}

func NewNATSpec(transaction string) (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))
    if err != nil {
        return
    }
    _, err = self.jsvm.Run("var natspec = require('natspec');")
    if err != nil {
        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) {

    _, err = self.jsvm.Run("var method = '" + method + "';")
    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);")
    if err != nil {
        return err.Error()
    }
    return value.String()

}