FFI: Check for __new metamethod when calling a constructor.

This commit is contained in:
Mike Pall
2012-06-20 18:24:49 +02:00
parent e9e45313e7
commit 8b71ab1080
4 changed files with 65 additions and 44 deletions

View File

@@ -599,8 +599,9 @@ C type pointed to by the reference.
</p>
<p>
The pre-defined operations are always tried first before deferring to a
metamethod or index table (if any) for the corresponding ctype. An error
is raised if the metamethod lookup or index table lookup fails.
metamethod or index table (if any) for the corresponding ctype (except
for <tt>__new</tt>). An error is raised if the metamethod lookup or
index table lookup fails.
</p>
<h3 id="cdata_array">Indexing a cdata object</h3>
@@ -669,7 +670,12 @@ to <tt>foo.c</tt>.
<ul>
<li><b>Constructor</b>: a ctype object can be called and used as a
<a href="ext_ffi_api.html#ffi_new">constructor</a>.</li>
<a href="ext_ffi_api.html#ffi_new">constructor</a>. This is equivalent
to <tt>ffi.new(ct, ...)</tt>, unless a <tt>__new</tt> metamethod is
defined. The <tt>__new</tt> metamethod is called with the ctype object
plus any other arguments passed to the contructor. Note that you have to
use <tt>ffi.new</tt> inside of it, since calling <tt>ct(...)</tt> would
cause infinite recursion.</li>
<li><b>C&nbsp;function call</b>: a cdata function or cdata function
pointer can be called. The passed arguments are