api.html 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
  2. <html>
  3. <head>
  4. <title>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>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><li>
  27. <a class="current" href="api.html">API Extensions</a>
  28. </li></ul>
  29. </li><li>
  30. <a href="status.html">Status</a>
  31. <ul><li>
  32. <a href="changes.html">Changes</a>
  33. </li></ul>
  34. </li><li>
  35. <a href="faq.html">FAQ</a>
  36. </li><li>
  37. <a href="http://luajit.org/download.html">Download <span class="ext">&raquo;</span></a>
  38. </li></ul>
  39. </div>
  40. <div id="main">
  41. <p>
  42. LuaJIT is fully upwards-compatible with Lua 5.1. It supports all
  43. <a href="http://www.lua.org/manual/5.1/manual.html#5"><span class="ext">&raquo;</span>&nbsp;standard Lua
  44. library functions</a> and the full set of
  45. <a href="http://www.lua.org/manual/5.1/manual.html#3"><span class="ext">&raquo;</span>&nbsp;Lua/C API
  46. functions</a>.
  47. </p>
  48. <p>
  49. LuaJIT is also fully ABI-compatible to Lua 5.1 at the linker/dynamic
  50. loader level. This means you can compile a C&nbsp;module against the
  51. standard Lua headers and load the same shared library from either Lua
  52. or LuaJIT.
  53. </p>
  54. <h2 id="bit"><tt>bit.*</tt> &mdash; Bitwise Operations</h2>
  55. <p>
  56. LuaJIT supports all bitwise operations as defined by
  57. <a href="http://bitop.luajit.org"><span class="ext">&raquo;</span>&nbsp;Lua BitOp</a>:
  58. </p>
  59. <pre class="code">
  60. bit.tobit bit.tohex bit.bnot bit.band bit.bor bit.bxor
  61. bit.lshift bit.rshift bit.arshift bit.rol bit.ror bit.bswap
  62. </pre>
  63. <p>
  64. This module is a LuaJIT built-in &mdash; you don't need to download or
  65. install Lua BitOp. The Lua BitOp site has full documentation for all
  66. <a href="http://bitop.luajit.org/api.html"><span class="ext">&raquo;</span>&nbsp;Lua BitOp API functions</a>.
  67. </p>
  68. <p>
  69. Please make sure to <tt>require</tt> the module before using any of
  70. its functions:
  71. </p>
  72. <pre class="code">
  73. local bit = require("bit")
  74. </pre>
  75. <p>
  76. An already installed Lua BitOp module is ignored by LuaJIT.
  77. This way you can use bit operations from both Lua and LuaJIT on a
  78. shared installation.
  79. </p>
  80. <h2 id="jit"><tt>jit.*</tt> &mdash; JIT compiler control</h2>
  81. <p>
  82. The functions in this built-in module control the behavior
  83. of the JIT compiler engine.
  84. </p>
  85. <h3 id="jit_onoff"><tt>jit.on()<br>
  86. jit.off()</tt></h3>
  87. <p>
  88. Turns the whole JIT compiler on (default) or off.
  89. </p>
  90. <p>
  91. These functions are typically used with the command line options
  92. <tt>-j on</tt> or <tt>-j off</tt>.
  93. </p>
  94. <h3 id="jit_flush"><tt>jit.flush()</tt></h3>
  95. <p>
  96. Flushes the whole cache of compiled code.
  97. </p>
  98. <h3 id="jit_onoff_func"><tt>jit.on(func|true [,true|false])<br>
  99. jit.off(func|true [,true|false])<br>
  100. jit.flush(func|true [,true|false])</tt></h3>
  101. <p>
  102. <tt>jit.on</tt> enables JIT compilation for a Lua function (this is
  103. the default).
  104. </p>
  105. <p>
  106. <tt>jit.off</tt> disables JIT compilation for a Lua function and
  107. flushes any already compiled code from the code cache.
  108. </p>
  109. <p>
  110. <tt>jit.flush</tt> flushes the code, but doesn't affect the
  111. enable/disable status.
  112. </p>
  113. <p>
  114. The current function, i.e. the Lua function calling this library
  115. function, can also be specified by passing <tt>true</tt> as the first
  116. argument.
  117. </p>
  118. <p>
  119. If the second argument is <tt>true</tt>, JIT compilation is also
  120. enabled, disabled or flushed recursively for all sub-functions of a
  121. function. With <tt>false</tt> only the sub-functions are affected.
  122. </p>
  123. <p>
  124. The <tt>jit.on</tt> and <tt>jit.off</tt> functions only set a flag
  125. which is checked when the function is about to be compiled. They do
  126. not trigger immediate compilation.
  127. </p>
  128. <p>
  129. Typical usage is <tt>jit.off(true, true)</tt> in the main chunk
  130. of a module to turn off JIT compilation for the whole module for
  131. debugging purposes.
  132. </p>
  133. <h3 id="jit_flush_tr"><tt>status = jit.flush(tr)</tt></h3>
  134. <p>
  135. Tries to flush the code for the specified trace and all of its
  136. side traces from the cache. Returns <tt>true</tt> on success.
  137. Returns <tt>false</tt> if there are still links to this trace.
  138. </p>
  139. <h3 id="jit_status"><tt>status, ... = jit.status()</tt></h3>
  140. <p>
  141. Returns the current status of the JIT compiler. The first result is
  142. either <tt>true</tt> or <tt>false</tt> if the JIT compiler is turned
  143. on or off. The remaining results are strings for CPU-specific features
  144. and enabled optimizations.
  145. </p>
  146. <h3 id="jit_version"><tt>jit.version</tt></h3>
  147. <p>
  148. Contains the LuaJIT version string.
  149. </p>
  150. <h3 id="jit_version_num"><tt>jit.version_num</tt></h3>
  151. <p>
  152. Contains the version number of the LuaJIT core. Version xx.yy.zz
  153. is represented by the decimal number xxyyzz.
  154. </p>
  155. <h3 id="jit_arch"><tt>jit.arch</tt></h3>
  156. <p>
  157. Contains the target architecture name (CPU and optional ABI).
  158. </p>
  159. <h2 id="jit_opt"><tt>jit.opt.*</tt> &mdash; JIT compiler optimization control</h2>
  160. <p>
  161. This module provides the backend for the <tt>-O</tt> command line
  162. option.
  163. </p>
  164. <p>
  165. You can also use it programmatically, e.g.:
  166. </p>
  167. <pre class="code">
  168. jit.opt.start(2) -- same as -O2
  169. jit.opt.start("-dce")
  170. jit.opt.start("hotloop=10", "hotexit=2")
  171. </pre>
  172. <p>
  173. Unlike in LuaJIT 1.x, the module is built-in and
  174. <b>optimization is turned on by default!</b>
  175. It's no longer necessary to run <tt>require("jit.opt").start()</tt>,
  176. which was one of the ways to enable optimization.
  177. </p>
  178. <h2 id="jit_util"><tt>jit.util.*</tt> &mdash; JIT compiler introspection</h2>
  179. <p>
  180. This module holds functions to introspect the bytecode, generated
  181. traces, the IR and the generated machine code. The functionality
  182. provided by this module is still in flux and therefore undocumented.
  183. </p>
  184. <p>
  185. The debug modules <tt>-jbc</tt>, <tt>-jv</tt> and <tt>-jdump</tt> make
  186. extensive use of these functions. Please check out their source code,
  187. if you want to know more.
  188. </p>
  189. <h2 id="c_api">C API extensions</h2>
  190. <p>
  191. LuaJIT adds some extensions to the Lua/C API. The LuaJIT include
  192. directory must be in the compiler search path (<tt>-I<i>path</i></tt>)
  193. to be able to include the required header for C code:
  194. </p>
  195. <pre class="code">
  196. #include "luajit.h"
  197. </pre>
  198. <p>
  199. Or for C++ code:
  200. </p>
  201. <pre class="code">
  202. #include "lua.hpp"
  203. </pre>
  204. <h2 id="luaJIT_setmode"><tt>luaJIT_setmode(L, idx, mode)</tt>
  205. &mdash; Control VM</h2>
  206. <p>
  207. This is a C API extension to allow control of the VM from C code. The
  208. full prototype of <tt>LuaJIT_setmode</tt> is:
  209. </p>
  210. <pre class="code">
  211. LUA_API int luaJIT_setmode(lua_State *L, int idx, int mode);
  212. </pre>
  213. <p>
  214. The returned status is either success (<tt>1</tt>) or failure (<tt>0</tt>).
  215. The second argument is either <tt>0</tt> or a stack index (similar to the
  216. other Lua/C API functions).
  217. </p>
  218. <p>
  219. The third argument specifies the mode, which is 'or'ed with a flag.
  220. The flag can be <tt>LUAJIT_MODE_OFF</tt> to turn a feature on,
  221. <tt>LUAJIT_MODE_ON</tt> to turn a feature off, or
  222. <tt>LUAJIT_MODE_FLUSH</tt> to flush cached code.
  223. </p>
  224. <p>
  225. The following modes are defined:
  226. </p>
  227. <h3 id="mode_engine"><tt>luaJIT_setmode(L, 0, LUAJIT_MODE_ENGINE|flag)</tt></h3>
  228. <p>
  229. Turn the whole JIT compiler on or off or flush the whole cache of compiled code.
  230. </p>
  231. <h3 id="mode_func"><tt>luaJIT_setmode(L, idx, LUAJIT_MODE_FUNC|flag)</tt><br>
  232. <tt>luaJIT_setmode(L, idx, LUAJIT_MODE_ALLFUNC|flag)</tt><br>
  233. <tt>luaJIT_setmode(L, idx, LUAJIT_MODE_ALLSUBFUNC|flag)</tt></h3>
  234. <p>
  235. This sets the mode for the function at the stack index <tt>idx</tt> or
  236. the parent of the calling function (<tt>idx = 0</tt>). It either
  237. enables JIT compilation for a function, disables it and flushes any
  238. already compiled code or only flushes already compiled code. This
  239. applies recursively to all sub-functions of the function with
  240. <tt>LUAJIT_MODE_ALLFUNC</tt> or only to the sub-functions with
  241. <tt>LUAJIT_MODE_ALLSUBFUNC</tt>.
  242. </p>
  243. <h3 id="mode_engine"><tt>luaJIT_setmode(L, trace,<br>
  244. &nbsp;&nbsp;LUAJIT_MODE_TRACE|LUAJIT_MODE_FLUSH)</tt></h3>
  245. <p>
  246. Tries to flush the code for the specified trace and all of its
  247. side traces from the cache.
  248. </p>
  249. <h3 id="mode_engine"><tt>luaJIT_setmode(L, idx, LUAJIT_MODE_WRAPCFUNC|flag)</tt></h3>
  250. <p>
  251. This mode defines a wrapper function for calls to C functions. If
  252. called with <tt>LUAJIT_MODE_ON</tt>, the stack index at <tt>idx</tt>
  253. must be a <tt>lightuserdata</tt> object holding a pointer to the wrapper
  254. function. From now on all C functions are called through the wrapper
  255. function. If called with <tt>LUAJIT_MODE_OFF</tt> this mode is turned
  256. off and all C functions are directly called.
  257. </p>
  258. <p>
  259. The wrapper function can be used for debugging purposes or to catch
  260. and convert foreign exceptions. Recommended usage can be seen in this
  261. C++ code excerpt:
  262. </p>
  263. <pre class="code">
  264. #include &lt;exception&gt;
  265. #include "lua.hpp"
  266. // Catch C++ exceptions and convert them to Lua error messages.
  267. // Customize as needed for your own exception classes.
  268. static int wrap_exceptions(lua_State *L, lua_CFunction f)
  269. {
  270. try {
  271. return f(L); // Call wrapped function and return result.
  272. } catch (const char *s) { // Catch and convert exceptions.
  273. lua_pushstring(L, s);
  274. } catch (std::exception& e) {
  275. lua_pushstring(L, e.what());
  276. } catch (...) {
  277. lua_pushliteral(L, "caught (...)");
  278. }
  279. return lua_error(L); // Rethrow as a Lua error.
  280. }
  281. static int myinit(lua_State *L)
  282. {
  283. ...
  284. // Define wrapper function and enable it.
  285. lua_pushlightuserdata(L, (void *)wrap_exceptions);
  286. luaJIT_setmode(L, -1, LUAJIT_MODE_WRAPCFUNC|LUAJIT_MODE_ON);
  287. lua_pop(L, 1);
  288. ...
  289. }
  290. </pre>
  291. <p>
  292. Note that you can only define <b>a single global wrapper function</b>,
  293. so be careful when using this mechanism from multiple C++ modules.
  294. Also note that this mechanism is not without overhead.
  295. </p>
  296. <p>
  297. LuaJIT already intercepts exception handling for systems using DWARF2
  298. stack unwinding (e.g. Linux or OSX) and for Windows/x64 (but <b>not</b>
  299. for Windows/x86). This is a zero-cost mechanism and always enabled.
  300. You don't need to use any wrapper functions, except when you want to get
  301. a more specific error message than <tt>"C++&nbsp;exception"</tt>.
  302. </p>
  303. <br class="flush">
  304. </div>
  305. <div id="foot">
  306. <hr class="hide">
  307. Copyright &copy; 2005-2010 Mike Pall
  308. <span class="noprint">
  309. &middot;
  310. <a href="contact.html">Contact</a>
  311. </span>
  312. </div>
  313. </body>
  314. </html>