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