scripting tool: Use project buffers in io.open (#26425)

This PR makes `io.open` use our own implementation again, but instead of
the real filesystem, it will now use the project's to check file
metadata and perform read and writes using project buffers.

This also cleans up the `io.open` implementation by splitting it into
multiple methods, adds tests for various File I/O patterns, and fixes a
few bugs in read formats.

Release Notes:

- N/A
This commit is contained in:
Agus Zubiaga 2025-03-11 00:52:16 -03:00 committed by GitHub
parent d562f58e76
commit bf11b888c3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 502 additions and 335 deletions

View file

@ -8,9 +8,9 @@ local sandbox = {}
-- to our in-memory log rather than to stdout, we will delete this loop (and re-enable
-- the I/O module being sandboxed below) to have things be sandboxed again.
for k, v in pairs(_G) do
if sandbox[k] == nil then
sandbox[k] = v
end
if sandbox[k] == nil then
sandbox[k] = v
end
end
-- Allow access to standard libraries (safe subset)
@ -29,24 +29,27 @@ sandbox.search = search
sandbox.outline = outline
-- Create a sandboxed version of LuaFileIO
local io = {}
-- local io = {};
--
-- For now we are using unsandboxed io
local io = _G.io;
-- File functions
io.open = sb_io_open
-- Add the sandboxed io library to the sandbox environment
-- sandbox.io = io -- Uncomment this line to re-enable sandboxed file I/O.
sandbox.io = io
-- Load the script with the sandbox environment
local user_script_fn, err = load(user_script, nil, "t", sandbox)
if not user_script_fn then
error("Failed to load user script: " .. tostring(err))
error("Failed to load user script: " .. tostring(err))
end
-- Execute the user script within the sandbox
local success, result = pcall(user_script_fn)
if not success then
error("Error executing user script: " .. tostring(result))
error("Error executing user script: " .. tostring(result))
end