<commit_before>package mains

import (
	"bufio"
	"fmt"
	"io"
	"io/ioutil"
	"os"
	"os/exec"
	"strings"

	"github.com/yuin/gopher-lua"
	"github.com/zetamatta/nyagos/texts"
)

const ioTblName = "io"

type ioLuaReader struct {
	reader *bufio.Reader
	closer io.Closer
	seeker io.Seeker
}

func (io *ioLuaReader) Close() error {
	if io.closer != nil {
		err := io.closer.Close()
		io.closer = nil
		io.reader = nil
		return err
	}
	return nil
}

type ioLuaWriter struct {
	writer *bufio.Writer
	closer io.Closer
	seeker io.Seeker
}

func (io *ioLuaWriter) Close() error {
	if io.writer != nil {
		io.writer.Flush()
	}
	if io.closer != nil {
		err := io.closer.Close()
		io.closer = nil
		io.writer = nil
		return err
	}
	return nil
}

func newIoLuaReader(L *lua.LState, r io.Reader, c io.Closer, s io.Seeker) *lua.LUserData {
	ud := L.NewUserData()
	ud.Value = &ioLuaReader{
		reader: bufio.NewReader(r),
		seeker: s,
		closer: c,
	}
	index := L.NewTable()
	L.SetField(index, "lines", L.NewFunction(fileLines))
	L.SetField(index, "close", L.NewFunction(fileClose))
	L.SetField(index, "read", L.NewFunction(fileRead))
	L.SetField(index, "seek", L.NewFunction(fileSeek))
	meta := L.NewTable()
	L.SetField(meta, "__index", index)
	L.SetMetatable(ud, meta)
	return ud
}

func fileClose(L *lua.LState) int {
	if ud, ok := L.Get(1).(*lua.LUserData); ok {
		if this, ok := ud.Value.(io.Closer); ok {
			err := this.Close()
			if err != nil {
				return lerror(L, err.Error())
			}
			return 1
		}
	}
	return lerror(L, "(file)close: not a file-handle")
}

func newIoLuaWriter(L *lua.LState, w io.Writer, c io.Closer, s io.Seeker) *lua.LUserData {
	ud := L.NewUserData()
	bw := bufio.NewWriter(w)
	ud.Value = &ioLuaWriter{
		writer: bw,
		seeker: s,
		closer: c,<commit_msg>Fix return value of Lua's file:Close()<commit_after>package mains

import (
	"bufio"
	"fmt"
	"io"
	"io/ioutil"
	"os"
	"os/exec"
	"strings"

	"github.com/yuin/gopher-lua"
	"github.com/zetamatta/nyagos/texts"
)

const ioTblName = "io"

type ioLuaReader struct {
	reader *bufio.Reader
	closer io.Closer
	seeker io.Seeker
}

func (io *ioLuaReader) Close() error {
	if io.closer != nil {
		err := io.closer.Close()
		io.closer = nil
		io.reader = nil
		return err
	}
	return nil
}

type ioLuaWriter struct {
	writer *bufio.Writer
	closer io.Closer
	seeker io.Seeker
}

func (io *ioLuaWriter) Close() error {
	if io.writer != nil {
		io.writer.Flush()
	}
	if io.closer != nil {
		err := io.closer.Close()
		io.closer = nil
		io.writer = nil
		return err
	}
	return nil
}

func newIoLuaReader(L *lua.LState, r io.Reader, c io.Closer, s io.Seeker) *lua.LUserData {
	ud := L.NewUserData()
	ud.Value = &ioLuaReader{
		reader: bufio.NewReader(r),
		seeker: s,
		closer: c,
	}
	index := L.NewTable()
	L.SetField(index, "lines", L.NewFunction(fileLines))
	L.SetField(index, "close", L.NewFunction(fileClose))
	L.SetField(index, "read", L.NewFunction(fileRead))
	L.SetField(index, "seek", L.NewFunction(fileSeek))
	meta := L.NewTable()
	L.SetField(meta, "__index", index)
	L.SetMetatable(ud, meta)
	return ud
}

func fileClose(L *lua.LState) int {
	if ud, ok := L.Get(1).(*lua.LUserData); ok {
		if this, ok := ud.Value.(io.Closer); ok {
			err := this.Close()
			if err != nil {
				return lerror(L, err.Error())
			}
      L.Push(lua.LTrue)
			return 1
		}
	}
	return lerror(L, "(file)close: not a file-handle")
}

func newIoLuaWriter(L *lua.LState, w io.Writer, c io.Closer, s io.Seeker) *lua.LUserData {
	ud := L.NewUserData()
	bw := bufio.NewWriter(w)
	ud.Value = &ioLuaWriter{
		writer: bw,
		seeker: s,
		closer: c,
{'fraction_non_alphanumeric': 0.16091954022988506, 'fraction_numerical': 0.00112139052425007, 'mean_word_length': 3.556832694763729, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4955512', 'n_tokens_mistral': 1603, 'n_tokens_neox': 1531, 'n_words': 341}