@@ -56,6 +56,7 @@ LUALIB_API void *isudata (lua_State *L, int ud, const char *tname) {
56
56
// checks user data is mushclient.doc, and returns pointer to world
57
57
static CMUSHclientDoc *doc (lua_State *L)
58
58
{
59
+ // mushclient_typename is "mushclient.world"
59
60
CMUSHclientDoc **ud = (CMUSHclientDoc **) isudata (L, 1 , mushclient_typename);
60
61
61
62
// if first argument is a world userdatum, take that as our world
@@ -79,15 +80,12 @@ CMUSHclientDoc **ud = (CMUSHclientDoc **) isudata (L, 1, mushclient_typename);
79
80
luaL_error (L, " world is no longer available" );
80
81
}
81
82
82
- // retrieve our state
83
- CMUSHclientDoc * pDoc;
83
+ // retrieve our state (world pointer)
84
84
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
91
89
92
90
return pDoc;
93
91
}
@@ -7465,74 +7463,53 @@ static flags_pair miniwindow_flags [] =
7465
7463
7466
7464
extern const struct luaL_reg *ptr_xmllib;
7467
7465
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
-
7479
7466
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)
7482
7469
7483
7470
int RegisterLuaRoutines (lua_State *L)
7484
7471
{
7485
7472
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
7488
7475
7476
+ // this line is doing: global_environment ["mushclient.document"] = world_pointer (CMUSHclientDoc)
7489
7477
lua_settable (L, LUA_ENVIRONINDEX);
7490
7478
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
7493
7481
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)
7495
7484
7496
- // load the "world" library
7485
+ // register the "world" library - leaves world library on stack
7497
7486
luaL_register (L, WORLD_LIBRARY, worldlib);
7498
7487
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
7503
7490
7504
7491
// we do this so you can just say "Note 'hello'" rather than
7505
7492
// world.Note 'hello'
7506
7493
7507
- // set the world library as the metatable for the global library
7494
+ // set the new table as the metatable for the global library
7508
7495
lua_setmetatable (L, -2 );
7509
7496
7510
-
7511
- // print function for debugging
7512
- lua_pushcfunction (L, l_print);
7497
+ // our print function
7498
+ lua_pushcfunction (L, l_print);
7513
7499
lua_setglobal (L, " print" );
7514
-
7515
7500
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:
7527
7502
7528
7503
// we make a metatable for the world in case they do a world.GetWorld ("name")
7529
7504
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
7532
7507
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
7536
7513
7537
7514
lua_settop (L, 0 ); // pop everything
7538
7515
@@ -7721,12 +7698,12 @@ static int our_lua_debug_function (lua_State *L)
7721
7698
return 0 ;
7722
7699
} // end of our_lua_debug_function
7723
7700
7724
- int our_exit_function (lua_State *L)
7701
+ static int our_exit_function (lua_State *L)
7725
7702
{
7726
7703
return luaL_error (L, LUA_QL (" os.exit" ) " not implemented in MUSHclient" );
7727
7704
} // end of our_exit_function
7728
7705
7729
- int our_popen_function (lua_State *L)
7706
+ static int our_popen_function (lua_State *L)
7730
7707
{
7731
7708
return luaL_error (L, LUA_QL (" io.popen" ) " not implemented in MUSHclient" );
7732
7709
} // end of our_popen_function
@@ -7736,17 +7713,15 @@ int DisableDLLs (lua_State * L)
7736
7713
if (!App.m_bEnablePackageLibrary )
7737
7714
{
7738
7715
// 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
7741
7717
7742
7718
// remove package.loadlib
7743
- lua_pushliteral (L, " loadlib" ); // package.loadlib
7744
7719
lua_pushnil (L);
7745
- lua_rawset (L, -3 ); // package.loadlib = nil
7720
+ lua_setfield (L, -2 , " loadlib" ); // package.loadlib = nil
7721
+
7746
7722
7747
7723
// 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
7750
7725
if (!lua_istable (L, -1 ))
7751
7726
luaL_error (L, LUA_QL (" package.loaders" ) " must be a table" );
7752
7727
@@ -7764,46 +7739,36 @@ int DisableDLLs (lua_State * L)
7764
7739
7765
7740
// change debug.debug to ours
7766
7741
7767
- lua_pushstring (L, LUA_DBLIBNAME); /* "debug" */
7768
- lua_rawget (L, LUA_GLOBALSINDEX); // get debug library
7742
+ lua_getglobal (L, LUA_DBLIBNAME); // package table
7769
7743
7770
7744
if (lua_istable (L, -1 ))
7771
7745
{
7772
- lua_pushstring (L, " debug" ); /* debug.debug */
7773
7746
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
7777
7748
} // debug library was there
7749
+ lua_pop (L, 1 ); // pop debug table or whatever
7778
7750
7779
7751
// get rid of os.exit
7780
7752
7781
- lua_pushstring (L, LUA_OSLIBNAME); /* "os" */
7782
- lua_rawget (L, LUA_GLOBALSINDEX); // get os library
7753
+ lua_getglobal (L, LUA_OSLIBNAME); // os table
7783
7754
7784
7755
if (lua_istable (L, -1 ))
7785
7756
{
7786
- lua_pushstring (L, " exit" ); /* os.exit */
7787
7757
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
7792
7759
} // os library was there
7760
+ lua_pop (L, 1 ); // pop os table or whatever
7793
7761
7794
7762
// get rid of io.popen
7795
7763
7796
- lua_pushstring (L, LUA_IOLIBNAME); /* "io" */
7797
- lua_rawget (L, LUA_GLOBALSINDEX); // get io library
7764
+ lua_getglobal (L, LUA_IOLIBNAME); // io table
7798
7765
7799
7766
if (lua_istable (L, -1 ))
7800
7767
{
7801
- lua_pushstring (L, " popen" ); /* io.popen */
7802
7768
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
7806
7770
} // io library was there
7771
+ lua_pop (L, 1 ); // pop io table or whatever
7807
7772
7808
7773
lua_settop (L, 0 ); // clear stack
7809
7774
return 0 ;
0 commit comments