You can also send any questions you have directly to me:
@@ -84,10 +84,17 @@
Contact
+
+Note: I cannot reply to GMail, Google Workplace, Outlook or Office365
+mail addresses, since they prefer to mindlessly filter out mails sent
+from small domains using independent mail servers, such as mine. If you
+don't like that, please complain to Google or Microsoft, not me.
+
+Unlike Lua strings, which are constants, string buffers are
+mutable sequences of 8-bit (binary-transparent) characters. Data
+can be stored, formatted and encoded into a string buffer and later
+converted, extracted or decoded.
+
+
+The convenient string buffer API simplifies common string manipulation
+tasks, that would otherwise require creating many intermediate strings.
+String buffers improve performance by eliminating redundant memory
+copies, object creation, string interning and garbage collection
+overhead. In conjunction with the FFI library, they allow zero-copy
+operations.
+
+
+The string buffer library also includes a high-performance
+serializer for Lua objects.
+
+
+
Work in Progress
+
+This library is a work in progress. More
+functionality will be added soon.
+
+
+
Using the String Buffer Library
+
+The string buffer library is built into LuaJIT by default, but it's not
+loaded by default. Add this to the start of every Lua file that needs
+one of its functions:
+
+
+local buffer = require("string.buffer")
+
+
+The convention for the syntax shown on this page is that buffer
+refers to the buffer library and buf refers to an individual
+buffer object.
+
+
+Please note the difference between a Lua function call, e.g.
+buffer.new() (with a dot) and a Lua method call, e.g.
+buf:reset() (with a colon).
+
+
+
Buffer Objects
+
+A buffer object is a garbage-collected Lua object. After creation with
+buffer.new(), it can (and should) be reused for many operations.
+When the last reference to a buffer object is gone, it will eventually
+be freed by the garbage collector, along with the allocated buffer
+space.
+
+
+Buffers operate like a FIFO (first-in first-out) data structure. Data
+can be appended (written) to the end of the buffer and consumed (read)
+from the front of the buffer. These operations may be freely mixed.
+
+
+The buffer space that holds the characters is managed automatically
+— it grows as needed and already consumed space is recycled. Use
+buffer.new(size) and buf:free(), if you need more
+control.
+
+
+The maximum size of a single buffer is the same as the maximum size of a
+Lua string, which is slightly below two gigabytes. For huge data sizes,
+neither strings nor buffers are the right data structure — use the
+FFI library to directly map memory or files up to the virtual memory
+limit of your OS.
+
+
+
Buffer Method Overview
+
+
+The buf:put*()-like methods append (write) characters to the
+end of the buffer.
+
+
+The buf:get*()-like methods consume (read) characters from the
+front of the buffer.
+
+
+Other methods, like buf:tostring() only read the buffer
+contents, but don't change the buffer.
+
+
+The buf:set() method allows zero-copy consumption of a string
+or an FFI cdata object as a buffer.
+
+
+The FFI-specific methods allow zero-copy read/write-style operations or
+modifying the buffer contents in-place. Please check the
+FFI caveats below, too.
+
+
+Methods that don't need to return anything specific, return the buffer
+object itself as a convenience. This allows method chaining, e.g.:
+buf:reset():encode(obj) or buf:skip(len):get()
+
+
+
+
Buffer Creation and Management
+
+
local buf = buffer.new([size [,options]])
+local buf = buffer.new([options])
+
+Creates a new buffer object.
+
+
+The optional size argument ensures a minimum initial buffer
+size. This is strictly an optimization when the required buffer size is
+known beforehand. The buffer space will grow as needed, in any case.
+
+Reset (empty) the buffer. The allocated buffer space is not freed and
+may be reused.
+
+
+
buf = buf:free()
+
+The buffer space of the buffer object is freed. The object itself
+remains intact, empty and may be reused.
+
+
+Note: you normally don't need to use this method. The garbage collector
+automatically frees the buffer space, when the buffer object is
+collected. Use this method, if you need to free the associated memory
+immediately.
+
+
+
Buffer Writers
+
+
buf = buf:put([str|num|obj] [,…])
+
+Appends a string str, a number num or any object
+obj with a __tostring metamethod to the buffer.
+Multiple arguments are appended in the given order.
+
+
+Appending a buffer to a buffer is possible and short-circuited
+internally. But it still involves a copy. Better combine the buffer
+writes to use a single buffer.
+
+
+
buf = buf:putf(format, …)
+
+Appends the formatted arguments to the buffer. The format
+string supports the same options as string.format().
+
+
+
buf = buf:putcdata(cdata, len)FFI
+
+Appends the given len number of bytes from the memory pointed
+to by the FFI cdata object to the buffer. The object needs to
+be convertible to a (constant) pointer.
+
+
+
buf = buf:set(str)
+buf = buf:set(cdata, len)FFI
+
+This method allows zero-copy consumption of a string or an FFI cdata
+object as a buffer. It stores a reference to the passed string
+str or the FFI cdata object in the buffer. Any buffer
+space originally allocated is freed. This is not an append
+operation, unlike the buf:put*() methods.
+
+
+After calling this method, the buffer behaves as if
+buf:free():put(str) or buf:free():put(cdata, len)
+had been called. However, the data is only referenced and not copied, as
+long as the buffer is only consumed.
+
+
+In case the buffer is written to later on, the referenced data is copied
+and the object reference is removed (copy-on-write semantics).
+
+
+The stored reference is an anchor for the garbage collector and keeps the
+originally passed string or FFI cdata object alive.
+
+
+
ptr, len = buf:reserve(size)FFI
+buf = buf:commit(used)FFI
+
+The reserve method reserves at least size bytes of
+write space in the buffer. It returns an uint8_t * FFI
+cdata pointer ptr that points to this space.
+
+
+The available length in bytes is returned in len. This is at
+least size bytes, but may be more to facilitate efficient
+buffer growth. You can either make use of the additional space or ignore
+len and only use size bytes.
+
+
+The commit method appends the used bytes of the
+previously returned write space to the buffer data.
+
+
+This pair of methods allows zero-copy use of C read-style APIs:
+
+
+local MIN_SIZE = 65536
+repeat
+ local ptr, len = buf:reserve(MIN_SIZE)
+ local n = C.read(fd, ptr, len)
+ if n == 0 then break end -- EOF.
+ if n < 0 then error("read error") end
+ buf:commit(n)
+until false
+
+
+The reserved write space is not initialized. At least the
+used bytes must be written to before calling the
+commit method. There's no need to call the commit
+method, if nothing is added to the buffer (e.g. on error).
+
+
+
Buffer Readers
+
+
len = #buf
+
+Returns the current length of the buffer data in bytes.
+
+
+
res = str|num|buf .. str|num|buf […]
+
+The Lua concatenation operator .. also accepts buffers, just
+like strings or numbers. It always returns a string and not a buffer.
+
+
+Note that although this is supported for convenience, this thwarts one
+of the main reasons to use buffers, which is to avoid string
+allocations. Rewrite it with buf:put() and buf:get().
+
+
+Mixing this with unrelated objects that have a __concat
+metamethod may not work, since these probably only expect strings.
+
+
+
buf = buf:skip(len)
+
+Skips (consumes) len bytes from the buffer up to the current
+length of the buffer data.
+
+
+
str, … = buf:get([len|nil] [,…])
+
+Consumes the buffer data and returns one or more strings. If called
+without arguments, the whole buffer data is consumed. If called with a
+number, up to len bytes are consumed. A nil argument
+consumes the remaining buffer space (this only makes sense as the last
+argument). Multiple arguments consume the buffer data in the given
+order.
+
+
+Note: a zero length or no remaining buffer data returns an empty string
+and not nil.
+
+
+
str = buf:tostring()
+str = tostring(buf)
+
+Creates a string from the buffer data, but doesn't consume it. The
+buffer remains unchanged.
+
+
+Buffer objects also define a __tostring metamethod. This means
+buffers can be passed to the global tostring() function and
+many other functions that accept this in place of strings. The important
+internal uses in functions like io.write() are short-circuited
+to avoid the creation of an intermediate string object.
+
+
+
ptr, len = buf:ref()FFI
+
+Returns an uint8_t * FFI cdata pointer ptr that
+points to the buffer data. The length of the buffer data in bytes is
+returned in len.
+
+
+The returned pointer can be directly passed to C functions that expect a
+buffer and a length. You can also do bytewise reads
+(local x = ptr[i]) or writes
+(ptr[i] = 0x40) of the buffer data.
+
+
+In conjunction with the skip method, this allows zero-copy use
+of C write-style APIs:
+
+
+repeat
+ local ptr, len = buf:ref()
+ if len == 0 then break end
+ local n = C.write(fd, ptr, len)
+ if n < 0 then error("write error") end
+ buf:skip(n)
+until n >= len
+
+
+Unlike Lua strings, buffer data is not implicitly
+zero-terminated. It's not safe to pass ptr to C functions that
+expect zero-terminated strings. If you're not using len, then
+you're doing something wrong.
+
+
+
Serialization of Lua Objects
+
+The following functions and methods allow high-speed serialization
+(encoding) of a Lua object into a string and decoding it back to a Lua
+object. This allows convenient storage and transport of structured
+data.
+
+
+The encoded data is in an internal binary
+format. The data can be stored in files, binary-transparent
+databases or transmitted to other LuaJIT instances across threads,
+processes or networks.
+
+
+Encoding speed can reach up to 1 Gigabyte/second on a modern desktop- or
+server-class system, even when serializing many small objects. Decoding
+speed is mostly constrained by object creation cost.
+
+
+The serializer handles most Lua types, common FFI number types and
+nested structures. Functions, thread objects, other FFI cdata and full
+userdata cannot be serialized (yet).
+
+
+The encoder serializes nested structures as trees. Multiple references
+to a single object will be stored separately and create distinct objects
+after decoding. Circular references cause an error.
+
+
+
Serialization Functions and Methods
+
+
str = buffer.encode(obj)
+buf = buf:encode(obj)
+
+Serializes (encodes) the Lua object obj. The stand-alone
+function returns a string str. The buffer method appends the
+encoding to the buffer.
+
+
+obj can be any of the supported Lua types — it doesn't
+need to be a Lua table.
+
+
+This function may throw an error when attempting to serialize
+unsupported object types, circular references or deeply nested tables.
+
+
+
obj = buffer.decode(str)
+obj = buf:decode()
+
+The stand-alone function deserializes (decodes) the string
+str, the buffer method deserializes one object from the
+buffer. Both return a Lua object obj.
+
+
+The returned object may be any of the supported Lua types —
+even nil.
+
+
+This function may throw an error when fed with malformed or incomplete
+encoded data. The stand-alone function throws when there's left-over
+data after decoding a single top-level object. The buffer method leaves
+any left-over data in the buffer.
+
+
+Attempting to deserialize an FFI type will throw an error, if the FFI
+library is not built-in or has not been loaded, yet.
+
+
+
Serialization Options
+
+The options table passed to buffer.new() may contain
+the following members (all optional):
+
+
+
+dict is a Lua table holding a dictionary of strings that
+commonly occur as table keys of objects you are serializing. These keys
+are compactly encoded as indexes during serialization. A well-chosen
+dictionary saves space and improves serialization performance.
+
+
+metatable is a Lua table holding a dictionary of metatables
+for the table objects you are serializing.
+
+
+
+dict needs to be an array of strings and metatable needs
+to be an array of tables. Both starting at index 1 and without holes (no
+nil in between). The tables are anchored in the buffer object and
+internally modified into a two-way index (don't do this yourself, just pass
+a plain array). The tables must not be modified after they have been passed
+to buffer.new().
+
+
+The dict and metatable tables used by the encoder and
+decoder must be the same. Put the most common entries at the front. Extend
+at the end to ensure backwards-compatibility — older encodings can
+then still be read. You may also set some indexes to false to
+explicitly drop backwards-compatibility. Old encodings that use these
+indexes will throw an error when decoded.
+
+
+Metatables that are not found in the metatable dictionary are
+ignored when encoding. Decoding returns a table with a nil
+metatable.
+
+
+Note: parsing and preparation of the options table is somewhat
+expensive. Create a buffer object only once and recycle it for multiple
+uses. Avoid mixing encoder and decoder buffers, since the
+buf:set() method frees the already allocated buffer space:
+
+In some contexts, it's desirable to do piecewise serialization of large
+datasets, also known as streaming.
+
+
+This serialization format can be safely concatenated and supports streaming.
+Multiple encodings can simply be appended to a buffer and later decoded
+individually:
+
+while #buf ~= 0 do
+ local obj = buf:decode()
+ -- Do something with obj.
+end
+
+
+Since the serialization format doesn't prepend a length to its encoding,
+network applications may need to transmit the length, too.
+
+
+
Serialization Format Specification
+
+This serialization format is designed for internal use by LuaJIT
+applications. Serialized data is upwards-compatible and portable across
+all supported LuaJIT platforms.
+
+
+It's an 8-bit binary format and not human-readable. It uses e.g.
+embedded zeroes and stores embedded Lua string objects unmodified, which
+are 8-bit-clean, too. Encoded data can be safely concatenated for
+streaming and later decoded one top-level object at a time.
+
+
+The encoding is reasonably compact, but tuned for maximum performance,
+not for minimum space usage. It compresses well with any of the common
+byte-oriented data compression algorithms.
+
+
+Although documented here for reference, this format is explicitly
+not intended to be a 'public standard' for structured data
+interchange across computer languages (like JSON or MessagePack). Please
+do not use it as such.
+
+
+The specification is given below as a context-free grammar with a
+top-level object as the starting point. Alternatives are
+separated by the | symbol and * indicates repeats.
+Grouping is implicit or indicated by {…}. Terminals are
+either plain hex numbers, encoded as bytes, or have a .format
+suffix.
+
+Many of the buffer methods can throw an error. Out-of-memory or usage
+errors are best caught with an outer wrapper for larger parts of code.
+There's not much one can do after that, anyway.
+
+
+OTOH, you may want to catch some errors individually. Buffer methods need
+to receive the buffer object as the first argument. The Lua colon-syntax
+obj:method() does that implicitly. But to wrap a method with
+pcall(), the arguments need to be passed like this:
+
+
+local ok, err = pcall(buf.encode, buf, obj)
+if not ok then
+ -- Handle error in err.
+end
+
+
+
FFI caveats
+
+The string buffer library has been designed to work well together with
+the FFI library. But due to the low-level nature of the FFI library,
+some care needs to be taken:
+
+
+First, please remember that FFI pointers are zero-indexed. The space
+returned by buf:reserve() and buf:ref() starts at the
+returned pointer and ends before len bytes after that.
+
+
+I.e. the first valid index is ptr[0] and the last valid index
+is ptr[len-1]. If the returned length is zero, there's no valid
+index at all. The returned pointer may even be NULL.
+
+
+The space pointed to by the returned pointer is only valid as long as
+the buffer is not modified in any way (neither append, nor consume, nor
+reset, etc.). The pointer is also not a GC anchor for the buffer object
+itself.
+
+
+Buffer data is only guaranteed to be byte-aligned. Casting the returned
+pointer to a data type with higher alignment may cause unaligned
+accesses. It depends on the CPU architecture whether this is allowed or
+not (it's always OK on x86/x64 and mostly OK on other modern
+architectures).
+
+
+FFI pointers or references do not count as GC anchors for an underlying
+object. E.g. an array allocated with ffi.new() is
+anchored by buf:set(array, len), but not by
+buf:set(array+offset, len). The addition of the offset
+creates a new pointer, even when the offset is zero. In this case, you
+need to make sure there's still a reference to the original array as
+long as its contents are in use by the buffer.
+
+
+Even though each LuaJIT VM instance is single-threaded (but you can
+create multiple VMs), FFI data structures can be accessed concurrently.
+Be careful when reading/writing FFI cdata from/to buffers to avoid
+concurrent accesses or modifications. In particular, the memory
+referenced by buf:set(cdata, len) must not be modified
+while buffer readers are working on it. Shared, but read-only memory
+mappings of files are OK, but only if the file does not change.
+
luaJIT_setmode(L, idx, LUAJIT_MODE_FUNC|flag)
This sets the mode for the function at the stack index idx or
the parent of the calling function (idx = 0). It either
enables JIT compilation for a function, disables it and flushes any
-already compiled code or only flushes already compiled code. This
+already compiled code, or only flushes already compiled code. This
applies recursively to all sub-functions of the function with
LUAJIT_MODE_ALLFUNC or only to the sub-functions with
LUAJIT_MODE_ALLSUBFUNC.
@@ -124,7 +124,7 @@
luaJIT_setmode(L, idx, LUAJIT_MODE_WRAPCFUNC|flag)
This mode defines a wrapper function for calls to C functions. If
called with LUAJIT_MODE_ON, the stack index at idx
must be a lightuserdata object holding a pointer to the wrapper
-function. From now on all C functions are called through the wrapper
+function. From now on, all C functions are called through the wrapper
function. If called with LUAJIT_MODE_OFF this mode is turned
off and all C functions are directly called.
@@ -173,7 +173,7 @@
The FFI library allows you to create and access C data
-structures. Of course the main use for this is for interfacing with
+structures. Of course, the main use for this is for interfacing with
C functions. But they can be used stand-alone, too.
@@ -167,7 +167,7 @@
Motivating Example: Using C Data Structures
both a substantial memory overhead as well as a performance overhead.
-Here's a sketch of a library that operates on color images plus a
+Here's a sketch of a library that operates on color images, plus a
simple benchmark. First, the plain Lua version:
@@ -182,7 +182,7 @@
Motivating Example: Using C Data Structures
return img
end
-local function image_to_grey(img, n)
+local function image_to_gray(img, n)
for i=1,n do
local y = floor(0.3*img[i].red + 0.59*img[i].green + 0.11*img[i].blue)
img[i].red = y; img[i].green = y; img[i].blue = y
@@ -192,14 +192,14 @@
Motivating Example: Using C Data Structures
local N = 400*400
local img = image_ramp_green(N)
for i=1,1000 do
- image_to_grey(img, N)
+ image_to_gray(img, N)
end
This creates a table with 160.000 pixels, each of which is a table
-holding four number values in the range of 0-255. First an image with
+holding four number values in the range of 0-255. First, an image with
a green ramp is created (1D for simplicity), then the image is
-converted to greyscale 1000 times. Yes, that's silly, but I was in
+converted to grayscale 1000 times. Yes, that's silly, but I was in
need of a simple example ...
@@ -306,7 +306,7 @@
Motivating Example: Using C Data Structures
~1.7x). Switching to a struct-of-arrays would help, too.
-However the resulting code would be less idiomatic and rather
+However, the resulting code would be less idiomatic and rather
error-prone. And it still doesn't get even close to the performance of
the FFI version of the code. Also, high-level data structures cannot
be easily passed to other C functions, especially I/O functions,
@@ -316,7 +316,7 @@
-Please note that external symbols are only declared, but they
+Please note, that external symbols are only declared, but they
are not bound to any specific address, yet. Binding is
achieved with C library namespaces (see below).
@@ -207,7 +207,7 @@
cdata = ffi.new(ct [,nelem] [,init...]) ffi.typeof(). Then use the ctype as a constructor repeatedly.
-Please note that an anonymous struct declaration implicitly
+Please note, that an anonymous struct declaration implicitly
creates a new and distinguished ctype every time you use it for
ffi.new(). This is probably not what you want,
especially if you create more than one cdata object. Different anonymous
@@ -254,12 +254,12 @@
ctype = ffi.metatype(ct, metatable)
contents of an __index table (if any) may be modified
afterwards. The associated metatable automatically applies to all uses
of this type, no matter how the objects are created or where they
-originate from. Note that pre-defined operations on types have
-precedence (e.g. declared field names cannot be overriden).
+originate from. Note that predefined operations on types have
+precedence (e.g. declared field names cannot be overridden).
All standard Lua metamethods are implemented. These are called directly,
-without shortcuts and on any mix of types. For binary operations, the
+without shortcuts, and on any mix of types. For binary operations, the
left operand is checked first for a valid ctype metamethod. The
__gc metamethod only applies to struct/union
types and performs an implicit ffi.gc()
@@ -490,7 +490,7 @@
cb:free()
Free the resources associated with a callback. The associated Lua
function is unanchored and may be garbage collected. The callback
-function pointer is no longer valid and must not be called anymore
+function pointer is no longer valid and must not be called again
(it may be reused by a subsequently created callback).
-It's only purpose is to parse C declarations, as found e.g. in
+Its only purpose is to parse C declarations, as found e.g. in
C header files. Although it does evaluate constant expressions,
it's not a C compiler. The body of inline
C function definitions is simply ignored.
@@ -163,7 +163,7 @@
C Language Support
-The following C types are pre-defined by the C parser (like
+The following C types are predefined by the C parser (like
a typedef, except re-declarations will be ignored):
@@ -581,9 +581,9 @@
Table Initializers
Operations on cdata Objects
-All of the standard Lua operators can be applied to cdata objects or a
+All standard Lua operators can be applied to cdata objects or a
mix of a cdata object and another Lua object. The following list shows
-the pre-defined operations.
+the predefined operations.
Reference types are dereferenced before performing each of
@@ -591,7 +591,7 @@
Operations on cdata Objects
C type pointed to by the reference.
-The pre-defined operations are always tried first before deferring to a
+The predefined operations are always tried first before deferring to a
metamethod or index table (if any) for the corresponding ctype (except
for __new). An error is raised if the metamethod lookup or
index table lookup fails.
@@ -641,7 +641,7 @@
Indexing a cdata object
A ctype object can be indexed with a string key, too. The only
-pre-defined operation is reading scoped constants of
+predefined operation is reading scoped constants of
struct/union types. All other accesses defer
to the corresponding metamethods or index tables (if any).
@@ -654,7 +654,7 @@
Indexing a cdata object
As a consequence, the elements of complex numbers and
vectors are immutable. But the elements of an aggregate holding these
-types may be modified of course. I.e. you cannot assign to
+types may be modified, of course. I.e. you cannot assign to
foo.c.im, but you can assign a (newly created) complex number
to foo.c.
@@ -672,9 +672,9 @@
Calling a cdata object
constructor. This is equivalent
to ffi.new(ct, ...), unless a __new metamethod is
defined. The __new metamethod is called with the ctype object
-plus any other arguments passed to the contructor. Note that you have to
-use ffi.new inside of it, since calling ct(...) would
-cause infinite recursion.
+plus any other arguments passed to the constructor. Note that you have to
+use ffi.new inside the metamethod, since calling ct(...)
+would cause infinite recursion.
C function call: a cdata function or cdata function
pointer can be called. The passed arguments are
@@ -685,7 +685,7 @@
Calling a cdata object
C function is called and the return value (if any) is
converted to a Lua object.
On Windows/x86 systems, __stdcall functions are automatically
-detected and a function declared as __cdecl (the default) is
+detected, and a function declared as __cdecl (the default) is
silently fixed up after the first call.
@@ -695,7 +695,7 @@
Arithmetic on cdata objects
Pointer arithmetic: a cdata pointer/array and a cdata
number or a Lua number can be added or subtracted. The number must be
-on the right hand side for a subtraction. The result is a pointer of
+on the right-hand side for a subtraction. The result is a pointer of
the same type with an address plus or minus the number value
multiplied by the element size in bytes. An error is raised if the
element size is undefined.
@@ -710,7 +710,7 @@
Arithmetic on cdata objects
minus) can be applied to two cdata numbers, or a cdata number and a
Lua number. If one of them is an uint64_t, the other side is
converted to an uint64_t and an unsigned arithmetic operation
-is performed. Otherwise both sides are converted to an
+is performed. Otherwise, both sides are converted to an
int64_t and a signed arithmetic operation is performed. The
result is a boxed 64 bit cdata object.
@@ -757,7 +757,7 @@
Comparisons of cdata objects
64 bit integer comparison: two cdata numbers, or a
cdata number and a Lua number can be compared with each other. If one
of them is an uint64_t, the other side is converted to an
-uint64_t and an unsigned comparison is performed. Otherwise
+uint64_t and an unsigned comparison is performed. Otherwise,
both sides are converted to an int64_t and a signed
comparison is performed.
@@ -782,9 +782,9 @@
cdata objects as table keys
A cdata object is treated like any other garbage-collected object and
is hashed and compared by its address for table indexing. Since
there's no interning for cdata value types, the same value may be
-boxed in different cdata objects with different addresses. Thus
+boxed in different cdata objects with different addresses. Thus,
t[1LL+1LL] and t[2LL] usually do not point to
-the same hash slot and they certainly do not point to the same
+the same hash slot, and they certainly do not point to the same
hash slot as t[2].
@@ -806,7 +806,7 @@
cdata objects as table keys
One obvious benefit: t[tonumber(2LL)]does point to
the same slot as t[2].
-
Otherwise use either tostring() on 64 bit integers
+
Otherwise, use either tostring() on 64 bit integers
or complex numbers or combine multiple fields of a cdata aggregate to
a Lua string (e.g. with
ffi.string()). Then
@@ -814,7 +814,7 @@
cdata objects as table keys
Create your own specialized hash table implementation using the
C types provided by the FFI library, just like you would in
-C code. Ultimately this may give much better performance than the
+C code. Ultimately, this may give much better performance than the
other alternatives or what a generic by-value hash table could
possibly provide.
@@ -858,7 +858,7 @@
Parameterized Types
The main use for parameterized types are libraries implementing abstract
data types
-(example),
+(» example),
similar to what can be achieved with C++ template metaprogramming.
Another use case are derived types of anonymous structs, which avoids
pollution of the global struct namespace.
@@ -880,7 +880,7 @@
Garbage Collection of cdata Objects
the end of the next GC cycle).
-Please note that pointers themselves are cdata objects, however they
+Please note, that pointers themselves are cdata objects, however they
are not followed by the garbage collector. So e.g. if you
assign a cdata array to a pointer, you must keep the cdata object
holding the array alive as long as the pointer is still in use:
@@ -929,18 +929,18 @@
Callbacks
This can happen implicitly due to the usual conversions, e.g. when
-passing a Lua function to a function pointer argument. Or you can use
+passing a Lua function to a function pointer argument. Or, you can use
ffi.cast() to explicitly cast a Lua function to a
C function pointer.
-Currently only certain C function types can be used as callback
+Currently, only certain C function types can be used as callback
functions. Neither C vararg functions nor functions with
pass-by-value aggregate argument or result types are supported. There
-are no restrictions for the kind of Lua functions that can be called
+are no restrictions on the kind of Lua functions that can be called
from the callback — no checks for the proper number of arguments
are made. The return value of the Lua function will be converted to the
-result type and an error will be thrown for invalid conversions.
+result type, and an error will be thrown for invalid conversions.
It's allowed to throw errors across a callback invocation, but it's not
@@ -1001,7 +1001,7 @@
Callback resource handling
__stdcall calls to Windows functions.
-For some use cases it's necessary to free up the resources or to
+For some use cases, it's necessary to free up the resources or to
dynamically redirect callbacks. Use an explicit cast to a
C function pointer and keep the resulting cdata object. Then use
the cb:free()
@@ -1054,7 +1054,7 @@
Callback performance
For new designs avoid push-style APIs: a C function repeatedly
-calling a callback for each result. Instead use pull-style APIs:
+calling a callback for each result. Instead, use pull-style APIs:
call a C function repeatedly to get a new result. Calls from Lua
to C via the FFI are much faster than the other way round. Most well-designed
libraries already use pull-style APIs (read/write, get/put).
@@ -1073,7 +1073,7 @@
C Library Namespaces
Indexing a C library namespace object with a symbol name (a Lua
-string) automatically binds it to the library. First the symbol type
+string) automatically binds it to the library. First, the symbol type
is resolved — it must have been declared with
ffi.cdef. Then the
symbol address is resolved by searching for the symbol name in the
@@ -1128,7 +1128,7 @@
C Library Namespaces
namespace objects and to the strings used to index it. This
effectively turns function cdata objects into constants. It's not
useful and actually counter-productive to explicitly cache these
-function objects, e.g. local strlen = ffi.C.strlen. OTOH it
+function objects, e.g. local strlen = ffi.C.strlen. OTOH, it
is useful to cache the namespace itself, e.g. local C =
ffi.C.
@@ -1153,14 +1153,14 @@
No Hand-holding!
interoperability with C code. Adding extra safety measures, like
bounds checks, would be futile. There's no way to detect
misdeclarations of C functions, since shared libraries only
-provide symbol names, but no type information. Likewise there's no way
+provide symbol names, but no type information. Likewise, there's no way
to infer the valid range of indexes for a returned pointer.
Again: the FFI library is a low-level library. This implies it needs
to be used with care, but it's flexibility and performance often
outweigh this concern. If you're a C or C++ developer, it'll be easy
-to apply your existing knowledge. OTOH writing code for the FFI
+to apply your existing knowledge. OTOH, writing code for the FFI
library is not for the faint of heart and probably shouldn't be the
first exercise for someone with little experience in Lua, C or C++.
@@ -1188,7 +1188,7 @@
Current Status
C declarations are not passed through a C pre-processor,
yet.
The C parser is able to evaluate most constant expressions
-commonly found in C header files. However it doesn't handle the
+commonly found in C header files. However, it doesn't handle the
full range of C expression semantics and may fail for some
obscure constructs.
static const declarations only work for integer types
@@ -1219,7 +1219,7 @@
Current Status
Table initializers.
Initialization of nested struct/union types.
Non-default initialization of VLA/VLS or large C types
-(> 128 bytes or > 16 array elements.
+(> 128 bytes or > 16 array elements).
Bitfield initializations.
Pointer differences for element sizes that are not a power of
two.
-Please note this doesn't define an ffi variable in the table
+Please note, this doesn't define an ffi variable in the table
of globals — you really need to use the local variable. The
require function ensures the library is only loaded once.
@@ -192,7 +192,7 @@
Accessing Standard System Functions
⑤ The poll()
function takes a couple more arguments we're not going to use. You can
simply use nil to pass a NULL pointer and 0
-for the nfds parameter. Please note that the
+for the nfds parameter. Please note, that the
number 0does not convert to a pointer value,
unlike in C++. You really have to pass pointers to pointer arguments
and numbers to number arguments.
@@ -216,7 +216,7 @@
Accessing Standard System Functions
Accessing the zlib Compression Library
The following code shows how to access the zlib compression library from Lua code.
+href="https://zlib.net/">» zlib compression library from Lua code.
We'll define two convenience wrapper functions that take a string and
compress or uncompress it to another string:
@@ -289,17 +289,17 @@
Accessing the zlib Compression Library
① This defines some of the
C functions provided by zlib. For the sake of this example, some
-type indirections have been reduced and it uses the pre-defined
+type indirections have been reduced and it uses the predefined
fixed-size integer types, while still adhering to the zlib API/ABI.
② This loads the zlib shared
-library. On POSIX systems it's named libz.so and usually
+library. On POSIX systems, it's named libz.so and usually
comes pre-installed. Since ffi.load() automatically adds any
missing standard prefixes/suffixes, we can simply load the
"z" library. On Windows it's named zlib1.dll and
you'll have to download it first from the
-zlib site. The check for
+» zlib site. The check for
ffi.os makes sure we pass the right name to
ffi.load().
@@ -322,7 +322,7 @@
Accessing the zlib Compression Library
In C you'd pass in the address of a local variable
(&buflen). But since there's no address-of operator in
-Lua, we'll just pass in a one-element array. Conveniently it can be
+Lua, we'll just pass in a one-element array. Conveniently, it can be
initialized with the maximum buffer size in one step. Calling the
actual zlib.compress2 function is then straightforward.
@@ -346,7 +346,7 @@
Accessing the zlib Compression Library
⑥ The uncompress
functions does the exact opposite of the compress function.
The compressed data doesn't include the size of the original string,
-so this needs to be passed in. Otherwise no surprises here.
+so this needs to be passed in. Otherwise, no surprises here.
⑦ The code, that makes use
@@ -380,7 +380,7 @@
Accessing the zlib Compression Library
wherever you'd want to use a number. That's why we get a away with
passing n to ffi.string() above. But other Lua
library functions or modules don't know how to deal with this. So for
-maximum portability one needs to use tonumber() on returned
+maximum portability, one needs to use tonumber() on returned
long results before passing them on. Otherwise the
application might work on some systems, but would fail in a POSIX/x64
environment.
@@ -452,7 +452,7 @@
Defining Metamethods for a C Type
④ If we run out of operators, we can
-define named methods, too. Here the __index table defines an
+define named methods, too. Here, the __index table defines an
area function. For custom indexing needs, one might want to
define __index and __newindexfunctions instead.
@@ -466,13 +466,13 @@
Defining Metamethods for a C Type
apply to any and all uses of this type.
-Please note that the association with a metatable is permanent and
+Please note, that the association with a metatable is permanent and
the metatable must not be modified afterwards! Ditto for the
__index table.
⑥ Here are some simple usage examples
-for the point type and their expected results. The pre-defined
+for the point type and their expected results. The predefined
operations (such as a.x) can be freely mixed with the newly
defined metamethods. Note that area is a method and must be
called with the Lua syntax for methods: a:area(), not
@@ -481,7 +481,7 @@
Defining Metamethods for a C Type
The C type metamethod mechanism is most useful when used in
conjunction with C libraries that are written in an object-oriented
-style. Creators return a pointer to a new instance and methods take an
+style. Creators return a pointer to a new instance, and methods take an
instance pointer as the first argument. Sometimes you can just point
__index to the library namespace and __gc to the
destructor and you're done. But often enough you'll want to add
@@ -567,7 +567,7 @@
To Cache or Not to Cache
This turns them into indirect calls and generates bigger and slower
-machine code. Instead you'll want to cache the namespace itself and
+machine code. Instead, you'll want to cache the namespace itself and
rely on the JIT compiler to eliminate the lookups:
-This sub-module holds functions to introspect the bytecode, generated
+This submodule holds functions to introspect the bytecode, generated
traces, the IR and the generated machine code. The functionality
provided by this module is still in flux and therefore undocumented.
Combinations of v/z with f/F/l produce two-level
views, e.g. -jp=vf or -jp=fv. This shows the time
spent in a VM state or zone vs. hotspots. This can be used to answer
-questions like "Which time consuming functions are only interpreted?" or
+questions like "Which time-consuming functions are only interpreted?" or
"What's the garbage collector overhead for a specific function?".
@@ -215,7 +215,7 @@
Low-level Lua API
This module can be used to implement your own higher-level profiler.
A typical profiling run starts the profiler, captures stack dumps in
the profiler callback, adds them to a hash table to aggregate the number
-of samples, stops the profiler and then analyzes all of the captured
+of samples, stops the profiler and then analyzes all captured
stack dumps. Other parameters can be sampled in the profiler callback,
too. But it's important not to spend too much time in the callback,
since this may skew the statistics.
@@ -269,9 +269,9 @@
dump = profile.dumpstack([thread,] fmt, depth)
formatted according to the fmt argument:
-
p — Preserve the full path for module names. Otherwise
+
p — Preserve the full path for module names. Otherwise,
only the file name is used.
-
f — Dump the function name if it can be derived. Otherwise
+
f — Dump the function name if it can be derived. Otherwise,
use module:line.
LuaJIT extends the standard Lua VM with new functionality and adds
-several extension modules. Please note this page is only about
+several extension modules. Please note, this page is only about
functional enhancements and not about performance enhancements,
such as the optimized VM, the faster interpreter or the JIT compiler.
The generated bytecode is portable and can be loaded on any architecture
-that LuaJIT supports, independent of word size or endianess. However the
+that LuaJIT supports, independent of word size or endianess. However, the
bytecode compatibility versions must match. Bytecode stays compatible
for dot releases (x.y.0 → x.y.1), but may change with major or
minor releases (2.0 → 2.1) or between any beta release. Foreign
@@ -227,7 +227,7 @@
table.clear(tab) clears a table
incremental array/hash part growth.
-Please note this function is meant for very specific situations. In most
+Please note, this function is meant for very specific situations. In most
cases it's better to replace the (usually single) link with a new table
and let the GC do its work.
@@ -237,7 +237,7 @@
Enhanced PRNG for math.random()
LuaJIT uses a Tausworthe PRNG with period 2^223 to implement
math.random() and math.randomseed(). The quality of
the PRNG results is much superior compared to the standard Lua
-implementation which uses the platform-specific ANSI rand().
+implementation, which uses the platform-specific ANSI rand().
The PRNG generates the same sequences from the same seeds on all
@@ -255,7 +255,7 @@
Enhanced PRNG for math.random()
io.* functions handle 64 bit file offsets
The file I/O functions in the standard io.* library handle
-64 bit file offsets. In particular this means it's possible
+64 bit file offsets. In particular, this means it's possible
to open files larger than 2 Gigabytes and to reposition or obtain
the current file position for offsets beyond 2 GB
(fp:seek() method).
@@ -392,29 +392,19 @@
C++ Exception Interoperability
Interoperability
-
POSIX/x64, DWARF2 unwinding
-
GCC 4.3+, Clang
+
External frame unwinding
+
GCC, Clang, MSVC
Full
-
ARM -DLUAJIT_UNWIND_EXTERNAL
-
GCC, Clang
-
Full
-
-
-
Other platforms, DWARF2 unwinding
+
Internal frame unwinding + DWARF2
GCC, Clang
Limited
-
-
Windows/x64
-
MSVC or WinSDK
-
Full
-
-
Windows/x86
-
Any
-
Full
+
Windows 64 bit
+
non-MSVC
+
Limited
Other platforms
@@ -435,7 +425,9 @@
C++ Exception Interoperability
on the C stack. The contents of the C++ exception object
pass through unmodified.
Lua errors can be caught on the C++ side with catch(...).
-The corresponding Lua error message can be retrieved from the Lua stack.
+The corresponding Lua error message can be retrieved from the Lua stack.
+For MSVC for Windows 64 bit this requires compilation of your C++ code
+with /EHa.
Throwing Lua errors across C++ frames is safe. C++ destructors
will be called.
The » LuaJIT wiki gathers community
-resources about LuaJIT.
News about Lua itself can be found at the
-Lua mailing list.
+» Lua mailing list.
The mailing list archives are worth checking out for older postings
about LuaJIT.
Q: Why do I get this error: "attempt to index global 'arg' (a nil value)"?
Q: My vararg functions fail after switching to LuaJIT!
LuaJIT is compatible to the Lua 5.1 language standard. It doesn't
support the implicit arg parameter for old-style vararg
functions from Lua 5.0. Please convert your code to the
-Lua 5.1
+» Lua 5.1
vararg syntax.
-
+
Q: Why do I get this error: "bad FPU precision"?
Q: I get weird behavior after initializing Direct3D.
Q: Some FPU operations crash after I load a Delphi DLL.
@@ -120,57 +116,76 @@
Frequently Asked Questions (FAQ)
Consider testing your application with older versions, too.
Similarly, the Borland/Delphi runtime modifies the FPU control word and
-enables FP exceptions. Of course this violates the Windows ABI, too.
-Please check the Delphi docs for the Set8087CW method.
-
+enables FP exceptions. Of course, this violates the Windows ABI, too.
+Please check the Delphi docs for the Set8087CW method.
-
+
Q: Sometimes Ctrl-C fails to stop my Lua program. Why?
The interrupt signal handler sets a Lua debug hook. But this is
-currently ignored by compiled code (this will eventually be fixed). If
-your program is running in a tight loop and never falls back to the
-interpreter, the debug hook never runs and can't throw the
-"interrupted!" error. In the meantime you have to press Ctrl-C
-twice to get stop your program. That's similar to when it's stuck
-running inside a C function under the Lua interpreter.
+ignored by compiled code. If your program is running in a tight loop
+and never falls back to the interpreter, the debug hook never runs and
+can't throw the "interrupted!" error.
+You have to press Ctrl-C twice to stop your program. That's similar
+to when it's stuck running inside a C function under the Lua interpreter.
-
-
Q: Why doesn't my favorite power-patch for Lua apply against LuaJIT?
-
Because it's a completely redesigned VM and has very little code
-in common with Lua anymore. Also, if the patch introduces changes to
-the Lua semantics, these would need to be reflected everywhere in the
-VM, from the interpreter up to all stages of the compiler. Please
-use only standard Lua language constructs. For many common needs you
-can use source transformations or use wrapper or proxy functions.
-The compiler will happily optimize away such indirections.
+
+
Q: Table iteration with pairs() does not result in the same order?
+
The order of table iteration is explicitly undefined by
+the Lua language standard.
+Different Lua implementations or versions may use different orders for
+otherwise identical tables. Different ways of constructing a table may
+result in different orders, too.
+Due to improved VM security, LuaJIT 2.1 may even use a different order
+on separate VM invocations or when string keys are newly interned.
+If your program relies on a deterministic order, it has a bug. Rewrite it,
+so it doesn't rely on the key order. Or sort the table keys, if you must.
-
+
+
Q: Can Lua code be safely sandboxed?
+
+Maybe for an extremely restricted subset of Lua and if you relentlessly
+scrutinize every single interface function you offer to the untrusted code.
+
+Although Lua provides some sandboxing functionality (setfenv(), hooks),
+it's very hard to get this right even for the Lua core libraries. Of course,
+you'll need to inspect any extension library, too. And there are libraries
+that are inherently unsafe, e.g. the FFI library.
+
+More reading material at the » Lua Wiki and » Wikipedia.
+
+Relatedly, loading untrusted bytecode is not safe!
+
+It's trivial to crash the Lua or LuaJIT VM with maliciously crafted bytecode.
+This is well known and there's no bytecode verification on purpose, so please
+don't report a bug about it. Check the mode parameter for the
+load*() functions to disable loading of bytecode.
+
+In general, the only promising approach is to sandbox Lua code at the
+process level and not the VM level.
+
+
+
+
Q: Lua runs everywhere. Why doesn't LuaJIT support my CPU?
Because it's a compiler — it needs to generate native
machine code. This means the code generator must be ported to each
architecture. And the fast interpreter is written in assembler and
must be ported, too. This is quite an undertaking.
The install documentation shows the supported
-architectures. Other architectures will follow based on sufficient user
-demand and/or sponsoring.
-
-
-
-
Q: When will feature X be added? When will the next version be released?
-
When it's ready.
-C'mon, it's open source — I'm doing it on my own time and you're
-getting it for free. You can either contribute a patch or sponsor
-the development of certain features, if they are important to you.
-
+architectures.
+Other architectures may follow based on sufficient user demand and
+market-relevance of the architecture. Sponsoring is required to develop
+the port itself, to integrate it and to continuously maintain it in the
+actively developed branches.
+The codebase has compatibility defines for some more systems, but
+without official support.
+
+
Toolchains
-LuaJIT currently builds out-of-the box on most systems.
-Here's the compatibility matrix for the supported combinations of
-operating systems, CPUs and compilers:
+Building LuaJIT requires a recent toolchain based on GCC, Clang/LLVM or
+MSVC++.
+
+The Makefile-based build system requires GNU Make and supports
+cross-builds. Batch files are provided for MSVC++ builds and console
+cross-builds.
+
+There are no plans to add historic architectures or to continue support
+for end-of-life (EOL) architectures, for which no new CPUs are commonly
+available anymore. Likewise, there are no plans to support marginal
+and/or de-facto-dead architectures.
+
Depending on your distribution, you may need to install a package for
@@ -185,14 +322,19 @@
Prerequisites
Debian/Ubuntu, install libc6-dev with the package manager.
-Download the current source package of LuaJIT (pick the .tar.gz),
-if you haven't already done so. Move it to a directory of your choice,
-open a terminal window and change to this directory. Now unpack the archive
-and change to the newly created directory:
+The recommended way to fetch the latest version is to do a pull from
+the git repository.
+
+
+Alternatively, download the latest source package of LuaJIT (pick the .tar.gz).
+Move it to a directory of your choice, open a terminal window and change
+to this directory. Now unpack the archive and change to the newly created
+directory (replace XX.YY.ZZ with the version you downloaded):
The supplied Makefiles try to auto-detect the settings needed for your
@@ -216,9 +358,12 @@
Building LuaJIT
make PREFIX=/home/myself/lj2
-Note for OSX: you must set the MACOSX_DEPLOYMENT_TARGET
-environment variable to a value supported by your toolchain.
+Note for macOS: you must set the MACOSX_DEPLOYMENT_TARGET
+environment variable to a value supported by your toolchain:
+
+MACOSX_DEPLOYMENT_TARGET=XX.YY make
+
Installing LuaJIT
The top-level Makefile installs LuaJIT by default under
@@ -245,13 +390,14 @@
Prerequisites
Either install one of the open source SDKs
(» MinGW or
-Cygwin), which come with a modified
+» Cygwin), which come with a modified
GCC plus the required development headers.
Or install Microsoft's Visual Studio (MSVC).
-Next, download the source package and unpack it using an archive manager
-(e.g. the Windows Explorer) to a directory of your choice.
+Next, pull from the git repository or download the source package and
+unpack it using an archive manager (e.g. the Windows Explorer) to
+a directory of your choice.
Building with MSVC
@@ -269,8 +415,8 @@
Building with MSVC
Building with MinGW or Cygwin
Open a command prompt window and make sure the MinGW or Cygwin programs
-are in your path. Then cd to the directory where
-you've unpacked the sources and run this command for MinGW:
+are in your path. Then cd to the directory of the git repository
+or where you've unpacked the sources. Then run this command for MinGW:
mingw32-make
@@ -325,7 +471,7 @@
Cross-compiling LuaJIT
target OS differ, or you'll get assembler or linker errors:
-
E.g. if you're compiling on a Windows or OSX host for embedded Linux or Android, you need to add TARGET_SYS=Linux to the examples below.
+
E.g. if you're compiling on a Windows or macOS host for embedded Linux or Android, you need to add TARGET_SYS=Linux to the examples below.
For a minimal target OS, you may need to disable the built-in allocator in src/Makefile and use TARGET_SYS=Other.
Don't forget to specify the same TARGET_SYS for the install step, too.
@@ -388,7 +534,7 @@
Cross-compiling LuaJIT
make CROSS=mipsel-linux- TARGET_CFLAGS="-mips64r2 -mabi=64"
-You can cross-compile for Android using the Android NDK.
+You can cross-compile for Android using the » Android NDK.
Please adapt the environment variables to match the install locations and the
desired target platform. E.g. Android 4.1 corresponds to ABI level 16.
-You can cross-compile for iOS 3.0+ (iPhone/iPad) using the iOS SDK:
+You can cross-compile for iOS 3.0+ (iPhone/iPad) using the » iOS SDK:
Note: the JIT compiler is disabled for iOS, because regular iOS Apps
@@ -433,8 +581,7 @@
Cross-compiling LuaJIT
Cross-compiling for consoles
Building LuaJIT for consoles requires both a supported host compiler
-(x86 or x64) and a cross-compiler (to PPC or ARM) from the official
-console SDK.
+(x86 or x64) and a cross-compiler from the official console SDK.
Due to restrictions on consoles, the JIT compiler is disabled and only
@@ -455,45 +602,58 @@
Cross-compiling for consoles
make HOST_CC="gcc -m32" CROSS=ppu-lv2-
-To cross-compile for PS4 from a Windows host,
-open a "Visual Studio .NET Command Prompt" (64 bit host compiler),
-cd to the directory where you've unpacked the sources and
-run the following commands:
+To cross-compile for the other consoles from a Windows host, open a
+"Native Tools Command Prompt for VS". You need to choose either the 32
+or the 64 bit version of the host compiler to match the target.
+Then cd to the src directory below where you've
+unpacked the sources and run the build command given in the table:
-
-cd src
-ps4build
-
-
-To cross-compile for PS Vita from a Windows host,
-open a "Visual Studio .NET Command Prompt" (32 bit host compiler),
-cd to the directory where you've unpacked the sources and
-run the following commands:
-
-
-cd src
-psvitabuild
-
-
-To cross-compile for Xbox 360 from a Windows host,
-open a "Visual Studio .NET Command Prompt" (32 bit host compiler),
-cd to the directory where you've unpacked the sources and run
-the following commands:
-
-
-cd src
-xedkbuild
-
+
+
+
Console
+
Bits
+
Build Command
+
+
+
PS4
+
64
+
ps4build
+
+
+
PS5
+
64
+
ps5build
+
+
+
PS Vita
+
32
+
psvitabuild
+
+
+
Xbox 360
+
32
+
xedkbuild
+
+
+
Xbox One
+
64
+
xb1build
+
+
+
Nintendo Switch NX32
+
32
+
nxbuild
+
+
+
Nintendo Switch NX64
+
64
+
nxbuild
+
+
-To cross-compile for Xbox One from a Windows host,
-open a "Visual Studio .NET Command Prompt" (64 bit host compiler),
-cd to the directory where you've unpacked the sources and run
-the following commands:
+Please check out the comments in the corresponding *.bat
+file for more options.
-
-cd src
-xb1build
-
Embedding LuaJIT
@@ -522,14 +682,6 @@
Embedding LuaJIT
intend to load Lua/C modules at runtime.
-
-If you're building a 64 bit application on OSX which links directly or
-indirectly against LuaJIT which is not built for LJ_GC64 mode,
-you need to link your main executable with these flags:
-
--pagezero_size 10000 -image_base 100000000
-
-
Additional hints for initializing LuaJIT using the C API functions:
@@ -538,16 +690,16 @@
Embedding LuaJIT
for embedding Lua or LuaJIT into your application.
Make sure you use luaL_newstate. Avoid using
lua_newstate, since this uses the (slower) default memory
-allocator from your system (no support for this on x64).
+allocator from your system (no support for this on 64 bit architectures).
Make sure you use luaL_openlibs and not the old Lua 5.0 style
of calling luaopen_base etc. directly.
To change or extend the list of standard libraries to load, copy
src/lib_init.c to your project and modify it accordingly.
-Make sure the jit library is loaded or the JIT compiler
+Make sure the jit library is loaded, or the JIT compiler
will not be activated.
The bit.* module for bitwise operations
is already built-in. There's no need to statically link
-Lua BitOp to your application.
There should be absolutely no need to patch luaconf.h or any
of the Makefiles. And please do not hand-pick files for your packages —
simply use whatever make install creates. There's a reason
-for all of the files and directories it creates.
+for all the files and directories it creates.
The build system uses GNU make and auto-detects most settings based on
@@ -614,7 +766,7 @@
LuaJIT is a Just-In-Time Compiler (JIT) for the
-Lua programming language.
+» Lua programming language.
Lua is a powerful, dynamic and light-weight programming language.
It may be embedded or used as a general-purpose, stand-alone language.
version of the regular lua stand-alone executable.
It supports the same basic options, too. luajit -h
prints a short list of the available options. Please have a look at the
-Lua manual
+» Lua manual
for details.
@@ -109,6 +109,7 @@
-b[options] input output
-t type — Set output file type (default: auto-detect from output name).
-a arch — Override architecture for object files (default: native).
-o os — Override OS for object files (default: native).
+
-F name — Override filename (default: input filename).
-e chunk — Use chunk string as input.
- (a single minus sign) — Use stdin as input and/or stdout as output.
@@ -182,9 +183,9 @@
-j cmd[=arg[,arg...]]
itself. For a description of their options and output format, please
read the comment block at the start of their source.
They can be found in the lib directory of the source
-distribution or installed under the jit directory. By default
-this is /usr/local/share/luajit-2.0.5/jit on POSIX
-systems.
+distribution or installed under the jit directory. By default,
+this is /usr/local/share/luajit-XX.YY.ZZ>/jit on POSIX
+systems (replace XX.YY.ZZ by the installed version).
-O[level]
@@ -214,11 +215,17 @@
-O[level]
You can either use this option multiple times (like -Ocse
-O-dce -Ohotloop=10) or separate several settings with a comma
(like -O+cse,-dce,hotloop=10). The settings are applied from
-left to right and later settings override earlier ones. You can freely
+left to right, and later settings override earlier ones. You can freely
mix the three forms, but note that setting an optimization level
overrides all earlier flags.
+Note that -Ofma is not enabled by default at any level,
+because it affects floating-point result accuracy. Only enable this,
+if you fully understand the trade-offs of FMA for performance (higher),
+determinism (lower) and numerical accuracy (higher).
+
+
Here are the available flags and at what optimization levels they
are enabled:
@@ -250,6 +257,8 @@
-O[level]
sink
•
Allocation/Store Sinking
fuse
•
Fusion of operands into instructions
+
+
fma
Fused multiply-add
Here are the parameters and their default settings:
@@ -293,7 +302,7 @@
-LuaJIT 2.0 is the current
-stable branch. This branch is in
-feature-freeze — new features will only be added to LuaJIT 2.1.
+This documentation is for LuaJIT 2.1.0-beta3. Please check the doc
+directory in each git branch for the version-specific documentation.
+
+
+The currently developed branches are LuaJIT 2.1 and LuaJIT 2.0.
+
+
+LuaJIT 2.0 is in feature-freeze — new features will only
+be added to LuaJIT 2.1.
Current Status
@@ -75,7 +81,7 @@
Current Status
There are some differences in implementation-defined behavior.
-These either have a good reason, are arbitrary design choices
+These either have a good reason, are arbitrary design choices,
or are due to quirks in the VM. The latter cases may get fixed if a
demonstrable need is shown.
@@ -84,30 +90,12 @@
Current Status
hooks for non-Lua functions) and shows slightly different behavior
in LuaJIT (no per-coroutine hooks, no tail call counting).
-
-Currently some out-of-memory errors from on-trace code are not
-handled correctly. The error may fall through an on-trace
-pcall or it may be passed on to the function set with
-lua_atpanic on x64. This issue will be fixed with the new
-garbage collector.
-
-
-LuaJIT on 64 bit systems provides a limited range of 47 bits for the
-legacy lightuserdata data type.
-This is only relevant on x64 systems which use the negative part of the
-virtual address space in user mode, e.g. Solaris/x64, and on ARM64 systems
-configured with a 48 bit or 52 bit VA.
-Avoid using lightuserdata to hold pointers that may point outside
-of that range, e.g. variables on the stack. In general, avoid this data
-type for new code and replace it with (much more performant) FFI bindings.
-FFI cdata pointers can address the full 64 bit range.
-