ext_ffi_semantics.html 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
  2. <html>
  3. <head>
  4. <title>FFI Semantics</title>
  5. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  6. <meta name="Author" content="Mike Pall">
  7. <meta name="Copyright" content="Copyright (C) 2005-2011, Mike Pall">
  8. <meta name="Language" content="en">
  9. <link rel="stylesheet" type="text/css" href="bluequad.css" media="screen">
  10. <link rel="stylesheet" type="text/css" href="bluequad-print.css" media="print">
  11. <style type="text/css">
  12. table.convtable { line-height: 1.2; }
  13. tr.convhead td { font-weight: bold; }
  14. td.convin { width: 11em; }
  15. td.convop { font-style: italic; width: 16em; }
  16. </style>
  17. </head>
  18. <body>
  19. <div id="site">
  20. <a href="http://luajit.org"><span>Lua<span id="logo">JIT</span></span></a>
  21. </div>
  22. <div id="head">
  23. <h1>FFI Semantics</h1>
  24. </div>
  25. <div id="nav">
  26. <ul><li>
  27. <a href="luajit.html">LuaJIT</a>
  28. <ul><li>
  29. <a href="install.html">Installation</a>
  30. </li><li>
  31. <a href="running.html">Running</a>
  32. </li></ul>
  33. </li><li>
  34. <a href="extensions.html">Extensions</a>
  35. <ul><li>
  36. <a href="ext_ffi.html">FFI Library</a>
  37. <ul><li>
  38. <a href="ext_ffi_tutorial.html">FFI Tutorial</a>
  39. </li><li>
  40. <a href="ext_ffi_api.html">ffi.* API</a>
  41. </li><li>
  42. <a class="current" href="ext_ffi_semantics.html">FFI Semantics</a>
  43. </li></ul>
  44. </li><li>
  45. <a href="ext_jit.html">jit.* Library</a>
  46. </li><li>
  47. <a href="ext_c_api.html">Lua/C API</a>
  48. </li></ul>
  49. </li><li>
  50. <a href="status.html">Status</a>
  51. <ul><li>
  52. <a href="changes.html">Changes</a>
  53. </li></ul>
  54. </li><li>
  55. <a href="faq.html">FAQ</a>
  56. </li><li>
  57. <a href="http://luajit.org/performance.html">Performance <span class="ext">&raquo;</span></a>
  58. </li><li>
  59. <a href="http://luajit.org/download.html">Download <span class="ext">&raquo;</span></a>
  60. </li></ul>
  61. </div>
  62. <div id="main">
  63. <p>
  64. This page describes the detailed semantics underlying the FFI library
  65. and its interaction with both Lua and C&nbsp;code.
  66. </p>
  67. <p>
  68. Given that the FFI library is designed to interface with C&nbsp;code
  69. and that declarations can be written in plain C&nbsp;syntax, <b>it
  70. closely follows the C&nbsp;language semantics</b>, wherever possible.
  71. Some minor concessions are needed for smoother interoperation with Lua
  72. language semantics.
  73. </p>
  74. <p>
  75. Please don't be overwhelmed by the contents of this page &mdash; this
  76. is a reference and you may need to consult it, if in doubt. It doesn't
  77. hurt to skim this page, but most of the semantics "just work" as you'd
  78. expect them to work. It should be straightforward to write
  79. applications using the LuaJIT FFI for developers with a C or C++
  80. background.
  81. </p>
  82. <p class="indent" style="color: #c00000;">
  83. Please note: this is an early public release of the FFI library. This
  84. does not comprise the final specification for the FFI semantics, yet.
  85. Some of the semantics may need to be changed, based on feedback from
  86. developers. Please <a href="contact.html">report</a> any problems you
  87. may encounter or any improvements you'd like to see &mdash; thank you!
  88. </p>
  89. <h2 id="clang">C Language Support</h2>
  90. <p>
  91. The FFI library has a built-in C&nbsp;parser with a minimal memory
  92. footprint. It's used by the <a href="ext_ffi_api.html">ffi.* library
  93. functions</a> to declare C&nbsp;types or external symbols.
  94. </p>
  95. <p>
  96. It's only purpose is to parse C&nbsp;declarations, as found e.g. in
  97. C&nbsp;header files. Although it does evaluate constant expressions,
  98. it's <em>not</em> a C&nbsp;compiler. The body of <tt>inline</tt>
  99. C&nbsp;function definitions is simply ignored.
  100. </p>
  101. <p>
  102. Also, this is <em>not</em> a validating C&nbsp;parser. It expects and
  103. accepts correctly formed C&nbsp;declarations, but it may choose to
  104. ignore bad declarations or show rather generic error messages. If in
  105. doubt, please check the input against your favorite C&nbsp;compiler.
  106. </p>
  107. <p>
  108. The C&nbsp;parser complies to the <b>C99 language standard</b> plus
  109. the following extensions:
  110. </p>
  111. <ul>
  112. <li>The <tt>'\e'</tt> escape in character and string literals.</li>
  113. <li>The C99/C++ boolean type, declared with the keywords <tt>bool</tt>
  114. or <tt>_Bool</tt>.</li>
  115. <li>Complex numbers, declared with the keywords <tt>complex</tt> or
  116. <tt>_Complex</tt>.</li>
  117. <li>Two complex number types: <tt>complex</tt> (aka
  118. <tt>complex&nbsp;double</tt>) and <tt>complex&nbsp;float</tt>.</li>
  119. <li>Vector types, declared with the GCC <tt>mode</tt> or
  120. <tt>vector_size</tt> attribute.</li>
  121. <li>Unnamed ('transparent') <tt>struct</tt>/<tt>union</tt> fields
  122. inside a <tt>struct</tt>/<tt>union</tt>.</li>
  123. <li>Incomplete <tt>enum</tt> declarations, handled like incomplete
  124. <tt>struct</tt> declarations.</li>
  125. <li>Unnamed <tt>enum</tt> fields inside a
  126. <tt>struct</tt>/<tt>union</tt>. This is similar to a scoped C++
  127. <tt>enum</tt>, except that declared constants are visible in the
  128. global namespace, too.</li>
  129. <li>Scoped <tt>static&nbsp;const</tt> declarations inside a
  130. <tt>struct</tt>/<tt>union</tt> (from C++).</li>
  131. <li>Zero-length arrays (<tt>[0]</tt>), empty
  132. <tt>struct</tt>/<tt>union</tt>, variable-length arrays (VLA,
  133. <tt>[?]</tt>) and variable-length structs (VLS, with a trailing
  134. VLA).</li>
  135. <li>C++ reference types (<tt>int&nbsp;&amp;x</tt>).</li>
  136. <li>Alternate GCC keywords with '<tt>__</tt>', e.g.
  137. <tt>__const__</tt>.</li>
  138. <li>GCC <tt>__attribute__</tt> with the following attributes:
  139. <tt>aligned</tt>, <tt>packed</tt>, <tt>mode</tt>,
  140. <tt>vector_size</tt>, <tt>cdecl</tt>, <tt>fastcall</tt>,
  141. <tt>stdcall</tt>.</li>
  142. <li>The GCC <tt>__extension__</tt> keyword and the GCC
  143. <tt>__alignof__</tt> operator.</li>
  144. <li>GCC <tt>__asm__("symname")</tt> symbol name redirection for
  145. function declarations.</li>
  146. <li>MSVC keywords for fixed-length types: <tt>__int8</tt>,
  147. <tt>__int16</tt>, <tt>__int32</tt> and <tt>__int64</tt>.</li>
  148. <li>MSVC <tt>__cdecl</tt>, <tt>__fastcall</tt>, <tt>__stdcall</tt>,
  149. <tt>__ptr32</tt>, <tt>__ptr64</tt>, <tt>__declspec(align(n))</tt>
  150. and <tt>#pragma&nbsp;pack</tt>.</li>
  151. <li>All other GCC/MSVC-specific attributes are ignored.</li>
  152. </ul>
  153. <p>
  154. The following C&nbsp;types are pre-defined by the C&nbsp;parser (like
  155. a <tt>typedef</tt>, except re-declarations will be ignored):
  156. </p>
  157. <ul>
  158. <li>Vararg handling: <tt>va_list</tt>, <tt>__builtin_va_list</tt>,
  159. <tt>__gnuc_va_list</tt>.</li>
  160. <li>From <tt>&lt;stddef.h&gt;</tt>: <tt>ptrdiff_t</tt>,
  161. <tt>size_t</tt>, <tt>wchar_t</tt>.</li>
  162. <li>From <tt>&lt;stdint.h&gt;</tt>: <tt>int8_t</tt>, <tt>int16_t</tt>,
  163. <tt>int32_t</tt>, <tt>int64_t</tt>, <tt>uint8_t</tt>,
  164. <tt>uint16_t</tt>, <tt>uint32_t</tt>, <tt>uint64_t</tt>,
  165. <tt>intptr_t</tt>, <tt>uintptr_t</tt>.</li>
  166. </ul>
  167. <p>
  168. You're encouraged to use these types in preference to the
  169. compiler-specific extensions or the target-dependent standard types.
  170. E.g. <tt>char</tt> differs in signedness and <tt>long</tt> differs in
  171. size, depending on the target architecture and platform ABI.
  172. </p>
  173. <p>
  174. The following C&nbsp;features are <b>not</b> supported:
  175. </p>
  176. <ul>
  177. <li>A declaration must always have a type specifier; it doesn't
  178. default to an <tt>int</tt> type.</li>
  179. <li>Old-style empty function declarations (K&amp;R) are not allowed.
  180. All C&nbsp;functions must have a proper prototype declaration. A
  181. function declared without parameters (<tt>int&nbsp;foo();</tt>) is
  182. treated as a function taking zero arguments, like in C++.</li>
  183. <li>The <tt>long double</tt> C&nbsp;type is parsed correctly, but
  184. there's no support for the related conversions, accesses or arithmetic
  185. operations.</li>
  186. <li>Wide character strings and character literals are not
  187. supported.</li>
  188. <li><a href="#status">See below</a> for features that are currently
  189. not implemented.</li>
  190. </ul>
  191. <h2 id="convert">C Type Conversion Rules</h2>
  192. <h3 id="convert_tolua">Conversions from C&nbsp;types to Lua objects</h3>
  193. <p>
  194. These conversion rules apply for <em>read accesses</em> to
  195. C&nbsp;types: indexing pointers, arrays or
  196. <tt>struct</tt>/<tt>union</tt> types; reading external variables or
  197. constant values; retrieving return values from C&nbsp;calls:
  198. </p>
  199. <table class="convtable">
  200. <tr class="convhead">
  201. <td class="convin">Input</td>
  202. <td class="convop">Conversion</td>
  203. <td class="convout">Output</td>
  204. </tr>
  205. <tr class="odd separate">
  206. <td class="convin"><tt>int8_t</tt>, <tt>int16_t</tt></td><td class="convop">&rarr;<sup>sign-ext</sup> <tt>int32_t</tt> &rarr; <tt>double</tt></td><td class="convout">number</td></tr>
  207. <tr class="even">
  208. <td class="convin"><tt>uint8_t</tt>, <tt>uint16_t</tt></td><td class="convop">&rarr;<sup>zero-ext</sup> <tt>int32_t</tt> &rarr; <tt>double</tt></td><td class="convout">number</td></tr>
  209. <tr class="odd">
  210. <td class="convin"><tt>int32_t</tt>, <tt>uint32_t</tt></td><td class="convop">&rarr; <tt>double</tt></td><td class="convout">number</td></tr>
  211. <tr class="even">
  212. <td class="convin"><tt>int64_t</tt>, <tt>uint64_t</tt></td><td class="convop">boxed value</td><td class="convout">64 bit int cdata</td></tr>
  213. <tr class="odd separate">
  214. <td class="convin"><tt>double</tt>, <tt>float</tt></td><td class="convop">&rarr; <tt>double</tt></td><td class="convout">number</td></tr>
  215. <tr class="even separate">
  216. <td class="convin"><tt>bool</tt></td><td class="convop">0 &rarr; <tt>false</tt>, otherwise <tt>true</tt></td><td class="convout">boolean</td></tr>
  217. <tr class="odd separate">
  218. <td class="convin">Complex number</td><td class="convop">boxed value</td><td class="convout">complex cdata</td></tr>
  219. <tr class="even">
  220. <td class="convin">Vector</td><td class="convop">boxed value</td><td class="convout">vector cdata</td></tr>
  221. <tr class="odd">
  222. <td class="convin">Pointer</td><td class="convop">boxed value</td><td class="convout">pointer cdata</td></tr>
  223. <tr class="even separate">
  224. <td class="convin">Array</td><td class="convop">boxed reference</td><td class="convout">reference cdata</td></tr>
  225. <tr class="odd">
  226. <td class="convin"><tt>struct</tt>/<tt>union</tt></td><td class="convop">boxed reference</td><td class="convout">reference cdata</td></tr>
  227. </table>
  228. <p>
  229. Bitfields or <tt>enum</tt> types are treated like their underlying
  230. type.
  231. </p>
  232. <p>
  233. Reference types are dereferenced <em>before</em> a conversion can take
  234. place &mdash; the conversion is applied to the C&nbsp;type pointed to
  235. by the reference.
  236. </p>
  237. <h3 id="convert_fromlua">Conversions from Lua objects to C&nbsp;types</h3>
  238. <p>
  239. These conversion rules apply for <em>write accesses</em> to
  240. C&nbsp;types: indexing pointers, arrays or
  241. <tt>struct</tt>/<tt>union</tt> types; initializing cdata objects;
  242. casts to C&nbsp;types; writing to external variables; passing
  243. arguments to C&nbsp;calls:
  244. </p>
  245. <table class="convtable">
  246. <tr class="convhead">
  247. <td class="convin">Input</td>
  248. <td class="convop">Conversion</td>
  249. <td class="convout">Output</td>
  250. </tr>
  251. <tr class="odd separate">
  252. <td class="convin">number</td><td class="convop">&rarr;</td><td class="convout"><tt>double</tt></td></tr>
  253. <tr class="even">
  254. <td class="convin">boolean</td><td class="convop"><tt>false</tt> &rarr; 0, <tt>true</tt> &rarr; 1</td><td class="convout"><tt>bool</tt></td></tr>
  255. <tr class="odd separate">
  256. <td class="convin">nil</td><td class="convop"><tt>NULL</tt> &rarr;</td><td class="convout"><tt>(void *)</tt></td></tr>
  257. <tr class="even">
  258. <td class="convin">userdata</td><td class="convop">userdata payload &rarr;</td><td class="convout"><tt>(void *)</tt></td></tr>
  259. <tr class="odd">
  260. <td class="convin">lightuserdata</td><td class="convop">lightuserdata address &rarr;</td><td class="convout"><tt>(void *)</tt></td></tr>
  261. <tr class="even separate">
  262. <td class="convin">string</td><td class="convop">match against <tt>enum</tt> constant</td><td class="convout"><tt>enum</tt></td></tr>
  263. <tr class="odd">
  264. <td class="convin">string</td><td class="convop">copy string data + zero-byte</td><td class="convout"><tt>int8_t[]</tt>, <tt>uint8_t[]</tt></td></tr>
  265. <tr class="even">
  266. <td class="convin">string</td><td class="convop">string data &rarr;</td><td class="convout"><tt>const char[]</tt></td></tr>
  267. <tr class="odd separate">
  268. <td class="convin">table</td><td class="convop"><a href="#init_table">table initializer</a></td><td class="convout">Array</td></tr>
  269. <tr class="even">
  270. <td class="convin">table</td><td class="convop"><a href="#init_table">table initializer</a></td><td class="convout"><tt>struct</tt>/<tt>union</tt></td></tr>
  271. <tr class="odd separate">
  272. <td class="convin">cdata</td><td class="convop">cdata payload &rarr;</td><td class="convout">C type</td></tr>
  273. </table>
  274. <p>
  275. If the result type of this conversion doesn't match the
  276. C&nbsp;type of the destination, the
  277. <a href="#convert_between">conversion rules between C&nbsp;types</a>
  278. are applied.
  279. </p>
  280. <p>
  281. Reference types are immutable after initialization ("no re-seating of
  282. references"). For initialization purposes or when passing values to
  283. reference parameters, they are treated like pointers. Note that unlike
  284. in C++, there's no way to implement automatic reference generation of
  285. variables under the Lua language semantics. If you want to call a
  286. function with a reference parameter, you need to explicitly pass a
  287. one-element array.
  288. </p>
  289. <h3 id="convert_between">Conversions between C&nbsp;types</h3>
  290. <p>
  291. These conversion rules are more or less the same as the standard
  292. C&nbsp;conversion rules. Some rules only apply to casts, or require
  293. pointer or type compatibility:
  294. </p>
  295. <table class="convtable">
  296. <tr class="convhead">
  297. <td class="convin">Input</td>
  298. <td class="convop">Conversion</td>
  299. <td class="convout">Output</td>
  300. </tr>
  301. <tr class="odd separate">
  302. <td class="convin">Signed integer</td><td class="convop">&rarr;<sup>narrow or sign-extend</sup></td><td class="convout">Integer</td></tr>
  303. <tr class="even">
  304. <td class="convin">Unsigned integer</td><td class="convop">&rarr;<sup>narrow or zero-extend</sup></td><td class="convout">Integer</td></tr>
  305. <tr class="odd">
  306. <td class="convin">Integer</td><td class="convop">&rarr;<sup>round</sup></td><td class="convout"><tt>double</tt>, <tt>float</tt></td></tr>
  307. <tr class="even">
  308. <td class="convin"><tt>double</tt>, <tt>float</tt></td><td class="convop">&rarr;<sup>trunc</sup> <tt>int32_t</tt> &rarr;<sup>narrow</sup></td><td class="convout"><tt>(u)int8_t</tt>, <tt>(u)int16_t</tt></td></tr>
  309. <tr class="odd">
  310. <td class="convin"><tt>double</tt>, <tt>float</tt></td><td class="convop">&rarr;<sup>trunc</sup></td><td class="convout"><tt>(u)int32_t</tt>, <tt>(u)int64_t</tt></td></tr>
  311. <tr class="even">
  312. <td class="convin"><tt>double</tt>, <tt>float</tt></td><td class="convop">&rarr;<sup>round</sup></td><td class="convout"><tt>float</tt>, <tt>double</tt></td></tr>
  313. <tr class="odd separate">
  314. <td class="convin">Number</td><td class="convop">n == 0 &rarr; 0, otherwise 1</td><td class="convout"><tt>bool</tt></td></tr>
  315. <tr class="even">
  316. <td class="convin"><tt>bool</tt></td><td class="convop"><tt>false</tt> &rarr; 0, <tt>true</tt> &rarr; 1</td><td class="convout">Number</td></tr>
  317. <tr class="odd separate">
  318. <td class="convin">Complex number</td><td class="convop">convert real part</td><td class="convout">Number</td></tr>
  319. <tr class="even">
  320. <td class="convin">Number</td><td class="convop">convert real part, imag = 0</td><td class="convout">Complex number</td></tr>
  321. <tr class="odd">
  322. <td class="convin">Complex number</td><td class="convop">convert real and imag part</td><td class="convout">Complex number</td></tr>
  323. <tr class="even separate">
  324. <td class="convin">Number</td><td class="convop">convert scalar and replicate</td><td class="convout">Vector</td></tr>
  325. <tr class="odd">
  326. <td class="convin">Vector</td><td class="convop">copy (same size)</td><td class="convout">Vector</td></tr>
  327. <tr class="even separate">
  328. <td class="convin"><tt>struct</tt>/<tt>union</tt></td><td class="convop">take base address (compat)</td><td class="convout">Pointer</td></tr>
  329. <tr class="odd">
  330. <td class="convin">Array</td><td class="convop">take base address (compat)</td><td class="convout">Pointer</td></tr>
  331. <tr class="even">
  332. <td class="convin">Function</td><td class="convop">take function address</td><td class="convout">Function pointer</td></tr>
  333. <tr class="odd separate">
  334. <td class="convin">Number</td><td class="convop">convert via <tt>uintptr_t</tt> (cast)</td><td class="convout">Pointer</td></tr>
  335. <tr class="even">
  336. <td class="convin">Pointer</td><td class="convop">convert address (compat/cast)</td><td class="convout">Pointer</td></tr>
  337. <tr class="odd">
  338. <td class="convin">Pointer</td><td class="convop">convert address (cast)</td><td class="convout">Integer</td></tr>
  339. <tr class="even">
  340. <td class="convin">Array</td><td class="convop">convert base address (cast)</td><td class="convout">Integer</td></tr>
  341. <tr class="odd separate">
  342. <td class="convin">Array</td><td class="convop">copy (compat)</td><td class="convout">Array</td></tr>
  343. <tr class="even">
  344. <td class="convin"><tt>struct</tt>/<tt>union</tt></td><td class="convop">copy (identical type)</td><td class="convout"><tt>struct</tt>/<tt>union</tt></td></tr>
  345. </table>
  346. <p>
  347. Bitfields or <tt>enum</tt> types are treated like their underlying
  348. type.
  349. </p>
  350. <p>
  351. Conversions not listed above will raise an error. E.g. it's not
  352. possible to convert a pointer to a complex number or vice versa.
  353. </p>
  354. <h3 id="convert_vararg">Conversions for vararg C&nbsp;function arguments</h3>
  355. <p>
  356. The following default conversion rules apply when passing Lua objects
  357. to the variable argument part of vararg C&nbsp;functions:
  358. </p>
  359. <table class="convtable">
  360. <tr class="convhead">
  361. <td class="convin">Input</td>
  362. <td class="convop">Conversion</td>
  363. <td class="convout">Output</td>
  364. </tr>
  365. <tr class="odd separate">
  366. <td class="convin">number</td><td class="convop">&rarr;</td><td class="convout"><tt>double</tt></td></tr>
  367. <tr class="even">
  368. <td class="convin">boolean</td><td class="convop"><tt>false</tt> &rarr; 0, <tt>true</tt> &rarr; 1</td><td class="convout"><tt>bool</tt></td></tr>
  369. <tr class="odd separate">
  370. <td class="convin">nil</td><td class="convop"><tt>NULL</tt> &rarr;</td><td class="convout"><tt>(void *)</tt></td></tr>
  371. <tr class="even">
  372. <td class="convin">userdata</td><td class="convop">userdata payload &rarr;</td><td class="convout"><tt>(void *)</tt></td></tr>
  373. <tr class="odd">
  374. <td class="convin">lightuserdata</td><td class="convop">lightuserdata address &rarr;</td><td class="convout"><tt>(void *)</tt></td></tr>
  375. <tr class="even separate">
  376. <td class="convin">string</td><td class="convop">string data &rarr;</td><td class="convout"><tt>const char *</tt></td></tr>
  377. <tr class="odd separate">
  378. <td class="convin"><tt>float</tt> cdata</td><td class="convop">&rarr;</td><td class="convout"><tt>double</tt></td></tr>
  379. <tr class="even">
  380. <td class="convin">Array cdata</td><td class="convop">take base address</td><td class="convout">Element pointer</td></tr>
  381. <tr class="odd">
  382. <td class="convin"><tt>struct</tt>/<tt>union</tt> cdata</td><td class="convop">take base address</td><td class="convout"><tt>struct</tt>/<tt>union</tt> pointer</td></tr>
  383. <tr class="even">
  384. <td class="convin">Function cdata</td><td class="convop">take function address</td><td class="convout">Function pointer</td></tr>
  385. <tr class="odd">
  386. <td class="convin">Any other cdata</td><td class="convop">no conversion</td><td class="convout">C type</td></tr>
  387. </table>
  388. <p>
  389. To pass a Lua object, other than a cdata object, as a specific type,
  390. you need to override the conversion rules: create a temporary cdata
  391. object with a constructor or a cast and initialize it with the value
  392. to pass:
  393. </p>
  394. <p>
  395. Assuming <tt>x</tt> is a Lua number, here's how to pass it as an
  396. integer to a vararg function:
  397. </p>
  398. <pre class="code">
  399. ffi.cdef[[
  400. int printf(const char *fmt, ...);
  401. ]]
  402. ffi.C.printf("integer value: %d\n", ffi.new("int", x))
  403. </pre>
  404. <p>
  405. If you don't do this, the default Lua number &rarr; <tt>double</tt>
  406. conversion rule applies. A vararg C&nbsp;function expecting an integer
  407. will see a garbled or uninitialized value.
  408. </p>
  409. <h2 id="init">Initializers</h2>
  410. <p>
  411. Creating a cdata object with
  412. <a href="ext_ffi_api.html#ffi_new"><tt>ffi.new()</tt></a> or the
  413. equivalent constructor syntax always initializes its contents, too.
  414. Different rules apply, depending on the number of optional
  415. initializers and the C&nbsp;types involved:
  416. </p>
  417. <ul>
  418. <li>If no initializers are given, the object is filled with zero bytes.</li>
  419. <li>Scalar types (numbers and pointers) accept a single initializer.
  420. The Lua object is <a href="#convert_fromlua">converted to the scalar
  421. C&nbsp;type</a>.</li>
  422. <li>Valarrays (complex numbers and vectors) are treated like scalars
  423. when a single initializer is given. Otherwise they are treated like
  424. regular arrays.</li>
  425. <li>Aggregate types (arrays and structs) accept either a single
  426. <a href="#init_table">table initializer</a> or a flat list of initializers.
  427. Byte arrays can be initialized with a Lua string, too.</li>
  428. <li>The elements of an array are initialized, starting at index zero.
  429. If a single initializer is given for an array, it's repeated for all
  430. remaining elements. This doesn't happen if two or more initializers
  431. are given: all remaining uninitialized elements are filled with zero
  432. bytes.</li>
  433. <li>The fields of a <tt>struct</tt> are initialized in the order of
  434. their declaration. Uninitialized fields are filled with zero
  435. bytes.</li>
  436. <li>Only the first field of a <tt>union</tt> can be initialized with a
  437. flat initializer.</li>
  438. <li>Elements or fields which are aggregates themselves are initialized
  439. with a <em>single</em> initializer, but this may be a table
  440. initializer or a compatible aggregate.</li>
  441. <li>Excess initializers cause an error.</li>
  442. </ul>
  443. <h2 id="init_table">Table Initializers</h2>
  444. <p>
  445. The following rules apply if a Lua table is used to initialize an
  446. Array or a <tt>struct</tt>/<tt>union</tt>:
  447. </p>
  448. <ul>
  449. <li>If the table index <tt>[0]</tt> is non-<tt>nil</tt>, then the
  450. table is assumed to be zero-based. Otherwise it's assumed to be
  451. one-based.</li>
  452. <li>Array elements, starting at index zero, are initialized one-by-one
  453. with the consecutive table elements, starting at either index
  454. <tt>[0]</tt> or <tt>[1]</tt>. This process stops at the first
  455. <tt>nil</tt> table element.</li>
  456. <li>If exactly one array element was initialized, it's repeated for
  457. all the remaining elements. Otherwise all remaining uninitialized
  458. elements are filled with zero bytes.</li>
  459. <li>If the table has a non-empty hash part, a
  460. <tt>struct</tt>/<tt>union</tt> is initialized by looking up each field
  461. name (as a string key) in the table. Each non-<tt>nil</tt> value is
  462. used to initialize the corresponding field.</li>
  463. <li>Otherwise a <tt>struct</tt>/<tt>union</tt> is initialized in the
  464. order of the declaration of its fields. Each field is initialized with
  465. the consecutive table elements, starting at either index <tt>[0]</tt>
  466. or <tt>[1]</tt>. This process stops at the first <tt>nil</tt> table
  467. element.</li>
  468. <li>Uninitialized fields of a <tt>struct</tt> are filled with zero
  469. bytes.</li>
  470. <li>Initialization of a <tt>union</tt> stops after one field has been
  471. initialized. If no field has been initialized, the <tt>union</tt> is
  472. filled with zero bytes.</li>
  473. <li>Elements or fields which are aggregates themselves are initialized
  474. with a <em>single</em> initializer, but this may be a nested table
  475. initializer (or a compatible aggregate).</li>
  476. <li>Excess initializers for an array cause an error. Excess
  477. initializers for a <tt>struct</tt>/<tt>union</tt> are ignored.
  478. Unrelated table entries are ignored, too.</li>
  479. </ul>
  480. <p>
  481. Example:
  482. </p>
  483. <pre class="code">
  484. local ffi = require("ffi")
  485. ffi.cdef[[
  486. struct foo { int a, b; };
  487. union bar { int i; double d; };
  488. struct nested { int x; struct foo y; };
  489. ]]
  490. ffi.new("int[3]", {}) --> 0, 0, 0
  491. ffi.new("int[3]", {1}) --> 1, 1, 1
  492. ffi.new("int[3]", {1,2}) --> 1, 2, 0
  493. ffi.new("int[3]", {1,2,3}) --> 1, 2, 3
  494. ffi.new("int[3]", {[0]=1}) --> 1, 1, 1
  495. ffi.new("int[3]", {[0]=1,2}) --> 1, 2, 0
  496. ffi.new("int[3]", {[0]=1,2,3}) --> 1, 2, 3
  497. ffi.new("int[3]", {[0]=1,2,3,4}) --> error: too many initializers
  498. ffi.new("struct foo", {}) --> a = 0, b = 0
  499. ffi.new("struct foo", {1}) --> a = 1, b = 0
  500. ffi.new("struct foo", {1,2}) --> a = 1, b = 2
  501. ffi.new("struct foo", {[0]=1,2}) --> a = 1, b = 2
  502. ffi.new("struct foo", {b=2}) --> a = 0, b = 2
  503. ffi.new("struct foo", {a=1,b=2,c=3}) --> a = 1, b = 2 'c' is ignored
  504. ffi.new("union bar", {}) --> i = 0, d = 0.0
  505. ffi.new("union bar", {1}) --> i = 1, d = ?
  506. ffi.new("union bar", {[0]=1,2}) --> i = 1, d = ? '2' is ignored
  507. ffi.new("union bar", {d=2}) --> i = ?, d = 2.0
  508. ffi.new("struct nested", {1,{2,3}}) --> x = 1, y.a = 2, y.b = 3
  509. ffi.new("struct nested", {x=1,y={2,3}}) --> x = 1, y.a = 2, y.b = 3
  510. </pre>
  511. <h2 id="cdata_ops">Operations on cdata Objects</h2>
  512. <p>
  513. All of the standard Lua operators can be applied to cdata objects or a
  514. mix of a cdata object and another Lua object. The following list shows
  515. the valid combinations. All other combinations currently raise an
  516. error.
  517. </p>
  518. <p>
  519. Reference types are dereferenced <em>before</em> performing each of
  520. the operations below &mdash; the operation is applied to the
  521. C&nbsp;type pointed to by the reference.
  522. </p>
  523. <p>
  524. The pre-defined operations are always tried first before deferring to a
  525. metamethod for a ctype (if defined).
  526. </p>
  527. <h3 id="cdata_array">Indexing a cdata object</h3>
  528. <ul>
  529. <li><b>Indexing a pointer/array</b>: a cdata pointer/array can be
  530. indexed by a cdata number or a Lua number. The element address is
  531. computed as the base address plus the number value multiplied by the
  532. element size in bytes. A read access loads the element value and
  533. <a href="#convert_tolua">converts it to a Lua object</a>. A write
  534. access <a href="#convert_fromlua">converts a Lua object to the element
  535. type</a> and stores the converted value to the element. An error is
  536. raised if the element size is undefined or a write access to a
  537. constant element is attempted.</li>
  538. <li><b>Dereferencing a <tt>struct</tt>/<tt>union</tt> field</b>: a
  539. cdata <tt>struct</tt>/<tt>union</tt> or a pointer to a
  540. <tt>struct</tt>/<tt>union</tt> can be dereferenced by a string key,
  541. giving the field name. The field address is computed as the base
  542. address plus the relative offset of the field. A read access loads the
  543. field value and <a href="#convert_tolua">converts it to a Lua
  544. object</a>. A write access <a href="#convert_fromlua">converts a Lua
  545. object to the field type</a> and stores the converted value to the
  546. field. An error is raised if a write access to a constant
  547. <tt>struct</tt>/<tt>union</tt> or a constant field is attempted.</li>
  548. <li><b>Indexing a complex number</b>: a complex number can be indexed
  549. either by a cdata number or a Lua number with the values 0 or 1, or by
  550. the strings <tt>"re"</tt> or <tt>"im"</tt>. A read access loads the
  551. real part (<tt>[0]</tt>, <tt>.re</tt>) or the imaginary part
  552. (<tt>[1]</tt>, <tt>.im</tt>) part of a complex number and
  553. <a href="#convert_tolua">converts it to a Lua number</a>. The
  554. sub-parts of a complex number are immutable &mdash; assigning to an
  555. index of a complex number raises an error. Accessing out-of-bound
  556. indexes returns unspecified results, but is guaranteed not to trigger
  557. memory access violations.</li>
  558. <li><b>Indexing a vector</b>: a vector is treated like an array for
  559. indexing purposes, except the vector elements are immutable &mdash;
  560. assigning to an index of a vector raises an error.</li>
  561. </ul>
  562. <p>
  563. Note: since there's (deliberately) no address-of operator, a cdata
  564. object holding a value type is effectively immutable after
  565. initialization. The JIT compiler benefits from this fact when applying
  566. certain optimizations.
  567. </p>
  568. <p>
  569. As a consequence of this, the <em>elements</em> of complex numbers and
  570. vectors are immutable. But the elements of an aggregate holding these
  571. types <em>may</em> be modified of course. I.e. you cannot assign to
  572. <tt>foo.c.im</tt>, but you can assign a (newly created) complex number
  573. to <tt>foo.c</tt>.
  574. </p>
  575. <h3 id="cdata_call">Calling a cdata object</h3>
  576. <ul>
  577. <li><b>Constructor</b>: a ctype object can be called and used as a
  578. <a href="ext_ffi_api.html#ffi_new">constructor</a>.</li>
  579. <li><b>C&nbsp;function call</b>: a cdata function or cdata function
  580. pointer can be called. The passed arguments are
  581. <a href="#convert_fromlua">converted to the C&nbsp;types</a> of the
  582. parameters given by the function declaration. Arguments passed to the
  583. variable argument part of vararg C&nbsp;function use
  584. <a href="#convert_vararg">special conversion rules</a>. This
  585. C&nbsp;function is called and the return value (if any) is
  586. <a href="#convert_tolua">converted to a Lua object</a>.<br>
  587. On Windows/x86 systems, <tt>__stdcall</tt> functions are automatically
  588. detected and a function declared as <tt>__cdecl</tt> (the default) is
  589. silently fixed up after the first call.</li>
  590. </ul>
  591. <h3 id="cdata_arith">Arithmetic on cdata objects</h3>
  592. <ul>
  593. <li><b>Pointer arithmetic</b>: a cdata pointer/array and a cdata
  594. number or a Lua number can be added or subtracted. The number must be
  595. on the right hand side for a subtraction. The result is a pointer of
  596. the same type with an address plus or minus the number value
  597. multiplied by the element size in bytes. An error is raised if the
  598. element size is undefined.</li>
  599. <li><b>Pointer difference</b>: two compatible cdata pointers/arrays
  600. can be subtracted. The result is the difference between their
  601. addresses, divided by the element size in bytes. An error is raised if
  602. the element size is undefined or zero.</li>
  603. <li><b>64&nbsp;bit integer arithmetic</b>: the standard arithmetic
  604. operators (<tt>+&nbsp;-&nbsp;*&nbsp;/&nbsp;%&nbsp;^</tt> and unary
  605. minus) can be applied to two cdata numbers, or a cdata number and a
  606. Lua number. If one of them is an <tt>uint64_t</tt>, the other side is
  607. converted to an <tt>uint64_t</tt> and an unsigned arithmetic operation
  608. is performed. Otherwise both sides are converted to an
  609. <tt>int64_t</tt> and a signed arithmetic operation is performed. The
  610. result is a boxed 64&nbsp;bit cdata object.<br>
  611. These rules ensure that 64&nbsp;bit integers are "sticky". Any
  612. expression involving at least one 64&nbsp;bit integer operand results
  613. in another one. The undefined cases for the division, modulo and power
  614. operators return <tt>2LL&nbsp;^&nbsp;63</tt> or
  615. <tt>2ULL&nbsp;^&nbsp;63</tt>.<br>
  616. You'll have to explicitly convert a 64&nbsp;bit integer to a Lua
  617. number (e.g. for regular floating-point calculations) with
  618. <tt>tonumber()</tt>. But note this may incur a precision loss.</li>
  619. </ul>
  620. <h3 id="cdata_comp">Comparisons of cdata objects</h3>
  621. <ul>
  622. <li><b>Pointer comparison</b>: two compatible cdata pointers/arrays
  623. can be compared. The result is the same as an unsigned comparison of
  624. their addresses. <tt>nil</tt> is treated like a <tt>NULL</tt> pointer,
  625. which is compatible with any other pointer type.</li>
  626. <li><b>64&nbsp;bit integer comparison</b>: two cdata numbers, or a
  627. cdata number and a Lua number can be compared with each other. If one
  628. of them is an <tt>uint64_t</tt>, the other side is converted to an
  629. <tt>uint64_t</tt> and an unsigned comparison is performed. Otherwise
  630. both sides are converted to an <tt>int64_t</tt> and a signed
  631. comparison is performed.</li>
  632. </ul>
  633. <h3 id="cdata_key">cdata objects as table keys</h3>
  634. <p>
  635. Lua tables may be indexed by cdata objects, but this doesn't provide
  636. any useful semantics &mdash; <b>cdata objects are unsuitable as table
  637. keys!</b>
  638. </p>
  639. <p>
  640. A cdata object is treated like any other garbage-collected object and
  641. is hashed and compared by its address for table indexing. Since
  642. there's no interning for cdata value types, the same value may be
  643. boxed in different cdata objects with different addresses. Thus
  644. <tt>t[1LL+1LL]</tt> and <tt>t[2LL]</tt> usually <b>do not</b> point to
  645. the same hash slot and they certainly <b>do not</b> point to the same
  646. hash slot as <tt>t[2]</tt>.
  647. </p>
  648. <p>
  649. It would seriously drive up implementation complexity and slow down
  650. the common case, if one were to add extra handling for by-value
  651. hashing and comparisons to Lua tables. Given the ubiquity of their use
  652. inside the VM, this is not acceptable.
  653. </p>
  654. <p>
  655. There are three viable alternatives, if you really need to use cdata
  656. objects as keys:
  657. </p>
  658. <ul>
  659. <li>If you can get by with the precision of Lua numbers
  660. (52&nbsp;bits), then use <tt>tonumber()</tt> on a cdata number or
  661. combine multiple fields of a cdata aggregate to a Lua number. Then use
  662. the resulting Lua number as a key when indexing tables.<br>
  663. One obvious benefit: <tt>t[tonumber(2LL)]</tt> <b>does</b> point to
  664. the same slot as <tt>t[2]</tt>.</li>
  665. <li>Otherwise use either <tt>tostring()</tt> on 64&nbsp;bit integers
  666. or complex numbers or combine multiple fields of a cdata aggregate to
  667. a Lua string (e.g. with
  668. <a href="ext_ffi_api.html#ffi_string"><tt>ffi.string()</tt></a>). Then
  669. use the resulting Lua string as a key when indexing tables.</li>
  670. <li>Create your own specialized hash table implementation using the
  671. C&nbsp;types provided by the FFI library, just like you would in
  672. C&nbsp;code. Ultimately this may give much better performance than the
  673. other alternatives or what a generic by-value hash table could
  674. possibly provide.</li>
  675. </ul>
  676. <h2 id="gc">Garbage Collection of cdata Objects</h2>
  677. <p>
  678. All explicitly (<tt>ffi.new()</tt>, <tt>ffi.cast()</tt> etc.) or
  679. implicitly (accessors) created cdata objects are garbage collected.
  680. You need to ensure to retain valid references to cdata objects
  681. somewhere on a Lua stack, an upvalue or in a Lua table while they are
  682. still in use. Once the last reference to a cdata object is gone, the
  683. garbage collector will automatically free the memory used by it (at
  684. the end of the next GC cycle).
  685. </p>
  686. <p>
  687. Please note that pointers themselves are cdata objects, however they
  688. are <b>not</b> followed by the garbage collector. So e.g. if you
  689. assign a cdata array to a pointer, you must keep the cdata object
  690. holding the array alive as long as the pointer is still in use:
  691. </p>
  692. <pre class="code">
  693. ffi.cdef[[
  694. typedef struct { int *a; } foo_t;
  695. ]]
  696. local s = ffi.new("foo_t", ffi.new("int[10]")) -- <span style="color:#c00000;">WRONG!</span>
  697. local a = ffi.new("int[10]") -- <span style="color:#00a000;">OK</span>
  698. local s = ffi.new("foo_t", a)
  699. -- Now do something with 's', but keep 'a' alive until you're done.
  700. </pre>
  701. <p>
  702. Similar rules apply for Lua strings which are implicitly converted to
  703. <tt>"const&nbsp;char&nbsp;*"</tt>: the string object itself must be
  704. referenced somewhere or it'll be garbage collected eventually. The
  705. pointer will then point to stale data, which may have already been
  706. overwritten. Note that <em>string literals</em> are automatically kept
  707. alive as long as the function containing it (actually its prototype)
  708. is not garbage collected.
  709. </p>
  710. <p>
  711. Objects which are passed as an argument to an external C&nbsp;function
  712. are kept alive until the call returns. So it's generally safe to
  713. create temporary cdata objects in argument lists. This is a common
  714. idiom for <a href="#convert_vararg">passing specific C&nbsp;types to
  715. vararg functions</a>.
  716. </p>
  717. <p>
  718. Memory areas returned by C functions (e.g. from <tt>malloc()</tt>)
  719. must be manually managed, of course (or use
  720. <a href="ext_ffi_api.html#ffi_gc"><tt>ffi.gc()</tt></a>)). Pointers to
  721. cdata objects are indistinguishable from pointers returned by C
  722. functions (which is one of the reasons why the GC cannot follow them).
  723. </p>
  724. <h2 id="clib">C Library Namespaces</h2>
  725. <p>
  726. A C&nbsp;library namespace is a special kind of object which allows
  727. access to the symbols contained in shared libraries or the default
  728. symbol namespace. The default
  729. <a href="ext_ffi_api.html#ffi_C"><tt>ffi.C</tt></a> namespace is
  730. automatically created when the FFI library is loaded. C&nbsp;library
  731. namespaces for specific shared libraries may be created with the
  732. <a href="ext_ffi_api.html#ffi_load"><tt>ffi.load()</tt></a> API
  733. function.
  734. </p>
  735. <p>
  736. Indexing a C&nbsp;library namespace object with a symbol name (a Lua
  737. string) automatically binds it to the library. First the symbol type
  738. is resolved &mdash; it must have been declared with
  739. <a href="ext_ffi_api.html#ffi_cdef"><tt>ffi.cdef</tt></a>. Then the
  740. symbol address is resolved by searching for the symbol name in the
  741. associated shared libraries or the default symbol namespace. Finally,
  742. the resulting binding between the symbol name, the symbol type and its
  743. address is cached. Missing symbol declarations or nonexistent symbol
  744. names cause an error.
  745. </p>
  746. <p>
  747. This is what happens on a <b>read access</b> for the different kinds of
  748. symbols:
  749. </p>
  750. <ul>
  751. <li>External functions: a cdata object with the type of the function
  752. and its address is returned.</li>
  753. <li>External variables: the symbol address is dereferenced and the
  754. loaded value is <a href="#convert_tolua">converted to a Lua object</a>
  755. and returned.</li>
  756. <li>Constant values (<tt>static&nbsp;const</tt> or <tt>enum</tt>
  757. constants): the constant is <a href="#convert_tolua">converted to a
  758. Lua object</a> and returned.</li>
  759. </ul>
  760. <p>
  761. This is what happens on a <b>write access</b>:
  762. </p>
  763. <ul>
  764. <li>External variables: the value to be written is
  765. <a href="#convert_fromlua">converted to the C&nbsp;type</a> of the
  766. variable and then stored at the symbol address.</li>
  767. <li>Writing to constant variables or to any other symbol type causes
  768. an error, like any other attempted write to a constant location.</li>
  769. </ul>
  770. <p>
  771. C&nbsp;library namespaces themselves are garbage collected objects. If
  772. the last reference to the namespace object is gone, the garbage
  773. collector will eventually release the shared library reference and
  774. remove all memory associated with the namespace. Since this may
  775. trigger the removal of the shared library from the memory of the
  776. running process, it's generally <em>not safe</em> to use function
  777. cdata objects obtained from a library if the namespace object may be
  778. unreferenced.
  779. </p>
  780. <p>
  781. Performance notice: the JIT compiler specializes to the identity of
  782. namespace objects and to the strings used to index it. This
  783. effectively turns function cdata objects into constants. It's not
  784. useful and actually counter-productive to explicitly cache these
  785. function objects, e.g. <tt>local strlen = ffi.C.strlen</tt>. OTOH it
  786. <em>is</em> useful to cache the namespace itself, e.g. <tt>local C =
  787. ffi.C</tt>.
  788. </p>
  789. <h2 id="policy">No Hand-holding!</h2>
  790. <p>
  791. The FFI library has been designed as <b>a low-level library</b>. The
  792. goal is to interface with C&nbsp;code and C&nbsp;data types with a
  793. minimum of overhead. This means <b>you can do anything you can do
  794. from&nbsp;C</b>: access all memory, overwrite anything in memory, call
  795. machine code at any memory address and so on.
  796. </p>
  797. <p>
  798. The FFI library provides <b>no memory safety</b>, unlike regular Lua
  799. code. It will happily allow you to dereference a <tt>NULL</tt>
  800. pointer, to access arrays out of bounds or to misdeclare
  801. C&nbsp;functions. If you make a mistake, your application might crash,
  802. just like equivalent C&nbsp;code would.
  803. </p>
  804. <p>
  805. This behavior is inevitable, since the goal is to provide full
  806. interoperability with C&nbsp;code. Adding extra safety measures, like
  807. bounds checks, would be futile. There's no way to detect
  808. misdeclarations of C&nbsp;functions, since shared libraries only
  809. provide symbol names, but no type information. Likewise there's no way
  810. to infer the valid range of indexes for a returned pointer.
  811. </p>
  812. <p>
  813. Again: the FFI library is a low-level library. This implies it needs
  814. to be used with care, but it's flexibility and performance often
  815. outweigh this concern. If you're a C or C++ developer, it'll be easy
  816. to apply your existing knowledge. OTOH writing code for the FFI
  817. library is not for the faint of heart and probably shouldn't be the
  818. first exercise for someone with little experience in Lua, C or C++.
  819. </p>
  820. <p>
  821. As a corollary of the above, the FFI library is <b>not safe for use by
  822. untrusted Lua code</b>. If you're sandboxing untrusted Lua code, you
  823. definitely don't want to give this code access to the FFI library or
  824. to <em>any</em> cdata object (except 64&nbsp;bit integers or complex
  825. numbers). Any properly engineered Lua sandbox needs to provide safety
  826. wrappers for many of the standard Lua library functions &mdash;
  827. similar wrappers need to be written for high-level operations on FFI
  828. data types, too.
  829. </p>
  830. <h2 id="status">Current Status</h2>
  831. <p>
  832. The initial release of the FFI library has some limitations and is
  833. missing some features. Most of these will be fixed in future releases.
  834. </p>
  835. <p>
  836. <a href="#clang">C language support</a> is
  837. currently incomplete:
  838. </p>
  839. <ul>
  840. <li>C&nbsp;declarations are not passed through a C&nbsp;pre-processor,
  841. yet.</li>
  842. <li>The C&nbsp;parser is able to evaluate most constant expressions
  843. commonly found in C&nbsp;header files. However it doesn't handle the
  844. full range of C&nbsp;expression semantics and may fail for some
  845. obscure constructs.</li>
  846. <li><tt>static const</tt> declarations only work for integer types
  847. up to 32&nbsp;bits. Neither declaring string constants nor
  848. floating-point constants is supported.</li>
  849. <li>Packed <tt>struct</tt> bitfields that cross container boundaries
  850. are not implemented.</li>
  851. <li>Native vector types may be defined with the GCC <tt>mode</tt> or
  852. <tt>vector_size</tt> attribute. But no operations other than loading,
  853. storing and initializing them are supported, yet.</li>
  854. <li>The <tt>volatile</tt> type qualifier is currently ignored by
  855. compiled code.</li>
  856. <li><a href="ext_ffi_api.html#ffi_cdef"><tt>ffi.cdef</tt></a> silently
  857. ignores all re-declarations.</li>
  858. </ul>
  859. <p>
  860. The JIT compiler already handles a large subset of all FFI operations.
  861. It automatically falls back to the interpreter for unimplemented
  862. operations (you can check for this with the
  863. <a href="running.html#opt_j"><tt>-jv</tt></a> command line option).
  864. The following operations are currently not compiled and may exhibit
  865. suboptimal performance, especially when used in inner loops:
  866. </p>
  867. <ul>
  868. <li>Array/<tt>struct</tt> copies and bulk initializations.</li>
  869. <li>Bitfield accesses and initializations.</li>
  870. <li>Vector operations.</li>
  871. <li>Table initializers.</li>
  872. <li>Initialization of nested <tt>struct</tt>/<tt>union</tt> types.</li>
  873. <li>Allocations of variable-length arrays or structs.</li>
  874. <li>Allocations of C&nbsp;types with a size &gt; 64&nbsp;bytes or an
  875. alignment &gt; 8&nbsp;bytes.</li>
  876. <li>Conversions from lightuserdata to <tt>void&nbsp;*</tt>.</li>
  877. <li>Pointer differences for element sizes that are not a power of
  878. two.</li>
  879. <li>Calls to non-cdecl or vararg C&nbsp;functions.</li>
  880. <li>Calls to C&nbsp;functions with aggregates passed or returned by
  881. value.</li>
  882. <li>Calls to ctype metamethods which are not plain functions.</li>
  883. <li>ctype <tt>__newindex</tt> tables and non-string lookups in ctype
  884. <tt>__index</tt> tables.</li>
  885. <li>Accesses to external variables in C&nbsp;library namespaces.</li>
  886. <li><tt>tostring()</tt> for cdata types.</li>
  887. <li>The following <a href="ext_ffi_api.html">ffi.* API</a> functions:
  888. <tt>ffi.sizeof()</tt>, <tt>ffi.alignof()</tt>, <tt>ffi.offsetof()</tt>.
  889. </ul>
  890. <p>
  891. Other missing features:
  892. </p>
  893. <ul>
  894. <li>Bit operations for 64&nbsp;bit types.</li>
  895. <li>Arithmetic for <tt>complex</tt> numbers.</li>
  896. <li>Callbacks from C&nbsp;code to Lua functions.</li>
  897. <li>Atomic handling of <tt>errno</tt>.</li>
  898. <li>Passing structs by value to vararg C&nbsp;functions.</li>
  899. <li><a href="extensions.html#exceptions">C++ exception interoperability</a>
  900. does not extend to C&nbsp;functions called via the FFI.</li>
  901. </ul>
  902. <br class="flush">
  903. </div>
  904. <div id="foot">
  905. <hr class="hide">
  906. Copyright &copy; 2005-2011 Mike Pall
  907. <span class="noprint">
  908. &middot;
  909. <a href="contact.html">Contact</a>
  910. </span>
  911. </div>
  912. </body>
  913. </html>