extensions.html 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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-2012, 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="http://luajit.org/download.html">Download <span class="ext">&raquo;</span></a>
  41. </li><li>
  42. <a href="install.html">Installation</a>
  43. </li><li>
  44. <a href="running.html">Running</a>
  45. </li></ul>
  46. </li><li>
  47. <a class="current" href="extensions.html">Extensions</a>
  48. <ul><li>
  49. <a href="ext_ffi.html">FFI Library</a>
  50. <ul><li>
  51. <a href="ext_ffi_tutorial.html">FFI Tutorial</a>
  52. </li><li>
  53. <a href="ext_ffi_api.html">ffi.* API</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://wiki.luajit.org/">Wiki <span class="ext">&raquo;</span></a>
  73. </li><li>
  74. <a href="http://luajit.org/list.html">Mailing List <span class="ext">&raquo;</span></a>
  75. </li></ul>
  76. </div>
  77. <div id="main">
  78. <p>
  79. LuaJIT is fully upwards-compatible with Lua 5.1. It supports all
  80. <a href="http://www.lua.org/manual/5.1/manual.html#5"><span class="ext">&raquo;</span>&nbsp;standard Lua
  81. library functions</a> and the full set of
  82. <a href="http://www.lua.org/manual/5.1/manual.html#3"><span class="ext">&raquo;</span>&nbsp;Lua/C API
  83. functions</a>.
  84. </p>
  85. <p>
  86. LuaJIT is also fully ABI-compatible to Lua 5.1 at the linker/dynamic
  87. loader level. This means you can compile a C&nbsp;module against the
  88. standard Lua headers and load the same shared library from either Lua
  89. or LuaJIT.
  90. </p>
  91. <p>
  92. LuaJIT extends the standard Lua VM with new functionality and adds
  93. several extension modules. Please note this page is only about
  94. <em>functional</em> enhancements and not about performance enhancements,
  95. such as the optimized VM, the faster interpreter or the JIT compiler.
  96. </p>
  97. <h2 id="modules">Extensions Modules</h2>
  98. <p>
  99. LuaJIT comes with several built-in extension modules:
  100. </p>
  101. <h3 id="bit"><tt>bit.*</tt> &mdash; Bitwise operations</h3>
  102. <p>
  103. LuaJIT supports all bitwise operations as defined by
  104. <a href="http://bitop.luajit.org"><span class="ext">&raquo;</span>&nbsp;Lua BitOp</a>:
  105. </p>
  106. <pre class="code">
  107. bit.tobit bit.tohex bit.bnot bit.band bit.bor bit.bxor
  108. bit.lshift bit.rshift bit.arshift bit.rol bit.ror bit.bswap
  109. </pre>
  110. <p>
  111. This module is a LuaJIT built-in &mdash; you don't need to download or
  112. install Lua BitOp. The Lua BitOp site has full documentation for all
  113. <a href="http://bitop.luajit.org/api.html"><span class="ext">&raquo;</span>&nbsp;Lua BitOp API functions</a>.
  114. </p>
  115. <p>
  116. Please make sure to <tt>require</tt> the module before using any of
  117. its functions:
  118. </p>
  119. <pre class="code">
  120. local bit = require("bit")
  121. </pre>
  122. <p>
  123. An already installed Lua BitOp module is ignored by LuaJIT.
  124. This way you can use bit operations from both Lua and LuaJIT on a
  125. shared installation.
  126. </p>
  127. <h3 id="ffi"><tt>ffi.*</tt> &mdash; FFI library</h3>
  128. <p>
  129. The <a href="ext_ffi.html">FFI library</a> allows calling external
  130. C&nbsp;functions and the use of C&nbsp;data structures from pure Lua
  131. code.
  132. </p>
  133. <h3 id="jit"><tt>jit.*</tt> &mdash; JIT compiler control</h3>
  134. <p>
  135. The functions in this module
  136. <a href="ext_jit.html">control the behavior of the JIT compiler engine</a>.
  137. </p>
  138. <h3 id="c_api">C API extensions</h3>
  139. <p>
  140. LuaJIT adds some
  141. <a href="ext_c_api.html">extra functions to the Lua/C API</a>.
  142. </p>
  143. <h2 id="library">Enhanced Standard Library Functions</h2>
  144. <h3 id="xpcall"><tt>xpcall(f, err [,args...])</tt> passes arguments</h3>
  145. <p>
  146. Unlike the standard implementation in Lua 5.1, <tt>xpcall()</tt>
  147. passes any arguments after the error function to the function
  148. which is called in a protected context.
  149. </p>
  150. <h3 id="load"><tt>loadfile()</tt> etc. handle UTF-8 source code</h3>
  151. <p>
  152. Non-ASCII characters are handled transparently by the Lua source code parser.
  153. This allows the use of UTF-8 characters in identifiers and strings.
  154. A UTF-8 BOM is skipped at the start of the source code.
  155. </p>
  156. <h3 id="tostring"><tt>tostring()</tt> etc. canonicalize NaN and &plusmn;Inf</h3>
  157. <p>
  158. All number-to-string conversions consistently convert non-finite numbers
  159. to the same strings on all platforms. NaN results in <tt>"nan"</tt>,
  160. positive infinity results in <tt>"inf"</tt> and negative infinity results
  161. in <tt>"-inf"</tt>.
  162. </p>
  163. <h3 id="tonumber"><tt>tonumber()</tt> etc. use builtin string to number conversion</h3>
  164. <p>
  165. All string-to-number conversions consistently convert integer and
  166. floating-point inputs in decimal and hexadecimal on all platforms.
  167. <tt>strtod()</tt> is <em>not</em> used anymore, which avoids numerous
  168. problems with poor C library implementations. The builtin conversion
  169. function provides full precision according to the IEEE-754 standard, it
  170. works independently of the current locale and it supports hex floating-point
  171. numbers (e.g. <tt>0x1.5p-3</tt>).
  172. </p>
  173. <h3 id="string_dump"><tt>string.dump(f [,strip])</tt> generates portable bytecode</h3>
  174. <p>
  175. An extra argument has been added to <tt>string.dump()</tt>. If set to
  176. <tt>true</tt>, 'stripped' bytecode without debug information is
  177. generated. This speeds up later bytecode loading and reduces memory
  178. usage. See also the
  179. <a href="running.html#opt_b"><tt>-b</tt> command line option</a>.
  180. </p>
  181. <p>
  182. The generated bytecode is portable and can be loaded on any architecture
  183. that LuaJIT supports, independent of word size or endianess. However the
  184. bytecode compatibility versions must match. Bytecode stays compatible
  185. for dot releases (x.y.0 &rarr; x.y.1), but may change with major or
  186. minor releases (2.0 &rarr; 2.1) or between any beta release. Foreign
  187. bytecode (e.g. from Lua 5.1) is incompatible and cannot be loaded.
  188. </p>
  189. <h3 id="math_random">Enhanced PRNG for <tt>math.random()</tt></h3>
  190. <p>
  191. LuaJIT uses a Tausworthe PRNG with period 2^223 to implement
  192. <tt>math.random()</tt> and <tt>math.randomseed()</tt>. The quality of
  193. the PRNG results is much superior compared to the standard Lua
  194. implementation which uses the platform-specific ANSI rand().
  195. </p>
  196. <p>
  197. The PRNG generates the same sequences from the same seeds on all
  198. platforms and makes use of all bits in the seed argument.
  199. <tt>math.random()</tt> without arguments generates 52 pseudo-random bits
  200. for every call. The result is uniformly distributed between 0.0 and 1.0.
  201. It's correctly scaled up and rounded for <tt>math.random(n&nbsp;[,m])</tt> to
  202. preserve uniformity.
  203. </p>
  204. <h3 id="io"><tt>io.*</tt> functions handle 64&nbsp;bit file offsets</h3>
  205. <p>
  206. The file I/O functions in the standard <tt>io.*</tt> library handle
  207. 64&nbsp;bit file offsets. In particular this means it's possible
  208. to open files larger than 2&nbsp;Gigabytes and to reposition or obtain
  209. the current file position for offsets beyond 2&nbsp;GB
  210. (<tt>fp:seek()</tt> method).
  211. </p>
  212. <h3 id="debug_meta"><tt>debug.*</tt> functions identify metamethods</h3>
  213. <p>
  214. <tt>debug.getinfo()</tt> and <tt>lua_getinfo()</tt> also return information
  215. about invoked metamethods. The <tt>namewhat</tt> field is set to
  216. <tt>"metamethod"</tt> and the <tt>name</tt> field has the name of
  217. the corresponding metamethod (e.g. <tt>"__index"</tt>).
  218. </p>
  219. <h2 id="resumable">Fully Resumable VM</h2>
  220. <p>
  221. The LuaJIT VM is fully resumable. This means you can yield from a
  222. coroutine even across contexts, where this would not possible with
  223. the standard Lua&nbsp;5.1 VM: e.g. you can yield across <tt>pcall()</tt>
  224. and <tt>xpcall()</tt>, across iterators and across metamethods.
  225. </p>
  226. <h2 id="lua52">Extensions from Lua 5.2</h2>
  227. <p>
  228. LuaJIT supports some language and library extensions from Lua&nbsp;5.2.
  229. Features that are unlikely to break existing code are unconditionally
  230. enabled:
  231. </p>
  232. <ul>
  233. <li><tt>goto</tt> and <tt>::labels::</tt>.</li>
  234. <li>Hex escapes <tt>'\x3F'</tt> and <tt>'\*'</tt> escape in strings.</li>
  235. <li><tt>load(string|reader, chunkname [,mode [,env]])</tt>.
  236. <tt>loadstring()</tt> is an alias.</li>
  237. <li><tt>math.log(x [,base])</tt>.
  238. <li><tt>string.rep(s, n [,sep])</tt>.
  239. <li><tt>string.format()</tt>: <tt>%q</tt> reversible.
  240. <tt>%s</tt> checks <tt>__tostring</tt>.
  241. <tt>%a</tt> and <tt>"%A</tt> added.</li>
  242. <li>String matching pattern <tt>%g</tt> added.</li>
  243. <li><tt>io.read("*L")</tt>.</li>
  244. <li><tt>io.lines()</tt> and <tt>file:lines()</tt> process
  245. <tt>io.read()</tt> options.</li>
  246. <li><tt>os.exit(status|true|false [,close])</tt>.</li>
  247. <li><tt>package.searchpath(name, path [, sep [, rep]])</tt>.</li>
  248. <li><tt>package.loadlib(name, "*")</tt>.</li>
  249. <li><tt>debug.getinfo()</tt> returns <tt>nparams</tt> and <tt>isvararg</tt>
  250. for option <tt>"u"</tt>.</li>
  251. <li><tt>debug.getlocal()</tt> accepts function instead of level.</li>
  252. <li><tt>debug.getlocal()</tt> and <tt>debug.setlocal()</tt> accept negative
  253. indexes for varargs.</li>
  254. <li><tt>debug.getupvalue()</tt> and <tt>debug.setupvalue()</tt> handle
  255. C&nbsp;functions.</li>
  256. <li><tt>debug.upvalueid()</tt> and <tt>debug.upvaluejoin()</tt>.</li>
  257. <li>Command line option <tt>-E</tt>.</li>
  258. <li>Command line checks <tt>__tostring</tt> for errors.</li>
  259. </ul>
  260. <p>
  261. Other features are only enabled, if LuaJIT is built with
  262. <tt>-DLUAJIT_ENABLE_LUA52COMPAT</tt>:
  263. </p>
  264. <ul>
  265. <li><tt>goto</tt> is a keyword and not a valid variable name anymore.</li>
  266. <li><tt>break</tt> can be placed anywhere. Empty statements (<tt>;;</tt>)
  267. are allowed.</li>
  268. <li><tt>__lt</tt>, <tt>__le</tt> are invoked for mixed types.</li>
  269. <li><tt>__len</tt> for tables. <tt>rawlen()</tt> library function.</li>
  270. <li><tt>pairs()</tt> and <tt>ipairs()</tt> check for <tt>__pairs</tt> and
  271. <tt>__ipairs</tt>.</li>
  272. <li><tt>coroutine.running()</tt> returns two results.</li>
  273. <li><tt>table.pack()</tt> and <tt>table.unpack()</tt>
  274. (same as <tt>unpack()</tt>).</li>
  275. <li><tt>io.write()</tt> and <tt>file:write()</tt> return file handle
  276. instead of <tt>true</tt>.</li>
  277. <li><tt>os.execute()</tt> and <tt>pipe:close()</tt> return detailed
  278. exit status.</li>
  279. <li><tt>debug.setmetatable()</tt> returns object.</li>
  280. <li><tt>debug.getuservalue()</tt> and <tt>debug.setuservalue()</tt>.</li>
  281. <li>Remove <tt>math.mod()</tt>, <tt>string.gfind()</tt>.
  282. </ul>
  283. <p>
  284. Note: this provides only partial compatibility with Lua 5.2 at the
  285. language and Lua library level. LuaJIT is API+ABI-compatible with
  286. Lua&nbsp;5.1, which prevents implementing features that would otherwise
  287. break the Lua/C API and ABI (e.g. <tt>_ENV</tt>).
  288. </p>
  289. <h2 id="exceptions">C++ Exception Interoperability</h2>
  290. <p>
  291. LuaJIT has built-in support for interoperating with C++&nbsp;exceptions.
  292. The available range of features depends on the target platform and
  293. the toolchain used to compile LuaJIT:
  294. </p>
  295. <table class="exc">
  296. <tr class="exchead">
  297. <td class="excplatform">Platform</td>
  298. <td class="exccompiler">Compiler</td>
  299. <td class="excinterop">Interoperability</td>
  300. </tr>
  301. <tr class="odd separate">
  302. <td class="excplatform">POSIX/x64, DWARF2 unwinding</td>
  303. <td class="exccompiler">GCC 4.3+</td>
  304. <td class="excinterop"><b style="color: #00a000;">Full</b></td>
  305. </tr>
  306. <tr class="even">
  307. <td class="excplatform">Other platforms, DWARF2 unwinding</td>
  308. <td class="exccompiler">GCC</td>
  309. <td class="excinterop"><b style="color: #c06000;">Limited</b></td>
  310. </tr>
  311. <tr class="odd">
  312. <td class="excplatform">Windows/x64</td>
  313. <td class="exccompiler">MSVC or WinSDK</td>
  314. <td class="excinterop"><b style="color: #00a000;">Full</b></td>
  315. </tr>
  316. <tr class="even">
  317. <td class="excplatform">Windows/x86</td>
  318. <td class="exccompiler">Any</td>
  319. <td class="excinterop"><b style="color: #a00000;">No</b></td>
  320. </tr>
  321. <tr class="odd">
  322. <td class="excplatform">Other platforms</td>
  323. <td class="exccompiler">Other compilers</td>
  324. <td class="excinterop"><b style="color: #a00000;">No</b></td>
  325. </tr>
  326. </table>
  327. <p>
  328. <b style="color: #00a000;">Full interoperability</b> means:
  329. </p>
  330. <ul>
  331. <li>C++&nbsp;exceptions can be caught on the Lua side with <tt>pcall()</tt>,
  332. <tt>lua_pcall()</tt> etc.</li>
  333. <li>C++&nbsp;exceptions will be converted to the generic Lua error
  334. <tt>"C++&nbsp;exception"</tt>, unless you use the
  335. <a href="ext_c_api.html#mode_wrapcfunc">C&nbsp;call wrapper</a> feature.</li>
  336. <li>It's safe to throw C++&nbsp;exceptions across non-protected Lua frames
  337. on the C&nbsp;stack. The contents of the C++&nbsp;exception object
  338. pass through unmodified.</li>
  339. <li>Lua errors can be caught on the C++ side with <tt>catch(...)</tt>.
  340. The corresponding Lua error message can be retrieved from the Lua stack.</li>
  341. <li>Throwing Lua errors across C++ frames is safe. C++ destructors
  342. will be called.</li>
  343. </ul>
  344. <p>
  345. <b style="color: #c06000;">Limited interoperability</b> means:
  346. </p>
  347. <ul>
  348. <li>C++&nbsp;exceptions can be caught on the Lua side with <tt>pcall()</tt>,
  349. <tt>lua_pcall()</tt> etc.</li>
  350. <li>C++&nbsp;exceptions will be converted to the generic Lua error
  351. <tt>"C++&nbsp;exception"</tt>, unless you use the
  352. <a href="ext_c_api.html#mode_wrapcfunc">C&nbsp;call wrapper</a> feature.</li>
  353. <li>C++&nbsp;exceptions will be caught by non-protected Lua frames and
  354. are rethrown as a generic Lua error. The C++&nbsp;exception object will
  355. be destroyed.</li>
  356. <li>Lua errors <b>cannot</b> be caught on the C++ side.</li>
  357. <li>Throwing Lua errors across C++ frames will <b>not</b> call
  358. C++ destructors.</li>
  359. </ul>
  360. <p>
  361. <b style="color: #a00000;">No interoperability</b> means:
  362. </p>
  363. <ul>
  364. <li>It's <b>not</b> safe to throw C++&nbsp;exceptions across Lua frames.</li>
  365. <li>C++&nbsp;exceptions <b>cannot</b> be caught on the Lua side.</li>
  366. <li>Lua errors <b>cannot</b> be caught on the C++ side.</li>
  367. <li>Throwing Lua errors across C++ frames will <b>not</b> call
  368. C++ destructors.</li>
  369. <li>Additionally, on Windows/x86 with SEH-based C++&nbsp;exceptions:
  370. it's <b>not</b> safe to throw a Lua error across any frames containing
  371. a C++ function with any try/catch construct or using variables with
  372. (implicit) destructors. This also applies to any functions which may be
  373. inlined in such a function. It doesn't matter whether <tt>lua_error()</tt>
  374. is called inside or outside of a try/catch or whether any object actually
  375. needs to be destroyed: the SEH chain is corrupted and this will eventually
  376. lead to the termination of the process.</li>
  377. </ul>
  378. <br class="flush">
  379. </div>
  380. <div id="foot">
  381. <hr class="hide">
  382. Copyright &copy; 2005-2012 Mike Pall
  383. <span class="noprint">
  384. &middot;
  385. <a href="contact.html">Contact</a>
  386. </span>
  387. </div>
  388. </body>
  389. </html>