00001 ///////////////////////////////////////////////////////////////////////////// 00002 // Name: wxlstate.h 00003 // Purpose: wxLuaState - a wxWidgets interface to Lua 00004 // Author: Ray Gilbert, John Labenski, J Winwood 00005 // Created: 14/11/2001 00006 // Copyright: (c) 2001-2002 Lomtick Software. All rights reserved. 00007 // Licence: wxWidgets licence 00008 ///////////////////////////////////////////////////////////////////////////// 00009 00010 #ifndef _WXLSTATE_H_ 00011 #define _WXLSTATE_H_ 00012 00013 #include "wxlua/include/wxldefs.h" 00014 #include "wxlua/include/wxlbind.h" 00015 #include "wx/filefn.h" 00016 #include "wx/filename.h" 00017 #include "wx/hashmap.h" 00018 00019 class WXDLLIMPEXP_WXLUA wxLuaEvent; 00020 class WXDLLIMPEXP_WXLUA wxLuaState; 00021 class WXDLLIMPEXP_WXLUA wxLuaStateData; 00022 class WXDLLIMPEXP_WXLUA wxLuaStateRefData; 00023 class WXDLLIMPEXP_WXLUA wxLuaEventCallback; 00024 class WXDLLIMPEXP_WXLUA wxLuaWinDestroyCallback; 00025 00026 // ---------------------------------------------------------------------------- 00027 // String functions - convert between Lua (ansi string) and wxString (encoded) 00028 // ---------------------------------------------------------------------------- 00029 00030 #define WXLUA_USE_WXSTR_CONVCURRENT 1 00031 00032 // Convert a 8-bit ANSI C Lua String into a wxString 00033 inline WXDLLIMPEXP_WXLUA wxString lua2wx(const char* luastr) 00034 { 00035 #if WXLUA_USE_WXSTR_CONVCURRENT 00036 00037 if (luastr == NULL) return wxEmptyString; // check for NULL 00038 00039 return wxString(luastr, *wxConvCurrent); 00040 00041 #else //!WXLUA_USE_WXSTR_CONVCURRENT 00042 #if wxUSE_UNICODE 00043 wxString str(luastr, wxConvUTF8); 00044 #else 00045 wxString str(wxConvUTF8.cMB2WC(luastr), *wxConvCurrent); 00046 #endif // wxUSE_UNICODE 00047 00048 if (str.IsEmpty()) 00049 str = wxConvertMB2WX(luastr); // old way that mostly works 00050 00051 return str; 00052 #endif //WXLUA_USE_WXSTR_CONVCURRENT 00053 } 00054 00055 // Convert a wxString to 8-bit ANSI C Lua String 00056 inline const WXDLLIMPEXP_WXLUA wxCharBuffer wx2lua(const wxString& wxstr) 00057 { 00058 #if WXLUA_USE_WXSTR_CONVCURRENT 00059 wxCharBuffer buffer(wxstr.mb_str(*wxConvCurrent)); 00060 return buffer; 00061 00062 #else //!WXLUA_USE_WXSTR_CONVCURRENT 00063 wxCharBuffer buffer(wxConvUTF8.cWC2MB(wxstr.wc_str(*wxConvCurrent))); // skieu 00064 00065 if ((buffer.data() == NULL) && !wxstr.IsEmpty()) 00066 buffer = wxConvertWX2MB(wxstr.c_str()); // old way that mostly works 00067 00068 return buffer; 00069 #endif //WXLUA_USE_WXSTR_CONVCURRENT 00070 } 00071 00072 00073 // Convert a wxString to 8-bit ANSI C Lua Buffer and store it 00074 class WXDLLIMPEXP_WXLUA wxLuaCharBuffer 00075 { 00076 public: 00077 wxLuaCharBuffer(const wxString &wxstr) : m_buffer(wx2lua(wxstr)) {} 00078 00079 size_t Length() const { return strlen((const char*)m_buffer); } 00080 const char *GetData() const { return (const char*)m_buffer; } 00081 00082 operator const char *() const { return m_buffer; } 00083 00084 wxCharBuffer m_buffer; // member since non virtual destructor in wxCharBuffer 00085 }; 00086 00087 // ---------------------------------------------------------------------------- 00088 // Special keys used by wxLua in the LUA_REGISTRYINDEX table. 00089 // 00090 // Note: We do not push a human readable string for these because Lua always 00091 // makes a copy and hashes the string, this takes a considerable amount of 00092 // time when benchmarked using valgrind. 00093 // ---------------------------------------------------------------------------- 00094 00095 // Light userdata used as keys in the Lua LUA_REGISTRYINDEX table for wxLua. 00096 // Note that even though these keys have human readable names as values, 00097 // they're not used, just the memory address. 00098 00099 // The key in the LUA_REGISTRYINDEX table that is a numerically keyed table indexed 00100 // on the wxLua types where each item is a userdata metatable for a C++ class. 00101 // Note: The wxLua types WXLUA_TXXX that correspond to the Lua LUA_TXXX types 00102 // are not stored in this table since they do not use our metatables. 00103 // The keys in this table are all > 1. They values are either tables or 0 00104 // if the wxLuaBinding containing the wxLua type was not registered. 00105 // LUA_REGISTRYINDEX[&wxlua_lreg_types_key][wxLua type number] = { metatable for a C++ class } 00106 extern WXDLLIMPEXP_DATA_WXLUA(const char*) wxlua_lreg_types_key; 00107 // The key in the LUA_REGISTRYINDEX table that is a numerically keyed table 00108 // with references to Lua objects we want to keep a handle to. The object could be 00109 // anything, a table, function, number, string, userdata... 00110 // LUA_REGISTRYINDEX[&wxlua_lreg_refs_key][ref number] = Lua object 00111 extern WXDLLIMPEXP_DATA_WXLUA(const char*) wxlua_lreg_refs_key; 00112 // The key in the LUA_REGISTRYINDEX table that is a numerically keyed table 00113 // with references to objects the wxLuaDebugData wants to keep a handle to by 00114 // storing their value for lookup. It is used only for the wxLuaDebugData. 00115 // LUA_REGISTRYINDEX[&wxlua_lreg_debug_refs_key][ref number] = Lua object 00116 extern WXDLLIMPEXP_DATA_WXLUA(const char*) wxlua_lreg_debug_refs_key; 00117 // The key that in the LUA_REGISTRYINDEX table that is a lookup table of string 00118 // C++ classname keys and lightuserdata pointers to the associated wxLuaBindClass struct. 00119 // LUA_REGISTRYINDEX[&wxlua_lreg_debug_refs_key][wxLuaBindClass.name] = lightuserdata(&wxLuaBindClass) 00120 extern WXDLLIMPEXP_DATA_WXLUA(const char*) wxlua_lreg_classes_key; 00121 // The key in the LUA_REGISTRYINDEX table that is a table 00122 // of Lua objects/functions assigned to wxLua userdata programatically in Lua. 00123 // LUA_REGISTRYINDEX[&wxlua_lreg_derivedmethods_key][lightuserdata(obj_ptr)] = 00124 // {["derived func/value name"] = wxLuaObject(Lua function/value), ...} 00125 extern WXDLLIMPEXP_DATA_WXLUA(const char*) wxlua_lreg_derivedmethods_key; 00126 // The key in the LUA_REGISTRYINDEX table who's value is a lightuserdata 00127 // of the wxLuaState for this lua_State. 00128 // LUA_REGISTRYINDEX[&wxlua_lreg_wxluastate_key] = lightuserdata(&wxLuaState) 00129 extern WXDLLIMPEXP_DATA_WXLUA(const char*) wxlua_lreg_wxluastate_key; 00130 // The key in the LUA_REGISTRYINDEX table that is a table of lightuserdata 00131 // wxLuaBindings and the ref to the Lua table they were installed into. 00132 // LUA_REGISTRYINDEX[&wxlua_lreg_wxluabindings_key] = {lightuserdata(&wxLuaBinding) = wxlua_lreg_refs_key ref#, ...} 00133 extern WXDLLIMPEXP_DATA_WXLUA(const char*) wxlua_lreg_wxluabindings_key; 00134 // The key in the LUA_REGISTRYINDEX table that is a table of all 00135 // objects that we've pushed into Lua using wxluaT_pushuserdatatype(). 00136 // Note: A single object like a wxWindow may be pushed with multiple wxLua types. 00137 // e.g. wxWindow* w = wx.wxWindow() retrieve the window later from wxObject* wxEvent:GetEventObject() 00138 // LUA_REGISTRYINDEX[&wxlua_lreg_weakobjects_key][lightuserdata(obj_ptr)] = 00139 // { wxLua type1 = weak fulluserdata, wxLua type2 = weak fulluserdata... } 00140 extern WXDLLIMPEXP_DATA_WXLUA(const char*) wxlua_lreg_weakobjects_key; 00141 // The key in the LUA_REGISTRYINDEX table that is a table of all 00142 // objects to delete that were added using wxluaO_addgcobject(). 00143 // Note that non wxObject classes use wxLUA_DECLARE_ENCAPSULATION so 00144 // the key is the object pointer and the value is the wxObject encapsulation. 00145 // Both the key and the value are the same if not encapsulated. 00146 // LUA_REGISTRYINDEX[&wxlua_lreg_gcobjects_key][lightuserdata(obj_ptr)] = 00147 // lightuserdata(wxObject derived class) 00148 extern WXDLLIMPEXP_DATA_WXLUA(const char*) wxlua_lreg_gcobjects_key; 00149 // The key in the LUA_REGISTRYINDEX table that is a table of all 00150 // wxLuaEventCallbacks that we've created. 00151 // LUA_REGISTRYINDEX[&wxlua_lreg_evtcallbacks_key][lightuserdata(&wxLuaEventCallback)] = 00152 // lightuserdata(&wxEvtHandler) 00153 extern WXDLLIMPEXP_DATA_WXLUA(const char*) wxlua_lreg_evtcallbacks_key; 00154 // The key in the LUA_REGISTRYINDEX table that is a table of wxWindow keys and 00155 // wxLuaWinDestroyCallback values that we've created. 00156 // LUA_REGISTRYINDEX[&wxlua_lreg_windestroycallbacks_key][lightuserdata(&wxWindow)] = 00157 // lightuserdata(wxLuaWinDestroyCallback) 00158 extern WXDLLIMPEXP_DATA_WXLUA(const char*) wxlua_lreg_windestroycallbacks_key; 00159 // The key in the LUA_REGISTRYINDEX table that is a table of all 00160 // top level wxWindows that we've created and need to destroy when closed. 00161 // LUA_REGISTRYINDEX[&wxlua_lreg_topwindows_key][lightuserdata(&wxWindow)] = 1 00162 extern WXDLLIMPEXP_DATA_WXLUA(const char*) wxlua_lreg_topwindows_key; 00163 // The key in the LUA_REGISTRYINDEX table that has a boolean value 00164 // of whether the Lua code has prepended a '_' to function name to indicate 00165 // that they want the base class function called. 00166 // LUA_REGISTRYINDEX[&wxlua_lreg_callbaseclassfunc_key] = true/false 00167 extern WXDLLIMPEXP_DATA_WXLUA(const char*) wxlua_lreg_callbaseclassfunc_key; 00168 // The key in the LUA_REGISTRYINDEX table that has a wxEventType (integer) value 00169 // of the current wxEvent is that is being run or wxEVT_NULL if not in an event. 00170 // LUA_REGISTRYINDEX[&wxlua_lreg_wxeventtype_key] = wxEventType (wxEVT_NULL) 00171 extern WXDLLIMPEXP_DATA_WXLUA(const char*) wxlua_lreg_wxeventtype_key; 00172 // The key in the LUA_REGISTRYINDEX table that has a wxLuaStateData class 00173 // lightuserdata value for the wxLuaState. 00174 // LUA_REGISTRYINDEX[&wxlua_lreg_wxluastatedata_key] = lightuserdata(&wxLuaStateData) 00175 extern WXDLLIMPEXP_DATA_WXLUA(const char*) wxlua_lreg_wxluastatedata_key; 00176 // The key in the LUA_REGISTRYINDEX table that is a weak keyed table of 00177 // the tables wxLua pushed into the registry with their keys as values. 00178 // This is used by the wxLuaDebugData to know if the table is one of the wxLua 00179 // registry tables for better wxLuaStackDialog performance. 00180 // LUA_REGISTRYINDEX[&wxlua_lreg_regtable_key][weak {wxlua_lreg_XXX_key table}] = 00181 // lightuserdata(&wxlua_lreg_XXX_key) 00182 extern WXDLLIMPEXP_DATA_WXLUA(const char*) wxlua_lreg_regtable_key; 00183 00184 // Light userdata used as keys in the metatables created for the class userdata objects. 00185 // Note that even though these keys have values, they're not used, just the memory address. 00186 00187 // wxLua userdata metatable structure: 00188 // { 00189 // lightuserdata(&wxlua_metatable_type_key) = wxLua type number in wxlua_lreg_types_key table 00190 // lightuserdata(&wxlua_metatable_wxluabindclass_key) = lightuserdata(&wxLuaBindClass) 00191 // __gc = function(wxlua_wxLuaBindClass__gc) 00192 // __index = function(wxlua_wxLuaBindClass__index) 00193 // __newindex = function(wxlua_wxLuaBindClass__newindex) 00194 // __tostring = function(wxlua_wxLuaBindClass__tostring) 00195 // } 00196 00197 // The key of a metatable used for wxLua userdata that is the wxLua type number in the 00198 // wxlua_lreg_types_key table this metatable is for. 00199 extern WXDLLIMPEXP_DATA_WXLUA(const char*) wxlua_metatable_type_key; 00200 // The key of a metatable used for wxLua userdata that stores a lightuserdata 00201 // of the wxLuaBindClass struct for this class. 00202 extern WXDLLIMPEXP_DATA_WXLUA(const char*) wxlua_metatable_wxluabindclass_key; 00203 00204 // ---------------------------------------------------------------------------- 00205 // Create one of the wxlua_lreg_XXX_key tables in the LUA_REGISTRYINDEX and 00206 // properly set the wxlua_lreg_regtablekey_key too. 00207 WXDLLIMPEXP_WXLUA void wxlua_lreg_createtable(lua_State* L, void* lightuserdata_reg_key, int narr = 0, int nrec = 0); 00208 00209 // ---------------------------------------------------------------------------- 00210 // The functions below are Lua C helper functions, some are also part of the wxLuaState 00211 // and you are recommended to use those if the wxLuaState is required. However 00212 // in some cases it may not be necessary to create a wxLuaState and just 00213 // calling these functions will suffice. Only the functions that do not 00214 // require the internal data from the wxLuaState are separated here. 00215 // ---------------------------------------------------------------------------- 00216 00217 // Translate the LUA_ERRXXX integers into a human readable string. 00218 // returns an empty string for an input of 0. 00219 WXDLLIMPEXP_WXLUA wxString wxlua_LUA_ERR_msg(int LUA_ERRx); 00220 00221 // Get information from the return value of lua_pcall(), luaL_loadbuffer(), etc 00222 // The errMsg input is filled with wxlua_LUA_ERR_msg() and if the current top 00223 // is > than top it tries to get Lua's error string from the top of the stack. 00224 // Returns true if the input status != 0 and the errMsg and line_num are filled. 00225 // If errMsg and line_num aren't NULL then fill them with the msg and line. 00226 // status is the return from lua_pcall(), luaL_loadbuffer(), etc, LUA_ERRxxx 00227 // top is the lua_gettop from before the call that may have generated the error. 00228 WXDLLIMPEXP_WXLUA bool wxlua_errorinfo(lua_State* L, int status, int top, wxString* errMsg = NULL, int* line_num = NULL); 00229 00230 00231 // Push the errorMsg on the stack and call luaL_error() 00232 WXDLLIMPEXP_WXLUA void LUACALL wxlua_error(lua_State* L, const char* errorMsg); 00233 wxLUA_UNICODE_ONLY(WXDLLIMPEXP_WXLUA inline void LUACALL wxlua_error(lua_State* L, const wxString& errorMsg) { wxlua_error(L, wx2lua(errorMsg)); }) 00234 00235 // Create an error message that the item at the stack_idx is not correct for a 00236 // function call and call wxlua_argerrormsg(). 00237 // The expectedType string should tell the user what is valid input and is a 00238 // string to be flexible for multiple valid types. 00239 // The error message format is: 00240 // "wxLua: Expected %s for parameter %d, but got a '%s'.", expectedType.c_str(), stack_idx, argType.c_str() 00241 // Typical expectedType strings would be wxT("a 'number'") 00242 WXDLLIMPEXP_WXLUA void LUACALL wxlua_argerror(lua_State *L, int stack_idx, const wxString& expectedType); 00243 // Create an error message for an incorrect function call and call wxlua_error(). 00244 // The message created has this format: 00245 // msg 00246 // "functionNameCalled(argName1, argName2, ...)" <-- from wxlua_getLuaArgsMsg() 00247 // "01. functionName(validArgName1, validArgName2, ...)" <-- from wxlua_getBindMethodArgsMsg() 00248 // "02. ..." 00249 WXDLLIMPEXP_WXLUA void LUACALL wxlua_argerrormsg(lua_State *L, const wxString& msg); 00250 00251 // Get the userdata at the stack index, if null_ptr then set the pointer wrapped 00252 // by Lua's userdata to NULL to clear it. 00253 WXDLLIMPEXP_WXLUA void* LUACALL wxlua_touserdata(lua_State* L, int stack_idx, bool null_ptr = false); 00254 00255 //---------------------------------------------------------------------------- 00256 // wxluaR_XXX - functions operate on the tables in Lua's LUA_REGISTRYINDEX which 00257 // are keyed on lightuserdata that use the luaL_ref() integer reference mechanism 00258 // to store objects. The 'R' stands for Registry or Reference. 00259 // 00260 // Possible values for the "void* lightuserdata_reg_key" are 00261 // &wxlua_lreg_types_key, &wxlua_lreg_refs_key, &wxlua_lreg_debug_refs_key 00262 // unless you are using these functions for your own table in the LUA_REGISTRYINDEX. 00263 //---------------------------------------------------------------------------- 00264 00265 // Create a reference to the object at stack index in a table with the key 00266 // lightuserdata_reg_key in the LUA_REGISTRYINDEX table. Does not pop the object. 00267 // Returns the table index or LUA_REFNIL if the item on the stack is none or nil (an error). 00268 WXDLLIMPEXP_WXLUA int LUACALL wxluaR_ref(lua_State* L, int stack_idx, void* lightuserdata_reg_key); 00269 // Remove a reference to the object at the index in a table with the key 00270 // lightuserdata_reg_key in the LUA_REGISTRYINDEX table, returns success. 00271 WXDLLIMPEXP_WXLUA bool LUACALL wxluaR_unref(lua_State* L, int wxlref_idx, void* lightuserdata_reg_key); 00272 // Push onto the top of the stack the object at the index in a table with the key 00273 // lightuserdata_reg_key in the LUA_REGISTRYINDEX table, if the index is LUA_REFNIL or the 00274 // value is nil it returns false and doesn't leave anything on the stack. 00275 WXDLLIMPEXP_WXLUA bool LUACALL wxluaR_getref(lua_State* L, int wxlref_idx, void* lightuserdata_reg_key); 00276 // Is the item at the stack_idx in the table with the key lightuserdata_reg_key 00277 // in the LUA_REGISTRYINDEX table. Returns the ref index or LUA_NOREF if it's not. 00278 WXDLLIMPEXP_WXLUA int LUACALL wxluaR_isrefed(lua_State* L, int stack_idx, void* lightuserdata_reg_key); 00279 00280 //---------------------------------------------------------------------------- 00281 // wxluaO_XXX - functions operate on wxLua "Objects" which are userdata wrapping 00282 // C++ class objects and are stored in the wxlua_lreg_weakobjects_key 00283 // and the wxlua_lreg_gcobjects_key table in the LUA_REGISTRYINDEX. 00284 //---------------------------------------------------------------------------- 00285 00286 enum wxLuaGCObject_Flags 00287 { 00288 WXLUA_DELETE_OBJECT_LAST = 0x0000, // Delete the object only if this is the 00289 // last userdata referece to it. 00290 00291 WXLUA_DELETE_OBJECT_ALL = 0x0001, // Delete the object and clear all 00292 // userdata references to it. 00293 }; 00294 00295 // Track this wxObject and delete it when Lua calls the __gc method for it. 00296 // The object is stored in the wxlua_lreg_gcobjects_key of the LUA_REGISTRYINDEX. 00297 // The second version is used when a non-wxObject class is encapsulated and 00298 // the obj_ptr points to the actual object that the wxObject encapsulates. 00299 // Note that the lua userdata internal pointer is the obj_ptr and the 00300 // wxobj is *only* stored in the wxlua_lreg_gcobjects_key. 00301 WXDLLIMPEXP_WXLUA void LUACALL wxluaO_addgcobject(lua_State* L, wxObject* wxobj); 00302 WXDLLIMPEXP_WXLUA void LUACALL wxluaO_addgcobject(lua_State* L, void* obj_ptr, wxObject* wxobj); 00303 // Remove this obj_ptr wrapped in a Lua userdata, udata, from the 00304 // wxlua_lreg_gcobjects_key table of the LUA_REGISTRYINDEX. 00305 // It is deleted depending on the flags enum wxLuaGCObject_Flags. 00306 // If flags = WXLUA_DELETE_OBJECT_ALL or if this is the last userdata it will also remove all 00307 // wxlua_lreg_weakobjects_key and wxlua_lreg_derivedmethods_key since the object is gone. 00308 WXDLLIMPEXP_WXLUA bool LUACALL wxluaO_deletegcobject(lua_State *L, void* udata, void *obj_ptr, int flags); 00309 // Remove this obj_ptr from the wxlua_lreg_gcobjects_key table of the 00310 // LUA_REGISTRYINDEX. The Lua userdata for the object stays in Lua and it's 00311 // assumed that someone else will delete the object (took ownership of it). 00312 WXDLLIMPEXP_WXLUA bool LUACALL wxluaO_undeletegcobject(lua_State *L, void *obj_ptr); 00313 // Check if this obj_ptr is in the wxlua_lreg_gcobjects_key table of the 00314 // LUA_REGISTRYINDEX. 00315 WXDLLIMPEXP_WXLUA bool LUACALL wxluaO_isgcobject(lua_State *L, void *obj_ptr); 00316 // Get a wxArrayString of the info in the wxlua_lreg_gcobjects_key LUA_REGISTRYINDEX table. 00317 // Strings are of the form "ClassName(&obj)" 00318 WXDLLIMPEXP_WXLUA wxArrayString LUACALL wxluaO_getgcobjectinfo(lua_State *L); 00319 00320 // Track the obj_ptr and its Lua userdata at udata_stack_idx which is of the 00321 // wxLua type in the wxlua_lreg_weakobjects_key table of the 00322 // LUA_REGISTRYINDEX so we can push it again if needed. 00323 WXDLLIMPEXP_WXLUA void LUACALL wxluaO_trackweakobject(lua_State *L, int udata_stack_idx, void *obj_ptr, int wxl_type); 00324 // Remove the obj_ptr key from the wxlua_lreg_weakobjects_key table of 00325 // the LUA_REGISTRYINDEX. It removes the metatable for the single Lua userdata, 00326 // "udata", since this function is called before the object is deleted. 00327 // e.g. p1 = wx.wxPoint(); p2 = p1; p2:delete(); p1:SetX(5) errors, but doesn't segfault. 00328 // If udata == NULL it removes ALL tracked userdata for this obj_ptr and clears 00329 // all of their metatables. 00330 WXDLLIMPEXP_WXLUA int LUACALL wxluaO_untrackweakobject(lua_State *L, void* udata, void *obj_ptr); 00331 // Check if this object with the given wxLua type is in the wxlua_lreg_weakobjects_key 00332 // table of the LUA_REGISTRYINDEX. 00333 // If the object is found with the right wxLua type and push_on_stack is true 00334 // the Lua userdata for the object is pushed on top of the stack. If it's not 00335 // found then it returns false and nothing is left on the stack. 00336 WXDLLIMPEXP_WXLUA bool LUACALL wxluaO_istrackedweakobject(lua_State *L, void *obj_ptr, int wxl_type, bool push_on_stack); 00337 // Get a wxArrayString of the info in the wxlua_lreg_weakobjects_key LUA_REGISTRYINDEX table. 00338 // Strings are of the form "&obj_ptr = wxLuaTypeName1(&udata, type=wxLuaType), ..." 00339 // If the object is casted to multiple types there will be wxLuaTypeName2(...) and so on. 00340 WXDLLIMPEXP_WXLUA wxArrayString LUACALL wxluaO_gettrackedweakobjectinfo(lua_State *L); 00341 00342 //---------------------------------------------------------------------------- 00343 // wxluaW_XXX - functions operate on tracked wxWindows stored in the 00344 // wxlua_lreg_topwindows_key in Lua's LUA_REGISTRYINDEX. 00345 //---------------------------------------------------------------------------- 00346 00347 // Add the wxObject which is presumably a wxWindow (this function checks) 00348 // to the wxlua_lreg_topwindows_key table of the LUA_REGISTRYINDEX table if 00349 // it has not already been added. 00350 WXDLLIMPEXP_WXLUA void LUACALL wxluaW_addtrackedwindow(lua_State *L, wxObject* wxobj); 00351 // Remove the wxWindow from the wxlua_lreg_topwindows_key table of the 00352 // LUA_REGISTRYINDEX table. 00353 WXDLLIMPEXP_WXLUA void LUACALL wxluaW_removetrackedwindow(lua_State *L, wxWindow* win); 00354 // Is this wxWindow or one of its parents already added to the 00355 // wxlua_lreg_topwindows_key table of the LUA_REGISTRYINDEX table? 00356 WXDLLIMPEXP_WXLUA bool LUACALL wxluaW_istrackedwindow(lua_State *L, wxWindow* win, bool check_parents); 00357 // Get a wxArrayString of the info in the wxlua_lreg_topwindows_key LUA_REGISTRYINDEX table. 00358 // Strings are of the form "ClassName(&win id=wxWindowID)" 00359 WXDLLIMPEXP_WXLUA wxArrayString LUACALL wxluaW_gettrackedwindowinfo(lua_State *L); 00360 00361 //---------------------------------------------------------------------------- 00362 // wxluaT_XXX - functions operate on wxLua types which are integers. 00363 // wxLua types for C++ classes are positive and the userdata metatables are 00364 // stored in the wxlua_lreg_types_key table in Lua's LUA_REGISTRYINDEX. 00365 // wxLua types matching LUA_TXXX types are negative, see WXLUA_TXXX. 00366 //---------------------------------------------------------------------------- 00367 00368 // Allocate a new table (a metatable for a userdata) with a 00369 // wxlua_metatable_type_key key equal to the input wxl_type and 00370 // store it in the wxlua_lreg_types_key LUA_REGISTRYINDEX table. 00371 // Returns the index into the wxLua types table which is a new wxLua type. 00372 // Leaves the new table on the top of the stack. 00373 WXDLLIMPEXP_WXLUA int LUACALL wxluaT_newmetatable(lua_State* L, int wxl_type); 00374 // Get the metatable for the wxLua type stored in the 00375 // wxlua_lreg_types_key LUA_REGISTRYINDEX table. 00376 // Returns true if the type's metatable was found and is on the stack, nothing 00377 // is left on the stack on failure. 00378 WXDLLIMPEXP_WXLUA bool LUACALL wxluaT_getmetatable(lua_State* L, int wxl_type); 00379 // Set the metatable of the userdata at top of stack to the table stored in the 00380 // wxlua_lreg_types_key LUA_REGISTRYINDEX table. 00381 WXDLLIMPEXP_WXLUA bool LUACALL wxluaT_setmetatable(lua_State* L, int wxl_type); 00382 00383 // Get the numeric wxLua type of the item at the stack index. 00384 // This is the wxLua equivalent of lua_type() but instead of returning 00385 // LUA_TXXX it returns WXLUA_TXXX for standard types. 00386 // If the object is a userdata it checks the metatable for the 00387 // wxlua_metatable_type_key to get the wxLua type where the type is 00388 // presumedly the index into the wxlua_lreg_types_key of the LUA_REGISTRYINDEX 00389 // table and denotes what type of C++ object this is. 00390 // Returns WXLUA_TUNKNOWN on failure. 00391 WXDLLIMPEXP_WXLUA int LUACALL wxluaT_type(lua_State* L, int stack_idx); 00392 00393 // Get a human readable name for the predefined WXLUA_TXXX or binding 00394 // wxluatype_XXX wxLua types stored in the wxlua_lreg_types_key 00395 // of the LUA_REGISTRYINDEX table. 00396 // This is the wxLua equivalent of lua_typename(L, luatype). 00397 // If the lua_State is not NULL then if the type is a wxLua type for classes 00398 // return the C++ class/struct name. 00399 // Returns empty string if the type is unknown. 00400 WXDLLIMPEXP_WXLUA wxString LUACALL wxluaT_typename(lua_State* L, int wxl_type); 00401 // Get a human readable name for the item at the stack index. 00402 // This is the wxLua equivalent of luaL_typename(L, stack_idx). 00403 // This function calls wxluaT_typename(L, wxluaT_type(L, stack_idx)) and is a 00404 // convenience function. 00405 WXDLLIMPEXP_WXLUA wxString LUACALL wxluaT_gettypename(lua_State* L, int stack_idx); 00406 // Get the luaL_typename(L, stack_idx) == lua_typename(lua_type(L, stack_idx)) as a wxString. 00407 // Returns one of the LUA_TXXX values. 00408 WXDLLIMPEXP_WXLUA wxString LUACALL wxlua_luaL_typename(lua_State* L, int stack_idx); 00409 00410 // Get the wxLua type for the class or struct with the given name 00411 WXDLLIMPEXP_WXLUA int LUACALL wxluaT_gettype(lua_State* L, const char* name); 00412 // Get the wxLuaBindClass* for this wxLua type or NULL if the type is invalid. 00413 // Gets the wxLuaBindClass from the metatable stored in the wxlua_lreg_types_key registry table 00414 // for the classes that have been installed into Lua. 00415 WXDLLIMPEXP_WXLUA const wxLuaBindClass* LUACALL wxluaT_getclass(lua_State* L, int wxl_type); 00416 // Get the wxLuaBindClass* for this class_name or NULL if the name is invalid. 00417 // Gets the wxLuaBindClass from the wxlua_lreg_classes_key table in the LUA_REGISTRYINDEX. 00418 WXDLLIMPEXP_WXLUA const wxLuaBindClass* LUACALL wxluaT_getclass(lua_State* L, const char* class_name); 00419 00420 // Is the item at stack_idx of the userdata type with or derived from the the given wxLua type. 00421 WXDLLIMPEXP_WXLUA bool wxluaT_isuserdatatype(lua_State* L, int stack_idx, int wxl_type); 00422 // Get the userdata object at the stack_idx that is of the wxLua class type or a 00423 // class derived from the wxLua type. If the userdata does not have the correct type, 00424 // or if the parameter isn't a userdata then wxlua_error() is called and NULL is returned. 00425 WXDLLIMPEXP_WXLUA void* LUACALL wxluaT_getuserdatatype(lua_State* L, int stack_idx, int wxl_type); 00426 // Push the obj_ptr onto the top of the stack wrapped in a newuserdata 00427 // with its metatable set to the table from wxluaR_getref(L, wxl_type, &wxlua_lreg_types_key). 00428 // Returns true if the wxLua type is known, the metatable set, and it's on the stack, nothing 00429 // is pushed on the stack if this returns false. 00430 // If the wxLua type is derived from the wxWindow type it will be added to the 00431 // wxlua_lreg_windestroycallbacks_key table. 00432 // If track=true then push the obj_ptr as a lightuser data key into the 00433 // wxlua_lreg_weakobjects_key table of the Lua LUA_REGISTRYINDEX table so 00434 // that if we need to push it again we just push the already created full userdata value. 00435 WXDLLIMPEXP_WXLUA bool LUACALL wxluaT_pushuserdatatype(lua_State* L, const void *obj_ptr, int wxl_type, bool track = true, bool allow_NULL = false); 00436 00437 // ---------------------------------------------------------------------------- 00438 // Functions to get info about the wxLua types. 00439 // Used to determine what to expect for a function call in the bindings. 00440 // ---------------------------------------------------------------------------- 00441 00442 // Is a class with the wxl_type equal to or derived from a class with the base_wxl_type. 00443 // 0 means same class, +1 means base is parent, +2 base is grandparent, ... 00444 // returns -1 if the wxLua type is not derived from the base type. 00445 WXDLLIMPEXP_WXLUA int LUACALL wxluaT_isderivedtype(lua_State* L, int wxl_type, int base_wxl_type); 00446 // Same as above, but works directly with the wxLuaBindClasses. 00447 WXDLLIMPEXP_WXLUA int LUACALL wxluaT_isderivedclass(const wxLuaBindClass* wxlClass, const wxLuaBindClass* base_wxlClass); 00448 // Verify if the luatype = lua_type(L, stack_idx) is valid for the 00449 // wxl_type which is one of the predefined WXLUA_TXXX or s_wxluaarg_XXX types. 00450 // Returns 1 if it matches, 0 if it doesn't, -1 if the wxl_type is not known. 00451 // Note that this function does not do a direct mapping between wxlua_luatowxluatype() 00452 // and wxlua_wxluatoluatype() since it allows a small amount of coersion between types. 00453 // If the input lua_State is not NULL it will account for the automatic conversion of 00454 // (wxString, wxArrayString, wxArrayInt) from the Lua type to wxLua type. 00455 WXDLLIMPEXP_WXLUA int LUACALL wxlua_iswxluatype(int luatype, int wxl_type, lua_State* L = NULL); 00456 // Get the wxLua type for the lua_type() = LUA_TXXX, returns -1 if unknown. 00457 WXDLLIMPEXP_WXLUA int wxlua_luatowxluatype(int luatype); 00458 // Get the lua_type() = LUA_TXXX for the predefined WXLUA_TXXX types. 00459 // returns -1 (LUA_TNONE) if the type was not one of the predefined types. 00460 WXDLLIMPEXP_WXLUA int wxlua_wxluatoluatype(int wxluatype); 00461 00462 // Is the object at the stack_idx a userdata object that wxLua has pushed into Lua? 00463 // This should be the same as 00464 // (lua_isuserdata(L, stack_idx) && !lua_islightuserdata(L, stack_idx)) 00465 #define wxlua_iswxuserdata(L, stack_idx) (lua_type((L), (stack_idx)) == LUA_TUSERDATA) 00466 00467 // Helper functions to get numbers, booleans and strings safer. 00468 // These validate that the object at the stack index specified is a string, bool, 00469 // int, or double number object or that the object can be converted to it. 00470 // Note: wxLua has a stricter sense of type than Lua and we don't want to 00471 // always allow coersion between types since oftentimes there's an error. 00472 WXDLLIMPEXP_WXLUA bool wxlua_iswxstringtype(lua_State* L, int stack_idx); 00473 #define wxlua_isstringtype(L, stack_idx) (wxlua_iswxluatype(lua_type(L, stack_idx), WXLUA_TSTRING) == 1) 00474 #define wxlua_isbooleantype(L, stack_idx) (wxlua_iswxluatype(lua_type(L, stack_idx), WXLUA_TBOOLEAN) == 1) 00475 #define wxlua_isintegertype(L, stack_idx) (wxlua_iswxluatype(lua_type(L, stack_idx), WXLUA_TINTEGER) == 1) 00476 #define wxlua_isnumbertype(L, stack_idx) (wxlua_iswxluatype(lua_type(L, stack_idx), WXLUA_TNUMBER) == 1) 00477 00478 // After verifying using wxlua_isXXXtype return the value, else call 00479 // wxlua_error() with a message that is appropriate for stack_idx to be a 00480 // parameter to a function call. (These are used in the bindings) 00481 // Note: The function wxLuaState::GetwxStringType does automatic conversion 00482 // of both a Lua string and a userdata wxString to a wxString. 00483 WXDLLIMPEXP_WXLUA const char* LUACALL wxlua_getstringtype(lua_State* L, int stack_idx); 00484 WXDLLIMPEXP_WXLUA wxString LUACALL wxlua_getwxStringtype(lua_State* L, int stack_idx); 00485 WXDLLIMPEXP_WXLUA bool LUACALL wxlua_getbooleantype(lua_State* L, int stack_idx); 00486 WXDLLIMPEXP_WXLUA long LUACALL wxlua_getenumtype(lua_State* L, int stack_idx); 00487 WXDLLIMPEXP_WXLUA long LUACALL wxlua_getintegertype(lua_State* L, int stack_idx); 00488 WXDLLIMPEXP_WXLUA unsigned long LUACALL wxlua_getuintegertype(lua_State* L, int stack_idx); 00489 WXDLLIMPEXP_WXLUA double LUACALL wxlua_getnumbertype(lua_State* L, int stack_idx); 00490 00491 // Helper functions to get/set tables of strings and ints 00492 // Validate that the object at the stack index specified is a table object. 00493 // This assumes that each table array entry is a string/number 00494 // or can be converted to a string/number using the 00495 // wxlua_isstring/numbertype definitions of what is a string/number. 00496 00497 // Convert the table at stack index to a "new" array of const char* strings. 00498 // Return a pointer to the array of strings. You need to delete the array, but not 00499 // the individual strings since Lua should still have them during the life of the 00500 // returned array, if not you will need to copy them. 00501 // Returns the number of character strings in the array in count. 00502 // See usage in the wxBitmap constructor for XPMs. 00503 WXDLLIMPEXP_WXLUA const char** LUACALL wxlua_getchararray(lua_State* L, int stack_idx, int& count); 00504 00505 // Convert a table array or a wxArrayString at the stack_idx to an array of wxStrings. 00506 // If it's a table, it must have integer keys and string or wxString values. 00507 // Returns a pointer to a new array of wxStrings and set the size in count. 00508 // You must delete the return value if not NULL. 00509 WXDLLIMPEXP_WXLUA wxString* LUACALL wxlua_getwxStringarray(lua_State* L, int stack_idx, int& count); 00510 // Convert a table array or a wxArrayInt at the stack_idx to an array of integers. 00511 // If it's a table, it must have integer keys and values. 00512 // Returns a pointer to a new array of ints and set the size in count 00513 // You must delete the return value if not NULL. 00514 WXDLLIMPEXP_WXLUA int* LUACALL wxlua_getintarray(lua_State* L, int stack_idx, int& count); 00515 00516 // Convert a table array or a wxArrayString object at the stack_idx to a wxArrayString. 00517 // If it's a table, it must have integer keys and string or wxString values. 00518 WXDLLIMPEXP_WXLUA wxLuaSmartwxArrayString LUACALL wxlua_getwxArrayString(lua_State* L, int stack_idx); 00519 // Convert a table array or a wxSortedArrayString object at the stack_idx to a wxSortedArrayString. 00520 // If it's a table, it must have integer keys and string or wxString values. 00521 WXDLLIMPEXP_WXLUA wxLuaSmartwxSortedArrayString LUACALL wxlua_getwxSortedArrayString(lua_State* L, int stack_idx); 00522 // Convert a table array or a wxArrayInt object at the stack_idx to a wxArrayInt. 00523 // If it's a table, it must have integer keys and values. 00524 WXDLLIMPEXP_WXLUA wxLuaSmartwxArrayInt LUACALL wxlua_getwxArrayInt(lua_State* L, int stack_idx); 00525 00526 00527 // Creates a Lua table array and pushes Lua strings into it, returns the number of items added. 00528 // The table is left on the stack. 00529 WXDLLIMPEXP_WXLUA int LUACALL wxlua_pushwxArrayStringtable(lua_State* L, const wxArrayString& strArray); 00530 // Creates a Lua table array and pushes the integers into it, returns the number of items added. 00531 // The table is left on the stack. 00532 WXDLLIMPEXP_WXLUA int LUACALL wxlua_pushwxArrayInttable(lua_State* L, const wxArrayInt& intArray); 00533 // Push the wxString into Lua after converting it. 00534 WXDLLIMPEXP_WXLUA void LUACALL wxlua_pushwxString(lua_State* L, const wxString& str); 00535 00536 // Helper function to concatenate a wxArrayString into a wxString. 00537 WXDLLIMPEXP_WXLUA wxString wxlua_concatwxArrayString(const wxArrayString& arr, const wxString& sep = wxT("\n")); 00538 00539 00540 // Push the program args into a global table called "args" as the Lua executable does. 00541 // start_n is the arg to start pushing until max args "argc". 00542 // returns the number of args pushed. 00543 WXDLLIMPEXP_WXLUA int wxlua_pushargs(lua_State* L, wxChar **argv, int argc, int start_n); 00544 00545 //---------------------------------------------------------------------------- 00546 // Derived class member functions for classes in wxLua. The data is stored 00547 // in the wxlua_lreg_derivedmethods_key table in the LUA_REGISTRYINDEX. 00548 //---------------------------------------------------------------------------- 00549 00550 // Add this derived method, a Lua function or value the user has set to a 00551 // wxLua userdata object that we will push onto the stack when they access 00552 // the __index of the object with the "method_name". The obj_ptr is the 00553 // object the Lua userdata stores and the new wxLuaObject wraps the Lua 00554 // function or value which will be deleted by wxLua when the userdata is deleted. 00555 WXDLLIMPEXP_WXLUA bool LUACALL wxlua_setderivedmethod(lua_State* L, void *obj_ptr, const char *method_name, wxLuaObject* wxlObj); 00556 // Is there a derived method for the given obj_ptr with the method_name that was 00557 // added by calling wxlua_setderivedmethod()? 00558 // If push_method then push the method onto the stack. 00559 WXDLLIMPEXP_WXLUA bool LUACALL wxlua_hasderivedmethod(lua_State* L, void *obj_ptr, const char *method_name, bool push_method); 00560 // Remove any derived functions or values for the obj_ptr that have been added with 00561 // wxlua_setderivedmethod(). 00562 // This is called when an object is being garbage collected by wxluaO_deletegcobject() 00563 // and probably shouldn't be called otherwise. 00564 WXDLLIMPEXP_WXLUA bool LUACALL wxlua_removederivedmethods(lua_State* L, void *obj_ptr); 00565 00566 //---------------------------------------------------------------------------- 00567 // Other functions for wxLua's keys in the LUA_REGISTRYINDEX 00568 //---------------------------------------------------------------------------- 00569 00570 // Get the wxlua_lreg_callbaseclassfunc_key value of the LUA_REGISTRYINDEX table 00571 // to determines whether a virtual C++ class member function should call its own 00572 // base class function or a wxLua derived method if it exists. 00573 WXDLLIMPEXP_WXLUA bool LUACALL wxlua_getcallbaseclassfunction(lua_State* L); 00574 // Set if the class member function call in Lua has a prepended '_' to imply that 00575 // the user wants the base class function and not the derived method in the 00576 // wxlua_lreg_derivedmethods_key table. 00577 // Sets the wxlua_lreg_callbaseclassfunc_key value of the LUA_REGISTRYINDEX table. 00578 WXDLLIMPEXP_WXLUA void LUACALL wxlua_setcallbaseclassfunction(lua_State* L, bool call_base); 00579 00580 // Get the wxlua_lreg_wxeventtype_key value of the LUA_REGISTRYINDEX table 00581 // to see if we're currently in a wxEvent callback. 00582 // Returns wxEVT_NULL if not in an event handler. 00583 // Be careful about destroying Lua when in an event handler. 00584 WXDLLIMPEXP_WXLUA wxEventType LUACALL wxlua_getwxeventtype(lua_State* L); 00585 // Set the wxlua_lreg_wxeventtype_key value of the LUA_REGISTRYINDEX table 00586 // with the current wxEventType we're in or wxEVT_NULL if none. 00587 WXDLLIMPEXP_WXLUA void LUACALL wxlua_setwxeventtype(lua_State* L, wxEventType evt_type); 00588 00589 // Get the wxlua_lreg_wxluastatedata_key wxLuaStateData value from 00590 // the LUA_REGISTRYINDEX table for the owner wxLuaState. 00591 // Note: It returns NULL if the lua_State is about to be closed. 00592 WXDLLIMPEXP_WXLUA wxLuaStateData* LUACALL wxlua_getwxluastatedata(lua_State* L); 00593 00594 //---------------------------------------------------------------------------- 00595 // wxLuaStateData - the internal data for the wxLuaState. 00596 // All members of this class should be accessed through the wxLuaState. 00597 // It is public only for people who need to get at the internals, there are 00598 // absolutely no guarantees that things won't change. 00599 //---------------------------------------------------------------------------- 00600 00601 class WXDLLIMPEXP_WXLUA wxLuaStateData 00602 { 00603 public: 00604 wxLuaStateData(); 00605 ~wxLuaStateData(); 00606 00607 bool m_is_running; // is the lua_State running a script 00608 bool m_is_closing; // are we currently being closed 00609 00610 int m_lua_debug_hook_count; // values from wxLuaState::SetLuaDebugHook() 00611 int m_lua_debug_hook_yield; 00612 int m_lua_debug_hook; 00613 bool m_lua_debug_hook_send_evt; 00614 00615 unsigned long m_last_debug_hook_time; // last time the debug hook was called 00616 00617 bool m_debug_hook_break; // should the lua_State break for next debug_hook 00618 wxString m_debug_hook_break_msg; // message when breaking in the debug_hook 00619 00620 wxEvtHandler *m_evtHandler; // event handler to send wxLuaEvents to 00621 wxWindowID m_id; // event id to send the events with 00622 }; 00623 00624 //---------------------------------------------------------------------------- 00625 // wxLuaStateRefData - the internal data for the wxLuaState. 00626 // please use the wxLuaState accessor functions 00627 //---------------------------------------------------------------------------- 00628 00629 #include "wx/hashmap.h" 00630 WX_DECLARE_VOIDPTR_HASH_MAP_WITH_DECL(wxLuaState *, wxHashMapLuaState, class WXDLLIMPEXP_WXLUA); 00631 00632 class WXDLLIMPEXP_WXLUA wxLuaStateRefData : public wxObjectRefData 00633 { 00634 public: 00635 wxLuaStateRefData(bool create_data = true); 00636 virtual ~wxLuaStateRefData(); 00637 00638 // destroy and cleanup the lua_State, returns success 00639 // if 'force' = true then make sure all wxWindows are destroyed. 00640 bool CloseLuaState(bool force); 00641 // clear all wxLuaEventCallbacks and wxLuaWinDestroyCallbacks on destruction 00642 void ClearCallbacks(); 00643 00644 // ------------------------------------------------------------------------ 00645 00646 lua_State* m_lua_State; // the lua_State that "is" Lua 00647 bool m_lua_State_static; // lua_close() the lua_State if !static 00648 bool m_lua_State_coroutine; // this is a coroutine, don't close it 00649 00650 wxLuaStateData* m_wxlStateData; // the data shared for this state 00651 bool m_own_stateData; // not a coroutine when true, so delete it when done 00652 }; 00653 00654 //---------------------------------------------------------------------------- 00655 // wxLuaState - a ref counted class to interface between C++ and Lua's C lua_State 00656 //---------------------------------------------------------------------------- 00657 00658 // enum wxLuaState_Type is for the function 00659 // wxLuaState::Create(lua_State* L, int state_type = wxLUASTATE_GETSTATE); 00660 enum wxLuaState_Type 00661 { 00662 wxLUASTATE_GETSTATE = 1, // Attach to a previously created wxLuaState's 00663 // lua_State refing the existing wxLuaStateRefData 00664 00665 wxLUASTATE_SETSTATE = 2, // Set the lua_State for the wxLuaState. 00666 // Does not call lua_openlibs() so you should have 00667 // called before setting it to the wxLuaState. 00668 00669 // The values below are to be ored with wxLUASTATE_SETSTATE only. 00670 wxLUASTATE_STATICSTATE = 0x10, // The lua_State is static and the wxLuaState 00671 // will not lua_close() it when Destroy()ed. 00672 wxLUASTATE_OPENBINDINGS = 0x20, // Install all the bindings in 00673 // wxLuaBinding::GetBindingList() into the 00674 // lua_State. You may install the bindings 00675 // one at a time using 00676 // wxLuaState::RegisterBinding(wxLuaBinding*) 00677 }; 00678 00679 // an invalid wxLuaState for comparison (like wxNullBitmap) 00680 extern WXDLLIMPEXP_DATA_WXLUA(wxLuaState) wxNullLuaState; 00681 00682 class WXDLLIMPEXP_WXLUA wxLuaState : public wxObject 00683 { 00684 public: 00685 // Default constructor or if create=true then 00686 // call the function Create(wxEvtHandler=NULL, id=wxID_ANY). 00687 wxLuaState(bool create = false) { if (create) Create(); } 00688 // Create a new lua_State and add the bindings. 00689 // Calls the function Create(wxEvtHandler, id). 00690 wxLuaState(wxEvtHandler *handler, wxWindowID id = wxID_ANY) { Create(handler, id); } 00691 // Create a wxLuaState from an existing lua_State. 00692 // Calls the function Create(lua_State, state_type), state_type is enum wxLuaState_Type. 00693 inline wxLuaState(lua_State* L, int state_type = wxLUASTATE_GETSTATE) { Create(L, state_type); } 00694 // Copy constructor, refs existing wxLuaState 00695 inline wxLuaState(const wxLuaState& wxlState) { Ref(wxlState); } 00696 00697 // ALWAYS Destroy() the wxLuaState instead of calling UnRef(), else circular 00698 // destruction since ref count goes to 0 before actually destroying the lua_State 00699 virtual ~wxLuaState() { Destroy(); } 00700 00701 // ----------------------------------------------------------------------- 00702 00703 // Ref the given wxLuaState 00704 void Create(const wxLuaState& wxlState); 00705 // Create a new lua_State and send wxLuaEvents to this handler. 00706 // The handler may be NULL to not send events to anyone. 00707 // Calls the function Create(lua_State, wxLUASTATE_USESTATE). 00708 bool Create(wxEvtHandler *handler = NULL, wxWindowID id = wxID_ANY); 00709 // Create a wxLuaState from an existing lua_State. 00710 // See enum wxLuaState_Type for infomation about state_type. 00711 bool Create(lua_State* L, int state_type = wxLUASTATE_GETSTATE); 00712 00713 // ----------------------------------------------------------------------- 00714 00715 // Is this wxLuaState valid, has refed data and its lua_State is created 00716 bool Ok() const; 00717 00718 // ----------------------------------------------------------------------- 00719 00720 // Destroy the refed data, use this instead of wxObject::UnRef(). 00721 // Only calls lua_close(L) if this is the last refed state and this was 00722 // created without the wxLUASTATE_STATICSTATE flag. 00723 // Note: if you have a top level window (wxFrame) open in Lua and exit the 00724 // C++ program your program will seem to "hang" because wxApp doesn't 00725 // exit with a top level window open. Call CloseLuaState(true) to ensure 00726 // all non parented (top level) windows are destroyed. 00727 // You must always call CloseLuaState() when you want to close Lua instead 00728 // of hoping that when you call Destroy() you have the last refed instance. 00729 void Destroy(); 00730 // Close the lua_State and if 'force' close all attached wxWindows 00731 // if !force then popup a dialog to ask if all wxWindows should be destroyed. 00732 // Only calls lua_close(L) if this is the last refed state and this was 00733 // created without the wxLUASTATE_STATICSTATE flag. 00734 bool CloseLuaState(bool force); 00735 // Are we currently being closed? Used when the garbage collector is running when 00736 // we don't care about cleaning Lua up so just delete the data. (internal use) 00737 bool IsClosing() const; 00738 00739 // ----------------------------------------------------------------------- 00740 00741 // Get the lua_State 00742 lua_State* GetLuaState() const; 00743 // Get the ref data (internal use) 00744 wxLuaStateRefData* GetLuaStateRefData() const { return (wxLuaStateRefData*)GetRefData(); } 00745 // Get the data for the lua_State in the ref data (internal use) 00746 wxLuaStateData* GetLuaStateData() const; 00747 00748 // ----------------------------------------------------------------------- 00749 00750 // Get the wxLuaState from the corresponding lua_State 00751 // returns wxNullLuaState if none found. 00752 static wxLuaState GetwxLuaState(lua_State* L); 00753 // A mapping between hashmap[lua_State* L] = wxLuaState* 00754 // Note: The hashed new wxLuaState is not Refed since we want to know when 00755 // the ref count goes to 1 for cleanup and it is deleted when 00756 // its wxLuaStateRefData is finally deleted. 00757 // Note: The coroutine lua_States are not hashed since we cannot know when 00758 // they are created or deleted. We must create wxLuaStates for them on the fly. 00759 static wxHashMapLuaState s_wxHashMapLuaState; 00760 00761 // ----------------------------------------------------------------------- 00762 00763 // In order for wxLua scripts to work from a C++ program's wxApp::OnInit() 00764 // and the Lua module you may have to set this variable to force the wxLua 00765 // code "wx.wxGetApp:MainLoop()" to not call wxApp::MainLoop(). 00766 // The issue is that within the function wxApp::OnInit() wxApp::IsMainLoopRunning() 00767 // returns false, but it will be running after OnInit() returns so we should 00768 // silently ignore the Lua code wanting to prematurely start the MainLoop. 00769 // Initialized to false, meaning not set. 00770 // Set to true for the Lua code "wx.wxGetApp:MainLoop()" to not call 00771 // the app's MainLoop() function. 00772 // 00773 // See the wxLua apps for usage. 00774 static bool sm_wxAppMainLoop_will_run; 00775 00776 // ----------------------------------------------------------------------- 00777 00778 // Get/Set the event handler that the wxLuaEvents from this will be sent to, can be NULL. 00779 // See wxEVT_LUA_XXX for a list of possible events that may be sent. 00780 void SetEventHandler(wxEvtHandler *evtHandler); 00781 wxEvtHandler *GetEventHandler() const; 00782 // Get/Set the wxWindowID that the wxLuaEvents will be sent with. 00783 void SetId(wxWindowID id); 00784 wxWindowID GetId() const; 00785 00786 // Sends the input wxLuaEvent, after checking that this is valid, to the 00787 // set wxEventHandler (may be NULL), see constructor or SetEventHandler(). 00788 // returns wxEvtHandler::ProcessEvent(event) 00789 bool SendEvent( wxLuaEvent &event ) const; 00790 00791 // ----------------------------------------------------------------------- 00792 00793 // Run a Lua file from disk using lua_loadfile() then LuaPCall(). 00794 // Leaves nresults on the stack, use LUA_MULTRET to leave them all. 00795 // Returns 0 on success or Lua's error code. 00796 // Sends a wxEVT_LUA_ERROR wxLuaEvent on error. 00797 int RunFile(const wxString &fileName, int nresults = 0); 00798 // Run a string that contains Lua code using luaL_loadbuffer() then LuaPCall(). 00799 // Leaves nresults on the stack, use LUA_MULTRET to leave them all. 00800 // Returns 0 on success or Lua's error code. 00801 // Sends a wxEVT_LUA_ERROR wxLuaEvent on error. 00802 int RunString(const wxString &script, const wxString& name = wxEmptyString, int nresults = 0); 00803 // Run a char array #included from bin2c compilation or something else 00804 // using luaL_loadbuffer() then LuaPCall(). 00805 // Leaves nresults on the stack, use LUA_MULTRET to leave them all. 00806 // Returns 0 on success or Lua's error code. 00807 // Sends a wxEVT_LUA_ERROR wxLuaEvent on error. 00808 int RunBuffer(const char buf[], size_t size, const wxString &name = wxT("= lua"), int nresults = 0); 00809 00810 int LuaDoString(const wxString &script, const wxString& name = wxEmptyString, int nresults = 0) { return RunString(script, name, nresults); } 00811 int LuaDoFile(const wxString &filename, int nresults = 0) { return RunFile(filename, nresults); } 00812 int LuaDoBuffer(const char *buffer, size_t len, const char *name, int nresults = 0) { return RunBuffer(buffer, len, lua2wx(name), nresults); } 00813 00814 // Is a program running now, running state is set for Run/File/String/Buffer 00815 bool IsRunning() const; 00816 00817 // Replacement for lua_pcall() 00818 // Returns 0 on success or Lua's error code. 00819 // Sends a wxEVT_LUA_ERROR wxLuaEvent on error. 00820 // narg is the number of args to the function to call. 00821 // nresults is the number of values expected to be returned and Lua 00822 // will adjust the stack to match. 00823 // Use LUA_MULTRET for a variable number of returns. 00824 int LuaPCall(int narg, int nresults); 00825 00826 // 00827 bool SendLuaErrorEvent(int status, int top); 00828 00829 // Get the wxEventType that Lua may currently be in, wxEVT_NULL if not in an 00830 // event handler. Be careful about destroying Lua when in an event handler. 00831 // See wxlua_getwxeventtype() 00832 wxEventType GetInEventType() const; 00833 // Set the wxEventType that the Lua code is currently running (internal use). 00834 // See wxlua_setwxeventtype() 00835 void SetInEventType(wxEventType eventType); 00836 00837 // ----------------------------------------------------------------------- 00838 00839 // Try to compile the Lua program. Creates new lua_State to test for syntax 00840 // errors and sends error events. See wxlua_errorinfo() for errMsg and line_num. 00841 int CompileString(const wxString &script, const wxString& name = wxEmptyString, 00842 wxString* errMsg = NULL, int* line_num = NULL); 00843 int CompileBuffer(const char buf[], size_t size, const wxString &name = wxEmptyString, 00844 wxString* errMsg = NULL, int* line_num = NULL); 00845 00846 // ----------------------------------------------------------------------- 00847 00848 // Break a currently running Lua program by setting the Lua debug hook to 00849 // be called for anything and breaking as soon as possible by calling 00850 // wxlua_error() with the message 00851 void DebugHookBreak(const wxString &message = wxT("Lua interpreter stopped")); 00852 // Clear a previously set DebugHookBreak(), resetting the debug hook 00853 // to the previous values 00854 void ClearDebugHookBreak(); 00855 // Has DebugHookBreak() been called and we're waiting for the next hook call? 00856 bool GetDebugHookBreak() const; 00857 // Get the message that will be sent when from a DebugHookBreak() call 00858 wxString GetDebugHookBreakMessage() const; 00859 00860 // Have Lua run an internal hook function with this mask 00861 // hook = LUA_MASKCALL | LUA_MASKRET | LUA_MASKLINE | LUA_MASKCOUNT 00862 // Every count hook mask a wxEVT_LUA_DEBUG_HOOK event is sent if send_debug_evt. 00863 // If yield_ms > 0 then wxYield is called every yield milliseconds. 00864 // Turn the hook off with count < 1 00865 // see lua_sethook() function 00866 void SetLuaDebugHook(int hook = LUA_MASKCALL|LUA_MASKRET|LUA_MASKLINE|LUA_MASKCOUNT, 00867 int count = 1000, int yield_ms = 100, 00868 bool send_debug_evt = false); 00869 int GetLuaDebugHook() const; 00870 int GetLuaDebugHookCount() const; 00871 int GetLuaDebugHookYield() const; 00872 bool GetLuaDebugHookSendEvt() const; 00873 00874 // Internally updated time that the debug hook was last called when running 00875 // Lua code and SetLuaDebugHook is turned on 00876 unsigned long GetLastLuaDebugHookTime() const; 00877 // Set to an specific time to control debug timing 00878 void SetLastLuaDebugHookTime(unsigned long t); 00879 00880 // ----------------------------------------------------------------------- 00881 // Binding functions 00882 00883 // Registers a new C function for Lua, see usage in wxlstate.cpp 00884 void RegisterFunction(lua_CFunction func, const char* funcName); 00885 wxLUA_UNICODE_ONLY(void RegisterFunction(lua_CFunction func, const wxString &funcName) { RegisterFunction(func, wx2lua(funcName)); }) 00886 00887 // Register a single wxLuaBinding, returns true on success. Nothing is 00888 // left on the stack. 00889 bool RegisterBinding(wxLuaBinding* binding); 00890 // Register all the bindings in the wxLuaBinding::GetBindingList(), this is done 00891 // automatically if the wxLuaState is created with wxLUASTATE_OPENBINDINGS. 00892 bool RegisterBindings(); 00893 00894 // Get the installed wxLuaBinding with the given 00895 // wxLuaBinding::GetBindingName() or NULL for no match. 00896 // See wxLuaBinding::GetLuaBinding(). 00897 wxLuaBinding* GetLuaBinding(const wxString& bindingName) const; 00898 00899 // Get wxLuaBindClass for given Lua Tag using wxLuaBindClass::wxluatype, 00900 // returns NULL on failure. See wxluaT_getclass(). 00901 const wxLuaBindClass* GetBindClass(int iClassTag) const; 00902 // Get wxLuaBindClass for given class name using wxLuaBindClass::name, 00903 // returns NULL on failure. See wxluaT_getclass(). 00904 const wxLuaBindClass* GetBindClass(const char* className) const; 00905 // Get the first wxLuaBindClass that has this particular wxLuaBindMethod 00906 // returns NULL on failure. See wxLuaBinding::GetBindClass(). 00907 const wxLuaBindClass* GetBindClass(const wxLuaBindMethod* wxlMethod) const; 00908 // Get the first wxLuaBindClass that has this particular wxLuaBindCFunc in its methods 00909 // returns NULL on failure. See wxLuaBinding::GetBindClass(). 00910 const wxLuaBindClass* GetBindClass(const wxLuaBindCFunc* wxlCFunc) const; 00911 // See wxluaT_isderivedtype(). 00912 int IsDerivedType(int wxl_type, int base_wxl_type) const; 00913 // Get wxLuaBindEvent for given wxEventType (wxEvent::GetEventType()) by finding 00914 // the matching wxLuaBindEvent::eventType. 00915 // See wxLuaBinding::GetBindEvent(). 00916 const wxLuaBindEvent* GetBindEvent(wxEventType eventType) const; 00917 00918 // See wxlua_setcallbaseclassfunction() and wxlua_getcallbaseclassfunction(). 00919 void SetCallBaseClassFunction(bool call_base); 00920 bool GetCallBaseClassFunction(); 00921 00922 // ----------------------------------------------------------------------- 00923 // memory tracking functions (internal use) 00924 00925 // See wxluaO_addgcobject(). 00926 void AddGCObject(wxObject *wxobj); 00927 // See wxluaO_addgcobject(). 00928 void AddGCObject(void* obj_ptr, wxObject *wxobj); 00929 // See wxluaO_deletegcobject(). 00930 bool DeleteGCObject(void* udata, void *obj_ptr, int flags); 00931 // See wxluaO_isgcobject(). 00932 bool IsGCObject(void *obj_ptr) const; 00933 // See wxluaO_getgcobjectinfo(). 00934 wxArrayString GetGCObjectInfo() const; 00935 00936 // Add a wxWindow to track and delete when we're closed, only track 00937 // the parent window, not its children. returns true if it was added. 00938 // Note: wxObject is used as the base class since we blindly call this 00939 // function for all objects with classinfo in the bindings and we 00940 // want to minimize the code in the bindings. 00941 void AddTrackedWindow(wxObject *win); 00942 // Don't track this window anymore and don't delete it. 00943 void RemoveTrackedWindow(wxWindow *win); 00944 // Is this window tracked, if check_parents see if a parent of it is. 00945 bool IsTrackedWindow(wxWindow *win, bool check_parents = true) const; 00946 // Get an array of strings "wxWindow_classname(&win id=wxWindowID)" 00947 wxArrayString GetTrackedWindowInfo() const; 00948 00949 // delete all stray wxWindow derived classes that have been destroyed 00950 // by wxWidgets (eg. a child window) 00951 // This function does not need to be called ever, for debugging perhaps? 00952 void GarbageCollectWindows(bool closeWindows); 00953 00954 // Add or remove a tracked wxLuaEventCallback connected to a wxEvtHandler 00955 void AddTrackedEventCallback(wxLuaEventCallback* callback); 00956 bool RemoveTrackedEventCallback(wxLuaEventCallback* callback); 00957 // Get an array of strings "wxEVT_XXX (wxEventType #) count#" 00958 wxArrayString GetTrackedEventCallbackInfo() const; 00959 00960 // Add or remove a tracked wxLuaWinDestroyCallback connected to wxEVT_DESTROY. 00961 void AddTrackedWinDestroyCallback(wxLuaWinDestroyCallback* callback); 00962 bool RemoveTrackedWinDestroyCallback(wxLuaWinDestroyCallback* callback); 00963 // Get an array of strings "wxWindow_classname count#" 00964 wxArrayString GetTrackedWinDestroyCallbackInfo() const; 00965 00966 // ----------------------------------------------------------------------- 00967 00968 // Push the errorMsg on the stack and call wxlua_error() 00969 void wxlua_Error(const char *errorMsg) const; 00970 wxLUA_UNICODE_ONLY(void wxlua_Error(const wxString& errorMsg) const { wxlua_Error(wx2lua(errorMsg)); }) 00971 00972 void* wxlua_ToUserdata(int stack_idx, bool null_ptr = false) const; 00973 00974 // ----------------------------------------------------------------------- 00975 // wxLua Lua Registry Table Functions 00976 00977 int wxluaR_Ref(int stack_idx, void* lightuserdata_reg_key); 00978 bool wxluaR_Unref(int wxlref_index, void* lightuserdata_reg_key); 00979 bool wxluaR_GetRef(int wxlref_index, void* lightuserdata_reg_key); 00980 00981 int wxluaT_NewMetatable(int wxl_type); 00982 bool wxluaT_SetMetatable(int wxl_type); 00983 int wxluaT_Type(int stack_idx) const; 00984 00985 bool wxluaT_PushUserDataType(const void *obj_ptr, int wxl_type, bool track); 00986 00987 // ----------------------------------------------------------------------- 00988 // wxLua get data type 00989 00990 // See wxlua_iswxluatype(). 00991 int IswxLuaType(int luatype, int wxl_type) const; 00992 // See wxluaT_isuserdatatype(). 00993 bool IsUserDataType(int stack_idx, int wxl_type) const; 00994 // See wxluaT_getuserdatatype(). 00995 void* GetUserDataType(int stack_idx, int iTag) const; 00996 00997 // helper functions to get numbers, booleans and strings safer 00998 00999 // See wxlua_getstringtype(). 01000 const char* GetStringType(int stack_idx); 01001 // See wxlua_getwxStringtype(). 01002 wxString GetwxStringType(int stack_idx); 01003 // See wxlua_getbooleantype(). 01004 bool GetBooleanType(int stack_idx); 01005 // See wxlua_getintegertype(). 01006 long GetIntegerType(int stack_idx); 01007 // See wxlua_getnumbertype(). 01008 double GetNumberType(int stack_idx); 01009 01010 // See wxlua_isXXXtype(). 01011 bool IsStringType(int stack_idx) const; 01012 bool IswxStringType(int stack_idx) const; 01013 bool IsBooleanType(int stack_idx) const; 01014 bool IsIntegerType(int stack_idx) const; 01015 bool IsNumberType(int stack_idx) const; 01016 01017 // See wxlua_getwxStringarray(). 01018 wxString* GetwxStringArray(int stack_idx, int &count); 01019 // See wxlua_getwxArrayString(). 01020 wxLuaSmartwxArrayString GetwxArrayString(int stack_idx); 01021 // See wxlua_getchararray(). 01022 const char** GetCharArray(int stack_idx, int &count); 01023 01024 // See wxlua_getintarray(). 01025 int* GetIntArray(int stack_idx, int &count); 01026 // See wxlua_getwxArrayInt(). 01027 wxLuaSmartwxArrayInt GetwxArrayInt(int stack_idx); 01028 01029 // See wxlua_pushwxArrayStringtable(). 01030 int PushwxArrayStringTable(const wxArrayString &strArray); 01031 // See wxlua_pushwxArrayInttable(). 01032 int PushwxArrayIntTable(const wxArrayInt &intArray); 01033 01034 // ----------------------------------------------------------------------- 01035 01036 // See wxluaT_typename(). 01037 wxString GetwxLuaTypeName(int wxl_type) const; 01038 01039 // ----------------------------------------------------------------------- 01040 01041 // See wxlua_setderivedmethod 01042 bool SetDerivedMethod(void *obj_ptr, const char *method_name, wxLuaObject* wxlObj); 01043 // See wxlua_hasderivedmethod(). 01044 bool HasDerivedMethod(void *obj_ptr, const char *method_name, bool push_method) const; 01045 // See wxlua_removederivedmethods() 01046 bool RemoveDerivedMethods(void *obj_ptr) const; 01047 // Find a derived method given an object and and a method name. 01048 // If the method can be found, return the valid wxLuaState it belongs to. 01049 // This function can be used for classes that implement virtual functions to 01050 // try to find a wxLuaState that may have overridden the function to call it. 01051 // It is probably easier to merely make a wxLuaState a class member for 01052 // faster lookup though. 01053 static wxLuaState GetDerivedMethodState(void *obj_ptr, const char *method_name); 01054 01055 // ----------------------------------------------------------------------- 01056 // C++ interface for the lua_State functions 01057 // functions prepended by lua_XXX directly call the 'C' lua_XXX function 01058 // The function names have been capitalized to allow case sensitive searching 01059 // ----------------------------------------------------------------------- 01060 // Raw basic Lua stack functions, lua.h 01061 01062 int lua_GetTop() const; 01063 void lua_SetTop(int index); 01064 void lua_PushValue(int index); 01065 void lua_Remove(int index); 01066 void lua_Pop(int count); 01067 void lua_Insert(int index); 01068 void lua_Replace(int index); 01069 int lua_CheckStack(int size); 01070 void lua_XMove(const wxLuaState& to, int n); 01071 01072 // ----------------------------------------------------------------------- 01073 // Raw Lua accesses functions (stack -> C), lua.h 01074 01075 bool lua_IsNumber(int index) const; 01076 bool lua_IsString(int index) const; 01077 bool lua_IsCFunction(int index) const; 01078 bool lua_IsUserdata(int index) const; 01079 int lua_Type(int index) const; 01080 wxString lua_TypeName(int type) const; 01081 01082 int lua_Equal(int index1, int index2) const; 01083 int lua_RawEqual(int index1, int index2) const; 01084 int lua_LessThan(int index1, int index2) const; 01085 01086 double lua_ToNumber(int index) const; 01087 int lua_ToInteger(int index) const; 01088 int lua_ToBoolean(int index) const; 01089 const char* lua_ToString(int index) const; 01090 wxString lua_TowxString(int index) const; // wxLua added 01091 size_t lua_StrLen(int index) const; 01092 size_t luaL_ObjLen(int index) const; 01093 lua_CFunction lua_ToCFunction(int index) const; 01094 void* lua_ToUserdata(int index) const; 01095 wxLuaState lua_ToThread(int index) const; 01096 const void* lua_ToPointer(int index) const; 01097 01098 // ----------------------------------------------------------------------- 01099 // Raw Lua push functions (C -> stack), lua.h 01100 01101 void lua_PushNil(); 01102 void lua_PushNumber(lua_Number n); 01103 void lua_PushInteger(lua_Integer n); 01104 void lua_PushLString(const char* s, size_t len); 01105 void lua_PushString(const char* s); 01106 wxLUA_UNICODE_ONLY(void lua_PushString(const wxString& s) { lua_PushString(wx2lua(s)); }) 01107 //wxString lua_PushVfString(); 01108 //wxString lua_PushFString(); 01109 void lua_PushCClosure(lua_CFunction fn, int n); 01110 void lua_PushBoolean(bool b); 01111 void lua_PushLightUserdata(void* p); 01112 //void lua_PushThread(lua_State* L); 01113 01114 // ----------------------------------------------------------------------- 01115 // Raw Lua get functions (Lua -> stack), lua.h 01116 01117 void lua_GetTable(int idx); 01118 void lua_GetField(int idx, const char* k); 01119 wxLUA_UNICODE_ONLY(void lua_GetField(int idx, const wxString& k) { lua_GetField(idx, wx2lua(k)); }) 01120 void lua_RawGet(int idx); 01121 void lua_RawGeti(int idx, int n); 01122 void lua_CreateTable(int narr, int nrec); 01123 void lua_NewTable(); 01124 void* lua_NewUserdata(size_t sz); 01125 int lua_GetMetatable(int objindex); 01126 void lua_GetFenv(int idx); 01127 01128 // ----------------------------------------------------------------------- 01129 // Raw Lua set functions (stack -> Lua), lua.h 01130 01131 void lua_SetTable(int idx); 01132 void lua_SetField(int idx, const char* k); 01133 wxLUA_UNICODE_ONLY(void lua_SetField(int idx, const wxString& k) { lua_SetField(idx, wx2lua(k)); }) 01134 void lua_RawSet(int idx); 01135 void lua_RawSeti(int idx, int n); 01136 int lua_SetMetatable(int objindex); 01137 int lua_SetFenv(int idx); 01138 01139 // ----------------------------------------------------------------------- 01140 // Raw Lua `load' and `call' functions (load and run Lua code), lua.h 01141 01142 void lua_Call(int nargs, int nresults); 01143 int lua_PCall(int nargs, int nresults, int errfunc); 01144 int lua_CPCall(lua_CFunction func, void *ud); 01145 int lua_Load(lua_Reader reader, void *dt, const char* chunkname); 01146 wxLUA_UNICODE_ONLY(int lua_Load(lua_Reader reader, void *dt, const wxString& chunkname) { return lua_Load(reader, dt, wx2lua(chunkname)); }) 01147 01148 int lua_Dump(lua_Writer writer, void *data); 01149 01150 // ----------------------------------------------------------------------- 01151 // Raw Lua coroutine functions, lua.h 01152 01153 int lua_Yield(int nresults); 01154 int lua_Resume(int narg); 01155 int lua_Status(); 01156 01157 // ----------------------------------------------------------------------- 01158 // Raw Lua garbage-collection functions, lua.h 01159 01160 int lua_GetGCCount(); 01161 01162 // ----------------------------------------------------------------------- 01163 // Raw Lua miscellaneous functions, lua.h 01164 01165 wxString lua_Version() const; 01166 int lua_Error(); 01167 int lua_Next(int idx); 01168 void lua_Concat(int n); 01169 01170 //LUA_API lua_Alloc (lua_getallocf) (lua_State *L, void **ud); 01171 //LUA_API void lua_setallocf (lua_State *L, lua_Alloc f, void *ud); 01172 01173 // ----------------------------------------------------------------------- 01174 // Raw Lua some useful "macros", lua.h 01175 01176 //lua_boxpointer(L,u) 01177 //lua_unboxpointer(L,i) 01178 //lua_pop(L,n) lua_settop(L, -(n)-1) 01179 01180 void lua_Register(const char* funcName, lua_CFunction f); 01181 wxLUA_UNICODE_ONLY(void lua_Register(const wxString& funcName, lua_CFunction f) { lua_Register(wx2lua(funcName), f); }) 01182 void lua_PushCFunction(lua_CFunction f); 01183 01184 bool lua_IsFunction(int idx) const; 01185 bool lua_IsTable(int idx) const; 01186 bool lua_IsLightUserdata(int idx) const; 01187 bool lua_IsNil(int idx) const; 01188 bool lua_IsBoolean(int idx) const; 01189 bool lua_IsThread(int idx) const; 01190 bool lua_IsNone(int idx) const; 01191 bool lua_IsNoneOrNil(int idx) const; 01192 01193 //lua_pushliteral(L, s) lua_pushlstring(L, "" s, (sizeof(s)/sizeof(char))-1) 01194 01195 void lua_SetGlobal(const char* s); 01196 void lua_GetGlobal(const char* s); 01197 01198 // ----------------------------------------------------------------------- 01199 // Raw Lua Debug functions, lua.h 01200 01201 int lua_GetStack(int level, lua_Debug* ar); 01202 int lua_GetInfo(const char* what, lua_Debug* ar); 01203 wxLUA_UNICODE_ONLY(int lua_GetInfo(const wxString& what, lua_Debug* ar) { return lua_GetInfo(wx2lua(what), ar); }) 01204 const char* lua_GetLocal(const lua_Debug* ar, int n); 01205 const char* lua_SetLocal(const lua_Debug* ar, int n); 01206 const char* lua_GetUpvalue(int funcindex, int n); 01207 const char* lua_SetUpvalue(int funcindex, int n); 01208 01209 int lua_SetHook(lua_Hook func, int mask, int count); 01210 lua_Hook lua_GetHook(); 01211 int lua_GetHookMask(); 01212 int lua_GetHookCount(); 01213 01214 // ----------------------------------------------------------------------- 01215 // Raw Lua auxlib functions, lauxlib.h 01216 01217 void luaI_OpenLib(const char *libname, const luaL_reg *l, int nup); 01218 void luaL_Register(const char *libname, const luaL_reg *l); 01219 int luaL_GetMetafield(int obj, const char *e); 01220 int luaL_CallMeta(int obj, const char *e); 01221 int luaL_TypeError(int narg, const char *tname); 01222 int luaL_ArgError(int numarg, const char *extramsg); 01223 const char *luaL_CheckLString(int numArg, size_t *l); 01224 const char *luaL_OptLString(int numArg, const char *def, size_t *len); 01225 lua_Number luaL_CheckNumber(int numArg); 01226 lua_Number luaL_OptNumber(int nArg, lua_Number def); 01227 lua_Integer luaL_CheckInteger(int numArg); 01228 lua_Integer luaL_OptInteger(int nArg, lua_Integer def); 01229 01230 void luaL_CheckStack(int sz, const char *msg); 01231 void luaL_CheckType(int narg, int t); 01232 void luaL_CheckAny(int narg); 01233 01234 int luaL_NewMetatable(const char *tname); 01235 void luaL_GetMetatable(const char *tname); 01236 void *luaL_CheckUdata(int ud, const char *tname); 01237 01238 void luaL_Where(int lvl); 01239 int luaL_Error(const char *fmt, ...); 01240 01241 int luaL_CheckOption(int narg, const char *def, const char *const lst[]); 01242 01243 int luaL_Ref(int t); 01244 void luaL_Unref(int t, int ref); 01245 01246 int luaL_LoadFile(const char *filename); 01247 int luaL_LoadBuffer(const char *buff, size_t sz, const char *name); 01248 int luaL_LoadString(const char *s); 01249 01250 //LUALIB_API lua_State *(luaL_newstate) (void); 01251 //LUALIB_API const char *(luaL_gsub) (lua_State *L, const char *s, const char *p, const char *r); 01252 //LUALIB_API const char *(luaL_findtable) (lua_State *L, int idx, const char *fname, int szhint); 01253 01254 // ----------------------------------------------------------------------- 01255 // Raw Lua some useful macros, lauxlib.h 01256 01257 void luaL_ArgCheck(bool condition, int numarg, const char* extramsg); 01258 const char* luaL_CheckString(int numArg); 01259 const char* luaL_OptString(int numArg, const char* def); 01260 int luaL_CheckInt(int numArg); 01261 int luaL_OptInt(int numArg, int def); 01262 long luaL_CheckLong(int numArg); 01263 long luaL_OptLong(int numArg, int def); 01264 01265 // ----------------------------------------------------------------------- 01266 // others 01267 01268 void GetGlobals(); 01269 01270 // ----------------------------------------------------------------------- 01271 // LUA_PATH 01272 01273 wxString GetLuaPath(); 01274 void AddLuaPath(const wxPathList& pathlist); 01275 void AddLuaPath(const wxFileName& filename); 01276 01277 // ----------------------------------------------------------------------- 01278 // operators 01279 01280 bool operator == (const wxLuaState& wxlState) const 01281 { return m_refData == wxlState.m_refData; } 01282 bool operator != (const wxLuaState& wxlState) const 01283 { return m_refData != wxlState.m_refData; } 01284 01285 wxLuaState& operator = (const wxLuaState& wxlState) 01286 { 01287 if ( (*this) != wxlState ) 01288 Create(wxlState); 01289 return *this; 01290 } 01291 01292 private: 01293 // ref counting code 01294 virtual wxObjectRefData *CreateRefData() const; 01295 //virtual wxObjectRefData *CloneRefData(const wxObjectRefData *data) const; 01296 01297 DECLARE_DYNAMIC_CLASS(wxLuaState) 01298 }; 01299 01300 //----------------------------------------------------------------------------- 01301 // wxLuaEvent - An event sent from the wxLuaState to the set wxEvtHandler 01302 // to alert the handler of print, 01303 //----------------------------------------------------------------------------- 01304 01305 class WXDLLIMPEXP_WXLUA wxLuaEvent: public wxNotifyEvent 01306 { 01307 public: 01308 wxLuaEvent(wxEventType commandType = wxEVT_NULL, wxWindowID id = wxID_ANY, 01309 const wxLuaState& wxlState = wxNullLuaState); 01310 01311 wxLuaEvent(const wxLuaEvent &event); 01312 01313 // use GetString method to retrieve info 01314 01315 // Get the line number in the code, -1 if unknown 01316 int GetLineNum() const { return m_commandInt; } 01317 01318 wxLuaState GetwxLuaState() const { return m_wxlState; } 01319 void SetwxLuaState(const wxLuaState& wxlState) { m_wxlState = wxlState; } 01320 01321 lua_State *GetLuaState() const { return m_wxlState.GetLuaState(); } 01322 // non null only for wxEVT_LUA_DEBUG_HOOK 01323 lua_Debug *GetLuaDebug() const { return m_lua_Debug; } 01324 01325 // If called from a wxEVT_LUA_DEBUG_HOOK the interpreter will stop 01326 void DebugHookBreak(bool stop) { m_debug_hook_break = stop; } 01327 01328 // implementation 01329 virtual wxEvent *Clone() const { return new wxLuaEvent(*this); } 01330 01331 wxLuaState m_wxlState; 01332 bool m_debug_hook_break; 01333 lua_Debug *m_lua_Debug; 01334 }; 01335 01336 BEGIN_DECLARE_EVENT_TYPES() 01337 // A wxLuaState is being created, sent at the end of 01338 // wxLuaState(wxEvtHandler, win id) or Create(wxEvtHandler, win id) 01339 DECLARE_EXPORTED_EVENT_TYPE(WXDLLIMPEXP_WXLUA, wxEVT_LUA_CREATION, 0) 01340 // Lua's print(...) statements and such, check GetString() 01341 DECLARE_EXPORTED_EVENT_TYPE(WXDLLIMPEXP_WXLUA, wxEVT_LUA_PRINT, 0) 01342 // an error in Lua has occurred, check GetString() for message 01343 DECLARE_EXPORTED_EVENT_TYPE(WXDLLIMPEXP_WXLUA, wxEVT_LUA_ERROR, 0) 01344 // see LuaDebugHook function 01345 DECLARE_EXPORTED_EVENT_TYPE(WXDLLIMPEXP_WXLUA, wxEVT_LUA_DEBUG_HOOK, 0) 01346 // after app starts, first idle 01347 //DECLARE_EXPORTED_EVENT_TYPE(WXDLLIMPEXP_WXLUA, wxEVT_LUA_INIT, 0) 01348 //DECLARE_EXPORTED_EVENT_TYPE(WXDLLIMPEXP_WXLUA, wxEVT_LUA_DEBUGGERATTACHED, 0) 01349 END_DECLARE_EVENT_TYPES() 01350 01351 typedef void (wxEvtHandler::*wxLuaEventFunction)(wxLuaEvent&); 01352 01353 #define wxLuaEventHandler(func) \ 01354 (wxObjectEventFunction)(wxEventFunction)(wxCommandEventFunction)(wxNotifyEventFunction)wxStaticCastEvent(wxLuaEventFunction, &func) 01355 01356 #define wx__DECLARE_WXLUAEVT(evt, id, fn) wx__DECLARE_EVT1(evt, id, wxLuaEventHandler(fn)) 01357 01358 #define EVT_LUA_CREATION(id, fn) wx__DECLARE_WXLUAEVT(wxEVT_LUA_CREATION, id, fn) 01359 #define EVT_LUA_PRINT(id, fn) wx__DECLARE_WXLUAEVT(wxEVT_LUA_PRINT, id, fn) 01360 #define EVT_LUA_ERROR(id, fn) wx__DECLARE_WXLUAEVT(wxEVT_LUA_ERROR, id, fn) 01361 #define EVT_LUA_DEBUG_HOOK(id, fn) wx__DECLARE_WXLUAEVT(wxEVT_LUA_DEBUG_HOOK, id, fn) 01362 01363 #endif // _WXLSTATE_H_