lua-users home
lua-l archive

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

On Fri, Apr 5, 2019 at 9:12 AM Sergey Kovalev wrote:
Let inroduce new reserved word "pure_function" into lua.
It should work similar to usual function except
all variables used inside are implicitly local
no access to any upvalues or global namespace _G
So it could interact with arguments it was passed.

This allows to isolate parts of code from whole program.

In lua 5.3.4 there is no way to do it.

There is a way.

function pure_function(source_text)
  return assert(load("local _ENV = {}; return "..source_text))()

Usage example:

local b=1

local sandbox = pure_function[[
function (x)
  return x^2

print(sandbox(5)) - will print 25

print("a=",a) -- will print 1
print("b=",b) -- will print 1