-
Notifications
You must be signed in to change notification settings - Fork 0
/
lua.go
84 lines (70 loc) · 1.76 KB
/
lua.go
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
package lua
import (
"github.com/spf13/afero"
"io"
"os"
"strings"
"github.com/tsatke/lua/internal/engine"
)
type Engine struct {
engine *engine.Engine
stdin io.Reader
stdout io.Writer
stderr io.Writer
workingDir string
scannerType ScannerType
}
func EvalString(in string) error {
_, err := NewEngine().EvalString(in)
return err
}
func NewEngine(opts ...Option) Engine {
e := Engine{
stdin: os.Stdin,
stdout: os.Stdout,
stderr: os.Stderr,
}
for _, opt := range opts {
opt(&e)
}
if e.workingDir == "" {
sysWd, _ := os.Getwd()
e.workingDir = sysWd
}
e.engine = engine.New(
engine.WithStdin(e.stdin),
engine.WithStdout(e.stdout),
engine.WithStderr(e.stderr),
engine.WithFs(afero.NewBasePathFs(afero.NewOsFs(), e.workingDir)),
)
return e
}
func (e Engine) EvalString(source string) (Values, error) {
return e.Eval(strings.NewReader(source))
}
func (e Engine) EvalFile(path string) (Values, error) {
results, err := e.engine.EvalFile(path)
if err != nil {
if luaErr, ok := err.(engine.Error); ok {
return nil, errorFromInternal(luaErr)
}
return nil, err
}
return valuesFromInternal(results...), nil
}
// Eval evaluates the bytes in the given reader. If an error occurs while parsing, or something strange
// happens internally, then an error will be returned. However, when Lua's error function is called, the
// error will be of type Error.
//
// The parsed source will be evaluated as chunk, and all values that the chunk may return are returned
// as Values.
func (e Engine) Eval(source io.Reader) (Values, error) {
results, err := e.engine.Eval(source)
if err != nil {
if luaErr, ok := err.(engine.Error); ok {
return nil, errorFromInternal(luaErr)
}
return nil, err
}
return valuesFromInternal(results...), nil
}