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