• meir's avatar
    Protect globals of both evals scripts and functions. · 3731580b
    meir authored
    Use the new `lua_enablereadonlytable` Lua API to protect the global tables of
    both evals scripts and functions. For eval scripts, the implemetation is easy,
    We simply call `lua_enablereadonlytable` on the global table to turn it into
    a readonly table.
    
    On functions its more complecated, we want to be able to switch globals between
    load run and function run. To achieve this, we create a new empty table that
    acts as the globals table for function, we control the actual globals using metatable
    manipulation. Notice that even if the user gets a pointer to the original tables, all
    the tables are set to be readonly (using `lua_enablereadonlytable` Lua API) so he can
    not change them. The following inlustration better explain the solution:
    
    ```
    Global table {} <- global table metatable {.__index = __real_globals__}
    ```
    
    The `__real_globals__` is set depends on the run context (function load or function call).
    
    Why this solution is needed and its not enough to simply switch globals?
    When we run in the context of function load and create our functions, our function gets
    the current globals that was set when they were created. Replacing the globals after
    the creation will not effect them. This is why this trick it mandatory.
    3731580b
function_lua.c 17.6 KB