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