From 04a966c80962a7c218d1ee37398b17447c0e60e6 Mon Sep 17 00:00:00 2001
From: Albert Graef <aggraef@gmail.com>
Date: Fri, 23 Mar 2018 11:09:28 +0100
Subject: [PATCH] lexpr, ltabfill: Lua 5.3 compatibility

---
 examples/lexpr.pd_lua    |  4 ++++
 examples/ltabfill.pd_lua | 23 +++++++++++++++++++++++
 2 files changed, 27 insertions(+)

diff --git a/examples/lexpr.pd_lua b/examples/lexpr.pd_lua
index 98ee8e1..94c2396 100644
--- a/examples/lexpr.pd_lua
+++ b/examples/lexpr.pd_lua
@@ -19,6 +19,10 @@ if not setfenv then -- Lua 5.2
     return f end
 end
 
+if not loadstring then -- Lua 5.3
+   loadstring = load
+end
+
 local function sandbox(e, f) -- only supports nullary f() with one return
   local g = getfenv(f)
   setfenv(f, e)
diff --git a/examples/ltabfill.pd_lua b/examples/ltabfill.pd_lua
index 56ef81b..d81c5cb 100644
--- a/examples/ltabfill.pd_lua
+++ b/examples/ltabfill.pd_lua
@@ -1,5 +1,28 @@
 local ltabfill = pd.Class:new():register("ltabfill")
 
+-- AG: Lua 5.2 compatibility (cf. https://stackoverflow.com/a/14554565)
+if not setfenv then -- Lua 5.2
+  -- based on http://lua-users.org/lists/lua-l/2010-06/msg00314.html
+  -- this assumes f is a function
+  local function findenv(f)
+    local level = 1
+    repeat
+      local name, value = debug.getupvalue(f, level)
+      if name == '_ENV' then return level, value end
+      level = level + 1
+    until name == nil
+    return nil end
+  getfenv = function (f) return(select(2, findenv(f)) or _G) end
+  setfenv = function (f, t)
+    local level = findenv(f)
+    if level then debug.setupvalue(f, level, t) end
+    return f end
+end
+
+if not loadstring then -- Lua 5.3
+   loadstring = load
+end
+
 local function sandbox(e, f, x) -- only supports unary f() with one return
   local g = getfenv(f)
   setfenv(f, e)
-- 
GitLab