From 2e98c3d0644fc0c265844908f43b7e4526dd819c Mon Sep 17 00:00:00 2001
From: Mike Pall
-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. @@ -161,7 +161,7 @@ function declarations.
-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):
-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 @@ -587,7 +587,7 @@ the operations below — the operation is applied to the 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. @@ -637,7 +637,7 @@ assigning to an index of a vector raises an error.
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).
@@ -650,7 +650,7 @@ certain optimizations.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.
@@ -669,8 +669,8 @@ through unions is explicitly detected and allowed. 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 constructor. Note that you have to -use ffi.new inside of it, since calling ct(...) would -cause infinite recursion. +use ffi.new inside the metamethod, since calling ct(...) +would cause infinite recursion.
@@ -786,7 +786,7 @@ the resulting Lua number as a key when indexing tables.
One obvious benefit: t[tonumber(2LL)] does point to
the same slot as t[2].
-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: @@ -909,18 +909,18 @@ of the function pointer and the Lua function object (closure).
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 @@ -981,7 +981,7 @@ convention cannot be automatically detected, unlike for __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() @@ -1034,7 +1034,7 @@ GUI application, which waits for user input most of the time, anyway.
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). @@ -1053,7 +1053,7 @@ function.
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 @@ -1108,7 +1108,7 @@ Performance notice: the JIT compiler specializes to the identity of 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.
@@ -1133,14 +1133,14 @@ This behavior is inevitable, since the goal is to provide full 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++.
@@ -1168,7 +1168,7 @@ currently incomplete: