Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add the ability to load a Lua filesystem instead of from OS #421

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions _state.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ type Options struct {
// If `MinimizeStackMemory` is set, the call stack will be automatically grown or shrank up to a limit of
// `CallStackSize` in order to minimize memory usage. This does incur a slight performance penalty.
MinimizeStackMemory bool
// Load lua files from LuaFileSystem instead of OS file-system.
LuaFileSystem LuaFileSystem
}

/* }}} */
Expand Down Expand Up @@ -534,6 +536,28 @@ func (rg *registry) IsFull() bool {

/* }}} */

/* luaFileSystem {{{ */
type LuaFileSystem interface {
Open(path string) (io.ReadCloser, error)
Stat(luapath string) (os.FileInfo, error)
}

func (ls *LState) Open(path string) (io.ReadCloser, error) {
if ls.Options.LuaFileSystem != nil {
return ls.Options.LuaFileSystem.Open(path)
}
return os.Open(path)
}

func (ls *LState) Stat(luapath string) (os.FileInfo, error) {
if ls.Options.LuaFileSystem != nil {
return ls.Options.LuaFileSystem.Stat(luapath)
}
return os.Stat(luapath)
}

/* }}} */

/* Global {{{ */

func newGlobal() *Global {
Expand Down
12 changes: 8 additions & 4 deletions auxlib.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,16 +350,20 @@ func (ls *LState) CallMeta(obj LValue, event string) LValue {
/* load and function call operations {{{ */

func (ls *LState) LoadFile(path string) (*LFunction, error) {
var file *os.File
var err error
var file io.Reader
if len(path) == 0 {
file = os.Stdin
} else {
file, err = os.Open(path)
defer file.Close()
readCloser, err := ls.Open(path)
defer func() {
if readCloser != nil {
readCloser.Close()
}
}()
if err != nil {
return nil, newApiErrorE(ApiErrorFile, err)
}
file = readCloser
}

reader := bufio.NewReader(file)
Expand Down
32 changes: 32 additions & 0 deletions auxlib_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package lua

import (
"embed"
"io"
"io/ioutil"
"os"
"testing"
Expand Down Expand Up @@ -335,3 +337,33 @@ func TestLoadFileForEmptyFile(t *testing.T) {
_, err = L.LoadFile(tmpFile.Name())
errorIfNotNil(t, err)
}

//go:embed _lua5.1-tests/all.lua
var luaTree embed.FS

type luaFileSystem struct {
fileSystem embed.FS
}

func (lfs *luaFileSystem) Open(path string) (io.ReadCloser, error) {
return lfs.fileSystem.Open(path)
}

func (lfs *luaFileSystem) Stat(path string) (os.FileInfo, error) {
file, err := lfs.fileSystem.Open(path)
if err != nil {
return nil, err
}
return file.Stat()
}

func TestLoadLuaFileSystemFile(t *testing.T) {
L := NewState(Options{LuaFileSystem: &luaFileSystem{fileSystem: luaTree}})
defer L.Close()

_, err := L.LoadFile("_lua5.1-tests/all.lua")
errorIfNotNil(t, err)

_, err = L.LoadFile("invalid.lua")
errorIfNil(t, err)
}
2 changes: 1 addition & 1 deletion loadlib.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func loFindFile(L *LState, name, pname string) (string, string) {
messages := []string{}
for _, pattern := range strings.Split(string(path), ";") {
luapath := strings.Replace(pattern, "?", name, -1)
if _, err := os.Stat(luapath); err == nil {
if _, err := L.Stat(luapath); err == nil {
return luapath, ""
} else {
messages = append(messages, err.Error())
Expand Down
24 changes: 24 additions & 0 deletions state.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ type Options struct {
// If `MinimizeStackMemory` is set, the call stack will be automatically grown or shrank up to a limit of
// `CallStackSize` in order to minimize memory usage. This does incur a slight performance penalty.
MinimizeStackMemory bool
// Load lua files from LuaFileSystem instead of OS file-system.
LuaFileSystem LuaFileSystem
}

/* }}} */
Expand Down Expand Up @@ -634,6 +636,28 @@ func (rg *registry) IsFull() bool {

/* }}} */

/* luaFileSystem {{{ */
type LuaFileSystem interface {
Open(path string) (io.ReadCloser, error)
Stat(luapath string) (os.FileInfo, error)
}

func (ls *LState) Open(path string) (io.ReadCloser, error) {
if ls.Options.LuaFileSystem != nil {
return ls.Options.LuaFileSystem.Open(path)
}
return os.Open(path)
}

func (ls *LState) Stat(luapath string) (os.FileInfo, error) {
if ls.Options.LuaFileSystem != nil {
return ls.Options.LuaFileSystem.Stat(luapath)
}
return os.Stat(luapath)
}

/* }}} */

/* Global {{{ */

func newGlobal() *Global {
Expand Down