From 1ee581de4ca488868f6c570e1802fb3da37302d8 Mon Sep 17 00:00:00 2001 From: Shmuel Zeigerman Date: Fri, 29 Nov 2024 20:54:41 +0200 Subject: [PATCH] LuaFAR: refactoring --- luafar/CMakeLists.txt | 11 ++++++----- luafar/lua_share/far2/test/macrotest.lua | 18 +++++++++--------- luafar/src/makeflags.lua | 6 +++--- luafar/src/mytimer.c | 4 ---- luafar/src/service.c | 18 ++++++++---------- 5 files changed, 26 insertions(+), 31 deletions(-) diff --git a/luafar/CMakeLists.txt b/luafar/CMakeLists.txt index 4ab3eb518..54260a98a 100644 --- a/luafar/CMakeLists.txt +++ b/luafar/CMakeLists.txt @@ -15,10 +15,6 @@ add_custom_command( DEPENDS ${SDK}/farplug-wide.h ${SDK}/farcolor.h ${SDK}/farkeys.h ${MAKEFLAGS} ${FARSRC}/DlgGuid.hpp ) -if (NOT ${CMAKE_SYSTEM_NAME} MATCHES "Android") - set(MYTIMER_C src/mytimer.c) -endif() - set(SOURCES ${CMAKE_CURRENT_BINARY_DIR}/farflags.c src/bit64.c @@ -27,7 +23,6 @@ set(SOURCES src/luamacro.c src/lusercontrol.c src/lutf8lib.c - ${MYTIMER_C} src/service.c src/slnunico.c src/sysutils.c @@ -44,6 +39,12 @@ set(SOURCES src/LPeg/lpvm.c ) +if (NOT (${CMAKE_SYSTEM_NAME} MATCHES "FreeBSD" OR + ${CMAKE_SYSTEM_NAME} MATCHES "DragonFly" OR + ${CMAKE_SYSTEM_NAME} MATCHES "Android")) + set(SOURCES ${SOURCES} src/mytimer.c) +endif() + add_library (${PROJECT_NAME} SHARED ${SOURCES}) set_property(TARGET ${PROJECT_NAME} PROPERTY C_STANDARD 11) target_include_directories(${PROJECT_NAME} PRIVATE ${LIBLUA_INCLUDE_DIRS}) diff --git a/luafar/lua_share/far2/test/macrotest.lua b/luafar/lua_share/far2/test/macrotest.lua index 5ea5cb5c7..4dffbfe01 100644 --- a/luafar/lua_share/far2/test/macrotest.lua +++ b/luafar/lua_share/far2/test/macrotest.lua @@ -31,10 +31,10 @@ local function assert_nil(v) assert(v==nil) return v; end local function assert_false(v) assert(v==false) return v; end local function assert_true(v) assert(v==true) return v; end -local function assert_range(val, low, high) - if low then assert(val >= low) end - if high then assert(val <= high) end - return val +local function assert_range(v, low, high) + if low then assert(v >= low) end + if high then assert(v <= high) end + return v end local function assert_numint(v) @@ -52,7 +52,7 @@ local function pack (...) return { n=select("#",...), ... } end -local TmpFileName = far.InMyTemp("tmp.tmp") +local TmpFileName = far.InMyTemp("macrotest.tmp") local function WriteTmpFile(...) local fp = assert(io.open(TmpFileName,"w")) @@ -111,9 +111,9 @@ end local function test_mf_akey() assert_eq(akey, mf.akey) - local k0,k1 = akey(0),akey(1) - assert(k0==far.NameToKey(MacroKey1) and k1==MacroKey1 or - k0==far.NameToKey(MacroKey2) and k1==MacroKey2) + local key,name = akey(0),akey(1) + assert(key==far.NameToKey(MacroKey1) and name==MacroKey1 or + key==far.NameToKey(MacroKey2) and name==MacroKey2) -- (the 2nd parameter is tested in function test_mf_eval). end @@ -413,7 +413,7 @@ local function test_mf_key() assert_eq (mf.key("foobar"), "") end --- Separate tests for mf.float and mf.string are locale-dependant, thus they are tested together. +-- Separate tests for mf.float and mf.string are locale-dependent, thus they are tested together. local function test_mf_float_and_string() local t = { 0, -0, 2.56e1, -5.37, -2.2e100, 2.2e-100 } for _,num in ipairs(t) do diff --git a/luafar/src/makeflags.lua b/luafar/src/makeflags.lua index d3edba995..0bc1628c7 100644 --- a/luafar/src/makeflags.lua +++ b/luafar/src/makeflags.lua @@ -129,11 +129,11 @@ typedef struct { local file_bottom = [[ -void add_flags (lua_State *L) +void push_far_flags (lua_State *L) { - int i; int nelem = sizeof(flags) / sizeof(flags[0]); - for (i=0; i @@ -394,5 +392,3 @@ int luaopen_timer(lua_State *L) lua_pushnil(L); return 1; } - -#endif // #if !defined(__FreeBSD__) && !defined(__DragonFly__) diff --git a/luafar/src/service.c b/luafar/src/service.c index d9e262502..727dc25a4 100644 --- a/luafar/src/service.c +++ b/luafar/src/service.c @@ -16,7 +16,7 @@ #include "util.h" #include "service.h" -extern void add_flags (lua_State *L); // from generated file farflags.c +extern void push_far_flags (lua_State *L); // from generated file farflags.c extern int luaopen_far_host(lua_State *L); extern int luaopen_regex (lua_State*); @@ -6244,8 +6244,7 @@ static int luaopen_far (lua_State *L) lua_setfield(L, LUA_REGISTRYINDEX, FAR_VIRTUALKEYS); luaL_register(L, "far", far_funcs); - lua_createtable(L, 0, 1600); - add_flags(L); + push_far_flags(L); lua_pushvalue(L, -1); lua_setfield(L, -3, "Flags"); lua_pushvalue(L, -1); // for compatibility with Far3 scripts @@ -6255,6 +6254,12 @@ static int luaopen_far (lua_State *L) luaopen_far_host(L); lua_setfield(L, -2, "Host"); +#if !defined(__FreeBSD__) && !defined(__DragonFly__) && !defined(__ANDROID__) + lua_pushcfunction(L, luaopen_timer); + lua_call(L, 0, 1); + lua_setfield(L, -2, "Timer"); +#endif + if (pd->Private && pd->PluginId == LuamacroId) { lua_pushcfunction(L, far_MacroCallFar); @@ -6277,13 +6282,6 @@ static int luaopen_far (lua_State *L) lua_setfield(L, -2, "__index"); luaL_register(L, NULL, filefilter_methods); -#if !defined(__FreeBSD__) && !defined(__DragonFly__) && !defined(__ANDROID__) - lua_getglobal(L, "far"); - lua_pushcfunction(L, luaopen_timer); - lua_call(L, 0, 1); - lua_setfield(L, -2, "Timer"); -#endif - lua_pushcfunction(L, luaopen_usercontrol); lua_call(L, 0, 0);