Skip to content

Commit

Permalink
fea: reuse @jtolio implementation
Browse files Browse the repository at this point in the history
- Base PR: Shopify#43
  • Loading branch information
marco-souza committed May 20, 2024
1 parent 5d657e3 commit 13adf4c
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 12 deletions.
6 changes: 3 additions & 3 deletions auxiliary.go
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,7 @@ func LoadFile(l *State, fileName, mode string) error {
}
if fileName == "" {
l.PushString("=stdin")
f = os.Stdin
f = l.stdin
} else {
l.PushString("@" + fileName)
var err error
Expand All @@ -509,7 +509,7 @@ func LoadFile(l *State, fileName, mode string) error {
}
s, _ := l.ToString(-1)
err := l.Load(r, s, mode)
if f != os.Stdin {
if f != l.stdin {
_ = f.Close()
}
switch err {
Expand Down Expand Up @@ -538,7 +538,7 @@ func NewStateEx() *State {
if l != nil {
_ = AtPanic(l, func(l *State) int {
s, _ := l.ToString(-1)
fmt.Fprintf(os.Stderr, "PANIC: unprotected error in call to Lua API (%s)\n", s)
fmt.Fprintf(l.stderr, "PANIC: unprotected error in call to Lua API (%s)\n", s)
return 0
})
}
Expand Down
17 changes: 12 additions & 5 deletions base.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package lua

import (
"io"
"os"
"runtime"
"strconv"
"strings"
Expand Down Expand Up @@ -215,13 +214,13 @@ var baseLibrary = []RegistryFunction{
panic("unreachable")
}
if i > 1 {
os.Stdout.WriteString("\t")
l.writeToStdout("\t")
}
os.Stdout.WriteString(s)
l.writeToStdout(s)
l.Pop(1) // pop result
}
os.Stdout.WriteString("\n")
os.Stdout.Sync()
l.writeToStdout("\n")
l.stdout.Sync()
return 0
}},
{"rawequal", func(l *State) int {
Expand Down Expand Up @@ -328,3 +327,11 @@ func BaseOpen(l *State) int {
l.SetField(-2, "_VERSION")
return 1
}

func (l *State) writeToStdout(s string) {
_, err := l.stdout.WriteString(s)
if err != nil {
Errorf(l, "failed writing to stdout: %v", err)
panic("unreachable")
}
}
8 changes: 8 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
module github.com/Shopify/go-lua

go 1.22

require github.com/stretchr/testify v1.9.0

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
6 changes: 3 additions & 3 deletions io.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,9 +316,9 @@ func IOOpen(l *State) int {
SetFunctions(l, fileHandleMethods, 0)
l.Pop(1)

registerStdFile(l, os.Stdin, input, "stdin")
registerStdFile(l, os.Stdout, output, "stdout")
registerStdFile(l, os.Stderr, "", "stderr")
registerStdFile(l, l.stdin, input, "stdin")
registerStdFile(l, l.stdout, output, "stdout")
registerStdFile(l, l.stderr, "", "stderr")

return 1
}
13 changes: 12 additions & 1 deletion lua.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"io"
"math"
"os"
"strings"
)

Expand Down Expand Up @@ -239,6 +240,7 @@ type State struct {
errorFunction int // current error handling function (stack index)
baseCallInfo callInfo // callInfo for first level (go calling lua)
protectFunction func()
stdout, stderr, stdin *os.File
}

type globalState struct {
Expand Down Expand Up @@ -455,7 +457,7 @@ func (l *State) Dump(w io.Writer) error {
// http://www.lua.org/manual/5.2/manual.html#lua_newstate
func NewState() *State {
v := float64(VersionNumber)
l := &State{allowHook: true, error: nil, nonYieldableCallCount: 1}
l := &State{allowHook: true, error: nil, nonYieldableCallCount: 1, stdout: os.Stdout, stderr: os.Stderr, stdin: os.Stdin}
g := &globalState{mainThread: l, registry: newTable(), version: &v, memoryErrorMessage: "not enough memory"}
l.global = g
l.initializeStack()
Expand Down Expand Up @@ -1529,3 +1531,12 @@ func (l *State) IsNoneOrNil(index int) bool { return l.TypeOf(index) <= TypeNil
//
// http://www.lua.org/manual/5.2/manual.html#lua_pushglobaltable
func (l *State) PushGlobalTable() { l.RawGetInt(RegistryIndex, RegistryIndexGlobals) }

// SetStdout redirects interpreter stdout to the given *os.File
func (l *State) SetStdout(stdout *os.File) { l.stdout = stdout }

// SetStderr redirects interpreter stderr to the given *os.File
func (l *State) SetStderr(stderr *os.File) { l.stderr = stderr }

// SetStdin redirects interpreter stdin from the given *os.File
func (l *State) SetStdin(stdin *os.File) { l.stdin = stdin }

0 comments on commit 13adf4c

Please sign in to comment.