diff options
author | Mike Pall <mike> | 2011-11-11 20:41:44 +0100 |
---|---|---|
committer | Mike Pall <mike> | 2011-11-11 20:41:44 +0100 |
commit | 0123e4fc895f4a52422dff05a29596e389b4749c (patch) | |
tree | e67a1c9bda90a721212973c204f357f4b7a2456b /doc/ext_ffi_tutorial.html | |
parent | fa1675baad93dbe503d834eb380b2a8efdf301fe (diff) | |
download | luajit-0123e4fc895f4a52422dff05a29596e389b4749c.tar.gz luajit-0123e4fc895f4a52422dff05a29596e389b4749c.tar.bz2 luajit-0123e4fc895f4a52422dff05a29596e389b4749c.zip |
FFI: Extend metamethod tutorial.
Diffstat (limited to 'doc/ext_ffi_tutorial.html')
-rw-r--r-- | doc/ext_ffi_tutorial.html | 21 |
1 files changed, 20 insertions, 1 deletions
diff --git a/doc/ext_ffi_tutorial.html b/doc/ext_ffi_tutorial.html index 9349aea4..fb46a842 100644 --- a/doc/ext_ffi_tutorial.html +++ b/doc/ext_ffi_tutorial.html | |||
@@ -15,6 +15,7 @@ span.mark { color: #4040c0; font-family: Courier New, Courier, monospace; | |||
15 | pre.mark { padding-left: 2em; } | 15 | pre.mark { padding-left: 2em; } |
16 | table.idiomtable { line-height: 1.2; } | 16 | table.idiomtable { line-height: 1.2; } |
17 | table.idiomtable tt { font-size: 100%; } | 17 | table.idiomtable tt { font-size: 100%; } |
18 | table.idiomtable td { vertical-align: top; } | ||
18 | tr.idiomhead td { font-weight: bold; } | 19 | tr.idiomhead td { font-weight: bold; } |
19 | td.idiomc { width: 12em; } | 20 | td.idiomc { width: 12em; } |
20 | td.idiomlua { width: 14em; } | 21 | td.idiomlua { width: 14em; } |
@@ -454,7 +455,7 @@ the origin. | |||
454 | <span class="mark">④</span> If we run out of operators, we can | 455 | <span class="mark">④</span> If we run out of operators, we can |
455 | define named methods, too. Here the <tt>__index</tt> table defines an | 456 | define named methods, too. Here the <tt>__index</tt> table defines an |
456 | <tt>area</tt> function. For custom indexing needs, one might want to | 457 | <tt>area</tt> function. For custom indexing needs, one might want to |
457 | define <tt>__index</tt> and <tt>__newindex</tt> functions instead. | 458 | define <tt>__index</tt> and <tt>__newindex</tt> <em>functions</em> instead. |
458 | </p> | 459 | </p> |
459 | <p> | 460 | <p> |
460 | <span class="mark">⑤</span> This associates the metamethods with | 461 | <span class="mark">⑤</span> This associates the metamethods with |
@@ -478,6 +479,24 @@ defined metamethods. Note that <tt>area</tt> is a method and must be | |||
478 | called with the Lua syntax for methods: <tt>a:area()</tt>, not | 479 | called with the Lua syntax for methods: <tt>a:area()</tt>, not |
479 | <tt>a.area()</tt>. | 480 | <tt>a.area()</tt>. |
480 | </p> | 481 | </p> |
482 | <p> | ||
483 | The C type metamethod mechanism is most useful when used in | ||
484 | conjunction with C libraries that are written in an object-oriented | ||
485 | style. Creators return a pointer to a new instance and methods take an | ||
486 | instance pointer as the first argument. Sometimes you can just point | ||
487 | <tt>__index</tt> to the library namespace and <tt>__gc</tt> to the | ||
488 | destructor and you're done. But often enough you'll want to add | ||
489 | convenience wrappers, e.g. to return actual Lua strings or when | ||
490 | returning multiple values. | ||
491 | </p> | ||
492 | <p> | ||
493 | Some C libraries only declare instance pointers as an opaque | ||
494 | <tt>void *</tt> type. In this case you can use a fake type for all | ||
495 | declarations, e.g. a pointer to a named (incomplete) struct will do: | ||
496 | <tt>typedef struct foo_type *foo_handle</tt>. The C side doesn't | ||
497 | know what you declare with the LuaJIT FFI, but as long as the underlying | ||
498 | types are compatible, everything still works. | ||
499 | </p> | ||
481 | 500 | ||
482 | <h2 id="idioms">Translating C Idioms</h2> | 501 | <h2 id="idioms">Translating C Idioms</h2> |
483 | <p> | 502 | <p> |