aboutsummaryrefslogtreecommitdiff
path: root/doc/ext_ffi_semantics.html
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--doc/ext_ffi_semantics.html58
1 files changed, 29 insertions, 29 deletions
diff --git a/doc/ext_ffi_semantics.html b/doc/ext_ffi_semantics.html
index 4909fedd..6c6f8ad7 100644
--- a/doc/ext_ffi_semantics.html
+++ b/doc/ext_ffi_semantics.html
@@ -84,7 +84,7 @@ footprint. It's used by the <a href="ext_ffi_api.html">ffi.* library
84functions</a> to declare C&nbsp;types or external symbols. 84functions</a> to declare C&nbsp;types or external symbols.
85</p> 85</p>
86<p> 86<p>
87It's only purpose is to parse C&nbsp;declarations, as found e.g. in 87Its only purpose is to parse C&nbsp;declarations, as found e.g. in
88C&nbsp;header files. Although it does evaluate constant expressions, 88C&nbsp;header files. Although it does evaluate constant expressions,
89it's <em>not</em> a C&nbsp;compiler. The body of <tt>inline</tt> 89it's <em>not</em> a C&nbsp;compiler. The body of <tt>inline</tt>
90C&nbsp;function definitions is simply ignored. 90C&nbsp;function definitions is simply ignored.
@@ -161,7 +161,7 @@ function declarations.</li>
161 161
162</ul> 162</ul>
163<p> 163<p>
164The following C&nbsp;types are pre-defined by the C&nbsp;parser (like 164The following C&nbsp;types are predefined by the C&nbsp;parser (like
165a <tt>typedef</tt>, except re-declarations will be ignored): 165a <tt>typedef</tt>, except re-declarations will be ignored):
166</p> 166</p>
167<ul> 167<ul>
@@ -577,9 +577,9 @@ ffi.new("struct nested", {x=1,y={2,3}}) --> x = 1, y.a = 2, y.b = 3
577 577
578<h2 id="cdata_ops">Operations on cdata Objects</h2> 578<h2 id="cdata_ops">Operations on cdata Objects</h2>
579<p> 579<p>
580All of the standard Lua operators can be applied to cdata objects or a 580All standard Lua operators can be applied to cdata objects or a
581mix of a cdata object and another Lua object. The following list shows 581mix of a cdata object and another Lua object. The following list shows
582the pre-defined operations. 582the predefined operations.
583</p> 583</p>
584<p> 584<p>
585Reference types are dereferenced <em>before</em> performing each of 585Reference types are dereferenced <em>before</em> performing each of
@@ -587,7 +587,7 @@ the operations below &mdash; the operation is applied to the
587C&nbsp;type pointed to by the reference. 587C&nbsp;type pointed to by the reference.
588</p> 588</p>
589<p> 589<p>
590The pre-defined operations are always tried first before deferring to a 590The predefined operations are always tried first before deferring to a
591metamethod or index table (if any) for the corresponding ctype (except 591metamethod or index table (if any) for the corresponding ctype (except
592for <tt>__new</tt>). An error is raised if the metamethod lookup or 592for <tt>__new</tt>). An error is raised if the metamethod lookup or
593index table lookup fails. 593index table lookup fails.
@@ -637,7 +637,7 @@ assigning to an index of a vector raises an error.</li>
637</ul> 637</ul>
638<p> 638<p>
639A ctype object can be indexed with a string key, too. The only 639A ctype object can be indexed with a string key, too. The only
640pre-defined operation is reading scoped constants of 640predefined operation is reading scoped constants of
641<tt>struct</tt>/<tt>union</tt> types. All other accesses defer 641<tt>struct</tt>/<tt>union</tt> types. All other accesses defer
642to the corresponding metamethods or index tables (if any). 642to the corresponding metamethods or index tables (if any).
643</p> 643</p>
@@ -650,7 +650,7 @@ certain optimizations.
650<p> 650<p>
651As a consequence, the <em>elements</em> of complex numbers and 651As a consequence, the <em>elements</em> of complex numbers and
652vectors are immutable. But the elements of an aggregate holding these 652vectors are immutable. But the elements of an aggregate holding these
653types <em>may</em> be modified of course. I.e. you cannot assign to 653types <em>may</em> be modified, of course. I.e. you cannot assign to
654<tt>foo.c.im</tt>, but you can assign a (newly created) complex number 654<tt>foo.c.im</tt>, but you can assign a (newly created) complex number
655to <tt>foo.c</tt>. 655to <tt>foo.c</tt>.
656</p> 656</p>
@@ -669,8 +669,8 @@ through unions is explicitly detected and allowed.
669to <tt>ffi.new(ct, ...)</tt>, unless a <tt>__new</tt> metamethod is 669to <tt>ffi.new(ct, ...)</tt>, unless a <tt>__new</tt> metamethod is
670defined. The <tt>__new</tt> metamethod is called with the ctype object 670defined. The <tt>__new</tt> metamethod is called with the ctype object
671plus any other arguments passed to the constructor. Note that you have to 671plus any other arguments passed to the constructor. Note that you have to
672use <tt>ffi.new</tt> inside of it, since calling <tt>ct(...)</tt> would 672use <tt>ffi.new</tt> inside the metamethod, since calling <tt>ct(...)</tt>
673cause infinite recursion.</li> 673would cause infinite recursion.</li>
674 674
675<li><b>C&nbsp;function call</b>: a cdata function or cdata function 675<li><b>C&nbsp;function call</b>: a cdata function or cdata function
676pointer can be called. The passed arguments are 676pointer can be called. The passed arguments are
@@ -681,7 +681,7 @@ variable argument part of vararg C&nbsp;function use
681C&nbsp;function is called and the return value (if any) is 681C&nbsp;function is called and the return value (if any) is
682<a href="#convert_tolua">converted to a Lua object</a>.<br> 682<a href="#convert_tolua">converted to a Lua object</a>.<br>
683On Windows/x86 systems, <tt>__stdcall</tt> functions are automatically 683On Windows/x86 systems, <tt>__stdcall</tt> functions are automatically
684detected and a function declared as <tt>__cdecl</tt> (the default) is 684detected, and a function declared as <tt>__cdecl</tt> (the default) is
685silently fixed up after the first call.</li> 685silently fixed up after the first call.</li>
686 686
687</ul> 687</ul>
@@ -691,7 +691,7 @@ silently fixed up after the first call.</li>
691 691
692<li><b>Pointer arithmetic</b>: a cdata pointer/array and a cdata 692<li><b>Pointer arithmetic</b>: a cdata pointer/array and a cdata
693number or a Lua number can be added or subtracted. The number must be 693number or a Lua number can be added or subtracted. The number must be
694on the right hand side for a subtraction. The result is a pointer of 694on the right-hand side for a subtraction. The result is a pointer of
695the same type with an address plus or minus the number value 695the same type with an address plus or minus the number value
696multiplied by the element size in bytes. An error is raised if the 696multiplied by the element size in bytes. An error is raised if the
697element size is undefined.</li> 697element size is undefined.</li>
@@ -706,7 +706,7 @@ operators (<tt>+&nbsp;-&nbsp;*&nbsp;/&nbsp;%&nbsp;^</tt> and unary
706minus) can be applied to two cdata numbers, or a cdata number and a 706minus) can be applied to two cdata numbers, or a cdata number and a
707Lua number. If one of them is an <tt>uint64_t</tt>, the other side is 707Lua number. If one of them is an <tt>uint64_t</tt>, the other side is
708converted to an <tt>uint64_t</tt> and an unsigned arithmetic operation 708converted to an <tt>uint64_t</tt> and an unsigned arithmetic operation
709is performed. Otherwise both sides are converted to an 709is performed. Otherwise, both sides are converted to an
710<tt>int64_t</tt> and a signed arithmetic operation is performed. The 710<tt>int64_t</tt> and a signed arithmetic operation is performed. The
711result is a boxed 64&nbsp;bit cdata object.<br> 711result is a boxed 64&nbsp;bit cdata object.<br>
712 712
@@ -737,7 +737,7 @@ which is compatible with any other pointer type.</li>
737<li><b>64&nbsp;bit integer comparison</b>: two cdata numbers, or a 737<li><b>64&nbsp;bit integer comparison</b>: two cdata numbers, or a
738cdata number and a Lua number can be compared with each other. If one 738cdata number and a Lua number can be compared with each other. If one
739of them is an <tt>uint64_t</tt>, the other side is converted to an 739of them is an <tt>uint64_t</tt>, the other side is converted to an
740<tt>uint64_t</tt> and an unsigned comparison is performed. Otherwise 740<tt>uint64_t</tt> and an unsigned comparison is performed. Otherwise,
741both sides are converted to an <tt>int64_t</tt> and a signed 741both sides are converted to an <tt>int64_t</tt> and a signed
742comparison is performed.<br> 742comparison is performed.<br>
743 743
@@ -762,9 +762,9 @@ keys!</b>
762A cdata object is treated like any other garbage-collected object and 762A cdata object is treated like any other garbage-collected object and
763is hashed and compared by its address for table indexing. Since 763is hashed and compared by its address for table indexing. Since
764there's no interning for cdata value types, the same value may be 764there's no interning for cdata value types, the same value may be
765boxed in different cdata objects with different addresses. Thus 765boxed in different cdata objects with different addresses. Thus,
766<tt>t[1LL+1LL]</tt> and <tt>t[2LL]</tt> usually <b>do not</b> point to 766<tt>t[1LL+1LL]</tt> and <tt>t[2LL]</tt> usually <b>do not</b> point to
767the same hash slot and they certainly <b>do not</b> point to the same 767the same hash slot, and they certainly <b>do not</b> point to the same
768hash slot as <tt>t[2]</tt>. 768hash slot as <tt>t[2]</tt>.
769</p> 769</p>
770<p> 770<p>
@@ -786,7 +786,7 @@ the resulting Lua number as a key when indexing tables.<br>
786One obvious benefit: <tt>t[tonumber(2LL)]</tt> <b>does</b> point to 786One obvious benefit: <tt>t[tonumber(2LL)]</tt> <b>does</b> point to
787the same slot as <tt>t[2]</tt>.</li> 787the same slot as <tt>t[2]</tt>.</li>
788 788
789<li>Otherwise use either <tt>tostring()</tt> on 64&nbsp;bit integers 789<li>Otherwise, use either <tt>tostring()</tt> on 64&nbsp;bit integers
790or complex numbers or combine multiple fields of a cdata aggregate to 790or complex numbers or combine multiple fields of a cdata aggregate to
791a Lua string (e.g. with 791a Lua string (e.g. with
792<a href="ext_ffi_api.html#ffi_string"><tt>ffi.string()</tt></a>). Then 792<a href="ext_ffi_api.html#ffi_string"><tt>ffi.string()</tt></a>). Then
@@ -794,7 +794,7 @@ use the resulting Lua string as a key when indexing tables.</li>
794 794
795<li>Create your own specialized hash table implementation using the 795<li>Create your own specialized hash table implementation using the
796C&nbsp;types provided by the FFI library, just like you would in 796C&nbsp;types provided by the FFI library, just like you would in
797C&nbsp;code. Ultimately this may give much better performance than the 797C&nbsp;code. Ultimately, this may give much better performance than the
798other alternatives or what a generic by-value hash table could 798other alternatives or what a generic by-value hash table could
799possibly provide.</li> 799possibly provide.</li>
800 800
@@ -860,7 +860,7 @@ garbage collector will automatically free the memory used by it (at
860the end of the next GC cycle). 860the end of the next GC cycle).
861</p> 861</p>
862<p> 862<p>
863Please note that pointers themselves are cdata objects, however they 863Please note, that pointers themselves are cdata objects, however they
864are <b>not</b> followed by the garbage collector. So e.g. if you 864are <b>not</b> followed by the garbage collector. So e.g. if you
865assign a cdata array to a pointer, you must keep the cdata object 865assign a cdata array to a pointer, you must keep the cdata object
866holding the array alive as long as the pointer is still in use: 866holding 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).
909</p> 909</p>
910<p> 910<p>
911This can happen implicitly due to the usual conversions, e.g. when 911This can happen implicitly due to the usual conversions, e.g. when
912passing a Lua function to a function pointer argument. Or you can use 912passing a Lua function to a function pointer argument. Or, you can use
913<tt>ffi.cast()</tt> to explicitly cast a Lua function to a 913<tt>ffi.cast()</tt> to explicitly cast a Lua function to a
914C&nbsp;function pointer. 914C&nbsp;function pointer.
915</p> 915</p>
916<p> 916<p>
917Currently only certain C&nbsp;function types can be used as callback 917Currently, only certain C&nbsp;function types can be used as callback
918functions. Neither C&nbsp;vararg functions nor functions with 918functions. Neither C&nbsp;vararg functions nor functions with
919pass-by-value aggregate argument or result types are supported. There 919pass-by-value aggregate argument or result types are supported. There
920are no restrictions for the kind of Lua functions that can be called 920are no restrictions on the kind of Lua functions that can be called
921from the callback &mdash; no checks for the proper number of arguments 921from the callback &mdash; no checks for the proper number of arguments
922are made. The return value of the Lua function will be converted to the 922are made. The return value of the Lua function will be converted to the
923result type and an error will be thrown for invalid conversions. 923result type, and an error will be thrown for invalid conversions.
924</p> 924</p>
925<p> 925<p>
926It's allowed to throw errors across a callback invocation, but it's not 926It's allowed to throw errors across a callback invocation, but it's not
@@ -981,7 +981,7 @@ convention cannot be automatically detected, unlike for
981<tt>__stdcall</tt> calls <em>to</em> Windows functions. 981<tt>__stdcall</tt> calls <em>to</em> Windows functions.
982</p> 982</p>
983<p> 983<p>
984For some use cases it's necessary to free up the resources or to 984For some use cases, it's necessary to free up the resources or to
985dynamically redirect callbacks. Use an explicit cast to a 985dynamically redirect callbacks. Use an explicit cast to a
986C&nbsp;function pointer and keep the resulting cdata object. Then use 986C&nbsp;function pointer and keep the resulting cdata object. Then use
987the <a href="ext_ffi_api.html#callback_free"><tt>cb:free()</tt></a> 987the <a href="ext_ffi_api.html#callback_free"><tt>cb:free()</tt></a>
@@ -1034,7 +1034,7 @@ GUI application, which waits for user input most of the time, anyway.
1034</p> 1034</p>
1035<p> 1035<p>
1036For new designs <b>avoid push-style APIs</b>: a C&nbsp;function repeatedly 1036For new designs <b>avoid push-style APIs</b>: a C&nbsp;function repeatedly
1037calling a callback for each result. Instead <b>use pull-style APIs</b>: 1037calling a callback for each result. Instead, <b>use pull-style APIs</b>:
1038call a C&nbsp;function repeatedly to get a new result. Calls from Lua 1038call a C&nbsp;function repeatedly to get a new result. Calls from Lua
1039to C via the FFI are much faster than the other way round. Most well-designed 1039to C via the FFI are much faster than the other way round. Most well-designed
1040libraries already use pull-style APIs (read/write, get/put). 1040libraries already use pull-style APIs (read/write, get/put).
@@ -1053,7 +1053,7 @@ function.
1053</p> 1053</p>
1054<p> 1054<p>
1055Indexing a C&nbsp;library namespace object with a symbol name (a Lua 1055Indexing a C&nbsp;library namespace object with a symbol name (a Lua
1056string) automatically binds it to the library. First the symbol type 1056string) automatically binds it to the library. First, the symbol type
1057is resolved &mdash; it must have been declared with 1057is resolved &mdash; it must have been declared with
1058<a href="ext_ffi_api.html#ffi_cdef"><tt>ffi.cdef</tt></a>. Then the 1058<a href="ext_ffi_api.html#ffi_cdef"><tt>ffi.cdef</tt></a>. Then the
1059symbol address is resolved by searching for the symbol name in the 1059symbol 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
1108namespace objects and to the strings used to index it. This 1108namespace objects and to the strings used to index it. This
1109effectively turns function cdata objects into constants. It's not 1109effectively turns function cdata objects into constants. It's not
1110useful and actually counter-productive to explicitly cache these 1110useful and actually counter-productive to explicitly cache these
1111function objects, e.g. <tt>local strlen = ffi.C.strlen</tt>. OTOH it 1111function objects, e.g. <tt>local strlen = ffi.C.strlen</tt>. OTOH, it
1112<em>is</em> useful to cache the namespace itself, e.g. <tt>local C = 1112<em>is</em> useful to cache the namespace itself, e.g. <tt>local C =
1113ffi.C</tt>. 1113ffi.C</tt>.
1114</p> 1114</p>
@@ -1133,14 +1133,14 @@ This behavior is inevitable, since the goal is to provide full
1133interoperability with C&nbsp;code. Adding extra safety measures, like 1133interoperability with C&nbsp;code. Adding extra safety measures, like
1134bounds checks, would be futile. There's no way to detect 1134bounds checks, would be futile. There's no way to detect
1135misdeclarations of C&nbsp;functions, since shared libraries only 1135misdeclarations of C&nbsp;functions, since shared libraries only
1136provide symbol names, but no type information. Likewise there's no way 1136provide symbol names, but no type information. Likewise, there's no way
1137to infer the valid range of indexes for a returned pointer. 1137to infer the valid range of indexes for a returned pointer.
1138</p> 1138</p>
1139<p> 1139<p>
1140Again: the FFI library is a low-level library. This implies it needs 1140Again: the FFI library is a low-level library. This implies it needs
1141to be used with care, but it's flexibility and performance often 1141to be used with care, but it's flexibility and performance often
1142outweigh this concern. If you're a C or C++ developer, it'll be easy 1142outweigh this concern. If you're a C or C++ developer, it'll be easy
1143to apply your existing knowledge. OTOH writing code for the FFI 1143to apply your existing knowledge. OTOH, writing code for the FFI
1144library is not for the faint of heart and probably shouldn't be the 1144library is not for the faint of heart and probably shouldn't be the
1145first exercise for someone with little experience in Lua, C or C++. 1145first exercise for someone with little experience in Lua, C or C++.
1146</p> 1146</p>
@@ -1168,7 +1168,7 @@ currently incomplete:
1168<li>C&nbsp;declarations are not passed through a C&nbsp;pre-processor, 1168<li>C&nbsp;declarations are not passed through a C&nbsp;pre-processor,
1169yet.</li> 1169yet.</li>
1170<li>The C&nbsp;parser is able to evaluate most constant expressions 1170<li>The C&nbsp;parser is able to evaluate most constant expressions
1171commonly found in C&nbsp;header files. However it doesn't handle the 1171commonly found in C&nbsp;header files. However, it doesn't handle the
1172full range of C&nbsp;expression semantics and may fail for some 1172full range of C&nbsp;expression semantics and may fail for some
1173obscure constructs.</li> 1173obscure constructs.</li>
1174<li><tt>static const</tt> declarations only work for integer types 1174<li><tt>static const</tt> declarations only work for integer types