rum-goggles/v1/vendor/github.com/robertkrimen/otto/script.go

110 lines
2.8 KiB
Go
Raw Normal View History

package otto
import (
"bytes"
"encoding/gob"
"errors"
)
2024-04-04 14:46:14 +00:00
// ErrVersion is an error which represents a version mismatch.
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
2024-04-04 14:46:14 +00:00
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)
2024-04-04 14:46:14 +00:00
func (o *Otto) Compile(filename string, src interface{}) (*Script, error) {
return o.CompileWithSourceMap(filename, src, nil)
}
// CompileWithSourceMap does the same thing as Compile, but with the obvious
// difference of applying a source map.
2024-04-04 14:46:14 +00:00
func (o *Otto) CompileWithSourceMap(filename string, src, sm interface{}) (*Script, error) {
program, err := o.runtime.parse(filename, src, sm)
if err != nil {
return nil, err
}
2024-04-04 14:46:14 +00:00
node := cmplParse(program)
script := &Script{
version: scriptVersion,
2024-04-04 14:46:14 +00:00
program: node,
filename: filename,
src: program.File.Source(),
}
return script, nil
}
2024-04-04 14:46:14 +00:00
func (s *Script) String() string {
return "// " + s.filename + "\n" + s.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.
2024-04-04 14:46:14 +00:00
func (s *Script) marshalBinary() ([]byte, error) {
var bfr bytes.Buffer
encoder := gob.NewEncoder(&bfr)
2024-04-04 14:46:14 +00:00
err := encoder.Encode(s.version)
if err != nil {
return nil, err
}
2024-04-04 14:46:14 +00:00
err = encoder.Encode(s.program)
if err != nil {
return nil, err
}
2024-04-04 14:46:14 +00:00
err = encoder.Encode(s.filename)
if err != nil {
return nil, err
}
2024-04-04 14:46:14 +00:00
err = encoder.Encode(s.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.
2024-04-04 14:46:14 +00:00
func (s *Script) unmarshalBinary(data []byte) (err error) { //nolint: nonamedreturns
decoder := gob.NewDecoder(bytes.NewReader(data))
defer func() {
if err != nil {
2024-04-04 14:46:14 +00:00
s.version = ""
s.program = nil
s.filename = ""
s.src = ""
}
}()
2024-04-04 14:46:14 +00:00
if err = decoder.Decode(&s.version); err != nil {
return err
}
2024-04-04 14:46:14 +00:00
if s.version != scriptVersion {
return ErrVersion
}
2024-04-04 14:46:14 +00:00
if err = decoder.Decode(&s.program); err != nil {
return err
}
2024-04-04 14:46:14 +00:00
if err = decoder.Decode(&s.filename); err != nil {
return err
}
2024-04-04 14:46:14 +00:00
return decoder.Decode(&s.src)
}