extensions.html 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
  2. <html>
  3. <head>
  4. <title>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-2011, 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. <style type="text/css">
  12. table.exc {
  13. line-height: 1.2;
  14. }
  15. tr.exchead td {
  16. font-weight: bold;
  17. }
  18. td.excplatform {
  19. width: 48%;
  20. }
  21. td.exccompiler {
  22. width: 29%;
  23. }
  24. td.excinterop {
  25. width: 23%;
  26. }
  27. </style>
  28. </head>
  29. <body>
  30. <div id="site">
  31. <a href="http://luajit.org"><span>Lua<span id="logo">JIT</span></span></a>
  32. </div>
  33. <div id="head">
  34. <h1>Extensions</h1>
  35. </div>
  36. <div id="nav">
  37. <ul><li>
  38. <a href="luajit.html">LuaJIT</a>
  39. <ul><li>
  40. <a href="install.html">Installation</a>
  41. </li><li>
  42. <a href="running.html">Running</a>
  43. </li></ul>
  44. </li><li>
  45. <a class="current" href="extensions.html">Extensions</a>
  46. <ul><li>
  47. <a href="ext_ffi.html">FFI Library</a>
  48. <ul><li>
  49. <a href="ext_ffi_tutorial.html">FFI Tutorial</a>
  50. </li><li>
  51. <a href="ext_ffi_api.html">ffi.* API</a>
  52. </li><li>
  53. <a href="ext_ffi_int64.html">64 bit Integers</a>
  54. </li><li>
  55. <a href="ext_ffi_semantics.html">FFI Semantics</a>
  56. </li></ul>
  57. </li><li>
  58. <a href="ext_jit.html">jit.* Library</a>
  59. </li><li>
  60. <a href="ext_c_api.html">Lua/C API</a>
  61. </li></ul>
  62. </li><li>
  63. <a href="status.html">Status</a>
  64. <ul><li>
  65. <a href="changes.html">Changes</a>
  66. </li></ul>
  67. </li><li>
  68. <a href="faq.html">FAQ</a>
  69. </li><li>
  70. <a href="http://luajit.org/performance.html">Performance <span class="ext">&raquo;</span></a>
  71. </li><li>
  72. <a href="http://luajit.org/download.html">Download <span class="ext">&raquo;</span></a>
  73. </li></ul>
  74. </div>
  75. <div id="main">
  76. <p>
  77. LuaJIT is fully upwards-compatible with Lua 5.1. It supports all
  78. <a href="http://www.lua.org/manual/5.1/manual.html#5"><span class="ext">&raquo;</span>&nbsp;standard Lua
  79. library functions</a> and the full set of
  80. <a href="http://www.lua.org/manual/5.1/manual.html#3"><span class="ext">&raquo;</span>&nbsp;Lua/C API
  81. functions</a>.
  82. </p>
  83. <p>
  84. LuaJIT is also fully ABI-compatible to Lua 5.1 at the linker/dynamic
  85. loader level. This means you can compile a C&nbsp;module against the
  86. standard Lua headers and load the same shared library from either Lua
  87. or LuaJIT.
  88. </p>
  89. <p>
  90. LuaJIT extends the standard Lua VM with new functionality and adds
  91. several extension modules. Please note that this page is only about
  92. <em>functional</em> enhancements and not about performance enhancements,
  93. such as the optimized VM, the faster interpreter or the JIT compiler.
  94. </p>
  95. <h2 id="modules">Extensions Modules</h2>
  96. <p>
  97. LuaJIT comes with several built-in extension modules:
  98. </p>
  99. <h3 id="bit"><tt>bit.*</tt> &mdash; Bitwise operations</h3>
  100. <p>
  101. LuaJIT supports all bitwise operations as defined by
  102. <a href="http://bitop.luajit.org"><span class="ext">&raquo;</span>&nbsp;Lua BitOp</a>:
  103. </p>
  104. <pre class="code">
  105. bit.tobit bit.tohex bit.bnot bit.band bit.bor bit.bxor
  106. bit.lshift bit.rshift bit.arshift bit.rol bit.ror bit.bswap
  107. </pre>
  108. <p>
  109. This module is a LuaJIT built-in &mdash; you don't need to download or
  110. install Lua BitOp. The Lua BitOp site has full documentation for all
  111. <a href="http://bitop.luajit.org/api.html"><span class="ext">&raquo;</span>&nbsp;Lua BitOp API functions</a>.
  112. </p>
  113. <p>
  114. Please make sure to <tt>require</tt> the module before using any of
  115. its functions:
  116. </p>
  117. <pre class="code">
  118. local bit = require("bit")
  119. </pre>
  120. <p>
  121. An already installed Lua BitOp module is ignored by LuaJIT.
  122. This way you can use bit operations from both Lua and LuaJIT on a
  123. shared installation.
  124. </p>
  125. <h3 id="ffi"><tt>ffi.*</tt> &mdash; FFI library</h3>
  126. <p>
  127. The <a href="ext_ffi.html">FFI library</a> allows calling external
  128. C&nbsp;functions and the use of C&nbsp;data structures from pure Lua
  129. code.
  130. </p>
  131. <h3 id="jit"><tt>jit.*</tt> &mdash; JIT compiler control</h3>
  132. <p>
  133. The functions in this module
  134. <a href="ext_jit.html">control the behavior of the JIT compiler engine</a>.
  135. </p>
  136. <h3 id="c_api">C API extensions</h3>
  137. <p>
  138. LuaJIT adds some
  139. <a href="ext_c_api.html">extra functions to the Lua/C API</a>.
  140. </p>
  141. <h2 id="library">Enhanced Standard Library Functions</h2>
  142. <h3 id="xpcall"><tt>xpcall(f, err [,args...])</tt> passes arguments</h3>
  143. <p>
  144. Unlike the standard implementation in Lua 5.1, <tt>xpcall()</tt>
  145. passes any arguments after the error function to the function
  146. which is called in a protected context.
  147. </p>
  148. <h3 id="load"><tt>loadfile()</tt> etc. handle UTF-8 source code</h3>
  149. <p>
  150. Non-ASCII characters are handled transparently by the Lua source code parser.
  151. This allows the use of UTF-8 characters in identifiers and strings.
  152. A UTF-8 BOM is skipped at the start of the source code.
  153. </p>
  154. <h3 id="tostring"><tt>tostring()</tt> etc. canonicalize NaN and &plusmn;Inf</h3>
  155. <p>
  156. All number-to-string conversions consistently convert non-finite numbers
  157. to the same strings on all platforms. NaN results in <tt>"nan"</tt>,
  158. positive infinity results in <tt>"inf"</tt> and negative infinity results
  159. in <tt>"-inf"</tt>.
  160. </p>
  161. <h3 id="math_random">Enhanced PRNG for <tt>math.random()</tt></h3>
  162. <p>
  163. LuaJIT uses a Tausworthe PRNG with period 2^223 to implement
  164. <tt>math.random()</tt> and <tt>math.randomseed()</tt>. The quality of
  165. the PRNG results is much superior compared to the standard Lua
  166. implementation which uses the platform-specific ANSI rand().
  167. </p>
  168. <p>
  169. The PRNG generates the same sequences from the same seeds on all
  170. platforms and makes use of all bits in the seed argument.
  171. <tt>math.random()</tt> without arguments generates 52 pseudo-random bits
  172. for every call. The result is uniformly distributed between 0 and 1.
  173. It's correctly scaled up and rounded for <tt>math.random(n&nbsp;[,m])</tt> to
  174. preserve uniformity.
  175. </p>
  176. <h3 id="io"><tt>io.*</tt> functions handle 64&nbsp;bit file offsets</h3>
  177. <p>
  178. The file I/O functions in the standard <tt>io.*</tt> library handle
  179. 64&nbsp;bit file offsets. In particular this means it's possible
  180. to open files larger than 2&nbsp;Gigabytes and to reposition or obtain
  181. the current file position for offsets beyond 2&nbsp;GB
  182. (<tt>fp:seek()</tt> method).
  183. </p>
  184. <h3 id="debug_meta"><tt>debug.*</tt> functions identify metamethods</h3>
  185. <p>
  186. <tt>debug.getinfo()</tt> and <tt>lua_getinfo()</tt> also return information
  187. about invoked metamethods. The <tt>namewhat</tt> field is set to
  188. <tt>"metamethod"</tt> and the <tt>name</tt> field has the name of
  189. the corresponding metamethod (e.g. <tt>"__index"</tt>).
  190. </p>
  191. <h2 id="resumable">Fully Resumable VM</h2>
  192. <p>
  193. The LuaJIT 2.x VM is fully resumable. This means you can yield from a
  194. coroutine even across contexts, where this would not possible with
  195. the standard Lua&nbsp;5.1 VM: e.g. you can yield across <tt>pcall()</tt>
  196. and <tt>xpcall()</tt>, across iterators and across metamethods.
  197. </p>
  198. <p>
  199. Note however that LuaJIT 2.x doesn't use
  200. <a href="http://coco.luajit.org/"><span class="ext">&raquo;</span>&nbsp;Coco</a> anymore. This means the
  201. overhead for creating coroutines is much smaller and no extra
  202. C&nbsp;stacks need to be allocated. OTOH you can no longer yield
  203. across arbitrary C&nbsp;functions. Keep this in mind when
  204. upgrading from LuaJIT 1.x.
  205. </p>
  206. <h2 id="exceptions">C++ Exception Interoperability</h2>
  207. <p>
  208. LuaJIT has built-in support for interoperating with C++&nbsp;exceptions.
  209. The available range of features depends on the target platform and
  210. the toolchain used to compile LuaJIT:
  211. </p>
  212. <table class="exc">
  213. <tr class="exchead">
  214. <td class="excplatform">Platform</td>
  215. <td class="exccompiler">Compiler</td>
  216. <td class="excinterop">Interoperability</td>
  217. </tr>
  218. <tr class="odd separate">
  219. <td class="excplatform">POSIX/x64, DWARF2 unwinding</td>
  220. <td class="exccompiler">GCC 4.3+</td>
  221. <td class="excinterop"><b style="color: #00a000;">Full</td>
  222. </tr>
  223. <tr class="even">
  224. <td class="excplatform">Other platforms, DWARF2 unwinding</td>
  225. <td class="exccompiler">GCC</td>
  226. <td class="excinterop"><b style="color: #c06000;">Limited</b></td>
  227. </tr>
  228. <tr class="odd">
  229. <td class="excplatform">Windows/x64</td>
  230. <td class="exccompiler">MSVC or WinSDK</td>
  231. <td class="excinterop"><b style="color: #00a000;">Full</td>
  232. </tr>
  233. <tr class="even">
  234. <td class="excplatform">Windows/x86</td>
  235. <td class="exccompiler">Any</td>
  236. <td class="excinterop"><b style="color: #a00000;">No</b></td>
  237. </tr>
  238. <tr class="odd">
  239. <td class="excplatform">Other platforms</td>
  240. <td class="exccompiler">Other compilers</td>
  241. <td class="excinterop"><b style="color: #a00000;">No</b></td>
  242. </tr>
  243. </table>
  244. <p>
  245. <b style="color: #00a000;">Full interoperability</b> means:
  246. </p>
  247. <ul>
  248. <li>C++&nbsp;exceptions can be caught on the Lua side with <tt>pcall()</tt>,
  249. <tt>lua_pcall()</tt> etc.</li>
  250. <li>C++&nbsp;exceptions will be converted to the generic Lua error
  251. <tt>"C++&nbsp;exception"</tt>, unless you use the
  252. <a href="ext_c_api.html#mode_wrapcfunc">C&nbsp;call wrapper</a> feature.</li>
  253. <li>It's safe to throw C++&nbsp;exceptions across non-protected Lua frames
  254. on the C&nbsp;stack. The contents of the C++&nbsp;exception object
  255. pass through unmodified.</li>
  256. <li>Lua errors can be caught on the C++ side with <tt>catch(...)</tt>.
  257. The corresponding Lua error message can be retrieved from the Lua stack.</li>
  258. <li>Throwing Lua errors across C++ frames is safe. C++ destructors
  259. will be called.</li>
  260. </ul>
  261. <p>
  262. <b style="color: #c06000;">Limited interoperability</b> means:
  263. </p>
  264. <ul>
  265. <li>C++&nbsp;exceptions can be caught on the Lua side with <tt>pcall()</tt>,
  266. <tt>lua_pcall()</tt> etc.</li>
  267. <li>C++&nbsp;exceptions will be converted to the generic Lua error
  268. <tt>"C++&nbsp;exception"</tt>, unless you use the
  269. <a href="ext_c_api.html#mode_wrapcfunc">C&nbsp;call wrapper</a> feature.</li>
  270. <li>C++&nbsp;exceptions will be caught by non-protected Lua frames and
  271. are rethrown as a generic Lua error. The C++&nbsp;exception object will
  272. be destroyed.</li>
  273. <li>Lua errors <b>cannot</b> be caught on the C++ side.</li>
  274. <li>Throwing Lua errors across C++ frames will <b>not</b> call
  275. C++ destructors.</li>
  276. </ul>
  277. <p>
  278. <b style="color: #a00000;">No interoperability</b> means:
  279. </p>
  280. <ul>
  281. <li>It's <b>not</b> safe to throw C++&nbsp;exceptions across Lua frames.</li>
  282. <li>C++&nbsp;exceptions <b>cannot</b> be caught on the Lua side.</li>
  283. <li>Lua errors <b>cannot</b> be caught on the C++ side.</li>
  284. <li>Throwing Lua errors across C++ frames will <b>not</b> call
  285. C++ destructors.</li>
  286. <li>Additionally, on Windows/x86 with SEH-based C++&nbsp;exceptions:
  287. it's <b>not</b> safe to throw a Lua error across any frames containing
  288. a C++ function with any try/catch construct or using variables with
  289. (implicit) destructors. This also applies to any functions which may be
  290. inlined in such a function. It doesn't matter whether <tt>lua_error()</tt>
  291. is called inside or outside of a try/catch or whether any object actually
  292. needs to be destroyed: the SEH chain is corrupted and this will eventually
  293. lead to the termination of the process.</li>
  294. </ul>
  295. <br class="flush">
  296. </div>
  297. <div id="foot">
  298. <hr class="hide">
  299. Copyright &copy; 2005-2011 Mike Pall
  300. <span class="noprint">
  301. &middot;
  302. <a href="contact.html">Contact</a>
  303. </span>
  304. </div>
  305. </body>
  306. </html>