From 9b2f23319c5ac84927774fca02b99bbb98057ebd Mon Sep 17 00:00:00 2001 From: William Wilgus Date: Mon, 3 May 2021 23:06:40 -0400 Subject: lua fix yellow and add temploader temp loader allows some lua requires to be loaded and later garbage collected unfortunately the module needs to be formatted in such a way to pass back a call table in order to keep the functions within from being garbage collected too early BE AWARE this bypasses the module loader which would allow code reuse so if you aren't careful this memory saving tool could spell disaster for free RAM if you load the same code multiple times Change-Id: I0b6f81e481b8c779edbd620c8403794f8353926f --- apps/plugins/lua/include_lua/create_kbd_layout.lua | 119 +++++++++++---------- apps/plugins/lua/include_lua/temploader.lua | 30 ++++++ apps/plugins/lua/lua.make | 3 +- apps/plugins/lua/rocklib.c | 8 +- 4 files changed, 102 insertions(+), 58 deletions(-) create mode 100644 apps/plugins/lua/include_lua/temploader.lua (limited to 'apps/plugins/lua') diff --git a/apps/plugins/lua/include_lua/create_kbd_layout.lua b/apps/plugins/lua/include_lua/create_kbd_layout.lua index bb3ab297ca..3d8f343cc9 100644 --- a/apps/plugins/lua/include_lua/create_kbd_layout.lua +++ b/apps/plugins/lua/include_lua/create_kbd_layout.lua @@ -1,74 +1,82 @@ --create keyboard layout --BILGUS 4/2021 -local function encode_short(n) - return string.char(bit.band(0x00FF, n), bit.rshift(bit.band(0xFF00, n), 8)) -end +-- kbdlayout = require("create_kbd_layout") +-- local layout = kbdlayout.create_keyboard_layout("abcd") +local _kbdlayout = {} do -function utf8decode(str) - local INVALID = 0xfffd - local t = {} - local function check_char(c) - local tail = false - local code - c = string.byte(c) - if (c <= 0x7f) or (c >= 0xc2) then - -- Start of new character - if (c < 0x80) then -- U-00000000 - U-0000007F, 1 string.byte - code = c; - elseif (c < 0xe0) then -- U-00000080 - U-000007FF, 2 string.bytes - tail = 1; - code = bit.band(c, 0x1f) - elseif (c < 0xf0) then -- U-00000800 - U-0000FFFF, 3 string.bytes - tail = 2; - code = bit.band(c, 0x0f) - elseif (c < 0xf5) then -- U-00010000 - U-001FFFFF, 4 string.bytes - tail = 3; - code = bit.band(c, 0x07) - else - -- Invalid size - code = 0xfffd; - end + local function encode_short(n) + return string.char(bit.band(0x00FF, n), bit.rshift(bit.band(0xFF00, n), 8)) + end - while tail and c ~= 0 do - tail = tail - 1 - if bit.band(c, 0xc0) == 0x80 then - -- Valid continuation character - code = bit.bor(bit.lshift(code, 6),bit.band(c, 0x3f)) + local function utf8decode(str) + local INVALID = 0xfffd + local t = {} + local function check_char(c) + local tail = false + local code + c = string.byte(c) + if (c <= 0x7f) or (c >= 0xc2) then + -- Start of new character + if (c < 0x80) then -- U-00000000 - U-0000007F, 1 string.byte + code = c; + elseif (c < 0xe0) then -- U-00000080 - U-000007FF, 2 string.bytes + tail = 1; + code = bit.band(c, 0x1f) + elseif (c < 0xf0) then -- U-00000800 - U-0000FFFF, 3 string.bytes + tail = 2; + code = bit.band(c, 0x0f) + elseif (c < 0xf5) then -- U-00010000 - U-001FFFFF, 4 string.bytes + tail = 3; + code = bit.band(c, 0x07) else - -- Invalid continuation char - code = INVALID; - break; + -- Invalid size + code = 0xfffd; + end + + while tail and c ~= 0 do + tail = tail - 1 + if bit.band(c, 0xc0) == 0x80 then + -- Valid continuation character + code = bit.bor(bit.lshift(code, 6),bit.band(c, 0x3f)) + else + -- Invalid continuation char + code = INVALID; + break; + end end + else + -- Invalid UTF-8 char + code = INVALID; end - else - -- Invalid UTF-8 char - code = INVALID; + -- currently we don't support chars above U-FFFF + t[#t + 1 ] = encode_short((code < 0x10000) and code or 0xfffd) end - -- currently we don't support chars above U-FFFF - t[#t + 1 ] = encode_short((code < 0x10000) and code or 0xfffd) + str:gsub(".", check_char) -- run check function for every char + return table.concat(t) end - str:gsub(".", check_char) -- run check function for every char - return table.concat(t) -end -function create_keyboard_layout(s_layout) - local insert = table.insert - lines = {} + local function create_keyboard_layout(s_layout) + local insert = table.insert + lines = {} - local t={} - for str in string.gmatch(s_layout, "([^\n]+)") do - local len = string.len(str) - lines[#lines + 1] = - table.concat({encode_short(len), utf8decode(str)}) - end - lines[#lines + 1] = encode_short(0xFEFF) + local t={} + for str in string.gmatch(s_layout, "([^\n]+)") do + local len = string.len(str) + lines[#lines + 1] = + table.concat({encode_short(len), utf8decode(str)}) + end + lines[#lines + 1] = encode_short(0xFEFF) - return table.concat(lines) + return table.concat(lines) + end + _kbdlayout.create_keyboard_layout = create_keyboard_layout + _kbdlayout.utf8decode = utf8decode end + --[[ local name = "Test_KBD_LAYOUT_" .. tostring(1) -local test = create_keyboard_layout("ABCDEFGHIJKLM\nNOPQRSTUVWXYZ\n0123456789") +local test = _kbdlayout.create_keyboard_layout("ABCDEFGHIJKLM\nNOPQRSTUVWXYZ\n0123456789") local file = io.open('/' .. name, "w+") -- overwrite, rb ignores the 'b' flag file:write(test)-- write the layout to the file now file:close() @@ -79,3 +87,4 @@ if not file then end rb.kbd_input(name, test) ]] +return _kbdlayout diff --git a/apps/plugins/lua/include_lua/temploader.lua b/apps/plugins/lua/include_lua/temploader.lua new file mode 100644 index 0000000000..69eae468a8 --- /dev/null +++ b/apps/plugins/lua/include_lua/temploader.lua @@ -0,0 +1,30 @@ +--[[ +temp loader allows some lua requires to be loaded and later garbage collected +unfortunately the module needs to be formatted in such a way to pass back a +call table in order to keep the functions within from being garbage collected +too early + +BE AWARE this bypasses the module loader which would allow code reuse +so if you aren't careful this memory saving tool could spell disaster +for free RAM if you load the same code multiple times +--]] + + +local function tempload(modulename) + --http://lua-users.org/wiki/LuaModulesLoader + local errmsg = "" + -- Find source + local modulepath = string.gsub(modulename, "%.", "/") + for path in string.gmatch(package.path, "([^;]+)") do + local filename = string.gsub(path, "%?", modulepath) + local file = io.open(filename, "r") + if file then + -- Compile and return the module + return assert(loadstring(assert(file:read("*a")), filename))() + end + errmsg = errmsg.."\n\tno file '"..filename.."' (temp loader)" + end + return errmsg +end + +return tempload diff --git a/apps/plugins/lua/lua.make b/apps/plugins/lua/lua.make index eada6b370f..60dfd24cdd 100644 --- a/apps/plugins/lua/lua.make +++ b/apps/plugins/lua/lua.make @@ -22,7 +22,8 @@ LUA_INCLUDELIST := $(addprefix $(LUA_BUILDDIR)/,audio.lua blit.lua color.lua \ math_ex.lua print.lua timer.lua playlist.lua pcm.lua \ sound.lua rbcompat.lua rbsettings.lua poly_points.lua \ printtable.lua printmenus.lua printsubmenu.lua \ - menubuttons.lua menucoresettings.lua create_kbd_layout.lua) + menubuttons.lua menucoresettings.lua create_kbd_layout.lua \ + temploader.lua) ifndef APP_TYPE ROCKS += $(LUA_BUILDDIR)/lua.rock diff --git a/apps/plugins/lua/rocklib.c b/apps/plugins/lua/rocklib.c index 050fbc73b2..2672d446fc 100644 --- a/apps/plugins/lua/rocklib.c +++ b/apps/plugins/lua/rocklib.c @@ -153,7 +153,7 @@ RB_WRAP(kbd_input) const char *input = lua_tostring(L, 1); size_t layout_len; - const char *layout = lua_tolstring(L, 2, &layout_len); + const unsigned char *layout = lua_tolstring(L, 2, &layout_len); char *buffer = luaL_prepbuffer(&b); if(input != NULL) @@ -161,8 +161,12 @@ RB_WRAP(kbd_input) else buffer[0] = '\0'; - if(layout_len <= 1 || (unsigned short)layout[layout_len - 1] != 0xFFFE) + if(layout_len <= 2 || + layout[layout_len - 1] != 0xFE || + layout[layout_len - 2] != 0xFF) + { layout = NULL; + } if(!rb->kbd_input(buffer, LUAL_BUFFERSIZE, (unsigned short *)layout)) { -- cgit