From 5a422f40c7051b399b94b38d0dbb1a988f918b1c Mon Sep 17 00:00:00 2001 From: Jaromil Date: Fri, 11 Aug 2023 23:14:32 +0200 Subject: [PATCH] fix: remove all goto from lua code enhance compatibility with Lua 5.1 base standard --- src/lua/.luacheckrc | 2 +- src/lua/inspect.lua | 12 ++-- src/lua/zencode.lua | 16 ++---- src/lua/zencode_data.lua | 119 +++++++++++++++++++-------------------- 4 files changed, 69 insertions(+), 80 deletions(-) diff --git a/src/lua/.luacheckrc b/src/lua/.luacheckrc index 2933023a6..c530ca41f 100644 --- a/src/lua/.luacheckrc +++ b/src/lua/.luacheckrc @@ -6,7 +6,7 @@ globals = { 'require', 'require_once','fif', 'deepmap', 'luatype', 'sort_pairs', 'empty', 'have', 'initkeyring', 'havekey', 'zenguard', 'exitcode', 'deprecated', 'mayhave', 'parse_prefix', 'strtok', 'strcasecmp', - 'uscore', 'debug_traceback', + 'uscore', 'debug_traceback', 'isnumber' 'G2','ABC','ECDH', 'check_codec', 'ZKP_challenge', 'SHA256', 'SHA512', 'sha256', 'sha512', 'printerr', 'act', 'notice', 'warn', 'error', 'xxx', 'fatal', 'trim', 'serialize', 'iszen', diff --git a/src/lua/inspect.lua b/src/lua/inspect.lua index 1f645c3ab..5c9b6c3df 100644 --- a/src/lua/inspect.lua +++ b/src/lua/inspect.lua @@ -65,17 +65,13 @@ local function export_arr(object, format) local ft = type(format) if format and ft == 'function' then conv_f = format - goto ok - end - if format and ft == 'string' then + elseif format and ft == 'string' then conv_f = get_encoding_function(format) - goto ok - end - if not CONF.output.encoding then + elseif not CONF.output.encoding then error('CONF.output.encoding is not configured', 2) + else + conv_f = CONF.debug.encoding.fun -- fallback to configured conversion function end - conv_f = CONF.debug.encoding.fun -- fallback to configured conversion function - ::ok:: ZEN.assert( type(conv_f) == 'function', 'export_arr conversion function not configured' diff --git a/src/lua/zencode.lua b/src/lua/zencode.lua index 2751eb59d..cbe06604b 100644 --- a/src/lua/zencode.lua +++ b/src/lua/zencode.lua @@ -563,8 +563,7 @@ function zencode:parse(text) for line in zencode_newline_iter(text) do linenum = linenum + 1 local tline = trim(line) -- saves trims in isempty / iscomment - if zencode_isempty(tline) then goto continue end - if zencode_iscomment(tline) then goto continue end + if not zencode_isempty(tline) and not zencode_iscomment(tline) then -- xxx('Line: '.. text, 3) -- max length for single zencode line is #define MAX_LINE -- hard-coded inside zenroom.h @@ -599,7 +598,8 @@ function zencode:parse(text) assert(fm(self.machine, { msg = tline, Z = self }), line.."\n ".. "Invalid transition from: "..self.machine.current) - ::continue:: + end + -- continue end collectgarbage'collect' return true @@ -777,12 +777,7 @@ function zencode:run() local x = self.AST[ZEN.current_instruction] ZEN.next_instruction = ZEN.next_instruction + 1 -- ZEN:trace(x.source) - if manage_branching(x) then - goto continue - end - if manage_foreach(x) then - goto continue - end + if not manage_branching(x) and not manage_foreach(x) then -- trigger upon switch to when or then section if x.from == 'given' and x.to ~= 'given' then -- delete IN memory @@ -808,7 +803,8 @@ function zencode:run() fatal(x.source) -- traceback print inside end collectgarbage 'collect' - ::continue:: + end + -- ::continue:: end -- PRINT output ZEN:trace('--- Zencode execution completed') diff --git a/src/lua/zencode_data.lua b/src/lua/zencode_data.lua index 1dda719a2..2182e0732 100644 --- a/src/lua/zencode_data.lua +++ b/src/lua/zencode_data.lua @@ -34,67 +34,64 @@ -- Used in scenario's schema declarations to cast to zenroom. type ZEN.get = function(obj, key, conversion, encoding) - if type(key) ~= 'string' then - error('ZEN.get key is not a string', 2) end - if conversion and type(conversion) ~= 'function' then - error('ZEN.get invalid conversion function', 2) end - if encoding and type(encoding) ~= 'function' then - error('ZEN.get invalid encoding function', 2) end - local k - if not obj then -- take from IN root - -- does not support to pick in WHO (use of 'my') - k = KIN[key] or IN[key] - else - if key == '.' then - k = obj - else - k = obj[key] - end - end - if not k then - error('Key not found in object conversion: ' .. key, 2) - end - local res = nil - local t = type(k) - -- BIG/INT default export is decimal - if conversion == INT.new and not encoding then - -- compare function address and use default - res = INT.from_decimal(k) - goto ok - end - if iszen(t) and conversion then - res = conversion(k) - goto ok - end - if iszen(t) and not conversion then - res = k - goto ok - end - if t == 'string' then - if encoding then - res = encoding(k) - else - res = CONF.input.encoding.fun(k) - end - if conversion then - res = conversion(res) - end - goto ok - end - if t == 'number' then - res = k - end - if t == 'table' then - res = deepmap(encoding or CONF.input.encoding.fun, k) - if conversion then - res = deepmap(conversion, res) - end - end - ::ok:: - assert( - ZEN.OK and res, - 'ZEN.get on invalid key: ' .. key .. ' (' .. t .. ')', 2) - return res + if type(key) ~= 'string' then + error('ZEN.get key is not a string', 2) + end + if conversion and type(conversion) ~= 'function' then + error('ZEN.get invalid conversion function', 2) + end + if encoding and type(encoding) ~= 'function' then + error('ZEN.get invalid encoding function', 2) + end + local k + if not obj then -- take from IN root + -- does not support to pick in WHO (use of 'my') + k = KIN[key] or IN[key] + else + if key == '.' then + k = obj + else + k = obj[key] + end + end + if not k then + error('Key not found in object conversion: ' .. key, 2) + end + local res = nil + local t = type(k) + -- BIG/INT default export is decimal + if conversion == INT.new and not encoding then + -- compare function address and use default + res = INT.from_decimal(k) + elseif iszen(t) and conversion then + res = conversion(k) + elseif iszen(t) and not conversion then + res = k + elseif t == 'string' then + if encoding then + res = encoding(k) + else + res = CONF.input.encoding.fun(k) + end + if conversion then + res = conversion(res) + end + else + if t == 'number' then + res = k + end + if t == 'table' then + res = deepmap(encoding or CONF.input.encoding.fun, k) + if conversion then + res = deepmap(conversion, res) + end + end + end + + assert( + ZEN.OK and res, + 'ZEN.get on invalid key: ' .. key .. ' (' .. t .. ')', 2) + return res end -- return leftmost and rightmost if definition string indicates