aboutsummaryrefslogtreecommitdiff
path: root/doc/ext_ffi_semantics.html
diff options
context:
space:
mode:
Diffstat (limited to 'doc/ext_ffi_semantics.html')
-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 d515a142..22f7f17c 100644
--- a/doc/ext_ffi_semantics.html
+++ b/doc/ext_ffi_semantics.html
@@ -88,7 +88,7 @@ footprint. It's used by the <a href="ext_ffi_api.html">ffi.* library
88functions</a> to declare C&nbsp;types or external symbols. 88functions</a> to declare C&nbsp;types or external symbols.
89</p> 89</p>
90<p> 90<p>
91It's only purpose is to parse C&nbsp;declarations, as found e.g. in 91Its only purpose is to parse C&nbsp;declarations, as found e.g. in
92C&nbsp;header files. Although it does evaluate constant expressions, 92C&nbsp;header files. Although it does evaluate constant expressions,
93it's <em>not</em> a C&nbsp;compiler. The body of <tt>inline</tt> 93it's <em>not</em> a C&nbsp;compiler. The body of <tt>inline</tt>
94C&nbsp;function definitions is simply ignored. 94C&nbsp;function definitions is simply ignored.
@@ -165,7 +165,7 @@ function declarations.</li>
165 165
166</ul> 166</ul>
167<p> 167<p>
168The following C&nbsp;types are pre-defined by the C&nbsp;parser (like 168The following C&nbsp;types are predefined by the C&nbsp;parser (like
169a <tt>typedef</tt>, except re-declarations will be ignored): 169a <tt>typedef</tt>, except re-declarations will be ignored):
170</p> 170</p>
171<ul> 171<ul>
@@ -583,9 +583,9 @@ ffi.new("struct nested", {x=1,y={2,3}}) --> x = 1, y.a = 2, y.b = 3
583 583
584<h2 id="cdata_ops">Operations on cdata Objects</h2> 584<h2 id="cdata_ops">Operations on cdata Objects</h2>
585<p> 585<p>
586All of the standard Lua operators can be applied to cdata objects or a 586All standard Lua operators can be applied to cdata objects or a
587mix of a cdata object and another Lua object. The following list shows 587mix of a cdata object and another Lua object. The following list shows
588the pre-defined operations. 588the predefined operations.
589</p> 589</p>
590<p> 590<p>
591Reference types are dereferenced <em>before</em> performing each of 591Reference types are dereferenced <em>before</em> performing each of
@@ -593,7 +593,7 @@ the operations below &mdash; the operation is applied to the
593C&nbsp;type pointed to by the reference. 593C&nbsp;type pointed to by the reference.
594</p> 594</p>
595<p> 595<p>
596The pre-defined operations are always tried first before deferring to a 596The predefined operations are always tried first before deferring to a
597metamethod or index table (if any) for the corresponding ctype (except 597metamethod or index table (if any) for the corresponding ctype (except
598for <tt>__new</tt>). An error is raised if the metamethod lookup or 598for <tt>__new</tt>). An error is raised if the metamethod lookup or
599index table lookup fails. 599index table lookup fails.
@@ -643,7 +643,7 @@ assigning to an index of a vector raises an error.</li>
643</ul> 643</ul>
644<p> 644<p>
645A ctype object can be indexed with a string key, too. The only 645A ctype object can be indexed with a string key, too. The only
646pre-defined operation is reading scoped constants of 646predefined operation is reading scoped constants of
647<tt>struct</tt>/<tt>union</tt> types. All other accesses defer 647<tt>struct</tt>/<tt>union</tt> types. All other accesses defer
648to the corresponding metamethods or index tables (if any). 648to the corresponding metamethods or index tables (if any).
649</p> 649</p>
@@ -656,7 +656,7 @@ certain optimizations.
656<p> 656<p>
657As a consequence, the <em>elements</em> of complex numbers and 657As a consequence, the <em>elements</em> of complex numbers and
658vectors are immutable. But the elements of an aggregate holding these 658vectors are immutable. But the elements of an aggregate holding these
659types <em>may</em> be modified of course. I.e. you cannot assign to 659types <em>may</em> be modified, of course. I.e. you cannot assign to
660<tt>foo.c.im</tt>, but you can assign a (newly created) complex number 660<tt>foo.c.im</tt>, but you can assign a (newly created) complex number
661to <tt>foo.c</tt>. 661to <tt>foo.c</tt>.
662</p> 662</p>
@@ -675,8 +675,8 @@ through unions is explicitly detected and allowed.
675to <tt>ffi.new(ct, ...)</tt>, unless a <tt>__new</tt> metamethod is 675to <tt>ffi.new(ct, ...)</tt>, unless a <tt>__new</tt> metamethod is
676defined. The <tt>__new</tt> metamethod is called with the ctype object 676defined. The <tt>__new</tt> metamethod is called with the ctype object
677plus any other arguments passed to the constructor. Note that you have to 677plus any other arguments passed to the constructor. Note that you have to
678use <tt>ffi.new</tt> inside of it, since calling <tt>ct(...)</tt> would 678use <tt>ffi.new</tt> inside the metamethod, since calling <tt>ct(...)</tt>
679cause infinite recursion.</li> 679would cause infinite recursion.</li>
680 680
681<li><b>C&nbsp;function call</b>: a cdata function or cdata function 681<li><b>C&nbsp;function call</b>: a cdata function or cdata function
682pointer can be called. The passed arguments are 682pointer can be called. The passed arguments are
@@ -687,7 +687,7 @@ variable argument part of vararg C&nbsp;function use
687C&nbsp;function is called and the return value (if any) is 687C&nbsp;function is called and the return value (if any) is
688<a href="#convert_tolua">converted to a Lua object</a>.<br> 688<a href="#convert_tolua">converted to a Lua object</a>.<br>
689On Windows/x86 systems, <tt>__stdcall</tt> functions are automatically 689On Windows/x86 systems, <tt>__stdcall</tt> functions are automatically
690detected and a function declared as <tt>__cdecl</tt> (the default) is 690detected, and a function declared as <tt>__cdecl</tt> (the default) is
691silently fixed up after the first call.</li> 691silently fixed up after the first call.</li>
692 692
693</ul> 693</ul>
@@ -697,7 +697,7 @@ silently fixed up after the first call.</li>
697 697
698<li><b>Pointer arithmetic</b>: a cdata pointer/array and a cdata 698<li><b>Pointer arithmetic</b>: a cdata pointer/array and a cdata
699number or a Lua number can be added or subtracted. The number must be 699number or a Lua number can be added or subtracted. The number must be
700on the right hand side for a subtraction. The result is a pointer of 700on the right-hand side for a subtraction. The result is a pointer of
701the same type with an address plus or minus the number value 701the same type with an address plus or minus the number value
702multiplied by the element size in bytes. An error is raised if the 702multiplied by the element size in bytes. An error is raised if the
703element size is undefined.</li> 703element size is undefined.</li>
@@ -712,7 +712,7 @@ operators (<tt>+&nbsp;-&nbsp;*&nbsp;/&nbsp;%&nbsp;^</tt> and unary
712minus) can be applied to two cdata numbers, or a cdata number and a 712minus) can be applied to two cdata numbers, or a cdata number and a
713Lua number. If one of them is an <tt>uint64_t</tt>, the other side is 713Lua number. If one of them is an <tt>uint64_t</tt>, the other side is
714converted to an <tt>uint64_t</tt> and an unsigned arithmetic operation 714converted to an <tt>uint64_t</tt> and an unsigned arithmetic operation
715is performed. Otherwise both sides are converted to an 715is performed. Otherwise, both sides are converted to an
716<tt>int64_t</tt> and a signed arithmetic operation is performed. The 716<tt>int64_t</tt> and a signed arithmetic operation is performed. The
717result is a boxed 64&nbsp;bit cdata object.<br> 717result is a boxed 64&nbsp;bit cdata object.<br>
718 718
@@ -759,7 +759,7 @@ which is compatible with any other pointer type.</li>
759<li><b>64&nbsp;bit integer comparison</b>: two cdata numbers, or a 759<li><b>64&nbsp;bit integer comparison</b>: two cdata numbers, or a
760cdata number and a Lua number can be compared with each other. If one 760cdata number and a Lua number can be compared with each other. If one
761of them is an <tt>uint64_t</tt>, the other side is converted to an 761of them is an <tt>uint64_t</tt>, the other side is converted to an
762<tt>uint64_t</tt> and an unsigned comparison is performed. Otherwise 762<tt>uint64_t</tt> and an unsigned comparison is performed. Otherwise,
763both sides are converted to an <tt>int64_t</tt> and a signed 763both sides are converted to an <tt>int64_t</tt> and a signed
764comparison is performed.<br> 764comparison is performed.<br>
765 765
@@ -784,9 +784,9 @@ keys!</b>
784A cdata object is treated like any other garbage-collected object and 784A cdata object is treated like any other garbage-collected object and
785is hashed and compared by its address for table indexing. Since 785is hashed and compared by its address for table indexing. Since
786there's no interning for cdata value types, the same value may be 786there's no interning for cdata value types, the same value may be
787boxed in different cdata objects with different addresses. Thus 787boxed in different cdata objects with different addresses. Thus,
788<tt>t[1LL+1LL]</tt> and <tt>t[2LL]</tt> usually <b>do not</b> point to 788<tt>t[1LL+1LL]</tt> and <tt>t[2LL]</tt> usually <b>do not</b> point to
789the same hash slot and they certainly <b>do not</b> point to the same 789the same hash slot, and they certainly <b>do not</b> point to the same
790hash slot as <tt>t[2]</tt>. 790hash slot as <tt>t[2]</tt>.
791</p> 791</p>
792<p> 792<p>
@@ -808,7 +808,7 @@ the resulting Lua number as a key when indexing tables.<br>
808One obvious benefit: <tt>t[tonumber(2LL)]</tt> <b>does</b> point to 808One obvious benefit: <tt>t[tonumber(2LL)]</tt> <b>does</b> point to
809the same slot as <tt>t[2]</tt>.</li> 809the same slot as <tt>t[2]</tt>.</li>
810 810
811<li>Otherwise use either <tt>tostring()</tt> on 64&nbsp;bit integers 811<li>Otherwise, use either <tt>tostring()</tt> on 64&nbsp;bit integers
812or complex numbers or combine multiple fields of a cdata aggregate to 812or complex numbers or combine multiple fields of a cdata aggregate to
813a Lua string (e.g. with 813a Lua string (e.g. with
814<a href="ext_ffi_api.html#ffi_string"><tt>ffi.string()</tt></a>). Then 814<a href="ext_ffi_api.html#ffi_string"><tt>ffi.string()</tt></a>). Then
@@ -816,7 +816,7 @@ use the resulting Lua string as a key when indexing tables.</li>
816 816
817<li>Create your own specialized hash table implementation using the 817<li>Create your own specialized hash table implementation using the
818C&nbsp;types provided by the FFI library, just like you would in 818C&nbsp;types provided by the FFI library, just like you would in
819C&nbsp;code. Ultimately this may give much better performance than the 819C&nbsp;code. Ultimately, this may give much better performance than the
820other alternatives or what a generic by-value hash table could 820other alternatives or what a generic by-value hash table could
821possibly provide.</li> 821possibly provide.</li>
822 822
@@ -882,7 +882,7 @@ garbage collector will automatically free the memory used by it (at
882the end of the next GC cycle). 882the end of the next GC cycle).
883</p> 883</p>
884<p> 884<p>
885Please note that pointers themselves are cdata objects, however they 885Please note, that pointers themselves are cdata objects, however they
886are <b>not</b> followed by the garbage collector. So e.g. if you 886are <b>not</b> followed by the garbage collector. So e.g. if you
887assign a cdata array to a pointer, you must keep the cdata object 887assign a cdata array to a pointer, you must keep the cdata object
888holding the array alive as long as the pointer is still in use: 888holding the array alive as long as the pointer is still in use:
@@ -931,18 +931,18 @@ of the function pointer and the Lua function object (closure).
931</p> 931</p>
932<p> 932<p>
933This can happen implicitly due to the usual conversions, e.g. when 933This can happen implicitly due to the usual conversions, e.g. when
934passing a Lua function to a function pointer argument. Or you can use 934passing a Lua function to a function pointer argument. Or, you can use
935<tt>ffi.cast()</tt> to explicitly cast a Lua function to a 935<tt>ffi.cast()</tt> to explicitly cast a Lua function to a
936C&nbsp;function pointer. 936C&nbsp;function pointer.
937</p> 937</p>
938<p> 938<p>
939Currently only certain C&nbsp;function types can be used as callback 939Currently, only certain C&nbsp;function types can be used as callback
940functions. Neither C&nbsp;vararg functions nor functions with 940functions. Neither C&nbsp;vararg functions nor functions with
941pass-by-value aggregate argument or result types are supported. There 941pass-by-value aggregate argument or result types are supported. There
942are no restrictions for the kind of Lua functions that can be called 942are no restrictions on the kind of Lua functions that can be called
943from the callback &mdash; no checks for the proper number of arguments 943from the callback &mdash; no checks for the proper number of arguments
944are made. The return value of the Lua function will be converted to the 944are made. The return value of the Lua function will be converted to the
945result type and an error will be thrown for invalid conversions. 945result type, and an error will be thrown for invalid conversions.
946</p> 946</p>
947<p> 947<p>
948It's allowed to throw errors across a callback invocation, but it's not 948It's allowed to throw errors across a callback invocation, but it's not
@@ -1003,7 +1003,7 @@ convention cannot be automatically detected, unlike for
1003<tt>__stdcall</tt> calls <em>to</em> Windows functions. 1003<tt>__stdcall</tt> calls <em>to</em> Windows functions.
1004</p> 1004</p>
1005<p> 1005<p>
1006For some use cases it's necessary to free up the resources or to 1006For some use cases, it's necessary to free up the resources or to
1007dynamically redirect callbacks. Use an explicit cast to a 1007dynamically redirect callbacks. Use an explicit cast to a
1008C&nbsp;function pointer and keep the resulting cdata object. Then use 1008C&nbsp;function pointer and keep the resulting cdata object. Then use
1009the <a href="ext_ffi_api.html#callback_free"><tt>cb:free()</tt></a> 1009the <a href="ext_ffi_api.html#callback_free"><tt>cb:free()</tt></a>
@@ -1056,7 +1056,7 @@ GUI application, which waits for user input most of the time, anyway.
1056</p> 1056</p>
1057<p> 1057<p>
1058For new designs <b>avoid push-style APIs</b>: a C&nbsp;function repeatedly 1058For new designs <b>avoid push-style APIs</b>: a C&nbsp;function repeatedly
1059calling a callback for each result. Instead <b>use pull-style APIs</b>: 1059calling a callback for each result. Instead, <b>use pull-style APIs</b>:
1060call a C&nbsp;function repeatedly to get a new result. Calls from Lua 1060call a C&nbsp;function repeatedly to get a new result. Calls from Lua
1061to C via the FFI are much faster than the other way round. Most well-designed 1061to C via the FFI are much faster than the other way round. Most well-designed
1062libraries already use pull-style APIs (read/write, get/put). 1062libraries already use pull-style APIs (read/write, get/put).
@@ -1075,7 +1075,7 @@ function.
1075</p> 1075</p>
1076<p> 1076<p>
1077Indexing a C&nbsp;library namespace object with a symbol name (a Lua 1077Indexing a C&nbsp;library namespace object with a symbol name (a Lua
1078string) automatically binds it to the library. First the symbol type 1078string) automatically binds it to the library. First, the symbol type
1079is resolved &mdash; it must have been declared with 1079is resolved &mdash; it must have been declared with
1080<a href="ext_ffi_api.html#ffi_cdef"><tt>ffi.cdef</tt></a>. Then the 1080<a href="ext_ffi_api.html#ffi_cdef"><tt>ffi.cdef</tt></a>. Then the
1081symbol address is resolved by searching for the symbol name in the 1081symbol address is resolved by searching for the symbol name in the
@@ -1130,7 +1130,7 @@ Performance notice: the JIT compiler specializes to the identity of
1130namespace objects and to the strings used to index it. This 1130namespace objects and to the strings used to index it. This
1131effectively turns function cdata objects into constants. It's not 1131effectively turns function cdata objects into constants. It's not
1132useful and actually counter-productive to explicitly cache these 1132useful and actually counter-productive to explicitly cache these
1133function objects, e.g. <tt>local strlen = ffi.C.strlen</tt>. OTOH it 1133function objects, e.g. <tt>local strlen = ffi.C.strlen</tt>. OTOH, it
1134<em>is</em> useful to cache the namespace itself, e.g. <tt>local C = 1134<em>is</em> useful to cache the namespace itself, e.g. <tt>local C =
1135ffi.C</tt>. 1135ffi.C</tt>.
1136</p> 1136</p>
@@ -1155,14 +1155,14 @@ This behavior is inevitable, since the goal is to provide full
1155interoperability with C&nbsp;code. Adding extra safety measures, like 1155interoperability with C&nbsp;code. Adding extra safety measures, like
1156bounds checks, would be futile. There's no way to detect 1156bounds checks, would be futile. There's no way to detect
1157misdeclarations of C&nbsp;functions, since shared libraries only 1157misdeclarations of C&nbsp;functions, since shared libraries only
1158provide symbol names, but no type information. Likewise there's no way 1158provide symbol names, but no type information. Likewise, there's no way
1159to infer the valid range of indexes for a returned pointer. 1159to infer the valid range of indexes for a returned pointer.
1160</p> 1160</p>
1161<p> 1161<p>
1162Again: the FFI library is a low-level library. This implies it needs 1162Again: the FFI library is a low-level library. This implies it needs
1163to be used with care, but it's flexibility and performance often 1163to be used with care, but it's flexibility and performance often
1164outweigh this concern. If you're a C or C++ developer, it'll be easy 1164outweigh this concern. If you're a C or C++ developer, it'll be easy
1165to apply your existing knowledge. OTOH writing code for the FFI 1165to apply your existing knowledge. OTOH, writing code for the FFI
1166library is not for the faint of heart and probably shouldn't be the 1166library is not for the faint of heart and probably shouldn't be the
1167first exercise for someone with little experience in Lua, C or C++. 1167first exercise for someone with little experience in Lua, C or C++.
1168</p> 1168</p>
@@ -1190,7 +1190,7 @@ currently incomplete:
1190<li>C&nbsp;declarations are not passed through a C&nbsp;pre-processor, 1190<li>C&nbsp;declarations are not passed through a C&nbsp;pre-processor,
1191yet.</li> 1191yet.</li>
1192<li>The C&nbsp;parser is able to evaluate most constant expressions 1192<li>The C&nbsp;parser is able to evaluate most constant expressions
1193commonly found in C&nbsp;header files. However it doesn't handle the 1193commonly found in C&nbsp;header files. However, it doesn't handle the
1194full range of C&nbsp;expression semantics and may fail for some 1194full range of C&nbsp;expression semantics and may fail for some
1195obscure constructs.</li> 1195obscure constructs.</li>
1196<li><tt>static const</tt> declarations only work for integer types 1196<li><tt>static const</tt> declarations only work for integer types