From 7b74dca5ecb1455ff525bce3b221a81da56a80dc Mon Sep 17 00:00:00 2001 From: garret Date: Thu, 1 Dec 2022 00:08:10 +0000 Subject: run scripts in a """"sandbox"""" don't have to fuck around with global variables any more, poggers --- make-feed.lua | 37 ++++++++++++++++++++++--------------- 1 file changed, 22 insertions(+), 15 deletions(-) (limited to 'make-feed.lua') diff --git a/make-feed.lua b/make-feed.lua index a0bcc89..0e34fdd 100755 --- a/make-feed.lua +++ b/make-feed.lua @@ -153,31 +153,38 @@ local function fake_depctrl(i) } end -local function run_file(file, extension) - local runner - local old_require = require - _G.require = function(obj) - if obj == "l0.DependencyControl" then - return fake_depctrl +local function sandbox_require(obj) + if obj == "l0.DependencyControl" then + return fake_depctrl + else + local got, lib = pcall(require, obj) + if got then + return lib else - local got, lib = pcall(old_require, obj) - if got then - return lib - else - err(file .. " tried to require " .. obj .. " but couldn't. skipping and hoping it won't matter.") - return {} --Some default value, hopefully it should be fine with it - end + err("tried to require " .. obj .. " but couldn't. skipping and hoping it won't matter.") + return {} --Some default value, hopefully it should be fine with it end end +end + +local function run_file(file, extension) + local runner + local env = deepcopy(_G) -- i dont care + env.require = sandbox_require + -- TODO: care if extension == "moon" then runner = moonscript.loadfile(file) else runner = loadfile(file) end + if runner == nil then err(file .. " didn't load!") return nil end + + setfenv(runner, env) + local worked, out = pcall(runner) - if not worked then err("error when loading "..file..": ".. out) end - _G.require = old_require + if not worked then err("error when executing "..file..": ".. out) return nil end + return env end local function get_macro_metadata(file) -- cgit v1.2.3-70-g09d2