Skip to content

Commit f6756e0

Browse files
committed
Fixed obscure Lua errors in filtering and validating callbacks
1 parent e2fb430 commit f6756e0

File tree

9 files changed

+46
-26
lines changed

9 files changed

+46
-26
lines changed

TextView.cpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1504,14 +1504,13 @@ void CTextView::OpenLuaDelayed ()
15041504

15051505
luaL_openlibs (L); // open all standard Lua libraries
15061506

1507-
luaopen_rex (L); // regular expression library
1508-
luaopen_bits (L); // bit manipulation library
1509-
luaopen_compress (L); // compression (utils) library
1510-
luaopen_progress_dialog (L); // progress dialog
1511-
luaopen_bc (L); // open bc library
1512-
luaopen_lsqlite3 (L); // open sqlite library
1513-
lua_pushcfunction(L, luaopen_lpeg); // open lpeg library
1514-
lua_call(L, 0, 0);
1507+
CallLuaCFunction (L, luaopen_rex); // regular expression library
1508+
CallLuaCFunction (L, luaopen_bits); // bit manipulation library
1509+
CallLuaCFunction (L, luaopen_compress); // compression (utils) library
1510+
CallLuaCFunction (L, luaopen_progress_dialog);// progress dialog
1511+
CallLuaCFunction (L, luaopen_bc); // open bc library
1512+
CallLuaCFunction (L, luaopen_lsqlite3); // open sqlite library
1513+
CallLuaCFunction (L, luaopen_lpeg); // open lpeg library
15151514

15161515
// add xml reader to utils lib
15171516
luaL_register (L, "utils", ptr_xmllib);

dialogs/FunctionListDlg.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ CFunctionListDlg::CFunctionListDlg(CWnd* pParent /*=NULL*/)
2626
m_bFunctions = false;
2727
m_bNoSort = false;
2828
m_L = NULL;
29+
m_iFilterIndex = LUA_NOREF;
2930
}
3031

3132

@@ -84,13 +85,13 @@ BOOL CFunctionListDlg::ReloadList ()
8485
string sValue = kv.sValue_;
8586
bool bWanted = false;
8687

87-
if (m_L && lua_type (m_L, 1) == LUA_TFUNCTION)
88+
if (m_L && m_iFilterIndex != LUA_NOREF)
8889
{
8990

91+
lua_rawgeti (m_L, LUA_REGISTRYINDEX, m_iFilterIndex);
92+
9093
// Lua filter: function f (filter, key, value) ... end
9194

92-
// filter function (make copy)
93-
lua_pushvalue (m_L, 1);
9495
// what they have currently typed
9596
lua_pushlstring (m_L, sFilter.c_str (), sFilter.size ());
9697

dialogs/FunctionListDlg.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ class CFunctionListDlg : public CDialog
3939

4040
// Lua state for filter function
4141
lua_State * m_L;
42+
int m_iFilterIndex;
4243

4344
// Overrides
4445
// ClassWizard generated virtual function overrides

dialogs/LuaInputBox.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ CLuaInputBox::CLuaInputBox(CWnd* pParent /*=NULL*/)
2828

2929
m_font = NULL;
3030
m_L = NULL;
31+
m_iValidationIndex = LUA_NOREF;
3132

3233
}
3334

@@ -49,14 +50,14 @@ void CLuaInputBox::DoDataExchange(CDataExchange* pDX)
4950
}
5051

5152

52-
if (m_L && lua_type (m_L, -1) == LUA_TFUNCTION)
53+
if (m_L && m_iValidationIndex != LUA_NOREF)
5354
{
5455
bool bWanted = false;
5556

57+
lua_rawgeti (m_L, LUA_REGISTRYINDEX, m_iValidationIndex);
58+
5659
// Lua validation: function f (value) ... end
5760

58-
// validate function (make copy)
59-
lua_pushvalue (m_L, -1);
6061
// what they have currently typed
6162
lua_pushlstring (m_L, m_strReply, m_strReply.GetLength ());
6263

dialogs/LuaInputBox.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ class CLuaInputBox : public CDialog
4444
CString m_strCancelbuttonLabel;
4545

4646
lua_State *m_L; // for validating
47+
int m_iValidationIndex; // where validation function is
4748

4849
// Overrides
4950
// ClassWizard generated virtual function overrides

dialogs/LuaInputEditDlg.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ CLuaInputEditDlg::CLuaInputEditDlg(CWnd* pParent /*=NULL*/)
2828

2929
m_font = NULL;
3030
m_L = NULL;
31+
m_iValidationIndex = LUA_NOREF;
3132

3233
}
3334

@@ -51,14 +52,14 @@ void CLuaInputEditDlg::DoDataExchange(CDataExchange* pDX)
5152
return;
5253
}
5354

54-
if (m_L && lua_type (m_L, -1) == LUA_TFUNCTION)
55+
if (m_L && m_iValidationIndex != LUA_NOREF)
5556
{
5657
bool bWanted = false;
5758

59+
lua_rawgeti (m_L, LUA_REGISTRYINDEX, m_iValidationIndex);
60+
5861
// Lua validation: function f (value) ... end
5962

60-
// validate function (make copy)
61-
lua_pushvalue (m_L, -1);
6263
// what they have currently typed
6364
lua_pushlstring (m_L, m_strReply, m_strReply.GetLength ());
6465

dialogs/LuaInputEditDlg.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ class CLuaInputEditDlg : public CDialog
4545
CString m_strCancelbuttonLabel;
4646

4747
lua_State *m_L; // for validating
48+
int m_iValidationIndex; // where validation function is
4849

4950
// Overrides
5051
// ClassWizard generated virtual function overrides

resource.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1562,7 +1562,7 @@
15621562
#ifdef APSTUDIO_INVOKED
15631563
#ifndef APSTUDIO_READONLY_SYMBOLS
15641564
#define _APS_3D_CONTROLS 1
1565-
#define _APS_NEXT_RESOURCE_VALUE 364
1565+
#define _APS_NEXT_RESOURCE_VALUE 365
15661566
#define _APS_NEXT_COMMAND_VALUE 33054
15671567
#define _APS_NEXT_CONTROL_VALUE 2897
15681568
#define _APS_NEXT_SYMED_VALUE 312

scripting/lua_utils.cpp

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -326,8 +326,6 @@ static int gen_inputbox (lua_State *L, T & msg)
326326
CString strOKbuttonLabel;
327327
CString strCancelbuttonLabel;
328328

329-
330-
331329
// if arg6 present, and a table, grab extra stuff
332330
if (lua_istable (L, nExtraStuffArg))
333331
{
@@ -348,10 +346,15 @@ static int gen_inputbox (lua_State *L, T & msg)
348346
if (!lua_isnil (L, -1))
349347
{
350348
if (!lua_isfunction (L, -1))
351-
luaL_error (L, "inputbox argument 6 value for 'validate' must be a function");
349+
luaL_error (L, "inputbox argument #6 value for 'validate' must be a function");
352350

353351
lua_pushvalue (L, -1); // function is now on top of stack
354352
msg.m_L = L; // non-NULL indicates we have function there
353+
354+
// we can't leave the function on the stack, that gets cleared from time to time
355+
// while the dialog box is running - so we store it in the registry and get the
356+
// unique index back
357+
msg.m_iValidationIndex = luaL_ref (L, LUA_REGISTRYINDEX);
355358
} // validate function there
356359

357360
} // table of extra stuff there
@@ -382,11 +385,16 @@ static int gen_inputbox (lua_State *L, T & msg)
382385
msg.m_strOKbuttonLabel = strOKbuttonLabel ;
383386
msg.m_strCancelbuttonLabel = strCancelbuttonLabel;
384387

388+
lua_settop (L, 0);
389+
385390
if (msg.DoModal () != IDOK)
386391
lua_pushnil (L);
387392
else
388393
lua_pushstring (L, msg.m_strReply);
389394

395+
// free up registry entry (is OK with LUA_NOREF)
396+
luaL_unref(L, LUA_REGISTRYINDEX, msg.m_iValidationIndex);
397+
390398
return 1;
391399
} // end of gen_inputbox
392400

@@ -1754,11 +1762,15 @@ CFunctionListDlg dlg;
17541762
if (!lua_isnil (L, 5))
17551763
{
17561764
luaL_checktype (L, 5, LUA_TFUNCTION);
1757-
lua_remove (L, 1); // get rid of bottom 4 items
1758-
lua_remove (L, 1);
1759-
lua_remove (L, 1);
1760-
lua_remove (L, 1);
1761-
dlg.m_L = L; // function is now at stack item 1
1765+
lua_pushvalue (L, 5); // function is now on top of stack
1766+
1767+
dlg.m_L = L;
1768+
1769+
// we can't leave the function on the stack, that gets cleared from time to time
1770+
// while the dialog box is running - so we store it in the registry and get the
1771+
// unique index back
1772+
dlg.m_iFilterIndex = luaL_ref (L, LUA_REGISTRYINDEX);
1773+
17621774
}
17631775
}
17641776

@@ -1777,6 +1789,9 @@ CFunctionListDlg dlg;
17771789
}
17781790
}
17791791

1792+
// free up registry entry (is OK with LUA_NOREF)
1793+
luaL_unref(L, LUA_REGISTRYINDEX, dlg.m_iFilterIndex);
1794+
17801795
return 1; // 1 result
17811796

17821797
} // end of filterpicker

0 commit comments

Comments
 (0)