Moonshark/moonshark.go
2025-07-14 20:45:26 -05:00

52 lines
1.2 KiB
Go

package main
import (
"fmt"
"os"
"path/filepath"
luajit "git.sharkk.net/Sky/LuaJIT-to-Go"
)
func main() {
if len(os.Args) < 2 {
fmt.Fprintf(os.Stderr, "Usage: %s <script.lua>\n", filepath.Base(os.Args[0]))
os.Exit(1)
}
scriptPath := os.Args[1]
// Check if file exists
if _, err := os.Stat(scriptPath); os.IsNotExist(err) {
fmt.Fprintf(os.Stderr, "Error: script file '%s' not found\n", scriptPath)
os.Exit(1)
}
// Create new Lua state with standard libraries
state := luajit.New()
if state == nil {
fmt.Fprintf(os.Stderr, "Error: failed to create Lua state\n")
os.Exit(1)
}
defer state.Close()
// Set up module system
registry := NewModuleRegistry()
if err := registry.LoadEmbeddedModules(); err != nil {
fmt.Fprintf(os.Stderr, "Warning: failed to load built-in modules: %v\n", err)
}
// Backup original require and install module system
BackupOriginalRequire(state)
if err := registry.InstallModules(state); err != nil {
fmt.Fprintf(os.Stderr, "Error: failed to install module system: %v\n", err)
os.Exit(1)
}
// Execute the script
if err := state.DoFile(scriptPath); err != nil {
fmt.Fprintf(os.Stderr, "Error executing '%s': %v\n", scriptPath, err)
os.Exit(1)
}
}