summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMike Pall <mike>2012-10-10 19:57:00 +0200
committerMike Pall <mike>2012-10-10 19:57:00 +0200
commit425f67c7d6b33382d04ee1a9ade31b79150ec953 (patch)
tree67ff32dcc1a158a06d12ef3a6580eb73a8a7790e
parent0b55e05d06bc760070b8ca9f99c01c3d8e67e7db (diff)
downloadluajit-425f67c7d6b33382d04ee1a9ade31b79150ec953.tar.gz
luajit-425f67c7d6b33382d04ee1a9ade31b79150ec953.tar.bz2
luajit-425f67c7d6b33382d04ee1a9ade31b79150ec953.zip
FFI: Update docs on FFI semantics.
-rw-r--r--doc/ext_ffi_semantics.html61
1 files changed, 51 insertions, 10 deletions
diff --git a/doc/ext_ffi_semantics.html b/doc/ext_ffi_semantics.html
index 36c3be32..2db9f882 100644
--- a/doc/ext_ffi_semantics.html
+++ b/doc/ext_ffi_semantics.html
@@ -83,12 +83,6 @@ expect them to work. It should be straightforward to write
83applications using the LuaJIT FFI for developers with a C or C++ 83applications using the LuaJIT FFI for developers with a C or C++
84background. 84background.
85</p> 85</p>
86<p class="indent" style="color: #c00000;">
87Please note: this doesn't comprise the final specification for the FFI
88semantics, yet. Some semantics may need to be changed, based on your
89feedback. Please <a href="contact.html">report</a> any problems you may
90encounter or any improvements you'd like to see &mdash; thank you!
91</p>
92 86
93<h2 id="clang">C Language Support</h2> 87<h2 id="clang">C Language Support</h2>
94<p> 88<p>
@@ -813,6 +807,55 @@ possibly provide.</li>
813 807
814</ul> 808</ul>
815 809
810<h2 id="param">Parameterized Types</h2>
811<p>
812To facilitate some abstractions, the two functions
813<a href="ext_ffi_api.html#ffi_typeof"><tt>ffi.typeof</tt></a> and
814<a href="ext_ffi_api.html#ffi_cdef"><tt>ffi.cdef</tt></a> support
815parameterized types in C&nbsp;declarations. Note: none of the other API
816functions taking a cdecl allow this.
817</p>
818<p>
819Any place you can write a <b><tt>typedef</tt> name</b>, an
820<b>identifier</b> or a <b>number</b> in a declaration, you can write
821<tt>$</tt> (the dollar sign) instead. These placeholders are replaced in
822order of appearance with the arguments following the cdecl string:
823</p>
824<pre class="code">
825-- Declare a struct with a parameterized field type and name:
826ffi.cdef([[
827typedef struct { $ $; } foo_t;
828]], type1, name1)
829
830-- Anonymous struct with dynamic names:
831local bar_t = ffi.typeof("struct { int $, $; }", name1, name2)
832-- Derived pointer type:
833local bar_ptr_t = ffi.typeof("$ *", bar_t)
834
835-- Parameterized dimensions work even where a VLA won't work:
836local matrix_t = ffi.typeof("uint8_t[$][$]", width, height)
837</pre>
838<p>
839Caveat: this is <em>not</em> simple text substitution! A passed ctype or
840cdata object is treated like the underlying type, a passed string is
841considered an identifier and a number is considered a number. You must
842not mix this up: e.g. passing <tt>"int"</tt> as a string doesn't work in
843place of a type, you'd need to use <tt>ffi.typeof("int")</tt> instead.
844</p>
845<p>
846The main use for parameterized types are libraries implementing abstract
847data types
848(<a href="http://www.freelists.org/post/luajit/ffi-type-of-pointer-to,8"><span class="ext">&raquo;</span>&nbsp;example</a>),
849similar to what can be achieved with C++ template metaprogramming.
850Another use case are derived types of anonymous structs, which avoids
851pollution of the global struct namespace.
852</p>
853<p>
854Please note that parameterized types are a nice tool and indispensable
855for certain use cases. But you'll want to use them sparingly in regular
856code, e.g. when all types are actually fixed.
857</p>
858
816<h2 id="gc">Garbage Collection of cdata Objects</h2> 859<h2 id="gc">Garbage Collection of cdata Objects</h2>
817<p> 860<p>
818All explicitly (<tt>ffi.new()</tt>, <tt>ffi.cast()</tt> etc.) or 861All explicitly (<tt>ffi.new()</tt>, <tt>ffi.cast()</tt> etc.) or
@@ -1156,10 +1199,8 @@ value.</li>
1156<li>ctype <tt>__newindex</tt> tables and non-string lookups in ctype 1199<li>ctype <tt>__newindex</tt> tables and non-string lookups in ctype
1157<tt>__index</tt> tables.</li> 1200<tt>__index</tt> tables.</li>
1158<li><tt>tostring()</tt> for cdata types.</li> 1201<li><tt>tostring()</tt> for cdata types.</li>
1159<li>Calls to the following <a href="ext_ffi_api.html">ffi.* API</a> 1202<li>Calls to <tt>ffi.cdef()</tt>, <tt>ffi.load()</tt> and
1160functions: <tt>cdef</tt>, <tt>load</tt>, <tt>typeof</tt>, 1203<tt>ffi.metatype()</tt>.</li>
1161<tt>metatype</tt>, <tt>gc</tt>, <tt>sizeof</tt>, <tt>alignof</tt>,
1162<tt>offsetof</tt>.</li>
1163</ul> 1204</ul>
1164<p> 1205<p>
1165Other missing features: 1206Other missing features: