summaryrefslogtreecommitdiff
path: root/doc/ext_ffi_tutorial.html
diff options
context:
space:
mode:
authorMike Pall <mike>2011-04-12 19:15:00 +0200
committerMike Pall <mike>2011-04-12 19:16:39 +0200
commit3b6f37dd2c336987251d53a4678396ef38921b3e (patch)
tree6e6176c37b600e461d078dbe663008dcc84d2418 /doc/ext_ffi_tutorial.html
parentfa5cd010e8e28c7fe2338d0fdd538e95ddd88bcc (diff)
downloadluajit-3b6f37dd2c336987251d53a4678396ef38921b3e.tar.gz
luajit-3b6f37dd2c336987251d53a4678396ef38921b3e.tar.bz2
luajit-3b6f37dd2c336987251d53a4678396ef38921b3e.zip
FFI: Add ctype metamethods and ffi.metatype().
Diffstat (limited to 'doc/ext_ffi_tutorial.html')
-rw-r--r--doc/ext_ffi_tutorial.html93
1 files changed, 93 insertions, 0 deletions
diff --git a/doc/ext_ffi_tutorial.html b/doc/ext_ffi_tutorial.html
index 38126865..d5b04bc6 100644
--- a/doc/ext_ffi_tutorial.html
+++ b/doc/ext_ffi_tutorial.html
@@ -386,6 +386,99 @@ application might work on some systems, but would fail in a POSIX/x64
386environment. 386environment.
387</p> 387</p>
388 388
389<h2 id="metatype">Defining Metamethods for a C&nbsp;Type</h2>
390<p>
391The following code explains how to define metamethods for a C type.
392We define a simple point type and add some operations to it:
393</p>
394<pre class="code mark">
395<span class="codemark">&nbsp;
396&#9312;
397
398
399
400&#9313;
401
402&#9314;
403
404&#9315;
405
406
407
408&#9316;
409
410&#9317;</span>local ffi = require("ffi")
411ffi.cdef[[
412<span style="color:#00a000;">typedef struct { double x, y; } point_t;</span>
413]]
414
415local point
416local mt = {
417 __add = function(a, b) return point(a.x+b.x, a.y+b.y) end,
418 __len = function(a) return math.sqrt(a.x*a.x + a.y*a.y) end,
419 __index = {
420 area = function(a) return a.x*a.x + a.y*a.y end,
421 },
422}
423point = ffi.metatype("point_t", mt)
424
425local a = point(3, 4)
426print(a.x, a.y) --> 3 4
427print(#a) --> 5
428print(a:area()) --> 25
429local b = a + point(0.5, 8)
430print(#b) --> 12.5
431</pre>
432<p>
433Here's the step-by-step explanation:
434</p>
435<p>
436<span class="mark">&#9312;</span> This defines the C&nbsp;type for a
437two-dimensional point object.
438</p>
439<p>
440<span class="mark">&#9313;</span> We have to declare the variable
441holding the point constructor first, because it's used inside of a
442metamethod.
443</p>
444<p>
445<span class="mark">&#9314;</span> Let's define an <tt>__add</tt>
446metamethod which adds the coordinates of two points and creates a new
447point object. For simplicity, this function assumes that both arguments
448are points. But it could be any mix of objects, if at least one operand
449is of the required type (e.g. adding a point plus a number or vice
450versa). Our <tt>__len</tt> metamethod returns the distance of a point to
451the origin.
452</p>
453<p>
454<span class="mark">&#9315;</span> If we run out of operators, we can
455define named methods, too. Here the <tt>__index</tt> table defines an
456<tt>area</tt> function. For custom indexing needs, one might want to
457define <tt>__index</tt> and <tt>__newindex</tt> functions instead.
458</p>
459<p>
460<span class="mark">&#9316;</span> This associates the metamethods with
461our C&nbsp;type. This only needs to be done once. For convenience, a
462constructor is returned by
463<a href="ffi_ext_api.html#ffi_metatype"><tt>ffi.metatype()</tt></a>.
464We're not required to use it, though. The original C&nbsp;type can still
465be used e.g. to create an array of points. The metamethods automatically
466apply to any and all uses of this type.
467</p>
468<p>
469Please note that the association with a metatable is permanent and
470<b>the metatable must not be modified afterwards!</b> Ditto for the
471<tt>__index</tt> table.
472</p>
473<p>
474<span class="mark">&#9317;</span> Here are some simple usage examples
475for the point type and their expected results. The pre-defined
476operations (such as <tt>a.x</tt>) can be freely mixed with the newly
477defined metamethods. Note that <tt>area</tt> is a method and must be
478called with the Lua syntax for methods: <tt>a:area()</tt>, not
479<tt>a.area()</tt>.
480</p>
481
389<h2 id="idioms">Translating C&nbsp;Idioms</h2> 482<h2 id="idioms">Translating C&nbsp;Idioms</h2>
390<p> 483<p>
391Here's a list of common C&nbsp;idioms and their translation to the 484Here's a list of common C&nbsp;idioms and their translation to the