From 1ad3059ff0b98c9f395af0a60e9f0c723d77f01b Mon Sep 17 00:00:00 2001 From: Sky Johnson Date: Mon, 2 Jun 2025 12:29:45 -0500 Subject: [PATCH] fix fenv error --- runner/sandbox.go | 35 +++++++++++++++++++++++------------ 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/runner/sandbox.go b/runner/sandbox.go index 8153fdc..d9e236e 100644 --- a/runner/sandbox.go +++ b/runner/sandbox.go @@ -130,25 +130,36 @@ func (s *Sandbox) registerCoreFunctions(state *luajit.State) error { // Execute runs a Lua script in the sandbox with the given context func (s *Sandbox) Execute(state *luajit.State, bytecode []byte, ctx *Context) (*Response, error) { - // Use CallGlobal for cleaner function calling - results, err := state.CallGlobal("__execute_script", - func() any { - if err := state.LoadBytecode(bytecode, "script"); err != nil { - return nil - } - return "loaded_script" - }(), - ctx.Values) + // Load bytecode - pushes function onto stack + if err := state.LoadBytecode(bytecode, "script"); err != nil { + return nil, fmt.Errorf("failed to load bytecode: %w", err) + } - if err != nil { + // Stack: [function] + state.GetGlobal("__execute_script") // Stack: [function, __execute_script] + state.PushCopy(-2) // Stack: [function, __execute_script, function] + + // Push context using PushValue + if err := state.PushValue(ctx.Values); err != nil { + state.Pop(3) + return nil, fmt.Errorf("failed to push context: %w", err) + } + // Stack: [function, __execute_script, function, context] + + // Call __execute_script(function, context) + if err := state.Call(2, 1); err != nil { + state.Pop(1) // Clean up original function return nil, fmt.Errorf("script execution failed: %w", err) } + // Stack: [function, result] response := NewResponse() - if len(results) > 0 { - response.Body = results[0] + if result, err := state.ToValue(-1); err == nil { + response.Body = result } + state.SetTop(0) // Clear stack + extractHTTPResponseData(state, response) return response, nil }