PageRenderTime 70ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 2ms

/docs/wxlua/doxygen/html/wxlstate_8cpp_source.html

https://bitbucket.org/missdeer/luapack
HTML | 4250 lines | 4249 code | 0 blank | 1 comment | 0 complexity | 9800ca9bd30bf697b6a3754baaf54733 MD5 | raw file
Possible License(s): AGPL-3.0, LGPL-2.0, CC-BY-SA-3.0, ISC, LGPL-2.1, GPL-2.0

Large files files are truncated, but you can click here to view the full file

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
  5. <title>wxLua: /mnt/share/devel/lua/wxLua/modules/wxlua/src/wxlstate.cpp Source File</title>
  6. <link href="tabs.css" rel="stylesheet" type="text/css"/>
  7. <link href="doxygen.css" rel="stylesheet" type="text/css"/>
  8. </head>
  9. <body>
  10. <!-- Generated by Doxygen 1.6.3 -->
  11. <div class="navigation" id="top">
  12. <div class="tabs">
  13. <ul>
  14. <li><a href="main.html"><span>Main&nbsp;Page</span></a></li>
  15. <li><a href="annotated.html"><span>Classes</span></a></li>
  16. <li class="current"><a href="files.html"><span>Files</span></a></li>
  17. <li><a href="dirs.html"><span>Directories</span></a></li>
  18. </ul>
  19. </div>
  20. <div class="tabs">
  21. <ul>
  22. <li><a href="files.html"><span>File&nbsp;List</span></a></li>
  23. <li><a href="globals.html"><span>File&nbsp;Members</span></a></li>
  24. </ul>
  25. </div>
  26. <div class="navpath"><a class="el" href="dir_15d5a205ad383328522db6a6be296af1.html">modules</a>&nbsp;&raquo;&nbsp;<a class="el" href="dir_b59b58daa92d206701e75411b2cef894.html">wxlua</a>&nbsp;&raquo;&nbsp;<a class="el" href="dir_83fe9eb5cda99cf12108735ec0aac68a.html">src</a>
  27. </div>
  28. </div>
  29. <div class="contents">
  30. <h1>wxlstate.cpp</h1><a href="wxlstate_8cpp.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="comment">/////////////////////////////////////////////////////////////////////////////</span>
  31. <a name="l00002"></a>00002 <span class="comment"></span><span class="comment">// Name: wxlstate.cpp</span>
  32. <a name="l00003"></a>00003 <span class="comment">// Purpose: wxLuaState, a wxWidgets interface to Lua</span>
  33. <a name="l00004"></a>00004 <span class="comment">// Author: Ray Gilbert, John Labenski, J Winwood (Reuben Thomas for bitlib at bottom)</span>
  34. <a name="l00005"></a>00005 <span class="comment">// Created: 14/11/2001</span>
  35. <a name="l00006"></a>00006 <span class="comment">// Copyright: (c) 2001-2002 Lomtick Software. All rights reserved.</span>
  36. <a name="l00007"></a>00007 <span class="comment">// Licence: wxWidgets licence</span><span class="comment"></span>
  37. <a name="l00008"></a>00008 <span class="comment">/////////////////////////////////////////////////////////////////////////////</span>
  38. <a name="l00009"></a>00009 <span class="comment"></span>
  39. <a name="l00010"></a>00010 <span class="comment">// For compilers that support precompilation, includes &quot;wx/wx.h&quot;</span>
  40. <a name="l00011"></a>00011 <span class="preprocessor">#include &quot;wx/wxprec.h&quot;</span>
  41. <a name="l00012"></a>00012
  42. <a name="l00013"></a>00013 <span class="preprocessor">#ifdef __BORLANDC__</span>
  43. <a name="l00014"></a>00014 <span class="preprocessor"></span><span class="preprocessor"> #pragma hdrstop</span>
  44. <a name="l00015"></a>00015 <span class="preprocessor"></span><span class="preprocessor">#endif</span>
  45. <a name="l00016"></a>00016 <span class="preprocessor"></span>
  46. <a name="l00017"></a>00017 <span class="comment">// for all others, include the necessary headers</span>
  47. <a name="l00018"></a>00018 <span class="preprocessor">#ifndef WX_PRECOMP</span>
  48. <a name="l00019"></a>00019 <span class="preprocessor"></span><span class="preprocessor"> #include &quot;wx/wx.h&quot;</span>
  49. <a name="l00020"></a>00020 <span class="preprocessor">#endif</span>
  50. <a name="l00021"></a>00021 <span class="preprocessor"></span>
  51. <a name="l00022"></a>00022 <span class="preprocessor">#include &quot;<a class="code" href="wxlstate_8h.html">wxlua/include/wxlstate.h</a>&quot;</span>
  52. <a name="l00023"></a>00023 <span class="preprocessor">#include &quot;<a class="code" href="wxlcallb_8h.html">wxlua/include/wxlcallb.h</a>&quot;</span>
  53. <a name="l00024"></a>00024 <span class="preprocessor">#include &quot;wx/tokenzr.h&quot;</span>
  54. <a name="l00025"></a>00025
  55. <a name="l00026"></a>00026 <span class="comment">//#include &quot;wxluadebug/include/wxldebug.h&quot; // for debugging only</span>
  56. <a name="l00027"></a>00027
  57. <a name="l00028"></a><a class="code" href="wxlstate_8cpp.html#a0ef6804bd2cb63f4fa705727f8871cbd">00028</a> <span class="keyword">const</span> <span class="keywordtype">char</span>* <a class="code" href="wxlstate_8h.html#a0ef6804bd2cb63f4fa705727f8871cbd">wxlua_lreg_types_key</a> = <span class="stringliteral">&quot;wxLua metatable class types&quot;</span>;
  58. <a name="l00029"></a><a class="code" href="wxlstate_8cpp.html#ac4ac0a3575fafde31e659ced85f69c4d">00029</a> <span class="keyword">const</span> <span class="keywordtype">char</span>* <a class="code" href="wxlstate_8h.html#ac4ac0a3575fafde31e659ced85f69c4d">wxlua_lreg_refs_key</a> = <span class="stringliteral">&quot;wxLua Lua object refs&quot;</span>;
  59. <a name="l00030"></a><a class="code" href="wxlstate_8cpp.html#a30c2d5556a4a278368b48fe0b7bb3fda">00030</a> <span class="keyword">const</span> <span class="keywordtype">char</span>* <a class="code" href="wxlstate_8h.html#a30c2d5556a4a278368b48fe0b7bb3fda">wxlua_lreg_debug_refs_key</a> = <span class="stringliteral">&quot;wxLuaDebugData refs&quot;</span>;
  60. <a name="l00031"></a><a class="code" href="wxlstate_8cpp.html#a0bcf696ed6d6e2b4218e9a8852a164ef">00031</a> <span class="keyword">const</span> <span class="keywordtype">char</span>* <a class="code" href="wxlstate_8h.html#a0bcf696ed6d6e2b4218e9a8852a164ef">wxlua_lreg_classes_key</a> = <span class="stringliteral">&quot;wxLuaBindClass structs&quot;</span>;
  61. <a name="l00032"></a><a class="code" href="wxlstate_8cpp.html#a274b8b7aae6f647e3e69d098962b7b1d">00032</a> <span class="keyword">const</span> <span class="keywordtype">char</span>* <a class="code" href="wxlstate_8h.html#a274b8b7aae6f647e3e69d098962b7b1d">wxlua_lreg_derivedmethods_key</a> = <span class="stringliteral">&quot;wxLua derived class methods&quot;</span>;
  62. <a name="l00033"></a><a class="code" href="wxlstate_8cpp.html#a921299ed63513972e3bda3678fc835c0">00033</a> <span class="keyword">const</span> <span class="keywordtype">char</span>* <a class="code" href="wxlstate_8h.html#a921299ed63513972e3bda3678fc835c0">wxlua_lreg_wxluastate_key</a> = <span class="stringliteral">&quot;wxLuaState&quot;</span>;
  63. <a name="l00034"></a><a class="code" href="wxlstate_8cpp.html#a80a0d4c84529abdcf6b46237431832ce">00034</a> <span class="keyword">const</span> <span class="keywordtype">char</span>* <a class="code" href="wxlstate_8h.html#a80a0d4c84529abdcf6b46237431832ce">wxlua_lreg_wxluabindings_key</a> = <span class="stringliteral">&quot;wxLuaBindings&quot;</span>;
  64. <a name="l00035"></a><a class="code" href="wxlstate_8cpp.html#af255531a04d81a0d82556c0d1fe8e7e7">00035</a> <span class="keyword">const</span> <span class="keywordtype">char</span>* <a class="code" href="wxlstate_8h.html#af255531a04d81a0d82556c0d1fe8e7e7">wxlua_lreg_weakobjects_key</a> = <span class="stringliteral">&quot;wxLua objects pushed&quot;</span>;
  65. <a name="l00036"></a><a class="code" href="wxlstate_8cpp.html#a7ec5afec8a64a29e28fefec4a1105790">00036</a> <span class="keyword">const</span> <span class="keywordtype">char</span>* <a class="code" href="wxlstate_8h.html#a7ec5afec8a64a29e28fefec4a1105790">wxlua_lreg_gcobjects_key</a> = <span class="stringliteral">&quot;wxLua gc objects to delete&quot;</span>;
  66. <a name="l00037"></a><a class="code" href="wxlstate_8cpp.html#a034985fb7df51670c3b6a024329496fa">00037</a> <span class="keyword">const</span> <span class="keywordtype">char</span>* <a class="code" href="wxlstate_8h.html#a034985fb7df51670c3b6a024329496fa">wxlua_lreg_evtcallbacks_key</a> = <span class="stringliteral">&quot;wxLuaEventCallbacks&quot;</span>;
  67. <a name="l00038"></a><a class="code" href="wxlstate_8cpp.html#ae13b14073477671929c41823f198c33d">00038</a> <span class="keyword">const</span> <span class="keywordtype">char</span>* <a class="code" href="wxlstate_8h.html#ae13b14073477671929c41823f198c33d">wxlua_lreg_windestroycallbacks_key</a> = <span class="stringliteral">&quot;wxLuaWinDestoyCallbacks&quot;</span>;
  68. <a name="l00039"></a><a class="code" href="wxlstate_8cpp.html#aab2351a78a8949184e1910cc1f9e7463">00039</a> <span class="keyword">const</span> <span class="keywordtype">char</span>* <a class="code" href="wxlstate_8h.html#aab2351a78a8949184e1910cc1f9e7463">wxlua_lreg_topwindows_key</a> = <span class="stringliteral">&quot;wxLua top level wxWindows&quot;</span>;
  69. <a name="l00040"></a><a class="code" href="wxlstate_8cpp.html#a91d0a86787b3a1de5921590ce6a0b3e8">00040</a> <span class="keyword">const</span> <span class="keywordtype">char</span>* <a class="code" href="wxlstate_8h.html#a91d0a86787b3a1de5921590ce6a0b3e8">wxlua_lreg_callbaseclassfunc_key</a> = <span class="stringliteral">&quot;wxLua CallBaseClassFunc&quot;</span>;
  70. <a name="l00041"></a><a class="code" href="wxlstate_8cpp.html#af24ccc441c899b3028cfc5d4099c739a">00041</a> <span class="keyword">const</span> <span class="keywordtype">char</span>* <a class="code" href="wxlstate_8h.html#af24ccc441c899b3028cfc5d4099c739a">wxlua_lreg_wxeventtype_key</a> = <span class="stringliteral">&quot;wxLua wxEventType&quot;</span>;
  71. <a name="l00042"></a><a class="code" href="wxlstate_8cpp.html#aba1ad42faf08e4b50bca48b5ba9d8ece">00042</a> <span class="keyword">const</span> <span class="keywordtype">char</span>* <a class="code" href="wxlstate_8h.html#aba1ad42faf08e4b50bca48b5ba9d8ece">wxlua_lreg_wxluastatedata_key</a> = <span class="stringliteral">&quot;wxLuaStateData&quot;</span>;
  72. <a name="l00043"></a><a class="code" href="wxlstate_8cpp.html#ab84cb2515893ab069d7d165208e68aad">00043</a> <span class="keyword">const</span> <span class="keywordtype">char</span>* <a class="code" href="wxlstate_8h.html#ab84cb2515893ab069d7d165208e68aad">wxlua_lreg_regtable_key</a> = <span class="stringliteral">&quot;wxLua LUA_REGISTRYINDEX tables&quot;</span>;
  73. <a name="l00044"></a>00044
  74. <a name="l00045"></a><a class="code" href="wxlstate_8cpp.html#a663c0f4ffbb1923edf3055b4c8c81ff9">00045</a> <span class="keyword">const</span> <span class="keywordtype">char</span>* <a class="code" href="wxlstate_8h.html#a663c0f4ffbb1923edf3055b4c8c81ff9">wxlua_metatable_type_key</a> = <span class="stringliteral">&quot;wxLua metatable class type&quot;</span>;
  75. <a name="l00046"></a><a class="code" href="wxlstate_8cpp.html#a8719cfeea23ebd1cb309de8296777da6">00046</a> <span class="keyword">const</span> <span class="keywordtype">char</span>* <a class="code" href="wxlstate_8h.html#a8719cfeea23ebd1cb309de8296777da6">wxlua_metatable_wxluabindclass_key</a> = <span class="stringliteral">&quot;wxLua metatable wxLuaBindClass&quot;</span>;
  76. <a name="l00047"></a>00047
  77. <a name="l00048"></a>00048 <a class="code" href="classwxLuaState.html">wxLuaState</a> <a class="code" href="wxlstate_8h.html#aef06b6591011cd68b9e370d5e702d255">wxNullLuaState</a>(<span class="keyword">false</span>);
  78. <a name="l00049"></a>00049
  79. <a name="l00050"></a>00050 <span class="keywordtype">int</span> LUACALL <a class="code" href="wxlstate_8cpp.html#ab03a3095ab5d0f4a427b4c5b10aa4e5c">luaopen_bit</a>(lua_State *L); <span class="comment">// implemented at bottom of file</span>
  80. <a name="l00051"></a>00051
  81. <a name="l00052"></a>00052 <span class="comment">// ----------------------------------------------------------------------------</span>
  82. <a name="l00053"></a>00053 <span class="comment">// C functions for Lua used in wxLuaState</span>
  83. <a name="l00054"></a>00054 <span class="comment">// ----------------------------------------------------------------------------</span>
  84. <a name="l00055"></a>00055
  85. <a name="l00056"></a>00056 <span class="comment">// The print function that we push into Lua replacing &quot;print(...)&quot;</span>
  86. <a name="l00057"></a>00057 <span class="comment">// to generate wxLuaEvent(wxEVT_LUA_PRINT, ...)</span>
  87. <a name="l00058"></a>00058 <span class="comment">// Code copied from Lua&#39;s luaB_print() function in lbaselib.c</span>
  88. <a name="l00059"></a><a class="code" href="wxlstate_8cpp.html#a7c982cbdf126f61b0868fe40baf150c4">00059</a> <span class="keywordtype">int</span> LUACALL <a class="code" href="wxlstate_8cpp.html#a7c982cbdf126f61b0868fe40baf150c4">wxlua_printFunction</a>( lua_State *L )
  89. <a name="l00060"></a>00060 {
  90. <a name="l00061"></a>00061 <a class="code" href="classwxLuaState.html">wxLuaState</a> wxlState(L); <span class="comment">// doesn&#39;t have to be ok</span>
  91. <a name="l00062"></a>00062
  92. <a name="l00063"></a>00063 wxString msg;
  93. <a name="l00064"></a>00064 <span class="keywordtype">int</span> i, n = lua_gettop(L);
  94. <a name="l00065"></a>00065
  95. <a name="l00066"></a>00066 <span class="comment">// Use the Lua tostring() function to print them as Lua would</span>
  96. <a name="l00067"></a>00067 lua_getglobal(L, <span class="stringliteral">&quot;tostring&quot;</span>);
  97. <a name="l00068"></a>00068
  98. <a name="l00069"></a>00069 <span class="keywordflow">if</span> (!lua_isfunction(L, -1))
  99. <a name="l00070"></a>00070 {
  100. <a name="l00071"></a>00071 <span class="comment">// This code is also used in wxledit.cpp, wxLuaShell::RunString()</span>
  101. <a name="l00072"></a>00072 msg = wxT(<span class="stringliteral">&quot;wxLua ERROR: Unable to print() without the tostring() function. Did you remove it?&quot;</span>);
  102. <a name="l00073"></a>00073 lua_pop(L, 1); <span class="comment">// pop the nil or whatever replaced tostring()</span>
  103. <a name="l00074"></a>00074 n = 0; <span class="comment">// don&#39;t let for loop run</span>
  104. <a name="l00075"></a>00075 }
  105. <a name="l00076"></a>00076
  106. <a name="l00077"></a>00077 <span class="keywordflow">for</span> (i = 1; i &lt;= n; ++i)
  107. <a name="l00078"></a>00078 {
  108. <a name="l00079"></a>00079 <span class="keywordflow">if</span> (i &gt; 1) msg.Append(wxT(<span class="stringliteral">&quot;\t&quot;</span>)); <span class="comment">// Lua uses a tab too in luaB_print</span>
  109. <a name="l00080"></a>00080
  110. <a name="l00081"></a>00081 <span class="keyword">const</span> <span class="keywordtype">char</span> *s;
  111. <a name="l00082"></a>00082 lua_pushvalue(L, -1); <span class="comment">/* function to be called */</span>
  112. <a name="l00083"></a>00083 lua_pushvalue(L, i); <span class="comment">/* value to print */</span>
  113. <a name="l00084"></a>00084 lua_call(L, 1, 1);
  114. <a name="l00085"></a>00085 s = lua_tostring(L, -1); <span class="comment">/* get result */</span>
  115. <a name="l00086"></a>00086 <span class="keywordflow">if</span> (s == NULL)
  116. <a name="l00087"></a>00087 {
  117. <a name="l00088"></a>00088 <span class="comment">// FIXME use wxlua_error here? right now wxlua_error doesn&#39;t do anything special - JL</span>
  118. <a name="l00089"></a>00089 <span class="keywordflow">return</span> luaL_error(L, LUA_QL(<span class="stringliteral">&quot;tostring&quot;</span>) <span class="stringliteral">&quot; must return a string to &quot;</span>
  119. <a name="l00090"></a>00090 LUA_QL(<span class="stringliteral">&quot;print&quot;</span>));
  120. <a name="l00091"></a>00091 }
  121. <a name="l00092"></a>00092 msg += <a class="code" href="wxlstate_8h.html#aaa3dbc7e667edd1e7409725fe338f4e8">lua2wx</a>(s);
  122. <a name="l00093"></a>00093 lua_pop(L, 1); <span class="comment">/* pop result */</span>
  123. <a name="l00094"></a>00094 }
  124. <a name="l00095"></a>00095
  125. <a name="l00096"></a>00096 <span class="keywordflow">if</span> (!msg.IsEmpty() &amp;&amp; wxlState.<a class="code" href="classwxLuaState.html#a0a2d167b9d6d1259066d9aa771ad075e">Ok</a>())
  126. <a name="l00097"></a>00097 {
  127. <a name="l00098"></a>00098 <a class="code" href="classwxLuaEvent.html">wxLuaEvent</a> event(wxEVT_LUA_PRINT, wxlState.<a class="code" href="classwxLuaState.html#a92715872ca9f5f4b6e77344410963350">GetId</a>(), wxlState);
  128. <a name="l00099"></a>00099 <span class="keyword">event</span>.SetString(msg);
  129. <a name="l00100"></a>00100 wxlState.<a class="code" href="classwxLuaState.html#a1f0e0a065e8c3ea25c0ebe5c7258f7ae">SendEvent</a>(event);
  130. <a name="l00101"></a>00101 }
  131. <a name="l00102"></a>00102 <span class="keywordflow">else</span> <span class="keywordflow">if</span> (!msg.IsEmpty())
  132. <a name="l00103"></a>00103 wxPrintf(wxT(<span class="stringliteral">&quot;%s\n&quot;</span>), msg.c_str()); <span class="comment">// Lua puts a \n too</span>
  133. <a name="l00104"></a>00104
  134. <a name="l00105"></a>00105 <span class="keywordflow">return</span> 0; <span class="comment">// no items put onto stack</span>
  135. <a name="l00106"></a>00106 }
  136. <a name="l00107"></a>00107
  137. <a name="l00108"></a><a class="code" href="wxlstate_8cpp.html#a9eab35a72e3a0d8e1a669fb831a5272d">00108</a> <span class="keywordtype">void</span> LUACALL <a class="code" href="wxlstate_8cpp.html#a9eab35a72e3a0d8e1a669fb831a5272d">wxlua_debugHookFunction</a>(lua_State *L, lua_Debug *LDebug)
  138. <a name="l00109"></a>00109 {
  139. <a name="l00110"></a>00110 <span class="comment">// NULL when shutting down.</span>
  140. <a name="l00111"></a>00111 <a class="code" href="classwxLuaStateData.html">wxLuaStateData</a>* wxlStateData = <a class="code" href="wxlstate_8h.html#a4c7701b4b056e2f84f380be559bb2482">wxlua_getwxluastatedata</a>(L);
  141. <a name="l00112"></a>00112 <span class="keywordflow">if</span> (!wxlStateData) <span class="keywordflow">return</span>;
  142. <a name="l00113"></a>00113
  143. <a name="l00114"></a>00114 <span class="comment">// FIXME - for select event types we don&#39;t want to do anything</span>
  144. <a name="l00115"></a>00115 wxEventType evtType = <a class="code" href="wxlstate_8h.html#a5442b3e5a2f8dca8fb920c00279385ac">wxlua_getwxeventtype</a>(L);
  145. <a name="l00116"></a>00116 <span class="keywordflow">if</span> ((evtType != wxEVT_NULL))
  146. <a name="l00117"></a>00117 <span class="comment">//(evtType == wxEVT_IDLE) &amp;&amp; (evtType == wxEVT_PAINT) &amp;&amp;</span>
  147. <a name="l00118"></a>00118 <span class="comment">//(evtType == wxEVT_DESTROY) &amp;&amp; (evtType == wxEVT_CLOSE_WINDOW))</span>
  148. <a name="l00119"></a>00119 <span class="keywordflow">return</span>;
  149. <a name="l00120"></a>00120
  150. <a name="l00121"></a>00121 <span class="comment">// they want to break the program, restore old debug hook, then error out</span>
  151. <a name="l00122"></a>00122 <span class="keywordflow">if</span> (wxlStateData-&gt;<a class="code" href="classwxLuaStateData.html#abb85e04d6c6dd5a5b31ec55c3c3fdd8f">m_debug_hook_break</a>)
  152. <a name="l00123"></a>00123 {
  153. <a name="l00124"></a>00124 <span class="comment">// It&#39;s ok that we get the wxLuaState here since we&#39;re stopping anyway.</span>
  154. <a name="l00125"></a>00125 <a class="code" href="classwxLuaState.html">wxLuaState</a> wxlState(L);
  155. <a name="l00126"></a>00126
  156. <a name="l00127"></a>00127 <span class="comment">// restore hook to previous state</span>
  157. <a name="l00128"></a>00128 wxlState.<a class="code" href="classwxLuaState.html#a61b2c790f1b50df9c8b41ef6cc8e58e9">ClearDebugHookBreak</a>();
  158. <a name="l00129"></a>00129 <a class="code" href="wxlstate_8h.html#a1abe07e896f1e813fc28da0e0d92961a">wxlua_error</a>(L, wxlStateData-&gt;<a class="code" href="classwxLuaStateData.html#a375859ee7c4407fb3c5588b39705de2d">m_debug_hook_break_msg</a>);
  159. <a name="l00130"></a>00130 <span class="keywordflow">return</span>;
  160. <a name="l00131"></a>00131 }
  161. <a name="l00132"></a>00132
  162. <a name="l00133"></a>00133 <span class="comment">// Assume they&#39;ve set the wxEvtHandler, ok if not, but it wouldn&#39;t make sense</span>
  163. <a name="l00134"></a>00134 <span class="comment">// We use wxLuaState::SendEvent() because it sets wxEvent::SetEventObject() for us.</span>
  164. <a name="l00135"></a>00135 <span class="keywordflow">if</span> (wxlStateData-&gt;<a class="code" href="classwxLuaStateData.html#adee2d0bdd61585268fc43c18bd0d1116">m_lua_debug_hook_send_evt</a>)
  165. <a name="l00136"></a>00136 {
  166. <a name="l00137"></a>00137 <a class="code" href="classwxLuaState.html">wxLuaState</a> wxlState(L);
  167. <a name="l00138"></a>00138
  168. <a name="l00139"></a>00139 <span class="keywordtype">int</span> ret = 0;
  169. <a name="l00140"></a>00140 ret = lua_getinfo(L, <span class="stringliteral">&quot;l&quot;</span>, LDebug); <span class="comment">// line (ldebug.currentline)</span>
  170. <a name="l00141"></a>00141
  171. <a name="l00142"></a>00142 <a class="code" href="classwxLuaEvent.html">wxLuaEvent</a> event(wxEVT_LUA_DEBUG_HOOK, wxlState.<a class="code" href="classwxLuaState.html#a92715872ca9f5f4b6e77344410963350">GetId</a>(), wxlState);
  172. <a name="l00143"></a>00143 <span class="keyword">event</span>.m_lua_Debug = LDebug;
  173. <a name="l00144"></a>00144 <span class="keyword">event</span>.SetInt(LDebug-&gt;currentline);
  174. <a name="l00145"></a>00145 wxlState.<a class="code" href="classwxLuaState.html#a1f0e0a065e8c3ea25c0ebe5c7258f7ae">SendEvent</a>( event );
  175. <a name="l00146"></a>00146 <span class="keywordflow">if</span> (event.m_debug_hook_break)
  176. <a name="l00147"></a>00147 wxlState.<a class="code" href="classwxLuaState.html#a4925fb35a1622f81fc9c5dc5820e27d8">wxlua_Error</a>(<span class="stringliteral">&quot;Lua interpreter stopped.&quot;</span>);
  177. <a name="l00148"></a>00148 }
  178. <a name="l00149"></a>00149
  179. <a name="l00150"></a>00150 <span class="comment">// Try to yield *after* sending event to allow C++ gui update</span>
  180. <a name="l00151"></a>00151 <span class="keywordflow">if</span> (wxlStateData-&gt;<a class="code" href="classwxLuaStateData.html#a16aa632cff2de6c2ee2e2cab9a03b9a1">m_lua_debug_hook_yield</a> &gt; 0)
  181. <a name="l00152"></a>00152 {
  182. <a name="l00153"></a>00153 <span class="comment">// yield based on number of ms passed NOT every hook event</span>
  183. <a name="l00154"></a>00154 <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> last_time = wxlStateData-&gt;<a class="code" href="classwxLuaStateData.html#aa7500e45a81ca35fc3fe1b66c2aa38fd">m_last_debug_hook_time</a>;
  184. <a name="l00155"></a>00155 <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> cur_time = wxGetLocalTimeMillis().GetLo();
  185. <a name="l00156"></a>00156
  186. <a name="l00157"></a>00157 <span class="keywordflow">if</span> ((cur_time &gt; last_time + wxlStateData-&gt;<a class="code" href="classwxLuaStateData.html#a16aa632cff2de6c2ee2e2cab9a03b9a1">m_lua_debug_hook_yield</a>) ||
  187. <a name="l00158"></a>00158 (cur_time &lt; last_time)) <span class="comment">// wrapped</span>
  188. <a name="l00159"></a>00159 {
  189. <a name="l00160"></a>00160 wxlStateData-&gt;<a class="code" href="classwxLuaStateData.html#aa7500e45a81ca35fc3fe1b66c2aa38fd">m_last_debug_hook_time</a> = cur_time;
  190. <a name="l00161"></a>00161
  191. <a name="l00162"></a>00162 <span class="keywordtype">bool</span> painting = (evtType == wxEVT_PAINT);
  192. <a name="l00163"></a>00163
  193. <a name="l00164"></a>00164 <span class="comment">/*</span>
  194. <a name="l00165"></a>00165 <span class="comment"> wxLongToLongHashMap::iterator it;</span>
  195. <a name="l00166"></a>00166 <span class="comment"> wxLongToLongHashMap* hashMap = &amp;wxlState.GetLuaStateRefData()-&gt;m_wxlStateData-&gt;m_trackedObjects;</span>
  196. <a name="l00167"></a>00167 <span class="comment"> for (it = hashMap-&gt;begin(); it != hashMap-&gt;end(); ++it)</span>
  197. <a name="l00168"></a>00168 <span class="comment"> {</span>
  198. <a name="l00169"></a>00169 <span class="comment"> wxObject* obj = (wxObject*)it-&gt;second;</span>
  199. <a name="l00170"></a>00170 <span class="comment"> if (obj &amp;&amp; wxDynamicCast(obj, wxPaintDC))</span>
  200. <a name="l00171"></a>00171 <span class="comment"> {</span>
  201. <a name="l00172"></a>00172 <span class="comment"> painting = true;</span>
  202. <a name="l00173"></a>00173 <span class="comment"> break;</span>
  203. <a name="l00174"></a>00174 <span class="comment"> }</span>
  204. <a name="l00175"></a>00175 <span class="comment"> }</span>
  205. <a name="l00176"></a>00176 <span class="comment">*/</span>
  206. <a name="l00177"></a>00177
  207. <a name="l00178"></a>00178 <span class="keywordflow">if</span> (!painting)
  208. <a name="l00179"></a>00179 wxYield(); <span class="comment">//IfNeeded();</span>
  209. <a name="l00180"></a>00180 }
  210. <a name="l00181"></a>00181 }
  211. <a name="l00182"></a>00182 }
  212. <a name="l00183"></a>00183
  213. <a name="l00184"></a>00184 <span class="comment">// ----------------------------------------------------------------------------</span>
  214. <a name="l00185"></a>00185
  215. <a name="l00186"></a><a class="code" href="wxlstate_8cpp.html#a7185a0c6fb640503f4921f15811ff4ac">00186</a> <span class="keywordtype">void</span> <a class="code" href="wxlstate_8h.html#a78fe2540c7e61145be1372073f0c37f7">wxlua_lreg_createtable</a>(lua_State* L, <span class="keywordtype">void</span>* lightuserdata_reg_key, <span class="keywordtype">int</span> narr, <span class="keywordtype">int</span> nrec)
  216. <a name="l00187"></a>00187 {
  217. <a name="l00188"></a>00188 <span class="comment">// clear the old ref to the table, even though it&#39;s weak kv</span>
  218. <a name="l00189"></a>00189 <span class="comment">// it doesn&#39;t get cleared until the gc runs</span>
  219. <a name="l00190"></a>00190 lua_pushlightuserdata(L, &amp;<a class="code" href="wxlstate_8h.html#ab84cb2515893ab069d7d165208e68aad">wxlua_lreg_regtable_key</a>); <span class="comment">// push key</span>
  220. <a name="l00191"></a>00191 lua_rawget(L, LUA_REGISTRYINDEX); <span class="comment">// get table</span>
  221. <a name="l00192"></a>00192 lua_pushlightuserdata(L, lightuserdata_reg_key); <span class="comment">// push key</span>
  222. <a name="l00193"></a>00193 lua_rawget(L, LUA_REGISTRYINDEX); <span class="comment">// get table or nil</span>
  223. <a name="l00194"></a>00194 <span class="keywordflow">if</span> (lua_istable(L, -1))
  224. <a name="l00195"></a>00195 {
  225. <a name="l00196"></a>00196 lua_pushnil(L); <span class="comment">// push value</span>
  226. <a name="l00197"></a>00197 lua_rawset(L, -3); <span class="comment">// clear t[key] = nil</span>
  227. <a name="l00198"></a>00198 lua_pop(L, 1); <span class="comment">// pop wxlua_lreg_regtable_key table</span>
  228. <a name="l00199"></a>00199 }
  229. <a name="l00200"></a>00200 <span class="keywordflow">else</span>
  230. <a name="l00201"></a>00201 lua_pop(L, 2); <span class="comment">// pop nil and wxlua_lreg_regtable_key table</span>
  231. <a name="l00202"></a>00202
  232. <a name="l00203"></a>00203 <span class="comment">// Add new LUA_REGISTRYINDEX[&amp;wxlua_lreg_regtable_key][lightuserdata_reg_key table] = lightuserdata_reg_key</span>
  233. <a name="l00204"></a>00204 lua_pushlightuserdata(L, lightuserdata_reg_key); <span class="comment">// push key</span>
  234. <a name="l00205"></a>00205 lua_createtable(L, narr, nrec); <span class="comment">// push value</span>
  235. <a name="l00206"></a>00206 lua_pushlightuserdata(L, &amp;<a class="code" href="wxlstate_8h.html#ab84cb2515893ab069d7d165208e68aad">wxlua_lreg_regtable_key</a>); <span class="comment">// push key</span>
  236. <a name="l00207"></a>00207 lua_rawget(L, LUA_REGISTRYINDEX); <span class="comment">// get wxlua_lreg_regtable_key table</span>
  237. <a name="l00208"></a>00208 lua_pushvalue(L, -2); <span class="comment">// push key (copy of the new table)</span>
  238. <a name="l00209"></a>00209 lua_pushvalue(L, -4); <span class="comment">// push value (copy of lightuserdata key)</span>
  239. <a name="l00210"></a>00210 lua_rawset(L, -3); <span class="comment">// set t[key] = value; pops key and value</span>
  240. <a name="l00211"></a>00211 lua_pop(L, 1); <span class="comment">// pop wxlua_lreg_regtable_key table</span>
  241. <a name="l00212"></a>00212 lua_rawset(L, LUA_REGISTRYINDEX); <span class="comment">// set the value</span>
  242. <a name="l00213"></a>00213 }
  243. <a name="l00214"></a>00214
  244. <a name="l00215"></a>00215 <span class="comment">// ----------------------------------------------------------------------------</span>
  245. <a name="l00216"></a>00216 <span class="comment">// Lua helper functions</span>
  246. <a name="l00217"></a>00217 <span class="comment">// ----------------------------------------------------------------------------</span>
  247. <a name="l00218"></a>00218
  248. <a name="l00219"></a><a class="code" href="wxlstate_8cpp.html#ab2ae5dfea9cc867ed46ed094ce4e768f">00219</a> wxString <a class="code" href="wxlstate_8h.html#ab2ae5dfea9cc867ed46ed094ce4e768f">wxlua_LUA_ERR_msg</a>(<span class="keywordtype">int</span> LUA_ERRx)
  249. <a name="l00220"></a>00220 {
  250. <a name="l00221"></a>00221 <span class="keywordflow">switch</span> (LUA_ERRx)
  251. <a name="l00222"></a>00222 {
  252. <a name="l00223"></a>00223 <span class="keywordflow">case</span> 0 : <span class="keywordflow">return</span> wxEmptyString;
  253. <a name="l00224"></a>00224 <span class="keywordflow">case</span> LUA_YIELD : <span class="keywordflow">return</span> wxT(<span class="stringliteral">&quot;Lua: Thread is suspended&quot;</span>);
  254. <a name="l00225"></a>00225 <span class="keywordflow">case</span> LUA_ERRRUN : <span class="keywordflow">return</span> wxT(<span class="stringliteral">&quot;Lua: Error while running chunk&quot;</span>);
  255. <a name="l00226"></a>00226 <span class="keywordflow">case</span> LUA_ERRSYNTAX : <span class="keywordflow">return</span> wxT(<span class="stringliteral">&quot;Lua: Syntax error during pre-compilation&quot;</span>);
  256. <a name="l00227"></a>00227 <span class="keywordflow">case</span> LUA_ERRMEM : <span class="keywordflow">return</span> wxT(<span class="stringliteral">&quot;Lua: Memory allocation error&quot;</span>);
  257. <a name="l00228"></a>00228 <span class="keywordflow">case</span> LUA_ERRERR : <span class="keywordflow">return</span> wxT(<span class="stringliteral">&quot;Lua: Generic error or an error occurred while running the error handler&quot;</span>);
  258. <a name="l00229"></a>00229 <span class="keywordflow">case</span> LUA_ERRFILE : <span class="keywordflow">return</span> wxT(<span class="stringliteral">&quot;Lua: Error occurred while opening file&quot;</span>);
  259. <a name="l00230"></a>00230 }
  260. <a name="l00231"></a>00231
  261. <a name="l00232"></a>00232 <span class="keywordflow">return</span> wxT(<span class="stringliteral">&quot;Lua: Unknown LUA_ERRx error value&quot;</span>);
  262. <a name="l00233"></a>00233 }
  263. <a name="l00234"></a>00234
  264. <a name="l00235"></a><a class="code" href="wxlstate_8cpp.html#a0c9eeb57ad65f8e174030f03af466331">00235</a> <span class="keywordtype">bool</span> <a class="code" href="wxlstate_8h.html#a34d4dc64c6615a3e7c4fea5f6fb5840f">wxlua_errorinfo</a>(lua_State* L, <span class="keywordtype">int</span> status, <span class="keywordtype">int</span> top, wxString* errorMsg_, <span class="keywordtype">int</span>* line_num_)
  265. <a name="l00236"></a>00236 {
  266. <a name="l00237"></a>00237 <span class="keywordflow">if</span> (status == 0)
  267. <a name="l00238"></a>00238 <span class="keywordflow">return</span> <span class="keyword">false</span>;
  268. <a name="l00239"></a>00239
  269. <a name="l00240"></a>00240 <span class="keywordtype">int</span> newtop = lua_gettop(L);
  270. <a name="l00241"></a>00241
  271. <a name="l00242"></a>00242 wxString errorMsg = <a class="code" href="wxlstate_8h.html#ab2ae5dfea9cc867ed46ed094ce4e768f">wxlua_LUA_ERR_msg</a>(status);
  272. <a name="l00243"></a>00243
  273. <a name="l00244"></a>00244 <span class="keywordflow">switch</span>(status)
  274. <a name="l00245"></a>00245 {
  275. <a name="l00246"></a>00246 <span class="keywordflow">case</span> LUA_ERRMEM:
  276. <a name="l00247"></a>00247 <span class="keywordflow">case</span> LUA_ERRERR:
  277. <a name="l00248"></a>00248 {
  278. <a name="l00249"></a>00249 <span class="keywordflow">if</span> (newtop &gt; top)
  279. <a name="l00250"></a>00250 errorMsg += wxT(<span class="stringliteral">&quot;\n&quot;</span>);
  280. <a name="l00251"></a>00251 <span class="keywordflow">break</span>;
  281. <a name="l00252"></a>00252 }
  282. <a name="l00253"></a>00253 <span class="keywordflow">case</span> LUA_ERRRUN:
  283. <a name="l00254"></a>00254 <span class="keywordflow">case</span> LUA_ERRFILE:
  284. <a name="l00255"></a>00255 <span class="keywordflow">case</span> LUA_ERRSYNTAX:
  285. <a name="l00256"></a>00256 <span class="keywordflow">default</span>:
  286. <a name="l00257"></a>00257 {
  287. <a name="l00258"></a>00258 <span class="keywordflow">if</span> (newtop &gt; top)
  288. <a name="l00259"></a>00259 errorMsg += wxT(<span class="stringliteral">&quot;\n&quot;</span>) + <a class="code" href="wxlstate_8h.html#aaa3dbc7e667edd1e7409725fe338f4e8">lua2wx</a>(lua_tostring(L, -1));
  289. <a name="l00260"></a>00260 <span class="keywordflow">break</span>;
  290. <a name="l00261"></a>00261 }
  291. <a name="l00262"></a>00262 }
  292. <a name="l00263"></a>00263
  293. <a name="l00264"></a>00264 errorMsg += wxT(<span class="stringliteral">&quot;\n&quot;</span>);
  294. <a name="l00265"></a>00265
  295. <a name="l00266"></a>00266 <span class="comment">// Why can&#39;t I fill a lua_Debug here? Try to get the line number</span>
  296. <a name="l00267"></a>00267 <span class="comment">// by parsing the error message that looks like this, 3 is linenumber</span>
  297. <a name="l00268"></a>00268 <span class="comment">// [string &quot;a = 1(&quot;]:3: unexpected symbol near `&lt;eof&gt;&#39;</span>
  298. <a name="l00269"></a>00269 wxString lineStr = errorMsg;
  299. <a name="l00270"></a>00270 <span class="keywordtype">long</span> line_num = -1;
  300. <a name="l00271"></a>00271 <span class="keywordflow">while</span>(!lineStr.IsEmpty())
  301. <a name="l00272"></a>00272 {
  302. <a name="l00273"></a>00273 <span class="comment">// search through the str to find &#39;]:LONG:&#39; pattern</span>
  303. <a name="l00274"></a>00274 lineStr = lineStr.AfterFirst(wxT(<span class="charliteral">&#39;]&#39;</span>));
  304. <a name="l00275"></a>00275 <span class="keywordflow">if</span> ((lineStr.Length() &gt; 0) &amp;&amp; (lineStr.GetChar(0) == wxT(<span class="charliteral">&#39;:&#39;</span>)))
  305. <a name="l00276"></a>00276 {
  306. <a name="l00277"></a>00277 lineStr = lineStr.AfterFirst(wxT(<span class="charliteral">&#39;:&#39;</span>));
  307. <a name="l00278"></a>00278 <span class="keywordflow">if</span> (lineStr.IsEmpty() || lineStr.BeforeFirst(wxT(<span class="charliteral">&#39;:&#39;</span>)).ToLong(&amp;line_num))
  308. <a name="l00279"></a>00279 <span class="keywordflow">break</span>;
  309. <a name="l00280"></a>00280 }
  310. <a name="l00281"></a>00281 }
  311. <a name="l00282"></a>00282
  312. <a name="l00283"></a>00283 lua_settop(L, top); <span class="comment">// pops the message if any</span>
  313. <a name="l00284"></a>00284
  314. <a name="l00285"></a>00285 <span class="keywordflow">if</span> (errorMsg_) *errorMsg_ = errorMsg;
  315. <a name="l00286"></a>00286 <span class="keywordflow">if</span> (line_num_) *line_num_ = (int)line_num;
  316. <a name="l00287"></a>00287
  317. <a name="l00288"></a>00288 <span class="keywordflow">return</span> <span class="keyword">true</span>;
  318. <a name="l00289"></a>00289 }
  319. <a name="l00290"></a>00290
  320. <a name="l00291"></a><a class="code" href="wxlstate_8cpp.html#a1abe07e896f1e813fc28da0e0d92961a">00291</a> <span class="keywordtype">void</span> LUACALL <a class="code" href="wxlstate_8h.html#a1abe07e896f1e813fc28da0e0d92961a">wxlua_error</a>(lua_State *L, <span class="keyword">const</span> <span class="keywordtype">char</span> *errorMsg)
  321. <a name="l00292"></a>00292 {
  322. <a name="l00293"></a>00293 <span class="comment">// Use luaL_error(L, s) and not &quot;lua_pushstring(L, s); lua_error(L)&quot; since</span>
  323. <a name="l00294"></a>00294 <span class="comment">// luaL_error() provides the file and line number too.</span>
  324. <a name="l00295"></a>00295 luaL_error(L, <span class="stringliteral">&quot;%s&quot;</span>, errorMsg);
  325. <a name="l00296"></a>00296 }
  326. <a name="l00297"></a>00297
  327. <a name="l00298"></a><a class="code" href="wxlstate_8cpp.html#aba05dfc5f8a48e264d7463a7c6fee965">00298</a> <span class="keywordtype">void</span> LUACALL <a class="code" href="wxlstate_8h.html#aba05dfc5f8a48e264d7463a7c6fee965">wxlua_argerror</a>(lua_State *L, <span class="keywordtype">int</span> stack_idx, <span class="keyword">const</span> wxString&amp; expectedType)
  328. <a name="l00299"></a>00299 {
  329. <a name="l00300"></a>00300 wxString argType = <a class="code" href="wxlstate_8h.html#acf6d708626f3c7194d1f2aebb7a73b6e">wxlua_luaL_typename</a>(L, stack_idx);
  330. <a name="l00301"></a>00301
  331. <a name="l00302"></a>00302 wxString msg(wxString::Format(_(<span class="stringliteral">&quot;wxLua: Expected %s for parameter %d, but got a &#39;%s&#39;.&quot;</span>),
  332. <a name="l00303"></a>00303 expectedType.c_str(), stack_idx, argType.c_str()));
  333. <a name="l00304"></a>00304
  334. <a name="l00305"></a>00305 <a class="code" href="wxlstate_8h.html#a34376827e7b3466173127fba976217d0">wxlua_argerrormsg</a>(L, msg);
  335. <a name="l00306"></a>00306 }
  336. <a name="l00307"></a>00307
  337. <a name="l00308"></a><a class="code" href="wxlstate_8cpp.html#aa4db1ed30ef56e312dcf5e9cca75bcb2">00308</a> <span class="keywordtype">void</span> LUACALL <a class="code" href="wxlstate_8h.html#a34376827e7b3466173127fba976217d0">wxlua_argerrormsg</a>(lua_State *L, <span class="keyword">const</span> wxString&amp; msg_)
  338. <a name="l00309"></a>00309 {
  339. <a name="l00310"></a>00310 wxString funcArgs(wxT(<span class="stringliteral">&quot;\n&quot;</span>));
  340. <a name="l00311"></a>00311 wxString argMsg = <a class="code" href="wxlbind_8h.html#a15f78fa970b76c7963c3e11f90d988ca">wxlua_getLuaArgsMsg</a>(L, 1, lua_gettop(L));
  341. <a name="l00312"></a>00312
  342. <a name="l00313"></a>00313 <a class="code" href="structwxLuaBindMethod.html">wxLuaBindMethod</a>* wxlMethod = (<a class="code" href="structwxLuaBindMethod.html">wxLuaBindMethod</a> *)lua_touserdata(L, lua_upvalueindex(1)); <span class="comment">// lightuserdata</span>
  343. <a name="l00314"></a>00314 <span class="keywordflow">if</span> (wxlMethod != NULL)
  344. <a name="l00315"></a>00315 {
  345. <a name="l00316"></a>00316 <span class="comment">// Guarantee that this is a wxLuaBindMethod of ours so we don&#39;t crash.</span>
  346. <a name="l00317"></a>00317 <span class="comment">// Since we&#39;re going to error out we don&#39;t have to be quick about it.</span>
  347. <a name="l00318"></a>00318
  348. <a name="l00319"></a>00319 <span class="comment">// check if this method is part of a class</span>
  349. <a name="l00320"></a>00320 <span class="keyword">const</span> <a class="code" href="structwxLuaBindClass.html">wxLuaBindClass</a>* wxlClass = <a class="code" href="classwxLuaBinding.html#a7ee5e4f66198a0ef9e8cc4c2a6d0318e">wxLuaBinding::FindBindClass</a>(wxlMethod);
  350. <a name="l00321"></a>00321
  351. <a name="l00322"></a>00322 <span class="comment">// if not, check if it&#39;s a global C style function</span>
  352. <a name="l00323"></a>00323 <a class="code" href="classwxLuaBinding.html">wxLuaBinding</a>* binding = NULL;
  353. <a name="l00324"></a>00324 <span class="keywordflow">if</span> (wxlClass == NULL)
  354. <a name="l00325"></a>00325 binding = <a class="code" href="classwxLuaBinding.html#a463d056fe9bcb54f0408a8ae523ba64e">wxLuaBinding::FindMethodBinding</a>(wxlMethod);
  355. <a name="l00326"></a>00326
  356. <a name="l00327"></a>00327 <span class="keywordflow">if</span> ((wxlClass != NULL) || (binding != NULL))
  357. <a name="l00328"></a>00328 funcArgs += <a class="code" href="wxlbind_8h.html#a0cf1f6600d0f80e2135c6788a4afc39f">wxlua_getBindMethodArgsMsg</a>(L, wxlMethod);
  358. <a name="l00329"></a>00329 }
  359. <a name="l00330"></a>00330
  360. <a name="l00331"></a>00331 wxString msg = msg_ + wxT(<span class="stringliteral">&quot;\nFunction called: &#39;&quot;</span>) + argMsg + wxT(<span class="stringliteral">&quot;&#39;&quot;</span>) + funcArgs;
  361. <a name="l00332"></a>00332 <a class="code" href="wxlstate_8h.html#a1abe07e896f1e813fc28da0e0d92961a">wxlua_error</a>(L, msg);
  362. <a name="l00333"></a>00333 }
  363. <a name="l00334"></a>00334
  364. <a name="l00335"></a><a class="code" href="wxlstate_8cpp.html#adde66f46b163a0fc59e09904cf355792">00335</a> <span class="keywordtype">void</span>* LUACALL <a class="code" href="wxlstate_8h.html#ac5a0a65665274df59adbed667480e93e">wxlua_touserdata</a>(lua_State *L, <span class="keywordtype">int</span> stack_idx, <span class="keywordtype">bool</span> null_ptr <span class="comment">/*= false*/</span>)
  365. <a name="l00336"></a>00336 {
  366. <a name="l00337"></a>00337 <span class="keywordtype">void</span> *pdata = NULL;
  367. <a name="l00338"></a>00338 <span class="keywordtype">void</span> **ptr = (<span class="keywordtype">void</span> **)lua_touserdata(L, stack_idx);
  368. <a name="l00339"></a>00339
  369. <a name="l00340"></a>00340 <span class="keywordflow">if</span> (ptr != NULL)
  370. <a name="l00341"></a>00341 {
  371. <a name="l00342"></a>00342 pdata = *ptr; <span class="comment">// get the pointer the userdata holds</span>
  372. <a name="l00343"></a>00343 <span class="keywordflow">if</span> (null_ptr) <span class="comment">// NULL ptr so Lua won&#39;t try to gc it</span>
  373. <a name="l00344"></a>00344 *ptr = NULL;
  374. <a name="l00345"></a>00345 }
  375. <a name="l00346"></a>00346
  376. <a name="l00347"></a>00347 <span class="keywordflow">return</span> pdata;
  377. <a name="l00348"></a>00348 }
  378. <a name="l00349"></a>00349
  379. <a name="l00350"></a>00350 <span class="comment">// ----------------------------------------------------------------------------</span>
  380. <a name="l00351"></a>00351 <span class="comment">// wxluaR_XXX - functions operate on tables in Lua&#39;s LUA_REGISTRYINDEX</span>
  381. <a name="l00352"></a>00352 <span class="comment">// ----------------------------------------------------------------------------</span>
  382. <a name="l00353"></a>00353
  383. <a name="l00354"></a><a class="code" href="wxlstate_8cpp.html#a08525c218f0a90e660c0f816906da29e">00354</a> <span class="preprocessor">#define ABS_LUA_STKIDX(n, added_items) ((n) &gt; 0 ? (n) : (n)-(added_items))</span>
  384. <a name="l00355"></a>00355 <span class="preprocessor"></span>
  385. <a name="l00356"></a>00356 <span class="comment">// Note about luaL_ref() and luaL_unref().</span>
  386. <a name="l00357"></a>00357 <span class="comment">// ref creates integer numbers from 1 to ...</span>
  387. <a name="l00358"></a>00358 <span class="comment">// unref uses t[0] to hold the last unused reference and when you call unref</span>
  388. <a name="l00359"></a>00359 <span class="comment">// again the next unused ref points back to the first and t[0] points to the</span>
  389. <a name="l00360"></a>00360 <span class="comment">// last unrefed key.</span>
  390. <a name="l00361"></a>00361 <span class="comment">// eg. create 5 refs, get refs 1,2,3,4,5, then call unref on 3 then 4 then</span>
  391. <a name="l00362"></a>00362 <span class="comment">// call ref 3 times and the new references will be 4, 3, 6</span>
  392. <a name="l00363"></a>00363
  393. <a name="l00364"></a><a class="code" href="wxlstate_8cpp.html#a0879809a008c75f8b26fb9ee326efdbd">00364</a> <span class="keywordtype">int</span> <a class="code" href="wxlstate_8h.html#acbf0a32ad4b828026bd7f643557646d0">wxluaR_ref</a>(lua_State* L, <span class="keywordtype">int</span> stack_idx, <span class="keywordtype">void</span>* lightuserdata_reg_key)
  394. <a name="l00365"></a>00365 {
  395. <a name="l00366"></a>00366 <span class="comment">// nothing on stack to insert and don&#39;t bother inserting nil</span>
  396. <a name="l00367"></a>00367 <span class="keywordflow">if</span> (lua_isnoneornil(L, stack_idx))
  397. <a name="l00368"></a>00368 <span class="keywordflow">return</span> LUA_REFNIL;
  398. <a name="l00369"></a>00369
  399. <a name="l00370"></a>00370 lua_pushlightuserdata(L, lightuserdata_reg_key); <span class="comment">// push key</span>
  400. <a name="l00371"></a>00371 lua_rawget(L, LUA_REGISTRYINDEX); <span class="comment">// pop key, push value (table)</span>
  401. <a name="l00372"></a>00372
  402. <a name="l00373"></a>00373 lua_pushvalue(L, <a class="code" href="wxlstate_8cpp.html#a08525c218f0a90e660c0f816906da29e">ABS_LUA_STKIDX</a>(stack_idx,1)); <span class="comment">// push value to store</span>
  403. <a name="l00374"></a>00374
  404. <a name="l00375"></a>00375 <span class="keywordtype">int</span> ref_idx = luaL_ref(L, -2); <span class="comment">// t[ref_idx] = value; pops value</span>
  405. <a name="l00376"></a>00376
  406. <a name="l00377"></a>00377 <span class="comment">// We also store t[value] = table_idx for this table for faster lookup</span>
  407. <a name="l00378"></a>00378 <span class="keywordflow">if</span> (lightuserdata_reg_key == &amp;<a class="code" href="wxlstate_8h.html#a30c2d5556a4a278368b48fe0b7bb3fda">wxlua_lreg_debug_refs_key</a>)
  408. <a name="l00379"></a>00379 {
  409. <a name="l00380"></a>00380 lua_pushvalue(L, <a class="code" href="wxlstate_8cpp.html#a08525c218f0a90e660c0f816906da29e">ABS_LUA_STKIDX</a>(stack_idx,1)); <span class="comment">// push key</span>
  410. <a name="l00381"></a>00381 lua_pushnumber(L, ref_idx); <span class="comment">// push value</span>
  411. <a name="l00382"></a>00382 lua_rawset(L, -3); <span class="comment">// set t[key] = value; pops key and value</span>
  412. <a name="l00383"></a>00383 }
  413. <a name="l00384"></a>00384
  414. <a name="l00385"></a>00385 lua_pop(L, 1); <span class="comment">// pop table</span>
  415. <a name="l00386"></a>00386
  416. <a name="l00387"></a>00387 <span class="keywordflow">return</span> ref_idx;
  417. <a name="l00388"></a>00388 }
  418. <a name="l00389"></a>00389
  419. <a name="l00390"></a><a class="code" href="wxlstate_8cpp.html#a78110a111c2f4e0a94231d344611ce20">00390</a> <span class="keywordtype">bool</span> <a class="code" href="wxlstate_8h.html#a0578d177f35b4dd05177067425410009">wxluaR_unref</a>(lua_State* L, <span class="keywordtype">int</span> ref_idx, <span class="keywordtype">void</span>* lightuserdata_reg_key)
  420. <a name="l00391"></a>00391 {
  421. <a name="l00392"></a>00392 <span class="keywordflow">if</span> (ref_idx == LUA_REFNIL) <span class="comment">// nothing to remove</span>
  422. <a name="l00393"></a>00393 <span class="keywordflow">return</span> <span class="keyword">false</span>;
  423. <a name="l00394"></a>00394
  424. <a name="l00395"></a>00395 lua_pushlightuserdata(L, lightuserdata_reg_key); <span class="comment">// push key</span>
  425. <a name="l00396"></a>00396 lua_rawget(L, LUA_REGISTRYINDEX); <span class="comment">// pop key, push value (table)</span>
  426. <a name="l00397"></a>00397
  427. <a name="l00398"></a>00398 <span class="comment">// Also remove the t[value] = table_idx for this table</span>
  428. <a name="l00399"></a>00399 <span class="keywordflow">if</span> (lightuserdata_reg_key == &amp;<a class="code" href="wxlstate_8h.html#a30c2d5556a4a278368b48fe0b7bb3fda">wxlua_lreg_debug_refs_key</a>)
  429. <a name="l00400"></a>00400 {
  430. <a name="l00401"></a>00401 lua_pushnumber(L, ref_idx); <span class="comment">// push key</span>
  431. <a name="l00402"></a>00402 lua_rawget(L, -2); <span class="comment">// get t[key] = value; pop key, push value;</span>
  432. <a name="l00403"></a>00403
  433. <a name="l00404"></a>00404 lua_pushnil(L);
  434. <a name="l00405"></a>00405 lua_rawset(L, -3); <span class="comment">// t[value] = nil; pops key and value</span>
  435. <a name="l00406"></a>00406 }
  436. <a name="l00407"></a>00407
  437. <a name="l00408"></a>00408 luaL_unref(L, -1, ref_idx); <span class="comment">// remove key and value in refs table</span>
  438. <a name="l00409"></a>00409 <span class="comment">// note: this key will be used for the next wxluaR_ref()</span>
  439. <a name="l00410"></a>00410
  440. <a name="l00411"></a>00411 lua_pop(L, 1); <span class="comment">// pop table</span>
  441. <a name="l00412"></a>00412
  442. <a name="l00413"></a>00413 <span class="keywordflow">return</span> <span class="keyword">true</span>;
  443. <a name="l00414"></a>00414 }
  444. <a name="l00415"></a>00415
  445. <a name="l00416"></a><a class="code" href="wxlstate_8cpp.html#a72d9705f9b5d46e0183888f4846d8e82">00416</a> <span class="keywordtype">bool</span> LUACALL <a class="code" href="wxlstate_8h.html#ae7108b396d79179f517a7dc478c2cb43">wxluaR_getref</a>(lua_State *L, <span class="keywordtype">int</span> ref_idx, <span class="keywordtype">void</span>* lightuserdata_reg_key)
  446. <a name="l00417"></a>00417 {
  447. <a name="l00418"></a>00418 <span class="keywordflow">if</span> (ref_idx == LUA_REFNIL) <span class="comment">// nothing to get</span>
  448. <a name="l00419"></a>00419 <span class="keywordflow">return</span> <span class="keyword">false</span>;
  449. <a name="l00420"></a>00420
  450. <a name="l00421"></a>00421 lua_pushlightuserdata(L, lightuserdata_reg_key); <span class="comment">// push key</span>
  451. <a name="l00422"></a>00422 lua_rawget(L, LUA_REGISTRYINDEX); <span class="comment">// pop key, push value (table)</span>
  452. <a name="l00423"></a>00423
  453. <a name="l00424"></a>00424 lua_rawgeti(L, -1, ref_idx); <span class="comment">// get t[ref_idx] = value; push value</span>
  454. <a name="l00425"></a>00425
  455. <a name="l00426"></a>00426 <span class="keywordflow">if</span> (lua_isnil(L, -1)) <span class="comment">// not a valid table key</span>
  456. <a name="l00427"></a>00427 {
  457. <a name="l00428"></a>00428 lua_pop(L, 2); <span class="comment">// pop nil and table</span>
  458. <a name="l00429"></a>00429

Large files files are truncated, but you can click here to view the full file