diff --git a/functions/registry.go b/functions/registry.go index 6613d58..c81d725 100644 --- a/functions/registry.go +++ b/functions/registry.go @@ -2,6 +2,7 @@ package functions import ( "maps" + "sync" "Moonshark/functions/http" @@ -24,3 +25,20 @@ func GetAll() Registry { return registry } + +var ( + storedBytecode []byte + bytecodeMutex sync.RWMutex +) + +func SetStoredBytecode(bytecode []byte) { + bytecodeMutex.Lock() + defer bytecodeMutex.Unlock() + storedBytecode = bytecode +} + +func GetStoredBytecode() []byte { + bytecodeMutex.RLock() + defer bytecodeMutex.RUnlock() + return storedBytecode +} diff --git a/modules/http.lua b/modules/http.lua index cd1037d..7feca09 100644 --- a/modules/http.lua +++ b/modules/http.lua @@ -101,7 +101,7 @@ end -- HTTP MODULE FUNCTIONS -- ====================================================================== -function http.createServer(options) +function http.newServer(options) options = options or {} local serverID = moonshark.http_create_server() @@ -119,7 +119,7 @@ end -- Convenience function to create and start server in one call function http.listen(addr, handler) - local server = http.createServer() + local server = http.newServer() if handler then server:get("/*", handler) diff --git a/moonshark.go b/moonshark.go index 5082a11..346fea2 100644 --- a/moonshark.go +++ b/moonshark.go @@ -59,8 +59,22 @@ func main() { fmt.Fprintf(os.Stderr, "Warning: failed to add script directory to package.path: %v\n", err) } - // Execute the script - if err := state.DoFile(scriptPath); err != nil { + // Read script file + scriptContent, err := os.ReadFile(scriptPath) + if err != nil { + fmt.Fprintf(os.Stderr, "Error reading script file '%s': %v\n", scriptPath, err) + os.Exit(1) + } + + // Compile script to bytecode + bytecode, err := state.CompileBytecode(string(scriptContent), scriptPath) + if err != nil { + fmt.Fprintf(os.Stderr, "Error compiling '%s': %v\n", scriptPath, err) + os.Exit(1) + } + + // Execute the compiled bytecode + if err := state.LoadAndRunBytecode(bytecode, scriptPath); err != nil { fmt.Fprintf(os.Stderr, "Error executing '%s': %v\n", scriptPath, err) os.Exit(1) }