Skip to content

Commit 9cf2e6c

Browse files
committed
LuaFAR: refactoring
1 parent 55d855b commit 9cf2e6c

4 files changed

Lines changed: 86 additions & 86 deletions

File tree

plugins/luamacro/_globalinfo.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
function export.GetGlobalInfo()
22
return {
3-
Version = { 3, 0, 0, 923 },
3+
Version = { 3, 0, 0, 924 },
44
MinFarVersion = { 3, 0, 0, 6678 },
55
Guid = win.Uuid("4EBBEFC8-2084-4B7F-94C0-692CE136894D"),
66
Title = "LuaMacro",

plugins/luamacro/changelog

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
shmuel 2026-04-28 12:37:25+03:00 - build 924
2+
3+
1. LuaFAR: refactoring.
4+
15
shmuel 2026-04-27 18:48:05+03:00 - build 923
26

37
1. LuaFAR: refactoring.

plugins/luamacro/luafar/lf_luamacro.c

Lines changed: 80 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -32,26 +32,95 @@ static void InitMPR (lua_State* L, struct MacroPluginReturn *mpr, size_t nargs,
3232
if (nargs)
3333
{
3434
mpr->Values = (struct FarMacroValue*)lua_newuserdata(L, nargs*sizeof(struct FarMacroValue));
35-
lua_setfield(L, -2, "MacroPluginReturn");
35+
lua_setfield(L, -2, "MacroPluginReturn"); // protect from garbage collection
3636
}
3737
mpr->Count = nargs;
3838
mpr->ReturnType = ReturnType;
3939
}
4040

41-
HANDLE Open_Luamacro(lua_State* L, const struct OpenInfo *Info)
41+
static void FillMPR (lua_State* L, struct MacroPluginReturn* Ret, intptr_t ReturnType)
4242
{
43-
struct OpenMacroPluginInfo* om_info = (struct OpenMacroPluginInfo*)Info->Data;
44-
int calltype = om_info->CallType;
45-
size_t argc = om_info->Data ? om_info->Data->Count : 0; // store Data->Count: 'Data' will be invalid after FL_PushParams()
43+
lua_getfield(L,-1,"n");
44+
int nargs = lua_type(L,-1)==LUA_TNUMBER ? (int)lua_tointeger(L,-1) : (int)lua_objlen(L,-2);
45+
lua_pop(L,1);
46+
if (nargs < 0) nargs = 0;
47+
48+
InitMPR(L, Ret, (size_t)nargs, ReturnType);
49+
50+
for(int idx=0; idx<nargs; idx++)
51+
{
52+
INT64 val64;
53+
Ret->Values[idx].Type = FMVT_NIL;
54+
lua_rawgeti(L,-1,idx+1);
55+
56+
switch (lua_type(L, -1))
57+
{
58+
case LUA_TNUMBER:
59+
Ret->Values[idx].Type = FMVT_DOUBLE;
60+
Ret->Values[idx].Value.Double = lua_tonumber(L, -1);
61+
lua_pop(L,1);
62+
break;
63+
64+
case LUA_TSTRING:
65+
Ret->Values[idx].Type = FMVT_STRING;
66+
Ret->Values[idx].Value.String = check_utf8_string(L, -1, NULL);
67+
lua_rawseti(L,-2,idx+1);
68+
break;
69+
70+
case LUA_TBOOLEAN:
71+
Ret->Values[idx].Type = FMVT_BOOLEAN;
72+
Ret->Values[idx].Value.Boolean = lua_toboolean(L, -1);
73+
lua_pop(L,1);
74+
break;
75+
76+
case LUA_TLIGHTUSERDATA:
77+
Ret->Values[idx].Type = FMVT_POINTER;
78+
Ret->Values[idx].Value.Pointer = lua_touserdata(L, -1);
79+
lua_rawseti(L,-2,idx+1);
80+
break;
81+
82+
case LUA_TTABLE:
83+
lua_getfield(L, -1, TKEY_BINARY);
84+
if (lua_type(L, -1) == LUA_TSTRING)
85+
{
86+
Ret->Values[idx].Type = FMVT_BINARY;
87+
Ret->Values[idx].Value.Binary.Data = (char*)lua_tostring(L,-1);
88+
Ret->Values[idx].Value.Binary.Size = lua_objlen(L,-1);
89+
lua_rawseti(L,-3,idx+1);
90+
}
91+
lua_pop(L,1);
92+
break;
4693

94+
default:
95+
if (bit64_getvalue(L, -1, &val64))
96+
{
97+
Ret->Values[idx].Type = FMVT_INTEGER;
98+
Ret->Values[idx].Value.Integer = val64;
99+
lua_pop(L,1);
100+
}
101+
else
102+
{
103+
Ret->Values[idx].Type = FMVT_NIL;
104+
lua_pop(L,1);
105+
}
106+
break;
107+
}
108+
}
109+
}
110+
111+
HANDLE Open_Luamacro(lua_State* L, const struct OpenInfo *Info)
112+
{
47113
if (!IsEqualGUID(GetPluginData(L)->PluginId, LuamacroGuid))
48114
{
49115
lua_pop(L, 1);
50116
return NULL;
51117
}
52118

119+
struct OpenMacroPluginInfo* om_info = (struct OpenMacroPluginInfo*)Info->Data;
120+
size_t argc = om_info->Data ? om_info->Data->Count : 0; // store Data->Count: 'Data' will be invalid after FL_PushParams()
121+
53122
lua_pushinteger(L, Info->OpenFrom); //+2
54-
lua_pushinteger(L, calltype); //+3
123+
lua_pushinteger(L, om_info->CallType); //+3
55124
if (om_info->Data && !FL_PushParams(L, om_info->Data))
56125
{
57126
lua_pop(L, 3);
@@ -74,87 +143,14 @@ HANDLE Open_Luamacro(lua_State* L, const struct OpenInfo *Info)
74143
lua_pop(L,1);
75144
lua_pushvalue(L,-1);
76145
}
146+
77147
if (!lua_istable(L,-1))
78-
{
79148
InitMPR(L, &om_info->Ret, 0, ReturnType);
80-
lua_pop(L,2);
81-
return (HANDLE)1;
82-
}
83149
else
84-
{
85-
struct MacroPluginReturn* Ret = &om_info->Ret;
86-
87-
lua_getfield(L,-1,"n");
88-
int nargs = lua_type(L,-1)==LUA_TNUMBER ? (int)lua_tointeger(L,-1) : (int)lua_objlen(L,-2);
89-
lua_pop(L,1);
90-
if (nargs < 0)
91-
nargs = 0;
92-
93-
InitMPR(L, Ret, (size_t)nargs, ReturnType);
94-
95-
for(int idx=0; idx<nargs; idx++)
96-
{
97-
Ret->Values[idx].Type = FMVT_NIL;
98-
lua_rawgeti(L,-1,idx+1);
99-
100-
switch (lua_type(L, -1))
101-
{
102-
case LUA_TNUMBER:
103-
Ret->Values[idx].Type = FMVT_DOUBLE;
104-
Ret->Values[idx].Value.Double = lua_tonumber(L, -1);
105-
lua_pop(L,1);
106-
break;
107-
108-
case LUA_TSTRING:
109-
Ret->Values[idx].Type = FMVT_STRING;
110-
Ret->Values[idx].Value.String = check_utf8_string(L, -1, NULL);
111-
lua_rawseti(L,-2,idx+1);
112-
break;
113-
114-
case LUA_TBOOLEAN:
115-
Ret->Values[idx].Type = FMVT_BOOLEAN;
116-
Ret->Values[idx].Value.Boolean = lua_toboolean(L, -1);
117-
lua_pop(L,1);
118-
break;
119-
120-
case LUA_TLIGHTUSERDATA:
121-
Ret->Values[idx].Type = FMVT_POINTER;
122-
Ret->Values[idx].Value.Pointer = lua_touserdata(L, -1);
123-
lua_rawseti(L,-2,idx+1);
124-
break;
125-
126-
case LUA_TTABLE:
127-
lua_getfield(L, -1, TKEY_BINARY);
128-
if (lua_type(L, -1) == LUA_TSTRING)
129-
{
130-
Ret->Values[idx].Type = FMVT_BINARY;
131-
Ret->Values[idx].Value.Binary.Data = (char*)lua_tostring(L,-1);
132-
Ret->Values[idx].Value.Binary.Size = lua_objlen(L,-1);
133-
lua_rawseti(L,-3,idx+1);
134-
}
135-
lua_pop(L,1);
136-
break;
137-
138-
default:
139-
INT64 val64;
140-
if (bit64_getvalue(L, -1, &val64))
141-
{
142-
Ret->Values[idx].Type = FMVT_INTEGER;
143-
Ret->Values[idx].Value.Integer = val64;
144-
lua_pop(L,1);
145-
}
146-
else
147-
{
148-
Ret->Values[idx].Type = FMVT_NIL;
149-
lua_pop(L,1);
150-
}
151-
break;
152-
}
153-
}
150+
FillMPR(L, &om_info->Ret, ReturnType);
154151

155-
lua_pop(L,2);
156-
return (HANDLE)1;
157-
}
152+
lua_pop(L,2);
153+
return (HANDLE)1;
158154
}
159155

160156
return NULL;
@@ -263,7 +259,7 @@ int far_MacroCallFar(lua_State *L)
263259
fmc.Callback = MacroCallFarCallback;
264260
fmc.CallbackData = &cbdata;
265261

266-
for(int idx=0; idx<(int)fmc.Count; idx++)
262+
for(size_t idx=0; idx < fmc.Count; idx++)
267263
{
268264
ConvertLuaValue(L, idx+2, fmc.Values+idx);
269265
if (fmc.Values[idx].Type == FMVT_UNKNOWN)
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
#include <farversion.hpp>
22

3-
#define PLUGIN_BUILD 923
3+
#define PLUGIN_BUILD 924

0 commit comments

Comments
 (0)