Page 1 of 1

Make table "love" local only to main.lua

Posted: Wed May 04, 2022 12:59 pm
by Taki
Hi, this is my first question on the forum :awesome:.

I want to prevent lua files outside main.lua from accessing the love table. Example:

Code: Select all

-- main.lua
require "test"

Code: Select all

-- test.lua
love.graphics.setColor(1, 1, 1) 
-- return error:
--test.lua:1: attempt to index global 'love' (a nil value)
or

Code: Select all

-- test.lua
print(love) -- return nil
I tried to use lua sandbox https://github.com/kikito/lua-sandbox, but even the cited examples don't work.
If it is possible to do this, how can I do it?

Re: Make table "love" local only to main.lua

Posted: Wed May 04, 2022 5:02 pm
by GVovkiv
try play around with environments with https://www.lua.org/pil/14.3.html

Re: Make table "love" local only to main.lua

Posted: Wed May 04, 2022 5:31 pm
by MrFariator
The simplest method might be

Code: Select all

-- at the top of main.lua
local love = love
_G.love = nil
Because the love api is wrapped in a single table, assigning a reference into a local variable and then setting the global to nil should prevent its use in other files.

The linked sandbox library should be able to achieve this, too, you'll just have to correctly pass the environment to use.

Little curious about what the specific reason or use-case might be.