Skip to content

Commit cef6a2d

Browse files
committed
LuaFAR: add a few stack operations to Macro-API engine.
1 parent c554345 commit cef6a2d

10 files changed

Lines changed: 121 additions & 22 deletions

File tree

far/changelog

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
--------------------------------------------------------------------------------
2+
shmuel 2025-10-24 20:22:16+03:00 - build 6576
3+
4+
1. Add a few stack operations to Macro-API engine.
5+
16
--------------------------------------------------------------------------------
27
drkns 2025-10-20 23:56:08+01:00 - build 6575
38

far/macroapi.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,31 @@ class FarMacroApi
130130
return PushValue(static_cast<long long>(Value));
131131
}
132132

133+
int StackGetTop() const
134+
{
135+
FarMacroValue val(FMVT_STACKGETTOP);
136+
SendValue(val);
137+
return val.Integer;
138+
}
139+
void StackSetTop(int top) const
140+
{
141+
FarMacroValue val(FMVT_STACKSETTOP);
142+
val.Integer = top;
143+
SendValue(val);
144+
}
145+
void StackPop(int count) const
146+
{
147+
FarMacroValue val(FMVT_STACKPOP);
148+
val.Integer = count;
149+
SendValue(val);
150+
}
151+
void StackPushValue(int pos) const
152+
{
153+
FarMacroValue val(FMVT_STACKPUSHVALUE);
154+
val.Integer = pos;
155+
SendValue(val);
156+
}
157+
133158
void absFunc() const;
134159
void ascFunc() const;
135160
void atoiFunc() const;

far/plugin.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1484,6 +1484,12 @@ enum FARMACROVARTYPE
14841484
FMVT_NEWTABLE = 12,
14851485
FMVT_SETTABLE = 13,
14861486
FMVT_DIALOG = 14,
1487+
FMVT_TABLE = 15,
1488+
FMVT_GETTABLE = 16,
1489+
FMVT_STACKPOP = 17,
1490+
FMVT_STACKGETTOP = 18,
1491+
FMVT_STACKSETTOP = 19,
1492+
FMVT_STACKPUSHVALUE = 20,
14871493
};
14881494

14891495
struct FarMacroValue

far/vbuild.m4

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
6575
1+
6576

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, 896 },
3+
Version = { 3, 0, 0, 897 },
44
MinFarVersion = { 3, 0, 0, 6564 },
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 2025-10-24 20:23:49+03:00 - build 897
2+
3+
1. LuaFAR: add a few stack operations to Macro-API engine.
4+
15
zg 2025-09-28 23:12:02+03:00 - build 896
26

37
1. Menu.GetItemExtendedData now has 1st optional parameter: dialog handle.

plugins/luamacro/luafar/lf_exported.c

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -802,14 +802,6 @@ void PushFarMacroValue(lua_State* L, const struct FarMacroValue* val)
802802
lua_setfield(L, -2, "type");
803803
break;
804804

805-
case FMVT_NEWTABLE:
806-
lua_newtable(L);
807-
break;
808-
809-
case FMVT_SETTABLE:
810-
lua_settable(L, -3);
811-
break;
812-
813805
default:
814806
lua_pushnil(L);
815807
break;

plugins/luamacro/luafar/lf_luamacro.c

Lines changed: 64 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -166,19 +166,71 @@ HANDLE Open_Luamacro(lua_State* L, const struct OpenInfo *Info)
166166
typedef struct
167167
{
168168
lua_State *L;
169-
int ret_avail;
169+
int start_stack;
170+
int max_stack;
170171
int error;
171172
} mcfc_data;
172173

173174
static void WINAPI MacroCallFarCallback(void *Data, struct FarMacroValue *Val, size_t Count)
174175
{
175176
mcfc_data *cbdata = (mcfc_data*)Data;
177+
if (cbdata->error)
178+
return;
179+
176180
(void) Count;
177-
if (!cbdata->error && cbdata->ret_avail > 0)
181+
lua_State *L = cbdata->L;
182+
int top = lua_gettop(L);
183+
int stack_avail = cbdata->max_stack - top;
184+
185+
switch(Val->Type)
178186
{
179-
cbdata->error = (Val->Type == FMVT_ERROR);
180-
cbdata->ret_avail += (Val->Type == FMVT_SETTABLE) ? 2 : -1;
181-
PushFarMacroValue(cbdata->L, Val);
187+
case FMVT_NEWTABLE:
188+
if (stack_avail > 0)
189+
lua_newtable(L);
190+
break;
191+
192+
case FMVT_SETTABLE:
193+
if (lua_istable(L, -3))
194+
lua_settable(L, -3);
195+
break;
196+
197+
case FMVT_GETTABLE:
198+
if (stack_avail > 0)
199+
{
200+
lua_gettable(L, -3);
201+
ConvertLuaValue(L, -1, Val);
202+
}
203+
break;
204+
205+
case FMVT_STACKPOP:
206+
int param = (int)Val->Value.Integer;
207+
if (param > 0 && (top - param) >= cbdata->start_stack)
208+
lua_pop(L, param);
209+
break;
210+
211+
case FMVT_STACKGETTOP:
212+
Val->Type = FMVT_INTEGER;
213+
Val->Value.Integer = lua_gettop(L);
214+
break;
215+
216+
case FMVT_STACKSETTOP:
217+
int val = (int)Val->Value.Integer;
218+
if (val >= cbdata->start_stack && val <= cbdata->max_stack)
219+
lua_settop(L, val);
220+
break;
221+
222+
case FMVT_STACKPUSHVALUE:
223+
if (stack_avail > 0)
224+
lua_pushvalue(L, (int)Val->Value.Integer);
225+
break;
226+
227+
default:
228+
if (stack_avail > 0)
229+
{
230+
cbdata->error = (Val->Type == FMVT_ERROR);
231+
PushFarMacroValue(L, Val);
232+
}
233+
break;
182234
}
183235
}
184236

@@ -187,17 +239,18 @@ int far_MacroCallFar(lua_State *L)
187239
enum { MAXARG=32, MAXRET=32 };
188240
struct FarMacroValue args[MAXARG];
189241
struct FarMacroCall fmc;
190-
int idx, ret, pushed;
191-
mcfc_data cbdata = { L, MAXRET, 0 };
242+
int top = lua_gettop(L);
243+
mcfc_data cbdata = { L, top, top + MAXRET, 0 };
192244
TPluginData *pd = GetPluginData(L);
193245
struct MacroPrivateInfo *privateInfo = (struct MacroPrivateInfo*)pd->Info->Private;
246+
194247
int opcode = (int)luaL_checkinteger(L, 1);
195-
fmc.Count = lua_gettop(L) - 1;
248+
fmc.Count = top - 1;
196249
fmc.Values = fmc.Count<=MAXARG ? args:(struct FarMacroValue*)malloc(fmc.Count*sizeof(struct FarMacroValue));
197250
fmc.Callback = MacroCallFarCallback;
198251
fmc.CallbackData = &cbdata;
199252

200-
for(idx=0; idx<(int)fmc.Count; idx++)
253+
for(int idx=0; idx<(int)fmc.Count; idx++)
201254
{
202255
ConvertLuaValue(L, idx+2, fmc.Values+idx);
203256
if (fmc.Values[idx].Type == FMVT_UNKNOWN)
@@ -213,9 +266,9 @@ int far_MacroCallFar(lua_State *L)
213266
}
214267

215268
lua_checkstack(L, MAXRET);
216-
ret = (int) privateInfo->CallFar(opcode, &fmc);
269+
int ret = (int) privateInfo->CallFar(opcode, &fmc);
217270
FP_PROTECT(); // protect from plugins activating FPU exceptions
218-
pushed = MAXRET - cbdata.ret_avail;
271+
int pushed = lua_gettop(L) - top;
219272
if (fmc.Values != args)
220273
free(fmc.Values);
221274
if (cbdata.error)

plugins/luamacro/luafar/lf_service.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,20 @@ void ConvertLuaValue (lua_State *L, int pos, struct FarMacroValue *target)
463463
target->Value.Binary.Data = (void*)lua_tolstring(L, -1, &target->Value.Binary.Size);
464464
}
465465
lua_pop(L,1);
466+
// lua_rawgeti(L,pos,0);
467+
// if (lua_type(L,-1) == LUA_TSTRING && !strcmp("binary", lua_tostring(L,-1)))
468+
// {
469+
// lua_rawgeti(L,pos,1);
470+
// target->Type = FMVT_BINARY;
471+
// target->Value.Binary.Size = 0;
472+
// target->Value.Binary.Data = (void*)lua_tolstring(L, -1, &target->Value.Binary.Size);
473+
// lua_pop(L,1);
474+
// }
475+
// else
476+
// {
477+
// target->Type = FMVT_TABLE;
478+
// }
479+
// lua_pop(L,1);
466480
}
467481
else if (type == LUA_TBOOLEAN)
468482
{
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 896
3+
#define PLUGIN_BUILD 897

0 commit comments

Comments
 (0)