aboutsummaryrefslogtreecommitdiffstats
path: root/Godeps/_workspace/src/github.com/obscuren/otto/script.go
blob: ed8aebbf49e15eea18a9c6c095aae558bda43ce4 (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package otto

import (
    "bytes"
    "encoding/gob"
    "errors"

    "github.com/robertkrimen/otto/parser"
)

var ErrVersion = errors.New("version mismatch")

var scriptVersion = "2014-04-13/1"

// Script is a handle for some (reusable) JavaScript.
// Passing a Script value to a run method will evaluate the JavaScript.
//
type Script struct {
    version  string
    program  *_nodeProgram
    filename string
    src      string
}

// Compile will parse the given source and return a Script value or nil and
// an error if there was a problem during compilation.
//
//      script, err := vm.Compile("", `var abc; if (!abc) abc = 0; abc += 2; abc;`)
//      vm.Run(script)
//
func (self *Otto) Compile(filename string, src interface{}) (*Script, error) {
    {
        src, err := parser.ReadSource(filename, src)
        if err != nil {
            return nil, err
        }

        program, err := self.runtime.parse(filename, src)
        if err != nil {
            return nil, err
        }

        cmpl_program := cmpl_parse(program)

        script := &Script{
            version:  scriptVersion,
            program:  cmpl_program,
            filename: filename,
            src:      string(src),
        }

        return script, nil
    }
}

func (self *Script) String() string {
    return "// " + self.filename + "\n" + self.src
}

// MarshalBinary will marshal a script into a binary form. A marshalled script
// that is later unmarshalled can be executed on the same version of the otto runtime.
//
// The binary format can change at any time and should be considered unspecified and opaque.
//
func (self *Script) marshalBinary() ([]byte, error) {
    var bfr bytes.Buffer
    encoder := gob.NewEncoder(&bfr)
    err := encoder.Encode(self.version)
    if err != nil {
        return nil, err
    }
    err = encoder.Encode(self.program)
    if err != nil {
        return nil, err
    }
    err = encoder.Encode(self.filename)
    if err != nil {
        return nil, err
    }
    err = encoder.Encode(self.src)
    if err != nil {
        return nil, err
    }
    return bfr.Bytes(), nil
}

// UnmarshalBinary will vivify a marshalled script into something usable. If the script was
// originally marshalled on a different version of the otto runtime, then this method
// will return an error.
//
// The binary format can change at any time and should be considered unspecified and opaque.
//
func (self *Script) unmarshalBinary(data []byte) error {
    decoder := gob.NewDecoder(bytes.NewReader(data))
    err := decoder.Decode(&self.version)
    if err != nil {
        goto error
    }
    if self.version != scriptVersion {
        err = ErrVersion
        goto error
    }
    err = decoder.Decode(&self.program)
    if err != nil {
        goto error
    }
    err = decoder.Decode(&self.filename)
    if err != nil {
        goto error
    }
    err = decoder.Decode(&self.src)
    if err != nil {
        goto error
    }
    return nil
error:
    self.version = ""
    self.program = nil
    self.filename = ""
    self.src = ""
    return err
}