summaryrefslogtreecommitdiff
path: root/etc/setfallback.lua
diff options
context:
space:
mode:
authorLua Team <team@lua.org>1998-07-11 12:00:00 +0000
committerrepogen <>1998-07-11 12:00:00 +0000
commit377347776f1f3d820f92151f70bec667f96d5e6b (patch)
treecdb3ba26158df33547dfe765547177afcee119d1 /etc/setfallback.lua
parent4f8c5d0f284e1f4da717aea5008915f185cd2e05 (diff)
downloadlua-github-3.1.tar.gz
Lua 3.13.1
Diffstat (limited to 'etc/setfallback.lua')
-rw-r--r--etc/setfallback.lua55
1 files changed, 55 insertions, 0 deletions
diff --git a/etc/setfallback.lua b/etc/setfallback.lua
new file mode 100644
index 00000000..783b8667
--- /dev/null
+++ b/etc/setfallback.lua
@@ -0,0 +1,55 @@
+--------------------------------------------------------------
+-- Definition of "setfallback" using tag methods
+-- (for compatibility with old code)
+--------------------------------------------------------------
+
+
+-- default fallbacks for each event:
+local defaults = {
+ gettable = function () error('indexed expression not a table') end,
+ settable = function () error('indexed expression not a table') end,
+ index = function () return nil end,
+ getglobal = function () return nil end,
+ arith = function () error('number expected in arithmetic operation') end,
+ order = function () error('incompatible types in comparison') end,
+ concat = function () error('string expected in concatenation') end,
+ gc = function () return nil end,
+ ['function'] = function () error('called expression not a function') end,
+ error = function (s) write(_STDERR, s, '\n') end,
+}
+
+
+function setfallback (name, func)
+
+ -- set the given function as the tag method for all "standard" tags
+ -- (since some combinations may cause errors, use call to avoid messages)
+ local fillvalids = function (n, func)
+ call(settagmethod, {0, n, func}, 'x', nil)
+ call(settagmethod, {tag(0), n, func}, 'x', nil)
+ call(settagmethod, {tag(''), n, func}, 'x', nil)
+ call(settagmethod, {tag{}, n, func}, 'x', nil)
+ call(settagmethod, {tag(function () end), n, func}, 'x', nil)
+ call(settagmethod, {tag(settagmethod), n, func}, 'x', nil)
+ call(settagmethod, {tag(nil), n, func}, 'x', nil)
+ end
+
+ assert(type(func) == 'function')
+ local oldfunc
+ if name == 'error' then
+ oldfunc = seterrormethod(func)
+ elseif name == 'getglobal' then
+ oldfunc = settagmethod(tag(nil), 'getglobal', func)
+ elseif name == 'arith' then
+ oldfunc = gettagmethod(tag(0), 'pow')
+ foreach({"add", "sub", "mul", "div", "unm", "pow"},
+ function(_, n) %fillvalids(n, %func) end)
+ elseif name == 'order' then
+ oldfunc = gettagmethod(tag(nil), 'lt')
+ foreach({"lt", "gt", "le", "ge"},
+ function(_, n) %fillvalids(n, %func) end)
+ else
+ oldfunc = gettagmethod(tag(nil), name)
+ fillvalids(name, func)
+ end
+ return oldfunc or rawgettable(%defaults, name)
+end