Fix frame traversal for __gc handler frames.
[luajit-2.0.git] / doc / ext_ffi.html
blob6b846c4104019589654e8f52219a002302e04736
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 Library</title>
5 <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
6 <meta name="Copyright" content="Copyright (C) 2005-2020">
7 <meta name="Language" content="en">
8 <link rel="stylesheet" type="text/css" href="bluequad.css" media="screen">
9 <link rel="stylesheet" type="text/css" href="bluequad-print.css" media="print">
10 </head>
11 <body>
12 <div id="site">
13 <a href="https://luajit.org"><span>Lua<span id="logo">JIT</span></span></a>
14 </div>
15 <div id="head">
16 <h1>FFI Library</h1>
17 </div>
18 <div id="nav">
19 <ul><li>
20 <a href="luajit.html">LuaJIT</a>
21 <ul><li>
22 <a href="https://luajit.org/download.html">Download <span class="ext">&raquo;</span></a>
23 </li><li>
24 <a href="install.html">Installation</a>
25 </li><li>
26 <a href="running.html">Running</a>
27 </li></ul>
28 </li><li>
29 <a href="extensions.html">Extensions</a>
30 <ul><li>
31 <a class="current" href="ext_ffi.html">FFI Library</a>
32 <ul><li>
33 <a href="ext_ffi_tutorial.html">FFI Tutorial</a>
34 </li><li>
35 <a href="ext_ffi_api.html">ffi.* API</a>
36 </li><li>
37 <a href="ext_ffi_semantics.html">FFI Semantics</a>
38 </li></ul>
39 </li><li>
40 <a href="ext_jit.html">jit.* Library</a>
41 </li><li>
42 <a href="ext_c_api.html">Lua/C API</a>
43 </li></ul>
44 </li><li>
45 <a href="status.html">Status</a>
46 </li><li>
47 <a href="faq.html">FAQ</a>
48 </li><li>
49 <a href="http://wiki.luajit.org/">Wiki <span class="ext">&raquo;</span></a>
50 </li><li>
51 <a href="https://luajit.org/list.html">Mailing List <span class="ext">&raquo;</span></a>
52 </li></ul>
53 </div>
54 <div id="main">
55 <p>
57 The FFI library allows <b>calling external C&nbsp;functions</b> and
58 <b>using C&nbsp;data structures</b> from pure Lua code.
60 </p>
61 <p>
63 The FFI library largely obviates the need to write tedious manual
64 Lua/C bindings in C. No need to learn a separate binding language
65 &mdash; <b>it parses plain C&nbsp;declarations!</b> These can be
66 cut-n-pasted from C&nbsp;header files or reference manuals. It's up to
67 the task of binding large libraries without the need for dealing with
68 fragile binding generators.
70 </p>
71 <p>
72 The FFI library is tightly integrated into LuaJIT (it's not available
73 as a separate module). The code generated by the JIT-compiler for
74 accesses to C&nbsp;data structures from Lua code is on par with the
75 code a C&nbsp;compiler would generate. Calls to C&nbsp;functions can
76 be inlined in JIT-compiled code, unlike calls to functions bound via
77 the classic Lua/C API.
78 </p>
79 <p>
80 This page gives a short introduction to the usage of the FFI library.
81 <em>Please use the FFI sub-topics in the navigation bar to learn more.</em>
82 </p>
84 <h2 id="call">Motivating Example: Calling External C Functions</h2>
85 <p>
86 It's really easy to call an external C&nbsp;library function:
87 </p>
88 <pre class="code mark">
89 <span class="codemark">&#9312;
90 &#9313;
93 &#9314;</span>local ffi = require("ffi")
94 ffi.cdef[[
95 <span style="color:#00a000;">int printf(const char *fmt, ...);</span>
97 ffi.C.printf("Hello %s!", "world")
98 </pre>
99 <p>
100 So, let's pick that apart:
101 </p>
103 <span class="mark">&#9312;</span> Load the FFI library.
104 </p>
106 <span class="mark">&#9313;</span> Add a C&nbsp;declaration
107 for the function. The part inside the double-brackets (in green) is
108 just standard C&nbsp;syntax.
109 </p>
111 <span class="mark">&#9314;</span> Call the named
112 C&nbsp;function &mdash; Yes, it's that simple!
113 </p>
114 <p style="font-size: 8pt;">
115 Actually, what goes on behind the scenes is far from simple: <span
116 style="color:#4040c0;">&#9314;</span> makes use of the standard
117 C&nbsp;library namespace <tt>ffi.C</tt>. Indexing this namespace with
118 a symbol name (<tt>"printf"</tt>) automatically binds it to the
119 standard C&nbsp;library. The result is a special kind of object which,
120 when called, runs the <tt>printf</tt> function. The arguments passed
121 to this function are automatically converted from Lua objects to the
122 corresponding C&nbsp;types.
123 </p>
125 Ok, so maybe the use of <tt>printf()</tt> wasn't such a spectacular
126 example. You could have done that with <tt>io.write()</tt> and
127 <tt>string.format()</tt>, too. But you get the idea ...
128 </p>
130 So here's something to pop up a message box on Windows:
131 </p>
132 <pre class="code">
133 local ffi = require("ffi")
134 ffi.cdef[[
135 <span style="color:#00a000;">int MessageBoxA(void *w, const char *txt, const char *cap, int type);</span>
137 ffi.C.MessageBoxA(nil, "Hello world!", "Test", 0)
138 </pre>
140 Bing! Again, that was far too easy, no?
141 </p>
142 <p style="font-size: 8pt;">
143 Compare this with the effort required to bind that function using the
144 classic Lua/C API: create an extra C&nbsp;file, add a C&nbsp;function
145 that retrieves and checks the argument types passed from Lua and calls
146 the actual C&nbsp;function, add a list of module functions and their
147 names, add a <tt>luaopen_*</tt> function and register all module
148 functions, compile and link it into a shared library (DLL), move it to
149 the proper path, add Lua code that loads the module aaaand ... finally
150 call the binding function. Phew!
151 </p>
153 <h2 id="cdata">Motivating Example: Using C Data Structures</h2>
155 The FFI library allows you to create and access C&nbsp;data
156 structures. Of course the main use for this is for interfacing with
157 C&nbsp;functions. But they can be used stand-alone, too.
158 </p>
160 Lua is built upon high-level data types. They are flexible, extensible
161 and dynamic. That's why we all love Lua so much. Alas, this can be
162 inefficient for certain tasks, where you'd really want a low-level
163 data type. E.g. a large array of a fixed structure needs to be
164 implemented with a big table holding lots of tiny tables. This imposes
165 both a substantial memory overhead as well as a performance overhead.
166 </p>
168 Here's a sketch of a library that operates on color images plus a
169 simple benchmark. First, the plain Lua version:
170 </p>
171 <pre class="code">
172 local floor = math.floor
174 local function image_ramp_green(n)
175 local img = {}
176 local f = 255/(n-1)
177 for i=1,n do
178 img[i] = { red = 0, green = floor((i-1)*f), blue = 0, alpha = 255 }
180 return img
183 local function image_to_grey(img, n)
184 for i=1,n do
185 local y = floor(0.3*img[i].red + 0.59*img[i].green + 0.11*img[i].blue)
186 img[i].red = y; img[i].green = y; img[i].blue = y
190 local N = 400*400
191 local img = image_ramp_green(N)
192 for i=1,1000 do
193 image_to_grey(img, N)
195 </pre>
197 This creates a table with 160.000 pixels, each of which is a table
198 holding four number values in the range of 0-255. First an image with
199 a green ramp is created (1D for simplicity), then the image is
200 converted to greyscale 1000 times. Yes, that's silly, but I was in
201 need of a simple example ...
202 </p>
204 And here's the FFI version. The modified parts have been marked in
205 bold:
206 </p>
207 <pre class="code mark">
208 <span class="codemark">&#9312;
214 &#9313;
216 &#9314;
217 &#9315;
224 &#9314;
225 &#9316;</span><b>local ffi = require("ffi")
226 ffi.cdef[[
227 </b><span style="color:#00a000;">typedef struct { uint8_t red, green, blue, alpha; } rgba_pixel;</span><b>
228 ]]</b>
230 local function image_ramp_green(n)
231 <b>local img = ffi.new("rgba_pixel[?]", n)</b>
232 local f = 255/(n-1)
233 for i=<b>0,n-1</b> do
234 <b>img[i].green = i*f</b>
235 <b>img[i].alpha = 255</b>
237 return img
240 local function image_to_grey(img, n)
241 for i=<b>0,n-1</b> do
242 local y = <b>0.3*img[i].red + 0.59*img[i].green + 0.11*img[i].blue</b>
243 img[i].red = y; img[i].green = y; img[i].blue = y
247 local N = 400*400
248 local img = image_ramp_green(N)
249 for i=1,1000 do
250 image_to_grey(img, N)
252 </pre>
254 Ok, so that wasn't too difficult:
255 </p>
257 <span class="mark">&#9312;</span> First, load the FFI
258 library and declare the low-level data type. Here we choose a
259 <tt>struct</tt> which holds four byte fields, one for each component
260 of a 4x8&nbsp;bit RGBA pixel.
261 </p>
263 <span class="mark">&#9313;</span> Creating the data
264 structure with <tt>ffi.new()</tt> is straightforward &mdash; the
265 <tt>'?'</tt> is a placeholder for the number of elements of a
266 variable-length array.
267 </p>
269 <span class="mark">&#9314;</span> C&nbsp;arrays are
270 zero-based, so the indexes have to run from <tt>0</tt> to
271 <tt>n-1</tt>. One might want to allocate one more element instead to
272 simplify converting legacy code.
273 </p>
275 <span class="mark">&#9315;</span> Since <tt>ffi.new()</tt>
276 zero-fills the array by default, we only need to set the green and the
277 alpha fields.
278 </p>
280 <span class="mark">&#9316;</span> The calls to
281 <tt>math.floor()</tt> can be omitted here, because floating-point
282 numbers are already truncated towards zero when converting them to an
283 integer. This happens implicitly when the number is stored in the
284 fields of each pixel.
285 </p>
287 Now let's have a look at the impact of the changes: first, memory
288 consumption for the image is down from 22&nbsp;Megabytes to
289 640&nbsp;Kilobytes (400*400*4 bytes). That's a factor of 35x less! So,
290 yes, tables do have a noticeable overhead. BTW: The original program
291 would consume 40&nbsp;Megabytes in plain Lua (on x64).
292 </p>
294 Next, performance: the pure Lua version runs in 9.57 seconds (52.9
295 seconds with the Lua interpreter) and the FFI version runs in 0.48
296 seconds on my machine (YMMV). That's a factor of 20x faster (110x
297 faster than the Lua interpreter).
298 </p>
299 <p style="font-size: 8pt;">
300 The avid reader may notice that converting the pure Lua version over
301 to use array indexes for the colors (<tt>[1]</tt> instead of
302 <tt>.red</tt>, <tt>[2]</tt> instead of <tt>.green</tt> etc.) ought to
303 be more compact and faster. This is certainly true (by a factor of
304 ~1.7x). Switching to a struct-of-arrays would help, too.
305 </p>
306 <p style="font-size: 8pt;">
307 However the resulting code would be less idiomatic and rather
308 error-prone. And it still doesn't get even close to the performance of
309 the FFI version of the code. Also, high-level data structures cannot
310 be easily passed to other C&nbsp;functions, especially I/O functions,
311 without undue conversion penalties.
312 </p>
313 <br class="flush">
314 </div>
315 <div id="foot">
316 <hr class="hide">
317 Copyright &copy; 2005-2020
318 <span class="noprint">
319 &middot;
320 <a href="contact.html">Contact</a>
321 </span>
322 </div>
323 </body>
324 </html>