From 6cee1333ef2b340e1b23e3051b9f433fc803d2ff Mon Sep 17 00:00:00 2001 From: Mike Pall Date: Wed, 20 Jun 2012 15:15:09 +0200 Subject: [PATCH] FFI: Resolve metamethods for constructors, too. --- doc/ext_ffi_semantics.html | 16 ++++++++++++---- src/lj_cdata.c | 1 + 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/doc/ext_ffi_semantics.html b/doc/ext_ffi_semantics.html index bacb4fb8..4b498fbe 100644 --- a/doc/ext_ffi_semantics.html +++ b/doc/ext_ffi_semantics.html @@ -590,8 +590,7 @@ ffi.new("struct nested", {x=1,y={2,3}}) --> x = 1, y.a = 2, y.b = 3

All of the standard Lua operators can be applied to cdata objects or a mix of a cdata object and another Lua object. The following list shows -the valid combinations. All other combinations currently raise an -error. +the pre-defined operations.

Reference types are dereferenced before performing each of @@ -600,7 +599,8 @@ 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). +metamethod or index table (if any) for the corresponding ctype. An error +is raised if the metamethod lookup or index table lookup fails.

Indexing a cdata object

@@ -625,7 +625,9 @@ field value and converts it to a Lua object. A write access converts a Lua object to the field type and stores the converted value to the field. An error is raised if a write access to a constant -struct/union or a constant field is attempted. +struct/union or a constant field is attempted. +Scoped enum constants or static constants are treated like a constant +field.
  • Indexing a complex number: a complex number can be indexed either by a cdata number or a Lua number with the values 0 or 1, or by @@ -644,6 +646,12 @@ assigning to an index of a vector raises an error.
  • +A ctype object can be indexed with a string key, too. The only +pre-defined operation is reading scoped constants of +struct/union types. All other accesses defer +to the corresponding metamethods or index tables (if any). +

    +

    Note: since there's (deliberately) no address-of operator, a cdata object holding a value type is effectively immutable after initialization. The JIT compiler benefits from this fact when applying diff --git a/src/lj_cdata.c b/src/lj_cdata.c index 482add4b..b477ce77 100644 --- a/src/lj_cdata.c +++ b/src/lj_cdata.c @@ -178,6 +178,7 @@ collect_attrib: if (fct && ctype_isconstval(fct->info)) return fct; } + ct = sct; /* Allow resolving metamethods for constructors, too. */ } } if (ctype_isptr(ct->info)) { /* Automatically perform '->'. */