lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Egor wrote:

> On Sun, Jul 23, 2017 at 8:48 PM, Mikhail Zajcev wrote:

> Other idea is to modify demo.lua:

>  1. Load user script in separate environment:
>     local env = {}
>     setmetatable(env, {__index = _ENV})





> This will not prevent user from inserting custom functions and
> tables in "math", "string" and "table" libraries.

> Example:


> math.primes = setmetatable({2,3,5,7,11}, {__index = function(t,k)
> ... end, __gc = function() print("BYE") end})


> In this case, clearing the "env" and collecting the garbage is not
> enough to trigger __gc metamethod of math.primes.

This can be fixed by making copies of standard library tables, available to user script, in "env", so it will see its own math, string, etc. tables instead of global ones, and they will be collected after deletion of "env".