Skip to content

Commit 5914bd8

Browse files
committed
Changes to the way Lua is initialized
1 parent 7dd4255 commit 5914bd8

File tree

2 files changed

+45
-80
lines changed

2 files changed

+45
-80
lines changed

install/readme.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
MUSHclient version 4.60
22
=======================
33

4-
Monday, 30th August 2010
4+
Sunday, 5th September 2010
55

66
Author: Nick Gammon
77
Web support: http://www.gammon.com.au/forum/

scripting/lua_methods.cpp

Lines changed: 44 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ LUALIB_API void *isudata (lua_State *L, int ud, const char *tname) {
5656
// checks user data is mushclient.doc, and returns pointer to world
5757
static CMUSHclientDoc *doc (lua_State *L)
5858
{
59+
// mushclient_typename is "mushclient.world"
5960
CMUSHclientDoc **ud = (CMUSHclientDoc **) isudata (L, 1, mushclient_typename);
6061

6162
// if first argument is a world userdatum, take that as our world
@@ -79,15 +80,12 @@ CMUSHclientDoc **ud = (CMUSHclientDoc **) isudata (L, 1, mushclient_typename);
7980
luaL_error (L, "world is no longer available");
8081
}
8182

82-
// retrieve our state
83-
CMUSHclientDoc * pDoc;
83+
// retrieve our state (world pointer)
8484

85-
/* retrieve the document */
86-
lua_pushstring(L, DOCUMENT_STATE); /* push address */
87-
lua_gettable(L, LUA_ENVIRONINDEX); /* retrieve value */
88-
89-
pDoc = (CMUSHclientDoc *) lua_touserdata(L, -1); /* convert to data */
90-
lua_pop(L, 1); /* pop result */
85+
// retrieve the document pointer from the environment table
86+
lua_getfield (L, LUA_ENVIRONINDEX, DOCUMENT_STATE); // get "mushclient.document" value
87+
CMUSHclientDoc * pDoc = (CMUSHclientDoc *) lua_touserdata (L, -1); // convert to world pointer
88+
lua_pop (L, 1); // pop document pointer
9189

9290
return pDoc;
9391
}
@@ -7465,74 +7463,53 @@ static flags_pair miniwindow_flags [] =
74657463

74667464
extern const struct luaL_reg *ptr_xmllib;
74677465

7468-
/*
7469-
static int l_errorhandler (lua_State *L)
7470-
{
7471-
7472-
::TMessageBox ("errorhandler");
7473-
7474-
return 0;
7475-
} // end of l_errorhandler
7476-
7477-
*/
7478-
74797466

7480-
// int win_io_popen (lua_State *L);
7481-
// int win_io_pclose (lua_State *L);
7467+
// Note stack contains: 1: "mushclient.document" (DOCUMENT_STATE)
7468+
// 2: pointer to document (CMUSHclientDoc)
74827469

74837470
int RegisterLuaRoutines (lua_State *L)
74847471
{
74857472

7486-
lua_newtable (L); // environment
7487-
lua_replace (L, LUA_ENVIRONINDEX);
7473+
lua_newtable (L); // make a new table
7474+
lua_replace (L, LUA_ENVIRONINDEX); // global environment is now this empty table
74887475

7476+
// this line is doing: global_environment ["mushclient.document"] = world_pointer (CMUSHclientDoc)
74897477
lua_settable(L, LUA_ENVIRONINDEX);
74907478

7491-
// we want the global table so we can put a metatable on it
7492-
// lua_getglobal (L, "_G");
7479+
// we want the global table so we can put a metatable on it (ie. _G)
7480+
lua_pushvalue (L, LUA_GLOBALSINDEX); // for setting the metatable later
74937481

7494-
lua_pushvalue (L, LUA_GLOBALSINDEX);
7482+
// add the __index metamethod to the world library
7483+
lua_newtable (L); // make a new table (the metatable)
74957484

7496-
// load the "world" library
7485+
// register the "world" library - leaves world library on stack
74977486
luaL_register (L, WORLD_LIBRARY, worldlib);
74987487

7499-
// add the __index metamethod to the world library
7500-
lua_pushliteral(L, "__index");
7501-
lua_pushvalue(L, -2); // push metatable
7502-
lua_rawset(L, -3); // metatable.__index = metatable
7488+
// make our new metatable.__index entry point to the world table
7489+
lua_setfield (L, -2, "__index"); // metatable __index function
75037490

75047491
// we do this so you can just say "Note 'hello'" rather than
75057492
// world.Note 'hello'
75067493

7507-
// set the world library as the metatable for the global library
7494+
// set the new table as the metatable for the global library
75087495
lua_setmetatable (L, -2);
75097496

7510-
7511-
// print function for debugging
7512-
lua_pushcfunction(L, l_print);
7497+
// our print function
7498+
lua_pushcfunction (L, l_print);
75137499
lua_setglobal(L, "print");
7514-
75157500

7516-
/*
7517-
7518-
// don't know why I had this ... debugging maybe?
7519-
7520-
// error handler
7521-
lua_pushcfunction(L, l_errorhandler);
7522-
lua_setglobal(L, "errorhandler");
7523-
7524-
*/
7525-
7526-
// see manual, 28.2, to see why we do this
7501+
// see manual, 28.2, to see why we do this bit:
75277502

75287503
// we make a metatable for the world in case they do a world.GetWorld ("name")
75297504

7530-
luaL_newmetatable(L, mushclient_typename);
7531-
luaL_register (L, NULL, worldlib_meta);
7505+
luaL_newmetatable (L, mushclient_typename); // ie. "mushclient.world"
7506+
luaL_register (L, NULL, worldlib_meta); // gives us a __tostring function
75327507

7533-
lua_pushliteral(L, "__index");
7534-
lua_getglobal (L, WORLD_LIBRARY);
7535-
lua_rawset(L, -3); // metatable.__index = world
7508+
// Note: this index is not for the main world document, but for getting a metadata
7509+
// world. That is something like: w = GetWorld ("myworld"); w:Note ("hello")
7510+
// -- in this case the __index entry makes Note available inside w
7511+
lua_getglobal (L, WORLD_LIBRARY); // ie the "world" table
7512+
lua_setfield (L, -2, "__index"); // metatable __index function
75367513

75377514
lua_settop(L, 0); // pop everything
75387515

@@ -7721,12 +7698,12 @@ static int our_lua_debug_function (lua_State *L)
77217698
return 0;
77227699
} // end of our_lua_debug_function
77237700

7724-
int our_exit_function (lua_State *L)
7701+
static int our_exit_function (lua_State *L)
77257702
{
77267703
return luaL_error(L, LUA_QL("os.exit") " not implemented in MUSHclient");
77277704
} // end of our_exit_function
77287705

7729-
int our_popen_function (lua_State *L)
7706+
static int our_popen_function (lua_State *L)
77307707
{
77317708
return luaL_error(L, LUA_QL("io.popen") " not implemented in MUSHclient");
77327709
} // end of our_popen_function
@@ -7736,17 +7713,15 @@ int DisableDLLs (lua_State * L)
77367713
if (!App.m_bEnablePackageLibrary)
77377714
{
77387715
// grab package library
7739-
lua_pushliteral (L, LUA_LOADLIBNAME); // "package"
7740-
lua_rawget (L, LUA_GLOBALSINDEX); // get package library
7716+
lua_getglobal (L, LUA_LOADLIBNAME); // package table
77417717

77427718
// remove package.loadlib
7743-
lua_pushliteral (L, "loadlib"); // package.loadlib
77447719
lua_pushnil (L);
7745-
lua_rawset (L, -3); // package.loadlib = nil
7720+
lua_setfield (L, -2, "loadlib"); // package.loadlib = nil
7721+
77467722

77477723
// grab package.loaders table
7748-
lua_pushliteral (L, "loaders"); // package.loaders
7749-
lua_rawget (L, -2); // get package.loaders table
7724+
lua_getfield (L, -1, "loaders"); // get package.loaders table
77507725
if (!lua_istable(L, -1))
77517726
luaL_error(L, LUA_QL("package.loaders") " must be a table");
77527727

@@ -7764,46 +7739,36 @@ int DisableDLLs (lua_State * L)
77647739

77657740
// change debug.debug to ours
77667741

7767-
lua_pushstring(L, LUA_DBLIBNAME); /* "debug" */
7768-
lua_rawget (L, LUA_GLOBALSINDEX); // get debug library
7742+
lua_getglobal (L, LUA_DBLIBNAME); // package table
77697743

77707744
if (lua_istable(L, -1))
77717745
{
7772-
lua_pushstring(L, "debug"); /* debug.debug */
77737746
lua_pushcfunction(L, our_lua_debug_function);
7774-
lua_rawset (L, -3); // set debug.debug to our function
7775-
7776-
lua_pop(L, 1); // pop debug table
7747+
lua_setfield (L, -2, "debug"); // set debug.debug to our function
77777748
} // debug library was there
7749+
lua_pop(L, 1); // pop debug table or whatever
77787750

77797751
// get rid of os.exit
77807752

7781-
lua_pushstring(L, LUA_OSLIBNAME); /* "os" */
7782-
lua_rawget (L, LUA_GLOBALSINDEX); // get os library
7753+
lua_getglobal (L, LUA_OSLIBNAME); // os table
77837754

77847755
if (lua_istable(L, -1))
77857756
{
7786-
lua_pushstring(L, "exit"); /* os.exit */
77877757
lua_pushcfunction(L, our_exit_function);
7788-
lua_rawset (L, -3); // set os.exit to our function
7789-
7790-
lua_pop(L, 1); // pop os table
7791-
7758+
lua_setfield (L, -2, "exit"); // set os.exit to our function
77927759
} // os library was there
7760+
lua_pop(L, 1); // pop os table or whatever
77937761

77947762
// get rid of io.popen
77957763

7796-
lua_pushstring(L, LUA_IOLIBNAME); /* "io" */
7797-
lua_rawget (L, LUA_GLOBALSINDEX); // get io library
7764+
lua_getglobal (L, LUA_IOLIBNAME); // io table
77987765

77997766
if (lua_istable(L, -1))
78007767
{
7801-
lua_pushstring(L, "popen"); /* io.popen */
78027768
lua_pushcfunction(L, our_popen_function);
7803-
lua_rawset (L, -3); // set io.popen to our function
7804-
7805-
lua_pop(L, 1); // pop io table
7769+
lua_setfield (L, -2, "popen"); // set io.poen to our function
78067770
} // io library was there
7771+
lua_pop(L, 1); // pop io table or whatever
78077772

78087773
lua_settop(L, 0); // clear stack
78097774
return 0;

0 commit comments

Comments
 (0)