From 3b6f37dd2c336987251d53a4678396ef38921b3e Mon Sep 17 00:00:00 2001 From: Mike Pall Date: Tue, 12 Apr 2011 19:15:00 +0200 Subject: [PATCH] FFI: Add ctype metamethods and ffi.metatype(). --- doc/ext_ffi_api.html | 25 ++++++ doc/ext_ffi_semantics.html | 15 +++- doc/ext_ffi_tutorial.html | 93 ++++++++++++++++++++++ src/Makefile.dep | 13 ++-- src/lib_ffi.c | 116 ++++++++++++++++++++++++++-- src/lj_asm.c | 6 +- src/lj_carith.c | 44 +++++++---- src/lj_cdata.c | 26 +++---- src/lj_crecord.c | 153 +++++++++++++++++++++++++++++-------- src/lj_ctype.c | 16 ++++ src/lj_ctype.h | 2 + 11 files changed, 426 insertions(+), 83 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 checks or to convert pointers to addresses or vice versa.

+

ctype = ffi.metatype(ct, metatable)

+

+Creates a ctype object for the given ct and associates it with +a metatable. Only struct/union types, complex numbers +and vectors are allowed. Other types may be wrapped in a +struct, if needed. +

+

+The association with a metatable is permanent and cannot be changed +afterwards. Neither the contents of the metatable nor the +contents of an __index table (if any) may be modified +afterwards. The associated metatable automatically applies to all uses +of this type, no matter how the objects are created or where they +originate from. Note that pre-defined operations on types have +precedence (e.g. declared field names cannot be overriden). +

+

+All standard Lua metamethods are implemented. These are called directly, +without shortcuts and on any mix of types. For binary operations, the +left operand is checked first for a valid ctype metamethod. The +__gc metamethod only applies to struct/union +types and performs an implicit ffi.gc() +call during creation of an instance. +

+

cdata = ffi.gc(cdata, finalizer)

Associates 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 before performing each of the operations below — the operation is applied to the C type pointed to by the reference.

+

+The pre-defined operations are always tried first before deferring to a +metamethod for a ctype (if defined). +

Indexing a cdata object