aboutsummaryrefslogtreecommitdiff
path: root/doc/extensions.html
diff options
context:
space:
mode:
Diffstat (limited to 'doc/extensions.html')
-rw-r--r--doc/extensions.html169
1 files changed, 132 insertions, 37 deletions
diff --git a/doc/extensions.html b/doc/extensions.html
index 38b2c470..16e88505 100644
--- a/doc/extensions.html
+++ b/doc/extensions.html
@@ -1,8 +1,8 @@
1<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> 1<!DOCTYPE html>
2<html> 2<html>
3<head> 3<head>
4<title>Extensions</title> 4<title>Extensions</title>
5<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> 5<meta charset="utf-8">
6<meta name="Copyright" content="Copyright (C) 2005-2026"> 6<meta name="Copyright" content="Copyright (C) 2005-2026">
7<meta name="Language" content="en"> 7<meta name="Language" content="en">
8<link rel="stylesheet" type="text/css" href="bluequad.css" media="screen"> 8<link rel="stylesheet" type="text/css" href="bluequad.css" media="screen">
@@ -54,9 +54,13 @@ td.excinterop {
54<a href="ext_ffi_semantics.html">FFI Semantics</a> 54<a href="ext_ffi_semantics.html">FFI Semantics</a>
55</li></ul> 55</li></ul>
56</li><li> 56</li><li>
57<a href="ext_buffer.html">String Buffers</a>
58</li><li>
57<a href="ext_jit.html">jit.* Library</a> 59<a href="ext_jit.html">jit.* Library</a>
58</li><li> 60</li><li>
59<a href="ext_c_api.html">Lua/C API</a> 61<a href="ext_c_api.html">Lua/C API</a>
62</li><li>
63<a href="ext_profiler.html">Profiler</a>
60</li></ul> 64</li></ul>
61</li><li> 65</li><li>
62<a href="https://luajit.org/status.html">Status <span class="ext">&raquo;</span></a> 66<a href="https://luajit.org/status.html">Status <span class="ext">&raquo;</span></a>
@@ -106,6 +110,9 @@ bit.lshift bit.rshift bit.arshift bit.rol bit.ror bit.bswap
106This module is a LuaJIT built-in &mdash; you don't need to download or 110This module is a LuaJIT built-in &mdash; you don't need to download or
107install Lua BitOp. The Lua BitOp site has full documentation for all 111install Lua BitOp. The Lua BitOp site has full documentation for all
108<a href="https://bitop.luajit.org/api.html"><span class="ext">&raquo;</span>&nbsp;Lua BitOp API functions</a>. 112<a href="https://bitop.luajit.org/api.html"><span class="ext">&raquo;</span>&nbsp;Lua BitOp API functions</a>.
113The FFI adds support for
114<a href="ext_ffi_semantics.html#cdata_arith">64&nbsp;bit bitwise operations</a>,
115using the same API functions.
109</p> 116</p>
110<p> 117<p>
111Please make sure to <tt>require</tt> the module before using any of 118Please make sure to <tt>require</tt> the module before using any of
@@ -139,6 +146,11 @@ LuaJIT adds some
139<a href="ext_c_api.html">extra functions to the Lua/C API</a>. 146<a href="ext_c_api.html">extra functions to the Lua/C API</a>.
140</p> 147</p>
141 148
149<h3 id="profiler">Profiler</h3>
150<p>
151LuaJIT has an <a href="ext_profiler.html">integrated profiler</a>.
152</p>
153
142<h2 id="library">Enhanced Standard Library Functions</h2> 154<h2 id="library">Enhanced Standard Library Functions</h2>
143 155
144<h3 id="xpcall"><tt>xpcall(f, err [,args...])</tt> passes arguments</h3> 156<h3 id="xpcall"><tt>xpcall(f, err [,args...])</tt> passes arguments</h3>
@@ -148,13 +160,33 @@ passes any arguments after the error function to the function
148which is called in a protected context. 160which is called in a protected context.
149</p> 161</p>
150 162
151<h3 id="load"><tt>loadfile()</tt> etc. handle UTF-8 source code</h3> 163<h3 id="load"><tt>load*()</tt> handle UTF-8 source code</h3>
152<p> 164<p>
153Non-ASCII characters are handled transparently by the Lua source code parser. 165Non-ASCII characters are handled transparently by the Lua source code parser.
154This allows the use of UTF-8 characters in identifiers and strings. 166This allows the use of UTF-8 characters in identifiers and strings.
155A UTF-8 BOM is skipped at the start of the source code. 167A UTF-8 BOM is skipped at the start of the source code.
156</p> 168</p>
157 169
170<h3 id="load_mode"><tt>load*()</tt> add a mode parameter</h3>
171<p>
172As an extension from Lua 5.2, the functions <tt>loadstring()</tt>,
173<tt>loadfile()</tt> and (new) <tt>load()</tt> add an optional
174<tt>mode</tt> parameter.
175</p>
176<p>
177The default mode string is <tt>"bt"</tt>, which allows loading of both
178source code and bytecode. Use <tt>"t"</tt> to allow only source code
179or <tt>"b"</tt> to allow only bytecode to be loaded.
180</p>
181<p>
182By default, the <tt>load*</tt> functions generate the native bytecode format.
183For cross-compilation purposes, add <tt>W</tt> to the mode string to
184force the 32 bit format and <tt>X</tt> to force the 64 bit format.
185Add both to force the opposite format. Note that non-native bytecode
186generated by <tt>load*</tt> cannot be run, but can still be passed
187to <tt>string.dump</tt>.
188</p>
189
158<h3 id="tostring"><tt>tostring()</tt> etc. canonicalize NaN and &plusmn;Inf</h3> 190<h3 id="tostring"><tt>tostring()</tt> etc. canonicalize NaN and &plusmn;Inf</h3>
159<p> 191<p>
160All number-to-string conversions consistently convert non-finite numbers 192All number-to-string conversions consistently convert non-finite numbers
@@ -166,7 +198,7 @@ in <tt>"-inf"</tt>.
166<h3 id="tonumber"><tt>tonumber()</tt> etc. use builtin string to number conversion</h3> 198<h3 id="tonumber"><tt>tonumber()</tt> etc. use builtin string to number conversion</h3>
167<p> 199<p>
168All string-to-number conversions consistently convert integer and 200All string-to-number conversions consistently convert integer and
169floating-point inputs in decimal and hexadecimal on all platforms. 201floating-point inputs in decimal, hexadecimal and binary on all platforms.
170<tt>strtod()</tt> is <em>not</em> used anymore, which avoids numerous 202<tt>strtod()</tt> is <em>not</em> used anymore, which avoids numerous
171problems with poor C library implementations. The builtin conversion 203problems with poor C library implementations. The builtin conversion
172function provides full precision according to the IEEE-754 standard, it 204function provides full precision according to the IEEE-754 standard, it
@@ -174,21 +206,58 @@ works independently of the current locale and it supports hex floating-point
174numbers (e.g. <tt>0x1.5p-3</tt>). 206numbers (e.g. <tt>0x1.5p-3</tt>).
175</p> 207</p>
176 208
177<h3 id="string_dump"><tt>string.dump(f [,strip])</tt> generates portable bytecode</h3> 209<h3 id="string_dump"><tt>string.dump(f [,mode])</tt> generates portable bytecode</h3>
178<p> 210<p>
179An extra argument has been added to <tt>string.dump()</tt>. If set to 211An extra argument has been added to <tt>string.dump()</tt>. If set to
180<tt>true</tt>, 'stripped' bytecode without debug information is 212<tt>true</tt> or to a string which contains the character <tt>s</tt>,
181generated. This speeds up later bytecode loading and reduces memory 213'stripped' bytecode without debug information is generated. This speeds
182usage. See also the 214up later bytecode loading and reduces memory usage. See also the
183<a href="running.html#opt_b"><tt>-b</tt> command line option</a>. 215<a href="running.html#opt_b"><tt>-b</tt> command line option</a>.
184</p> 216</p>
185<p> 217<p>
186The generated bytecode is portable and can be loaded on any architecture 218The generated bytecode is portable and can be loaded on any architecture
187that LuaJIT supports, independent of word size or endianess. However, the 219that LuaJIT supports. However, the bytecode compatibility versions must
188bytecode compatibility versions must match. Bytecode stays compatible 220match. Bytecode only stays compatible within a major+minor version
189for dot releases (x.y.0 &rarr; x.y.1), but may change with major or 221(x.y.aaa &rarr; x.y.bbb), except for development branches. Foreign bytecode
190minor releases (2.0 &rarr; 2.1) or between any beta release. Foreign 222(e.g. from Lua 5.1) is incompatible and cannot be loaded.
191bytecode (e.g. from Lua 5.1) is incompatible and cannot be loaded. 223</p>
224<p>
225Note: <tt>LJ_GC64</tt> mode requires a different frame layout, which implies
226a different, incompatible bytecode format between 32 bit and 64 bit ports.
227This may be rectified in the future. In the meantime, use the <tt>W</tt>
228and </tt>X</tt> <a href="#load_mode">modes of the <tt>load*</tt> functions</a>
229for cross-compilation purposes.
230</p>
231<p>
232Due to VM hardening, bytecode is not deterministic. Add <tt>d</tt> to the
233mode string to dump it in a deterministic manner: identical source code
234always gives a byte-for-byte identical bytecode dump. This feature is
235mainly useful for reproducible builds.
236</p>
237
238<h3 id="table_new"><tt>table.new(narray, nhash)</tt> allocates a pre-sized table</h3>
239<p>
240An extra library function <tt>table.new()</tt> can be made available via
241<tt>require("table.new")</tt>. This creates a pre-sized table, just like
242the C API equivalent <tt>lua_createtable()</tt>. This is useful for big
243tables if the final table size is known and automatic table resizing is
244too expensive.
245</p>
246
247<h3 id="table_clear"><tt>table.clear(tab)</tt> clears a table</h3>
248<p>
249An extra library function <tt>table.clear()</tt> can be made available
250via <tt>require("table.clear")</tt>. This clears all keys and values
251from a table, but preserves the allocated array/hash sizes. This is
252useful when a table, which is linked from multiple places, needs to be
253cleared and/or when recycling a table for use by the same context. This
254avoids managing backlinks, saves an allocation and the overhead of
255incremental array/hash part growth.
256</p>
257<p>
258Please note, this function is meant for very specific situations. In most
259cases it's better to replace the (usually single) link with a new table
260and let the GC do its work.
192</p> 261</p>
193 262
194<h3 id="math_random">Enhanced PRNG for <tt>math.random()</tt></h3> 263<h3 id="math_random">Enhanced PRNG for <tt>math.random()</tt></h3>
@@ -196,7 +265,7 @@ bytecode (e.g. from Lua 5.1) is incompatible and cannot be loaded.
196LuaJIT uses a Tausworthe PRNG with period 2^223 to implement 265LuaJIT uses a Tausworthe PRNG with period 2^223 to implement
197<tt>math.random()</tt> and <tt>math.randomseed()</tt>. The quality of 266<tt>math.random()</tt> and <tt>math.randomseed()</tt>. The quality of
198the PRNG results is much superior compared to the standard Lua 267the PRNG results is much superior compared to the standard Lua
199implementation, which uses the platform-specific ANSI rand(). 268implementation, which uses the platform-specific ANSI <tt>rand()</tt>.
200</p> 269</p>
201<p> 270<p>
202The PRNG generates the same sequences from the same seeds on all 271The PRNG generates the same sequences from the same seeds on all
@@ -207,6 +276,10 @@ It's correctly scaled up and rounded for <tt>math.random(n&nbsp;[,m])</tt> to
207preserve uniformity. 276preserve uniformity.
208</p> 277</p>
209<p> 278<p>
279Call <tt>math.randomseed()</tt> without any arguments to seed it from
280system entropy.
281</p>
282<p>
210Important: Neither this nor any other PRNG based on the simplistic 283Important: Neither this nor any other PRNG based on the simplistic
211<tt>math.random()</tt> API is suitable for cryptographic use. 284<tt>math.random()</tt> API is suitable for cryptographic use.
212</p> 285</p>
@@ -268,6 +341,26 @@ indexes for varargs.</li>
268<li><tt>debug.getupvalue()</tt> and <tt>debug.setupvalue()</tt> handle 341<li><tt>debug.getupvalue()</tt> and <tt>debug.setupvalue()</tt> handle
269C&nbsp;functions.</li> 342C&nbsp;functions.</li>
270<li><tt>debug.upvalueid()</tt> and <tt>debug.upvaluejoin()</tt>.</li> 343<li><tt>debug.upvalueid()</tt> and <tt>debug.upvaluejoin()</tt>.</li>
344<li>Lua/C API extensions:
345<tt>lua_version()</tt>
346<tt>lua_upvalueid()</tt>
347<tt>lua_upvaluejoin()</tt>
348<tt>lua_loadx()</tt>
349<tt>lua_copy()</tt>
350<tt>lua_tonumberx()</tt>
351<tt>lua_tointegerx()</tt>
352<tt>luaL_fileresult()</tt>
353<tt>luaL_execresult()</tt>
354<tt>luaL_loadfilex()</tt>
355<tt>luaL_loadbufferx()</tt>
356<tt>luaL_traceback()</tt>
357<tt>luaL_setfuncs()</tt>
358<tt>luaL_pushmodule()</tt>
359<tt>luaL_newlibtable()</tt>
360<tt>luaL_newlib()</tt>
361<tt>luaL_testudata()</tt>
362<tt>luaL_setmetatable()</tt>
363</li>
271<li>Command line option <tt>-E</tt>.</li> 364<li>Command line option <tt>-E</tt>.</li>
272<li>Command line checks <tt>__tostring</tt> for errors.</li> 365<li>Command line checks <tt>__tostring</tt> for errors.</li>
273</ul> 366</ul>
@@ -293,6 +386,8 @@ exit status.</li>
293<li><tt>debug.setmetatable()</tt> returns object.</li> 386<li><tt>debug.setmetatable()</tt> returns object.</li>
294<li><tt>debug.getuservalue()</tt> and <tt>debug.setuservalue()</tt>.</li> 387<li><tt>debug.getuservalue()</tt> and <tt>debug.setuservalue()</tt>.</li>
295<li>Remove <tt>math.mod()</tt>, <tt>string.gfind()</tt>.</li> 388<li>Remove <tt>math.mod()</tt>, <tt>string.gfind()</tt>.</li>
389<li><tt>package.searchers</tt>.</li>
390<li><tt>module()</tt> returns the module table.</li>
296</ul> 391</ul>
297<p> 392<p>
298Note: this provides only partial compatibility with Lua 5.2 at the 393Note: this provides only partial compatibility with Lua 5.2 at the
@@ -301,6 +396,21 @@ Lua&nbsp;5.1, which prevents implementing features that would otherwise
301break the Lua/C API and ABI (e.g. <tt>_ENV</tt>). 396break the Lua/C API and ABI (e.g. <tt>_ENV</tt>).
302</p> 397</p>
303 398
399<h2 id="lua53">Extensions from Lua 5.3</h2>
400<p>
401LuaJIT supports some extensions from Lua&nbsp;5.3:
402<ul>
403<li>Unicode escape <tt>'\u{XX...}'</tt> embeds the UTF-8 encoding in string literals.</li>
404<li>The argument table <tt>arg</tt> can be read (and modified) by <tt>LUA_INIT</tt> and <tt>-e</tt> chunks.</li>
405<li><tt>io.read()</tt> and <tt>file:read()</tt> accept formats with or without a leading <tt>*</tt>.</li>
406<li><tt>assert()</tt> accepts any type of error object.</li>
407<li><tt>table.move(a1, f, e, t [,a2])</tt>.</li>
408<li><tt>coroutine.isyieldable()</tt>.</li>
409<li>Lua/C API extensions:
410<tt>lua_isyieldable()</tt>
411</li>
412</ul>
413
304<h2 id="exceptions">C++ Exception Interoperability</h2> 414<h2 id="exceptions">C++ Exception Interoperability</h2>
305<p> 415<p>
306LuaJIT has built-in support for interoperating with C++&nbsp;exceptions. 416LuaJIT has built-in support for interoperating with C++&nbsp;exceptions.
@@ -314,26 +424,21 @@ the toolchain used to compile LuaJIT:
314<td class="excinterop">Interoperability</td> 424<td class="excinterop">Interoperability</td>
315</tr> 425</tr>
316<tr class="odd separate"> 426<tr class="odd separate">
317<td class="excplatform">POSIX/x64, DWARF2 unwinding</td> 427<td class="excplatform">External frame unwinding</td>
318<td class="exccompiler">GCC 4.3+</td> 428<td class="exccompiler">GCC, Clang, MSVC</td>
319<td class="excinterop"><b style="color: #00a000;">Full</b></td> 429<td class="excinterop"><b style="color: #00a000;">Full</b></td>
320</tr> 430</tr>
321<tr class="even"> 431<tr class="even">
322<td class="excplatform">Other platforms, DWARF2 unwinding</td> 432<td class="excplatform">Internal frame unwinding + DWARF2</td>
323<td class="exccompiler">GCC</td> 433<td class="exccompiler">GCC, Clang</td>
324<td class="excinterop"><b style="color: #c06000;">Limited</b></td> 434<td class="excinterop"><b style="color: #c06000;">Limited</b></td>
325</tr> 435</tr>
326<tr class="odd"> 436<tr class="odd">
327<td class="excplatform">Windows/x64</td> 437<td class="excplatform">Windows 64 bit</td>
328<td class="exccompiler">MSVC</td> 438<td class="exccompiler">non-MSVC</td>
329<td class="excinterop"><b style="color: #00a000;">Full</b></td> 439<td class="excinterop"><b style="color: #c06000;">Limited</b></td>
330</tr> 440</tr>
331<tr class="even"> 441<tr class="even">
332<td class="excplatform">Windows/x86</td>
333<td class="exccompiler">Any</td>
334<td class="excinterop"><b style="color: #a00000;">No</b></td>
335</tr>
336<tr class="odd">
337<td class="excplatform">Other platforms</td> 442<td class="excplatform">Other platforms</td>
338<td class="exccompiler">Other compilers</td> 443<td class="exccompiler">Other compilers</td>
339<td class="excinterop"><b style="color: #a00000;">No</b></td> 444<td class="excinterop"><b style="color: #a00000;">No</b></td>
@@ -352,9 +457,7 @@ the toolchain used to compile LuaJIT:
352on the C&nbsp;stack. The contents of the C++&nbsp;exception object 457on the C&nbsp;stack. The contents of the C++&nbsp;exception object
353pass through unmodified.</li> 458pass through unmodified.</li>
354<li>Lua errors can be caught on the C++ side with <tt>catch(...)</tt>. 459<li>Lua errors can be caught on the C++ side with <tt>catch(...)</tt>.
355The corresponding Lua error message can be retrieved from the Lua stack.<br> 460The corresponding Lua error message can be retrieved from the Lua stack.</li>
356For MSVC for Windows 64 bit this requires compilation of your C++ code
357with <tt>/EHa</tt>.</li>
358<li>Throwing Lua errors across C++ frames is safe. C++ destructors 461<li>Throwing Lua errors across C++ frames is safe. C++ destructors
359will be called.</li> 462will be called.</li>
360</ul> 463</ul>
@@ -384,14 +487,6 @@ C++ destructors.</li>
384<li>Lua errors <b>cannot</b> be caught on the C++ side.</li> 487<li>Lua errors <b>cannot</b> be caught on the C++ side.</li>
385<li>Throwing Lua errors across C++ frames will <b>not</b> call 488<li>Throwing Lua errors across C++ frames will <b>not</b> call
386C++ destructors.</li> 489C++ destructors.</li>
387<li>Additionally, on Windows/x86 with SEH-based C++&nbsp;exceptions:
388it's <b>not</b> safe to throw a Lua error across any frames containing
389a C++ function with any try/catch construct or using variables with
390(implicit) destructors. This also applies to any functions which may be
391inlined in such a function. It doesn't matter whether <tt>lua_error()</tt>
392is called inside or outside of a try/catch or whether any object actually
393needs to be destroyed: the SEH chain is corrupted and this will eventually
394lead to the termination of the process.</li>
395</ul> 490</ul>
396<br class="flush"> 491<br class="flush">
397</div> 492</div>