ext_c_api.html 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
  2. <html>
  3. <head>
  4. <title>Lua/C API Extensions</title>
  5. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  6. <meta name="Author" content="Mike Pall">
  7. <meta name="Copyright" content="Copyright (C) 2005-2010, Mike Pall">
  8. <meta name="Language" content="en">
  9. <link rel="stylesheet" type="text/css" href="bluequad.css" media="screen">
  10. <link rel="stylesheet" type="text/css" href="bluequad-print.css" media="print">
  11. </head>
  12. <body>
  13. <div id="site">
  14. <a href="http://luajit.org"><span>Lua<span id="logo">JIT</span></span></a>
  15. </div>
  16. <div id="head">
  17. <h1>Lua/C API Extensions</h1>
  18. </div>
  19. <div id="nav">
  20. <ul><li>
  21. <a href="luajit.html">LuaJIT</a>
  22. <ul><li>
  23. <a href="install.html">Installation</a>
  24. </li><li>
  25. <a href="running.html">Running</a>
  26. </li></ul>
  27. </li><li>
  28. <a href="extensions.html">Extensions</a>
  29. <ul><li>
  30. <a href="ext_jit.html">jit.* Library</a>
  31. </li><li>
  32. <a class="current" href="ext_c_api.html">Lua/C API</a>
  33. </li></ul>
  34. </li><li>
  35. <a href="status.html">Status</a>
  36. <ul><li>
  37. <a href="changes.html">Changes</a>
  38. </li></ul>
  39. </li><li>
  40. <a href="faq.html">FAQ</a>
  41. </li><li>
  42. <a href="http://luajit.org/performance.html">Performance <span class="ext">&raquo;</span></a>
  43. </li><li>
  44. <a href="http://luajit.org/download.html">Download <span class="ext">&raquo;</span></a>
  45. </li></ul>
  46. </div>
  47. <div id="main">
  48. <p>
  49. LuaJIT adds some extensions to the standard Lua/C API. The LuaJIT include
  50. directory must be in the compiler search path (<tt>-I<i>path</i></tt>)
  51. to be able to include the required header for C code:
  52. </p>
  53. <pre class="code">
  54. #include "luajit.h"
  55. </pre>
  56. <p>
  57. Or for C++ code:
  58. </p>
  59. <pre class="code">
  60. #include "lua.hpp"
  61. </pre>
  62. <h2 id="luaJIT_setmode"><tt>luaJIT_setmode(L, idx, mode)</tt>
  63. &mdash; Control VM</h2>
  64. <p>
  65. This is a C API extension to allow control of the VM from C code. The
  66. full prototype of <tt>LuaJIT_setmode</tt> is:
  67. </p>
  68. <pre class="code">
  69. LUA_API int luaJIT_setmode(lua_State *L, int idx, int mode);
  70. </pre>
  71. <p>
  72. The returned status is either success (<tt>1</tt>) or failure (<tt>0</tt>).
  73. The second argument is either <tt>0</tt> or a stack index (similar to the
  74. other Lua/C API functions).
  75. </p>
  76. <p>
  77. The third argument specifies the mode, which is 'or'ed with a flag.
  78. The flag can be <tt>LUAJIT_MODE_OFF</tt> to turn a feature on,
  79. <tt>LUAJIT_MODE_ON</tt> to turn a feature off, or
  80. <tt>LUAJIT_MODE_FLUSH</tt> to flush cached code.
  81. </p>
  82. <p>
  83. The following modes are defined:
  84. </p>
  85. <h3 id="mode_engine"><tt>luaJIT_setmode(L, 0, LUAJIT_MODE_ENGINE|flag)</tt></h3>
  86. <p>
  87. Turn the whole JIT compiler on or off or flush the whole cache of compiled code.
  88. </p>
  89. <h3 id="mode_func"><tt>luaJIT_setmode(L, idx, LUAJIT_MODE_FUNC|flag)</tt><br>
  90. <tt>luaJIT_setmode(L, idx, LUAJIT_MODE_ALLFUNC|flag)</tt><br>
  91. <tt>luaJIT_setmode(L, idx, LUAJIT_MODE_ALLSUBFUNC|flag)</tt></h3>
  92. <p>
  93. This sets the mode for the function at the stack index <tt>idx</tt> or
  94. the parent of the calling function (<tt>idx = 0</tt>). It either
  95. enables JIT compilation for a function, disables it and flushes any
  96. already compiled code or only flushes already compiled code. This
  97. applies recursively to all sub-functions of the function with
  98. <tt>LUAJIT_MODE_ALLFUNC</tt> or only to the sub-functions with
  99. <tt>LUAJIT_MODE_ALLSUBFUNC</tt>.
  100. </p>
  101. <h3 id="mode_trace"><tt>luaJIT_setmode(L, trace,<br>
  102. &nbsp;&nbsp;LUAJIT_MODE_TRACE|LUAJIT_MODE_FLUSH)</tt></h3>
  103. <p>
  104. Flushes the specified root trace and all of its side traces from the cache.
  105. The code for the trace will be retained as long as there are any other
  106. traces which link to it.
  107. </p>
  108. <h3 id="mode_wrapcfunc"><tt>luaJIT_setmode(L, idx, LUAJIT_MODE_WRAPCFUNC|flag)</tt></h3>
  109. <p>
  110. This mode defines a wrapper function for calls to C functions. If
  111. called with <tt>LUAJIT_MODE_ON</tt>, the stack index at <tt>idx</tt>
  112. must be a <tt>lightuserdata</tt> object holding a pointer to the wrapper
  113. function. From now on all C functions are called through the wrapper
  114. function. If called with <tt>LUAJIT_MODE_OFF</tt> this mode is turned
  115. off and all C functions are directly called.
  116. </p>
  117. <p>
  118. The wrapper function can be used for debugging purposes or to catch
  119. and convert foreign exceptions. But please read the section on
  120. <a href="extensions.html#exceptions">C++&nbsp;exception interoperability</a>
  121. first. Recommended usage can be seen in this C++ code excerpt:
  122. </p>
  123. <pre class="code">
  124. #include &lt;exception&gt;
  125. #include "lua.hpp"
  126. // Catch C++ exceptions and convert them to Lua error messages.
  127. // Customize as needed for your own exception classes.
  128. static int wrap_exceptions(lua_State *L, lua_CFunction f)
  129. {
  130. try {
  131. return f(L); // Call wrapped function and return result.
  132. } catch (const char *s) { // Catch and convert exceptions.
  133. lua_pushstring(L, s);
  134. } catch (std::exception& e) {
  135. lua_pushstring(L, e.what());
  136. } catch (...) {
  137. lua_pushliteral(L, "caught (...)");
  138. }
  139. return lua_error(L); // Rethrow as a Lua error.
  140. }
  141. static int myinit(lua_State *L)
  142. {
  143. ...
  144. // Define wrapper function and enable it.
  145. lua_pushlightuserdata(L, (void *)wrap_exceptions);
  146. luaJIT_setmode(L, -1, LUAJIT_MODE_WRAPCFUNC|LUAJIT_MODE_ON);
  147. lua_pop(L, 1);
  148. ...
  149. }
  150. </pre>
  151. <p>
  152. Note that you can only define <b>a single global wrapper function</b>,
  153. so be careful when using this mechanism from multiple C++ modules.
  154. Also note that this mechanism is not without overhead.
  155. </p>
  156. <br class="flush">
  157. </div>
  158. <div id="foot">
  159. <hr class="hide">
  160. Copyright &copy; 2005-2010 Mike Pall
  161. <span class="noprint">
  162. &middot;
  163. <a href="contact.html">Contact</a>
  164. </span>
  165. </div>
  166. </body>
  167. </html>