Skip to content

Commit b17c5b9

Browse files
committed
Opened Lua libraries in a more correct way
1 parent 890d720 commit b17c5b9

File tree

4 files changed

+50
-25
lines changed

4 files changed

+50
-25
lines changed

MUSHclient.cpp

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@
3030

3131
#include "dialogs\Splash.h"
3232
#include "direct.h"
33+
extern "C"
34+
{
35+
#include "scripting\number.h"
36+
void bc_free_numbers ();
37+
}
3338

3439
COLORREF xterm_256_colours [256];
3540

@@ -243,6 +248,8 @@ BOOL CMUSHclientApp::InitInstance()
243248
// where we do file browsing from
244249
strcpy (file_browsing_dir, working_dir);
245250

251+
// bc_init_numbers();
252+
246253

247254
// First free the string that was allocated by MFC in the startup
248255
// of CWinApp. The string is allocated before InitInstance is
@@ -1216,6 +1223,8 @@ int CMUSHclientApp::ExitInstance()
12161223

12171224
THEMEGLUE_FREE ();
12181225

1226+
// bc_free_numbers (); // free zero, one, two
1227+
12191228
// free the resources DLL
12201229
FreeLibrary (m_hInstDLL);
12211230
return CWinApp::ExitInstance();
@@ -1361,18 +1370,19 @@ void CMUSHclientApp::OpenLuaDelayed ()
13611370
return; // can't open Lua
13621371

13631372
luaL_openlibs (m_SpellChecker_Lua); // new way of opening all libraries
1364-
luaopen_rex (m_SpellChecker_Lua); // regular expression library
1365-
luaopen_bits (m_SpellChecker_Lua); // bit manipulation library
1366-
luaopen_compress (m_SpellChecker_Lua); // compression (utils) library
1367-
luaopen_progress_dialog (m_SpellChecker_Lua); // progress dialog
1368-
luaopen_bc (m_SpellChecker_Lua); // open bc library
1369-
luaopen_lsqlite3 (m_SpellChecker_Lua); // open sqlite library
1370-
lua_pushcfunction(m_SpellChecker_Lua, luaopen_lpeg); // open lpeg library
1371-
lua_call(m_SpellChecker_Lua, 0, 0);
1373+
1374+
CallLuaCFunction (m_SpellChecker_Lua, luaopen_rex); // regular expression library
1375+
CallLuaCFunction (m_SpellChecker_Lua, luaopen_bits); // bit manipulation library
1376+
CallLuaCFunction (m_SpellChecker_Lua, luaopen_compress); // compression (utils) library
1377+
CallLuaCFunction (m_SpellChecker_Lua, luaopen_progress_dialog);// progress dialog
1378+
CallLuaCFunction (m_SpellChecker_Lua, luaopen_bc); // open bc library
1379+
CallLuaCFunction (m_SpellChecker_Lua, luaopen_lsqlite3); // open sqlite library
1380+
CallLuaCFunction (m_SpellChecker_Lua, luaopen_lpeg); // open lpeg library
13721381

13731382
// add xml reader to utils lib
13741383
luaL_register (m_SpellChecker_Lua, "utils", ptr_xmllib);
13751384

1385+
13761386
lua_settop(m_SpellChecker_Lua, 0); // clear stack
13771387

13781388
// unless they explicitly enable it, remove ability to load DLLs
@@ -1711,13 +1721,12 @@ bool bSmallScreen = (iScreenX < 1024) || (iScreenY < 768);
17111721

17121722
luaL_openlibs (m_Translator_Lua); // new way of opening all libraries
17131723

1714-
luaopen_rex (m_Translator_Lua); // regular expression library
1715-
luaopen_bits (m_Translator_Lua); // bit manipulation library
1716-
luaopen_compress (m_Translator_Lua); // compression (utils) library
1717-
luaopen_bc (m_Translator_Lua); // open bc library
1718-
luaopen_lsqlite3 (m_Translator_Lua); // open sqlite library
1719-
lua_pushcfunction(m_Translator_Lua, luaopen_lpeg); // open lpeg library
1720-
lua_call(m_Translator_Lua, 0, 0);
1724+
CallLuaCFunction (m_Translator_Lua, luaopen_rex); // regular expression library
1725+
CallLuaCFunction (m_Translator_Lua, luaopen_bits); // bit manipulation library
1726+
CallLuaCFunction (m_Translator_Lua, luaopen_compress); // compression (utils) library
1727+
CallLuaCFunction (m_Translator_Lua, luaopen_bc); // open bc library
1728+
CallLuaCFunction (m_Translator_Lua, luaopen_lsqlite3); // open sqlite library
1729+
CallLuaCFunction (m_Translator_Lua, luaopen_lpeg); // open lpeg library
17211730

17221731
// add xml reader (and other stuff) to utils lib
17231732
luaL_register (m_Translator_Lua, "utils", ptr_xmllib);

scripting/lua_scripting.cpp

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -188,16 +188,13 @@ void CScriptEngine::OpenLuaDelayed ()
188188
lua_pushlightuserdata(L, (void *)m_pDoc); /* push value */
189189
lua_call(L, 2, 0);
190190

191-
luaopen_rex (L); // regular expression library
192-
luaopen_bits (L); // bit manipulation library
193-
luaopen_compress (L); // compression (utils) library
194-
luaopen_progress_dialog (L); // progress dialog
195-
// luaopen_trie (L); // open trie library
196-
luaopen_bc (L); // open bc library
197-
luaopen_lsqlite3 (L); // open sqlite library
198-
199-
lua_pushcfunction (L, luaopen_lpeg); // open lpeg library
200-
lua_call (L, 0, 0);
191+
CallLuaCFunction (L, luaopen_rex); // regular expression library
192+
CallLuaCFunction (L, luaopen_bits); // bit manipulation library
193+
CallLuaCFunction (L, luaopen_compress); // compression (utils) library
194+
CallLuaCFunction (L, luaopen_progress_dialog);// progress dialog
195+
CallLuaCFunction (L, luaopen_bc); // open bc library
196+
CallLuaCFunction (L, luaopen_lsqlite3); // open sqlite library
197+
CallLuaCFunction (L, luaopen_lpeg); // open lpeg library
201198

202199
lua_settop(L, 0); // clear stack
203200

@@ -773,3 +770,11 @@ int CallLuaWithTraceBack (lua_State *L, const int iArguments, const int iReturn)
773770
return error;
774771
} // end of CallLuaWithTraceBack
775772

773+
774+
// the more correct way of registering a Lua library -
775+
// this does a lua_call so we get the Lua environment in the C function
776+
void CallLuaCFunction (lua_State * L, lua_CFunction fn)
777+
{
778+
lua_pushcfunction (L, fn);
779+
lua_call (L, 0, 0);
780+
} // end of CallLuaCFunction

scripting/number.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,15 @@ bc_init_numbers ()
113113
_two_->n_value[0] = 2;
114114
}
115115

116+
/* Added by NJG to remove a memory leak */
117+
118+
void
119+
bc_free_numbers ()
120+
{
121+
bc_free_num (&_zero_);
122+
bc_free_num (&_one_);
123+
bc_free_num (&_two_);
124+
}
116125

117126
/* Make a copy of a number! Just increments the reference count! */
118127

scripting/scripting.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,3 +153,5 @@ class CKeyValuePair
153153

154154
}; // end of class CStringValuePair
155155

156+
// call a C function with the Lua environment
157+
void CallLuaCFunction (lua_State * L, lua_CFunction fn);

0 commit comments

Comments
 (0)