Hilbish/lua.go

81 lines
2.1 KiB
Go
Raw Normal View History

2021-03-19 23:03:11 +00:00
package main
import (
2021-03-31 02:37:08 +00:00
"fmt"
"os"
2022-03-28 21:40:36 +00:00
"hilbish/util"
"hilbish/golibs/bait"
/*
"hilbish/golibs/commander"
"hilbish/golibs/fs"
"hilbish/golibs/terminal"
*/
rt "github.com/arnodel/golua/runtime"
"github.com/arnodel/golua/lib"
2021-03-19 23:03:11 +00:00
)
var minimalconf = `hilbish.prompt '& '`
2021-12-09 03:31:48 +00:00
func luaInit() {
l = rt.New(os.Stdout)
lib.LoadAll(l)
2021-03-31 02:37:08 +00:00
lib.LoadLibs(l, hilbishLoader)
// yes this is stupid, i know
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("terminal", terminal.Loader)
2021-03-31 02:37:08 +00:00
cmds := commander.New()
2021-03-31 02:37:08 +00:00
// When a command from Lua is added, register it for use
cmds.Events.On("commandRegister", func(cmdName string, cmd *lua.LFunction) {
commands[cmdName] = cmd
})
cmds.Events.On("commandDeregister", func(cmdName string) {
delete(commands, cmdName)
})
l.PreloadModule("commander", cmds.Loader)
*/
2021-03-31 02:37:08 +00:00
hooks = bait.New()
// l.PreloadModule("bait", hooks.Loader)
// Add Ctrl-C handler
hooks.Em.On("signal.sigint", func() {
if !interactive {
os.Exit(0)
}
})
2021-03-31 02:37:08 +00:00
// Add more paths that Lua can require from
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)
2021-03-31 02:37:08 +00:00
data, err := os.ReadFile("prelude/init.lua")
2021-03-31 02:37:08 +00:00
if err != nil {
data, err = os.ReadFile(preloadPath)
2021-03-31 02:37:08 +00:00
if err != nil {
fmt.Fprintln(os.Stderr, "Missing preload file, builtins may be missing.")
2021-03-31 02:37:08 +00:00
}
}
chunk, _ = l.CompileAndLoadLuaChunk("", data, rt.TableValue(l.GlobalEnv()))
_, err = rt.Call1(l.MainThread(), rt.FunctionValue(chunk))
fmt.Println("prelude", err)
}
func runConfig(confpath string) {
2021-04-28 11:26:23 +00:00
if !interactive {
return
}
2022-03-28 21:40:36 +00:00
err := util.DoFile(l, confpath)
2021-03-31 02:37:08 +00:00
if err != nil {
fmt.Fprintln(os.Stderr, err, "\nAn error has occured while loading your config! Falling back to minimal default config.")
2022-03-28 21:40:36 +00:00
util.DoString(l, minimalconf)
2021-03-31 02:37:08 +00:00
}
}