refactor: start work on lua 5.4

lots of commented out code

ive found a go lua library which implements lua 5.4
and found an opportunity to start working on it.
this commit basically removes everything and just leaves
enough for the shell to be "usable" and able to start.
there are no builtins or libraries (besides the `hilbish` global)
lua5.4
TorchedSammy 2022-03-27 22:14:53 -04:00
parent 61c9e12a4a
commit 016a3a2ec7
Signed by: sammyette
GPG Key ID: 904FC49417B44DCD
12 changed files with 192 additions and 102 deletions

View File

@ -1,6 +1,6 @@
-- Default Hilbish config
local lunacolors = require 'lunacolors'
local bait = require 'bait'
--local bait = require 'bait'
local ansikit = require 'ansikit'
local function doPrompt(fail)
@ -13,6 +13,7 @@ print(lunacolors.format(hilbish.greeting))
doPrompt()
--[[
bait.catch('command.exit', function(code)
doPrompt(code ~= 0)
end)
@ -24,3 +25,4 @@ bait.catch('hilbish.vimMode', function(mode)
ansikit.cursorStyle(ansikit.lineCursor)
end
end)
]]--

119
api.go
View File

@ -4,24 +4,25 @@
package main
import (
"fmt"
// "fmt"
"os"
"os/exec"
// "os/exec"
"runtime"
"strings"
"syscall"
"time"
// "syscall"
// "time"
"hilbish/util"
"github.com/yuin/gopher-lua"
"github.com/maxlandon/readline"
"github.com/blackfireio/osinfo"
"mvdan.cc/sh/v3/interp"
rt "github.com/arnodel/golua/runtime"
"github.com/arnodel/golua/lib/packagelib"
// "github.com/maxlandon/readline"
// "github.com/blackfireio/osinfo"
// "mvdan.cc/sh/v3/interp"
)
var exports = map[string]lua.LGFunction {
"alias": hlalias,
var exports = map[string]util.LuaExport{
/*"alias": hlalias,
"appendPath": hlappendPath,
"complete": hlcomplete,
"cwd": hlcwd,
@ -31,24 +32,29 @@ var exports = map[string]lua.LGFunction {
"highlighter": hlhighlighter,
"hinter": hlhinter,
"multiprompt": hlmlprompt,
"prependPath": hlprependPath,
"prompt": hlprompt,
"inputMode": hlinputMode,
"prependPath": hlprependPath,*/
"prompt": util.LuaExport{hlprompt, 1, false},
/*"inputMode": hlinputMode,
"interval": hlinterval,
"read": hlread,
"run": hlrun,
"timeout": hltimeout,
"which": hlwhich,
"which": hlwhich,*/
}
var greeting string
var hshMod *lua.LTable
//var hshMod *lua.LTable
var hilbishLoader = packagelib.Loader{
Load: hilbishLoad,
Name: "hilbish",
}
func hilbishLoader(L *lua.LState) int {
mod := L.SetFuncs(L.NewTable(), exports)
hshMod = mod
func hilbishLoad(rtm *rt.Runtime) (rt.Value, func()) {
mod := rt.NewTable()
util.SetExports(rtm, mod, exports)
// hshMod = mod
host, _ := os.Hostname()
// host, _ := os.Hostname()
username := curuser.Username
if runtime.GOOS == "windows" {
@ -59,27 +65,31 @@ func hilbishLoader(L *lua.LState) int {
The nice lil shell for {blue}Lua{reset} fanatics!
Check out the {blue}{bold}guide{reset} command to get started.
`
/*
util.SetField(L, mod, "ver", lua.LString(version), "Hilbish version")
util.SetField(L, mod, "user", lua.LString(username), "Username of user")
util.SetField(L, mod, "host", lua.LString(host), "Host name of the machine")
util.SetField(L, mod, "home", lua.LString(curuser.HomeDir), "Home directory of the user")
util.SetField(L, mod, "dataDir", lua.LString(dataDir), "Directory for Hilbish's data files")
*/
util.SetField(rtm, mod, "dataDir", rt.StringValue(dataDir), "Directory for Hilbish's data files")
/*
util.SetField(L, mod, "interactive", lua.LBool(interactive), "If this is an interactive shell")
util.SetField(L, mod, "login", lua.LBool(interactive), "Whether this is a login shell")
util.SetField(L, mod, "greeting", lua.LString(greeting), "Hilbish's welcome message for interactive shells. It has Lunacolors formatting.")
util.SetField(l, mod, "vimMode", lua.LNil, "Current Vim mode of Hilbish (nil if not in Vim mode)")
util.SetField(L, mod, "login", lua.LBool(interactive), "Whether this is a login shell")*/
util.SetField(rtm, mod, "greeting", rt.StringValue(greeting), "Hilbish's welcome message for interactive shells. It has Lunacolors formatting.")
/*util.SetField(l, mod, "vimMode", lua.LNil, "Current Vim mode of Hilbish (nil if not in Vim mode)")
util.SetField(l, hshMod, "exitCode", lua.LNumber(0), "Exit code of last exected command")
util.Document(L, mod, "Hilbish's core API, containing submodules and functions which relate to the shell itself.")
*/
// hilbish.userDir table
hshuser := L.NewTable()
hshuser := rt.NewTable()
util.SetField(L, hshuser, "config", lua.LString(confDir), "User's config directory")
util.SetField(L, hshuser, "data", lua.LString(userDataDir), "XDG data directory")
util.Document(L, hshuser, "User directories to store configs and/or modules.")
L.SetField(mod, "userDir", hshuser)
util.SetField(rtm, hshuser, "config", rt.StringValue(confDir), "User's config directory")
util.SetField(rtm, hshuser, "data", rt.StringValue(userDataDir), "XDG data directory")
//util.Document(rtm, hshuser, "User directories to store configs and/or modules.")
mod.Set(rt.StringValue("userDir"), rt.TableValue(hshuser))
/*
// hilbish.os table
hshos := L.NewTable()
info, _ := osinfo.GetOSInfo()
@ -89,9 +99,11 @@ Check out the {blue}{bold}guide{reset} command to get started.
util.SetField(L, hshos, "version", lua.LString(info.Version), "Version of the current OS")
util.Document(L, hshos, "OS info interface")
L.SetField(mod, "os", hshos)
*/
// hilbish.aliases table
aliases = newAliases()
/*
aliasesModule := aliases.Loader(L)
util.Document(L, aliasesModule, "Alias inferface for Hilbish.")
L.SetField(mod, "aliases", aliasesModule)
@ -113,18 +125,28 @@ Check out the {blue}{bold}guide{reset} command to get started.
runnerModule := runnerModeLoader(L)
util.Document(L, runnerModule, "Runner/exec interface for Hilbish.")
L.SetField(mod, "runner", runnerModule)
*/
// hilbish.jobs table
jobs = newJobHandler()
/*
jobModule := jobs.loader(L)
util.Document(L, jobModule, "(Background) job interface.")
L.SetField(mod, "jobs", jobModule)
*/
L.Push(mod)
return 1
return rt.TableValue(mod), nil
}
func getenv(key, fallback string) string {
value := os.Getenv(key)
if len(value) == 0 {
return fallback
}
return value
}
/*
func luaFileComplete(L *lua.LState) int {
query := L.CheckString(1)
ctx := L.CheckString(2)
@ -168,16 +190,17 @@ func luaBinaryComplete(L *lua.LState) int {
return 1
}
*/
func setVimMode(mode string) {
util.SetField(l, hshMod, "vimMode", lua.LString(mode), "Current Vim mode of Hilbish (nil if not in Vim mode)")
hooks.Em.Emit("hilbish.vimMode", mode)
// util.SetField(l, hshMod, "vimMode", lua.LString(mode), "Current Vim mode of Hilbish (nil if not in Vim mode)")
// hooks.Em.Emit("hilbish.vimMode", mode)
}
func unsetVimMode() {
util.SetField(l, hshMod, "vimMode", lua.LNil, "Current Vim mode of Hilbish (nil if not in Vim mode)")
// util.SetField(l, hshMod, "vimMode", lua.LNil, "Current Vim mode of Hilbish (nil if not in Vim mode)")
}
/*
// run(cmd)
// Runs `cmd` in Hilbish's sh interpreter.
// --- @param cmd string
@ -206,14 +229,6 @@ func hlcwd(L *lua.LState) int {
return 1
}
func getenv(key, fallback string) string {
value := os.Getenv(key)
if len(value) == 0 {
return fallback
}
return value
}
// read(prompt) -> input?
// Read input from the user, using Hilbish's line editor/input reader.
// This is a separate instance from the one Hilbish actually uses.
@ -233,6 +248,7 @@ func hlread(L *lua.LState) int {
L.Push(lua.LString(input))
return 1
}
*/
/*
prompt(str)
@ -244,16 +260,24 @@ These will be formatted and replaced with the appropriate values.
`%h` - Hostname of device
--- @param str string
*/
func hlprompt(L *lua.LState) int {
prompt = L.CheckString(1)
func hlprompt(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
var prompt string
err := c.Check1Arg()
if err == nil {
prompt, err = c.StringArg(0)
}
if err != nil {
return nil, err
}
lr.SetPrompt(fmtPrompt(prompt))
return 0
return nil, nil
}
// multiprompt(str)
// Changes the continued line prompt to `str`
// --- @param str string
/*
func hlmlprompt(L *lua.LState) int {
multilinePrompt = L.CheckString(1)
@ -531,3 +555,4 @@ func hlhighlighter(L *lua.LState) int {
return 0
}
*/

25
exec.go
View File

@ -13,9 +13,9 @@ import (
"syscall"
"time"
"hilbish/util"
// "hilbish/util"
"github.com/yuin/gopher-lua"
rt "github.com/arnodel/golua/runtime"
"mvdan.cc/sh/v3/shell"
//"github.com/yuin/gopher-lua/parse"
"mvdan.cc/sh/v3/interp"
@ -24,15 +24,15 @@ import (
)
var errNotExec = errors.New("not executable")
var runnerMode lua.LValue = lua.LString("hybrid")
//var runnerMode lua.LValue = lua.LString("hybrid")
func runInput(input string, priv bool) {
running = true
cmdString := aliases.Resolve(input)
hooks.Em.Emit("command.preexec", input, cmdString)
if runnerMode.Type() == lua.LTString {
switch runnerMode.String() {
// if runnerMode.Type() == lua.LTString {
switch /*runnerMode.String()*/ "hybrid" {
case "hybrid":
_, err := handleLua(cmdString)
if err == nil {
@ -68,8 +68,9 @@ func runInput(input string, priv bool) {
}
cmdFinish(exitCode, cmdString, priv)
}
} else {
// } else {
// can only be a string or function so
/*
err := l.CallByParam(lua.P{
Fn: runnerMode,
NRet: 2,
@ -94,12 +95,13 @@ func runInput(input string, priv bool) {
fmt.Fprintln(os.Stderr, runErr)
}
cmdFinish(exitCode, cmdString, priv)
}
*/
// }
}
func handleLua(cmdString string) (uint8, error) {
// First try to load input, essentially compiling to bytecode
fn, err := l.LoadString(cmdString)
chunk, err := l.CompileAndLoadLuaChunk("", []byte(cmdString), rt.TableValue(l.GlobalEnv()))
if err != nil && noexecute {
fmt.Println(err)
/* if lerr, ok := err.(*lua.ApiError); ok {
@ -112,8 +114,7 @@ func handleLua(cmdString string) (uint8, error) {
}
// And if there's no syntax errors and -n isnt provided, run
if !noexecute {
l.Push(fn)
err = l.PCall(0, lua.MultRet, nil)
_, err = rt.Call1(l.MainThread(), rt.FunctionValue(chunk))
}
if err == nil {
return 0, nil
@ -180,6 +181,7 @@ func execCommand(cmd string) error {
}
// If command is defined in Lua then run it
/*
luacmdArgs := l.NewTable()
for _, str := range args[1:] {
luacmdArgs.Append(lua.LString(str))
@ -209,6 +211,7 @@ func execCommand(cmd string) error {
return interp.NewExitStatus(exitcode)
}
*/
err := lookpath(args[0])
if err == errNotExec {
@ -441,6 +444,6 @@ func cmdFinish(code uint8, cmdstr string, private bool) {
if interactive && !private {
handleHistory(cmdstr)
}
util.SetField(l, hshMod, "exitCode", lua.LNumber(code), "Exit code of last exected command")
// util.SetField(l, hshMod, "exitCode", lua.LNumber(code), "Exit code of last exected command")
hooks.Em.Emit("command.exit", code, cmdstr)
}

1
go.mod
View File

@ -3,6 +3,7 @@ module hilbish
go 1.16
require (
github.com/arnodel/golua v0.0.0-20220221163911-dfcf252b6f86
github.com/blackfireio/osinfo v1.0.3
github.com/chuckpreslar/emission v0.0.0-20170206194824-a7ddd980baf9
github.com/maxlandon/readline v0.1.0-beta.0.20211027085530-2b76cabb8036

18
go.sum
View File

@ -14,6 +14,13 @@ github.com/Rosettea/sh/v3 v3.4.0-0.dev.0.20220306140409-795a84b00b4e h1:P2XupP8S
github.com/Rosettea/sh/v3 v3.4.0-0.dev.0.20220306140409-795a84b00b4e/go.mod h1:R09vh/04ILvP2Gj8/Z9Jd0Dh0ZIvaucowMEs6abQpWs=
github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d h1:licZJFw2RwpHMqeKTCYkitsPqHNxTmd4SNR5r94FGM8=
github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d/go.mod h1:asat636LX7Bqt5lYEZ27JNDcqxfjdBQuJ/MM4CN/Lzo=
github.com/arnodel/edit v0.0.0-20220202110212-dfc8d7a13890/go.mod h1:AcpttpuZBaL9xl8/CX+Em4fBTUbwIkJ66RiAsJlNrBk=
github.com/arnodel/golua v0.0.0-20220121091306-866962c51982/go.mod h1:d8hJbh/X80uQdSGMu5HZ64LrIgPh1jQxcNhnAqggpWE=
github.com/arnodel/golua v0.0.0-20220221163911-dfcf252b6f86 h1:pe4Q2R6Ei90ucm3m2CtzPh0SrThi/FgMyC46+Cb5Q9g=
github.com/arnodel/golua v0.0.0-20220221163911-dfcf252b6f86/go.mod h1:9jzpYPiU2is0HVGCiuIOBSXdergHUW44IEjmuN1UrIE=
github.com/arnodel/strftime v0.1.6 h1:0hc0pUvk8KhEMXE+htyaOUV42zNcf/csIbjzEFCJqsw=
github.com/arnodel/strftime v0.1.6/go.mod h1:5NbK5XqYK8QpRZpqKNt4OlxLtIB8cotkLk4KTKzJfWs=
github.com/atotto/clipboard v0.1.4/go.mod h1:ZY9tmq7sm5xIbd9bOK4onWV4S6X0u6GY7Vn0Yu86PYI=
github.com/blackfireio/osinfo v1.0.3 h1:Yk2t2GTPjBcESv6nDSWZKO87bGMQgO+Hi9OoXPpxX8c=
github.com/blackfireio/osinfo v1.0.3/go.mod h1:Pd987poVNmd5Wsx6PRPw4+w7kLlf9iJxoRKPtPAjOrA=
github.com/chuckpreslar/emission v0.0.0-20170206194824-a7ddd980baf9 h1:xz6Nv3zcwO2Lila35hcb0QloCQsc38Al13RNEzWRpX4=
@ -26,6 +33,8 @@ github.com/creack/pty v1.1.15 h1:cKRCLMj3Ddm54bKSpemfQ8AtYFBhAI2MPmdys22fBdc=
github.com/creack/pty v1.1.15/go.mod h1:MOBLtS5ELjhRRrroQr9kyvTxUAFNvYEK993ew/Vr4O4=
github.com/evilsocket/islazy v1.10.6 h1:MFq000a1ByoumoJWlytqg0qon0KlBeUfPsDjY0hK0bo=
github.com/evilsocket/islazy v1.10.6/go.mod h1:OrwQGYg3DuZvXUfmH+KIZDjwTCbrjy48T24TUpGqVVw=
github.com/gdamore/encoding v1.0.0/go.mod h1:alR0ol34c49FCSBLjhosxzcPHQbf2trDkoo5dl+VrEg=
github.com/gdamore/tcell/v2 v2.4.0/go.mod h1:cTTuF84Dlj/RqmaCIV5p4w8uG1zWdk0SF6oBpwHp4fU=
github.com/google/renameio v1.0.1/go.mod h1:t/HQoYBZSsWSNK35C6CO/TpPLDVWvxOHboWUAweKUpk=
github.com/jessevdk/go-flags v1.5.0/go.mod h1:Fw0T6WPc1dYxT4mKEZRfG5kJhaTDP9pj1c2EWnYs/m4=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
@ -37,11 +46,14 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/layeh/gopher-luar v1.0.10 h1:8NIv4MX1Arz96kK4buGK1D87DyDxKZyq6KKvJ2diHp0=
github.com/layeh/gopher-luar v1.0.10/go.mod h1:TPnIVCZ2RJBndm7ohXyaqfhzjlZ+OA2SZR/YwL8tECk=
github.com/lucasb-eyer/go-colorful v1.0.3/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
github.com/mattn/go-runewidth v0.0.10/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk=
github.com/olekukonko/ts v0.0.0-20171002115256-78ecb04241c0 h1:LiZB1h0GIcudcDci2bxbqI6DXV8bF8POAnArqvRrIyw=
github.com/olekukonko/ts v0.0.0-20171002115256-78ecb04241c0/go.mod h1:F/7q8/HZz+TXjlsoZQQKVYvXTZaFH4QRa3y+j1p7MS0=
github.com/pborman/getopt v1.1.0 h1:eJ3aFZroQqq0bWmraivjQNt6Dmm5M0h2JcDW38/Azb0=
github.com/pborman/getopt v1.1.0/go.mod h1:FxXoW1Re00sQG/+KIkuSqRL/LwQgSkv7uyac+STFsbk=
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
@ -53,16 +65,22 @@ github.com/yuin/gopher-lua v0.0.0-20210529063254-f4c35e4016d9/go.mod h1:E1AXubJB
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c h1:5KslGYwFpkhGh+Q16bwMP3cOontH8FOep7tGV86Y7SQ=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190204203706-41f3e6584952/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210320140829-1e4c9ba3b0c4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210925032602-92d5a993a665/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220114195835-da31bd327af9/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220224120231-95c6836cb0e7 h1:BXxu8t6QN0G1uff4bzZzSkpsax8+ALqTGUtz08QrV00=
golang.org/x/sys v0.0.0-20220224120231-95c6836cb0e7/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220227234510-4e6760a101f9 h1:nhht2DYV/Sn3qOayu8lM+cU1ii9sTLUeBQwQQfUHtrs=
golang.org/x/sys v0.0.0-20220227234510-4e6760a101f9/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201210144234-2321bbc49cbf/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210916214954-140adaaadfaf/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 h1:JGgROgKl9N8DuW20oFS5gxc+lE67/N3FcwmBPMe7ArY=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
mvdan.cc/editorconfig v0.2.0/go.mod h1:lvnnD3BNdBYkhq+B4uBuFFKatfp02eB6HixDvEz91C0=

11
job.go
View File

@ -3,8 +3,6 @@ package main
import (
"sync"
"os"
"github.com/yuin/gopher-lua"
)
var jobs *jobHandler
@ -21,7 +19,7 @@ type job struct {
func (j *job) start(pid int) {
j.pid = pid
j.running = true
hooks.Em.Emit("job.start", j.lua())
// hooks.Em.Emit("job.start", j.lua())
}
func (j *job) stop() {
@ -31,13 +29,14 @@ func (j *job) stop() {
func (j *job) finish() {
j.running = false
hooks.Em.Emit("job.done", j.lua())
// hooks.Em.Emit("job.done", j.lua())
}
func (j *job) setHandle(handle *os.Process) {
j.proc = handle
}
/*
func (j *job) lua() *lua.LTable {
// returns lua table for job
// because userdata is gross
@ -62,6 +61,7 @@ func (j *job) luaStop(L *lua.LState) int {
return 0
}
*/
type jobHandler struct {
jobs map[int]*job
@ -96,7 +96,7 @@ func (j *jobHandler) getLatest() *job {
return j.jobs[j.latestID]
}
/*
func (j *jobHandler) loader(L *lua.LState) *lua.LTable {
jobFuncs := map[string]lua.LGFunction{
"all": j.luaAllJobs,
@ -134,3 +134,4 @@ func (j *jobHandler) luaAllJobs(L *lua.LState) int {
L.Push(jobTbl)
return 1
}
*/

52
lua.go
View File

@ -5,25 +5,29 @@ import (
"os"
"hilbish/golibs/bait"
/*
"hilbish/golibs/commander"
"hilbish/golibs/fs"
"hilbish/golibs/terminal"
"github.com/yuin/gopher-lua"
*/
rt "github.com/arnodel/golua/runtime"
"github.com/arnodel/golua/lib"
)
var minimalconf = `hilbish.prompt '& '`
func luaInit() {
l = lua.NewState()
l.OpenLibs()
l = rt.New(os.Stdout)
lib.LoadAll(l)
lib.LoadLibs(l, hilbishLoader)
// yes this is stupid, i know
l.PreloadModule("hilbish", hilbishLoader)
l.DoString("hilbish = require 'hilbish'")
chunk, _ := l.CompileAndLoadLuaChunk("", []byte("hilbish = require 'hilbish'"), rt.TableValue(l.GlobalEnv()))
_, err := rt.Call1(l.MainThread(), rt.FunctionValue(chunk))
fmt.Println("hsh load", err)
// Add fs and terminal module module to Lua
l.PreloadModule("fs", fs.Loader)
/* l.PreloadModule("fs", fs.Loader)
l.PreloadModule("terminal", terminal.Loader)
cmds := commander.New()
@ -35,10 +39,10 @@ func luaInit() {
delete(commands, cmdName)
})
l.PreloadModule("commander", cmds.Loader)
*/
hooks = bait.New()
l.PreloadModule("bait", hooks.Loader)
// l.PreloadModule("bait", hooks.Loader)
// Add Ctrl-C handler
hooks.Em.On("signal.sigint", func() {
if !interactive {
@ -46,29 +50,35 @@ func luaInit() {
}
})
l.SetGlobal("complete", l.NewFunction(hlcomplete))
// Add more paths that Lua can require from
l.DoString("package.path = package.path .. " + requirePaths)
chunk, _ = l.CompileAndLoadLuaChunk("", []byte("package.path = package.path .. " + requirePaths), rt.TableValue(l.GlobalEnv()))
_, err = rt.Call1(l.MainThread(), rt.FunctionValue(chunk))
fmt.Println("package path", err)
err := l.DoFile("prelude/init.lua")
data, err := os.ReadFile("prelude/init.lua")
if err != nil {
err = l.DoFile(preloadPath)
data, err = os.ReadFile(preloadPath)
if err != nil {
fmt.Fprintln(os.Stderr,
"Missing preload file, builtins may be missing.")
fmt.Fprintln(os.Stderr, "Missing preload file, builtins may be missing.")
}
}
chunk, _ = l.CompileAndLoadLuaChunk("", data, rt.TableValue(l.GlobalEnv()))
_, err = rt.Call1(l.MainThread(), rt.FunctionValue(chunk))
fmt.Println("prelude", err)
}
func runConfig(confpath string) {
if !interactive {
return
}
err := l.DoFile(confpath)
data, err := os.ReadFile(confpath)
if err != nil {
fmt.Fprintln(os.Stderr, err,
"\nAn error has occured while loading your config! Falling back to minimal default config.")
l.DoString(minimalconf)
fmt.Fprintln(os.Stderr, err, "\nAn error has occured while loading your config! Falling back to minimal default config.")
chunk, _ := l.CompileAndLoadLuaChunk("", []byte(minimalconf), rt.TableValue(l.GlobalEnv()))
_, err := rt.Call1(l.MainThread(), rt.FunctionValue(chunk))
fmt.Println(err)
}
chunk, _ := l.CompileAndLoadLuaChunk("", data, rt.TableValue(l.GlobalEnv()))
_, err = rt.Call1(l.MainThread(), rt.FunctionValue(chunk))
fmt.Println("config", err)
}

View File

@ -12,6 +12,7 @@ import (
"hilbish/golibs/bait"
rt "github.com/arnodel/golua/runtime"
"github.com/pborman/getopt"
"github.com/yuin/gopher-lua"
"github.com/maxlandon/readline"
@ -19,7 +20,7 @@ import (
)
var (
l *lua.LState
l *rt.Runtime
lr *lineReader
commands = map[string]*lua.LFunction{}
@ -151,7 +152,7 @@ func main() {
}
if getopt.NArgs() > 0 {
luaArgs := l.NewTable()
/*luaArgs := l.NewTable()
for _, arg := range getopt.Args() {
luaArgs.Append(lua.LString(arg))
}
@ -161,7 +162,7 @@ func main() {
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}*/
os.Exit(0)
}

9
rl.go
View File

@ -6,15 +6,16 @@ import (
"strings"
"github.com/maxlandon/readline"
"github.com/yuin/gopher-lua"
)
type lineReader struct {
rl *readline.Instance
}
var fileHist *fileHistory
/*
var hinter lua.LValue = lua.LNil
var highlighter lua.LValue = lua.LNil
*/
// other gophers might hate this naming but this is local, shut up
func newLineReader(prompt string, noHist bool) *lineReader {
@ -46,6 +47,7 @@ func newLineReader(prompt string, noHist bool) *lineReader {
}
hooks.Em.Emit("hilbish.vimAction", actionStr, args)
}
/*
rl.HintText = func(line []rune, pos int) []rune {
if hinter == lua.LNil {
return []rune{}
@ -163,6 +165,7 @@ func newLineReader(prompt string, noHist bool) *lineReader {
it is the responsibility of the completer
to work on subcommands and subcompletions
*/
/*
if cmpTbl, ok := luacompleteTable.(*lua.LTable); ok {
cmpTbl.ForEach(func(key lua.LValue, value lua.LValue) {
if key.Type() == lua.LTNumber {
@ -220,7 +223,7 @@ func newLineReader(prompt string, noHist bool) *lineReader {
}
}
return "", compGroup
}
}*/
return &lineReader{
rl,
@ -268,6 +271,7 @@ func (lr *lineReader) Resize() {
}
// lua module
/*
func (lr *lineReader) Loader(L *lua.LState) *lua.LTable {
lrLua := map[string]lua.LGFunction{
"add": lr.luaAddHistory,
@ -321,3 +325,4 @@ func (lr *lineReader) luaClearHistory(l *lua.LState) int {
fileHist.clear()
return 0
}
*/

View File

@ -1,7 +1,7 @@
package main
/*
import (
"github.com/yuin/gopher-lua"
)
func runnerModeLoader(L *lua.LState) *lua.LTable {
@ -44,3 +44,4 @@ func luaRunner(L *lua.LState) int {
return 2
}
*/

17
util/export.go 100644
View File

@ -0,0 +1,17 @@
package util
import (
rt "github.com/arnodel/golua/runtime"
)
type LuaExport struct {
Function rt.GoFunctionFunc
ArgNum int
Variadic bool
}
func SetExports(rtm *rt.Runtime, tbl *rt.Table, exports map[string]LuaExport) {
for name, export := range exports {
rtm.SetEnvGoFunc(tbl, name, export.Function, export.ArgNum, export.Variadic)
}
}

View File

@ -1,32 +1,38 @@
package util
import "github.com/yuin/gopher-lua"
import (
"github.com/yuin/gopher-lua"
rt "github.com/arnodel/golua/runtime"
)
// Document adds a documentation string to a module.
// It is accessible via the __doc metatable.
func Document(L *lua.LState, module lua.LValue, doc string) {
/*
mt := L.GetMetatable(module)
if mt == lua.LNil {
mt = L.NewTable()
L.SetMetatable(module, mt)
}
L.SetField(mt, "__doc", lua.LString(doc))
*/
}
// SetField sets a field in a table, adding docs for it.
// It is accessible via the __docProp metatable. It is a table of the names of the fields.
func SetField(L *lua.LState, module lua.LValue, field string, value lua.LValue, doc string) {
mt := L.GetMetatable(module)
if mt == lua.LNil {
mt = L.NewTable()
docProp := L.NewTable()
L.SetField(mt, "__docProp", docProp)
func SetField(rtm *rt.Runtime, module *rt.Table, field string, value rt.Value, doc string) {
mt := module.Metatable()
if mt == nil {
mt = rt.NewTable()
docProp := rt.NewTable()
mt.Set(rt.StringValue("__docProp"), rt.TableValue(docProp))
L.SetMetatable(module, mt)
module.SetMetatable(mt)
}
docProp := L.GetTable(mt, lua.LString("__docProp"))
docProp := mt.Get(rt.StringValue("__docProp"))
L.SetField(docProp, field, lua.LString(doc))
L.SetField(module, field, value)
docProp.AsTable().Set(rt.StringValue(field), rt.StringValue(doc))
module.Set(rt.StringValue(field), value)
}