From ac50b855f4a40d3341f26e2d4243cd2ade3a6115 Mon Sep 17 00:00:00 2001 From: Dave Lucia Date: Sun, 26 Jul 2026 23:23:15 -0400 Subject: [PATCH] website: parse playground snippets once per run LuaSandbox.eval/3 parsed every snippet twice: once via Lua.parse_chunk/1 for the bytecode pane and again inside Lua.eval!/2. Reuse the compiled chunk for evaluation; a parse failure still falls through to eval!/2 on the source so the CompilerException error path is unchanged. --- website/lib/website/lua_sandbox.ex | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/website/lib/website/lua_sandbox.ex b/website/lib/website/lua_sandbox.ex index 27e4cfed..a39f1438 100644 --- a/website/lib/website/lua_sandbox.ex +++ b/website/lib/website/lua_sandbox.ex @@ -212,13 +212,16 @@ defmodule Website.LuaSandbox do end) try do - bytecode = + # Parse once and reuse the chunk for both the bytecode pane and the + # evaluation itself. A parse failure falls through to eval!/2 on the + # source so the error path still raises the usual CompilerException. + {bytecode, runnable} = case Lua.parse_chunk(source) do - {:ok, %Lua.Chunk{prototype: proto}} -> disassemble(proto) - _ -> [] + {:ok, %Lua.Chunk{prototype: proto} = chunk} -> {disassemble(proto), chunk} + _ -> {[], source} end - {results, _lua} = Lua.eval!(lua, source) + {results, _lua} = Lua.eval!(lua, runnable) %{ status: :ok,