diff options
Diffstat (limited to 'doc/extensions.html')
| -rw-r--r-- | doc/extensions.html | 169 |
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">»</span></a> | 66 | <a href="https://luajit.org/status.html">Status <span class="ext">»</span></a> |
| @@ -106,6 +110,9 @@ bit.lshift bit.rshift bit.arshift bit.rol bit.ror bit.bswap | |||
| 106 | This module is a LuaJIT built-in — you don't need to download or | 110 | This module is a LuaJIT built-in — you don't need to download or |
| 107 | install Lua BitOp. The Lua BitOp site has full documentation for all | 111 | install Lua BitOp. The Lua BitOp site has full documentation for all |
| 108 | <a href="https://bitop.luajit.org/api.html"><span class="ext">»</span> Lua BitOp API functions</a>. | 112 | <a href="https://bitop.luajit.org/api.html"><span class="ext">»</span> Lua BitOp API functions</a>. |
| 113 | The FFI adds support for | ||
| 114 | <a href="ext_ffi_semantics.html#cdata_arith">64 bit bitwise operations</a>, | ||
| 115 | using the same API functions. | ||
| 109 | </p> | 116 | </p> |
| 110 | <p> | 117 | <p> |
| 111 | Please make sure to <tt>require</tt> the module before using any of | 118 | Please 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> | ||
| 151 | LuaJIT 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 | |||
| 148 | which is called in a protected context. | 160 | which 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> |
| 153 | Non-ASCII characters are handled transparently by the Lua source code parser. | 165 | Non-ASCII characters are handled transparently by the Lua source code parser. |
| 154 | This allows the use of UTF-8 characters in identifiers and strings. | 166 | This allows the use of UTF-8 characters in identifiers and strings. |
| 155 | A UTF-8 BOM is skipped at the start of the source code. | 167 | A 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> | ||
| 172 | As 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> | ||
| 177 | The default mode string is <tt>"bt"</tt>, which allows loading of both | ||
| 178 | source code and bytecode. Use <tt>"t"</tt> to allow only source code | ||
| 179 | or <tt>"b"</tt> to allow only bytecode to be loaded. | ||
| 180 | </p> | ||
| 181 | <p> | ||
| 182 | By default, the <tt>load*</tt> functions generate the native bytecode format. | ||
| 183 | For cross-compilation purposes, add <tt>W</tt> to the mode string to | ||
| 184 | force the 32 bit format and <tt>X</tt> to force the 64 bit format. | ||
| 185 | Add both to force the opposite format. Note that non-native bytecode | ||
| 186 | generated by <tt>load*</tt> cannot be run, but can still be passed | ||
| 187 | to <tt>string.dump</tt>. | ||
| 188 | </p> | ||
| 189 | |||
| 158 | <h3 id="tostring"><tt>tostring()</tt> etc. canonicalize NaN and ±Inf</h3> | 190 | <h3 id="tostring"><tt>tostring()</tt> etc. canonicalize NaN and ±Inf</h3> |
| 159 | <p> | 191 | <p> |
| 160 | All number-to-string conversions consistently convert non-finite numbers | 192 | All 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> |
| 168 | All string-to-number conversions consistently convert integer and | 200 | All string-to-number conversions consistently convert integer and |
| 169 | floating-point inputs in decimal and hexadecimal on all platforms. | 201 | floating-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 |
| 171 | problems with poor C library implementations. The builtin conversion | 203 | problems with poor C library implementations. The builtin conversion |
| 172 | function provides full precision according to the IEEE-754 standard, it | 204 | function 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 | |||
| 174 | numbers (e.g. <tt>0x1.5p-3</tt>). | 206 | numbers (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> |
| 179 | An extra argument has been added to <tt>string.dump()</tt>. If set to | 211 | An 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>, |
| 181 | generated. This speeds up later bytecode loading and reduces memory | 213 | 'stripped' bytecode without debug information is generated. This speeds |
| 182 | usage. See also the | 214 | up 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> |
| 186 | The generated bytecode is portable and can be loaded on any architecture | 218 | The generated bytecode is portable and can be loaded on any architecture |
| 187 | that LuaJIT supports, independent of word size or endianess. However, the | 219 | that LuaJIT supports. However, the bytecode compatibility versions must |
| 188 | bytecode compatibility versions must match. Bytecode stays compatible | 220 | match. Bytecode only stays compatible within a major+minor version |
| 189 | for dot releases (x.y.0 → x.y.1), but may change with major or | 221 | (x.y.aaa → x.y.bbb), except for development branches. Foreign bytecode |
| 190 | minor releases (2.0 → 2.1) or between any beta release. Foreign | 222 | (e.g. from Lua 5.1) is incompatible and cannot be loaded. |
| 191 | bytecode (e.g. from Lua 5.1) is incompatible and cannot be loaded. | 223 | </p> |
| 224 | <p> | ||
| 225 | Note: <tt>LJ_GC64</tt> mode requires a different frame layout, which implies | ||
| 226 | a different, incompatible bytecode format between 32 bit and 64 bit ports. | ||
| 227 | This may be rectified in the future. In the meantime, use the <tt>W</tt> | ||
| 228 | and </tt>X</tt> <a href="#load_mode">modes of the <tt>load*</tt> functions</a> | ||
| 229 | for cross-compilation purposes. | ||
| 230 | </p> | ||
| 231 | <p> | ||
| 232 | Due to VM hardening, bytecode is not deterministic. Add <tt>d</tt> to the | ||
| 233 | mode string to dump it in a deterministic manner: identical source code | ||
| 234 | always gives a byte-for-byte identical bytecode dump. This feature is | ||
| 235 | mainly 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> | ||
| 240 | An 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 | ||
| 242 | the C API equivalent <tt>lua_createtable()</tt>. This is useful for big | ||
| 243 | tables if the final table size is known and automatic table resizing is | ||
| 244 | too expensive. | ||
| 245 | </p> | ||
| 246 | |||
| 247 | <h3 id="table_clear"><tt>table.clear(tab)</tt> clears a table</h3> | ||
| 248 | <p> | ||
| 249 | An extra library function <tt>table.clear()</tt> can be made available | ||
| 250 | via <tt>require("table.clear")</tt>. This clears all keys and values | ||
| 251 | from a table, but preserves the allocated array/hash sizes. This is | ||
| 252 | useful when a table, which is linked from multiple places, needs to be | ||
| 253 | cleared and/or when recycling a table for use by the same context. This | ||
| 254 | avoids managing backlinks, saves an allocation and the overhead of | ||
| 255 | incremental array/hash part growth. | ||
| 256 | </p> | ||
| 257 | <p> | ||
| 258 | Please note, this function is meant for very specific situations. In most | ||
| 259 | cases it's better to replace the (usually single) link with a new table | ||
| 260 | and 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. | |||
| 196 | LuaJIT uses a Tausworthe PRNG with period 2^223 to implement | 265 | LuaJIT 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 |
| 198 | the PRNG results is much superior compared to the standard Lua | 267 | the PRNG results is much superior compared to the standard Lua |
| 199 | implementation, which uses the platform-specific ANSI rand(). | 268 | implementation, which uses the platform-specific ANSI <tt>rand()</tt>. |
| 200 | </p> | 269 | </p> |
| 201 | <p> | 270 | <p> |
| 202 | The PRNG generates the same sequences from the same seeds on all | 271 | The 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 [,m])</tt> to | |||
| 207 | preserve uniformity. | 276 | preserve uniformity. |
| 208 | </p> | 277 | </p> |
| 209 | <p> | 278 | <p> |
| 279 | Call <tt>math.randomseed()</tt> without any arguments to seed it from | ||
| 280 | system entropy. | ||
| 281 | </p> | ||
| 282 | <p> | ||
| 210 | Important: Neither this nor any other PRNG based on the simplistic | 283 | Important: 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 |
| 269 | C functions.</li> | 342 | C 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> |
| 298 | Note: this provides only partial compatibility with Lua 5.2 at the | 393 | Note: this provides only partial compatibility with Lua 5.2 at the |
| @@ -301,6 +396,21 @@ Lua 5.1, which prevents implementing features that would otherwise | |||
| 301 | break the Lua/C API and ABI (e.g. <tt>_ENV</tt>). | 396 | break 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> | ||
| 401 | LuaJIT supports some extensions from Lua 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> |
| 306 | LuaJIT has built-in support for interoperating with C++ exceptions. | 416 | LuaJIT has built-in support for interoperating with C++ 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: | |||
| 352 | on the C stack. The contents of the C++ exception object | 457 | on the C stack. The contents of the C++ exception object |
| 353 | pass through unmodified.</li> | 458 | pass 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>. |
| 355 | The corresponding Lua error message can be retrieved from the Lua stack.<br> | 460 | The corresponding Lua error message can be retrieved from the Lua stack.</li> |
| 356 | For MSVC for Windows 64 bit this requires compilation of your C++ code | ||
| 357 | with <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 |
| 359 | will be called.</li> | 462 | will 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 |
| 386 | C++ destructors.</li> | 489 | C++ destructors.</li> |
| 387 | <li>Additionally, on Windows/x86 with SEH-based C++ exceptions: | ||
| 388 | it's <b>not</b> safe to throw a Lua error across any frames containing | ||
| 389 | a C++ function with any try/catch construct or using variables with | ||
| 390 | (implicit) destructors. This also applies to any functions which may be | ||
| 391 | inlined in such a function. It doesn't matter whether <tt>lua_error()</tt> | ||
| 392 | is called inside or outside of a try/catch or whether any object actually | ||
| 393 | needs to be destroyed: the SEH chain is corrupted and this will eventually | ||
| 394 | lead to the termination of the process.</li> | ||
| 395 | </ul> | 490 | </ul> |
| 396 | <br class="flush"> | 491 | <br class="flush"> |
| 397 | </div> | 492 | </div> |
