aboutsummaryrefslogtreecommitdiff
path: root/doc
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
parentfa5cd010e8e28c7fe2338d0fdd538e95ddd88bcc (diff)
downloadluajit-3b6f37dd2c336987251d53a4678396ef38921b3e.tar.gz
luajit-3b6f37dd2c336987251d53a4678396ef38921b3e.tar.bz2
luajit-3b6f37dd2c336987251d53a4678396ef38921b3e.zip
FFI: Add ctype metamethods and ffi.metatype().
Diffstat (limited to 'doc')
-rw-r--r--doc/ext_ffi_api.html25
-rw-r--r--doc/ext_ffi_semantics.html15
-rw-r--r--doc/ext_ffi_tutorial.html93
3 files changed, 129 insertions, 4 deletions
diff --git a/doc/ext_ffi_api.html b/doc/ext_ffi_api.html
index 5bd4b80c..2b6d1d86 100644
--- a/doc/ext_ffi_api.html
+++ b/doc/ext_ffi_api.html
@@ -238,6 +238,31 @@ This functions is mainly useful to override the pointer compatibility
238checks or to convert pointers to addresses or vice versa. 238checks or to convert pointers to addresses or vice versa.
239</p> 239</p>
240 240
241<h3 id="ffi_metatype"><tt>ctype = ffi.metatype(ct, metatable)</tt></h3>
242<p>
243Creates a ctype object for the given <tt>ct</tt> and associates it with
244a metatable. Only <tt>struct</tt>/<tt>union</tt> types, complex numbers
245and vectors are allowed. Other types may be wrapped in a
246<tt>struct</tt>, if needed.
247</p>
248<p>
249The association with a metatable is permanent and cannot be changed
250afterwards. Neither the contents of the <tt>metatable</tt> nor the
251contents of an <tt>__index</tt> table (if any) may be modified
252afterwards. The associated metatable automatically applies to all uses
253of this type, no matter how the objects are created or where they
254originate from. Note that pre-defined operations on types have
255precedence (e.g. declared field names cannot be overriden).
256</p>
257<p>
258All standard Lua metamethods are implemented. These are called directly,
259without shortcuts and on any mix of types. For binary operations, the
260left operand is checked first for a valid ctype metamethod. The
261<tt>__gc</tt> metamethod only applies to <tt>struct</tt>/<tt>union</tt>
262types and performs an implicit <a href="#ffi_gc"><tt>ffi.gc()</tt></a>
263call during creation of an instance.
264</p>
265
241<h3 id="ffi_gc"><tt>cdata = ffi.gc(cdata, finalizer)</tt></h3> 266<h3 id="ffi_gc"><tt>cdata = ffi.gc(cdata, finalizer)</tt></h3>
242<p> 267<p>
243Associates a finalizer with a pointer or aggregate cdata object. The 268Associates a finalizer with a pointer or aggregate cdata object. The
diff --git a/doc/ext_ffi_semantics.html b/doc/ext_ffi_semantics.html
index f9a118a0..d9aa27c8 100644
--- a/doc/ext_ffi_semantics.html
+++ b/doc/ext_ffi_semantics.html
@@ -582,6 +582,10 @@ Reference types are dereferenced <em>before</em> performing each of
582the operations below &mdash; the operation is applied to the 582the operations below &mdash; the operation is applied to the
583C&nbsp;type pointed to by the reference. 583C&nbsp;type pointed to by the reference.
584</p> 584</p>
585<p>
586The pre-defined operations are always tried first before deferring to a
587metamethod for a ctype (if defined).
588</p>
585 589
586<h3 id="cdata_array">Indexing a cdata object</h3> 590<h3 id="cdata_array">Indexing a cdata object</h3>
587<ul> 591<ul>
@@ -803,9 +807,10 @@ vararg functions</a>.
803</p> 807</p>
804<p> 808<p>
805Memory areas returned by C functions (e.g. from <tt>malloc()</tt>) 809Memory areas returned by C functions (e.g. from <tt>malloc()</tt>)
806must be manually managed, of course. Pointers to cdata objects are 810must be manually managed, of course (or use
807indistinguishable from pointers returned by C functions (which is one 811<a href="ext_ffi_api.html#ffi_gc"><tt>ffi.gc()</tt></a>)). Pointers to
808of the reasons why the GC cannot follow them). 812cdata objects are indistinguishable from pointers returned by C
813functions (which is one of the reasons why the GC cannot follow them).
809</p> 814</p>
810 815
811<h2 id="clib">C Library Namespaces</h2> 816<h2 id="clib">C Library Namespaces</h2>
@@ -977,6 +982,9 @@ two.</li>
977value.</li> 982value.</li>
978<li>Calls to C&nbsp;functions with 64 bit arguments or return values 983<li>Calls to C&nbsp;functions with 64 bit arguments or return values
979on 32 bit CPUs.</li> 984on 32 bit CPUs.</li>
985<li>Calls to ctype metamethods which are not plain functions.</li>
986<li>ctype <tt>__newindex</tt> tables and non-string lookups in ctype
987<tt>__index</tt> tables.</li>
980<li>Accesses to external variables in C&nbsp;library namespaces.</li> 988<li>Accesses to external variables in C&nbsp;library namespaces.</li>
981<li><tt>tostring()</tt> for cdata types.</li> 989<li><tt>tostring()</tt> for cdata types.</li>
982<li>The following <a href="ext_ffi_api.html">ffi.* API</a> functions: 990<li>The following <a href="ext_ffi_api.html">ffi.* API</a> functions:
@@ -988,7 +996,6 @@ Other missing features:
988<ul> 996<ul>
989<li>Bit operations for 64&nbsp;bit types.</li> 997<li>Bit operations for 64&nbsp;bit types.</li>
990<li>Arithmetic for <tt>complex</tt> numbers.</li> 998<li>Arithmetic for <tt>complex</tt> numbers.</li>
991<li>User-defined metamethods for C&nbsp;types.</li>
992<li>Callbacks from C&nbsp;code to Lua functions.</li> 999<li>Callbacks from C&nbsp;code to Lua functions.</li>
993<li>Atomic handling of <tt>errno</tt>.</li> 1000<li>Atomic handling of <tt>errno</tt>.</li>
994<li>Passing structs by value to vararg C&nbsp;functions.</li> 1001<li>Passing structs by value to vararg C&nbsp;functions.</li>
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