ext_ffi_semantics.html 42 KB

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