From: Serge Petrenko <sergepetrenko@tarantool.org> To: vdavydov.dev@gmail.com Cc: tarantool-patches@freelists.org, Serge Petrenko <sergepetrenko@tarantool.org> Subject: [PATCH v3 4/5] lua/utils: add a function to register FFI metatypes. Date: Tue, 2 Jul 2019 20:27:51 +0300 [thread overview] Message-ID: <5530a0090722e5a1e44e0f680ed643ee6efee2b2.1562087728.git.sergepetrenko@tarantool.org> (raw) In-Reply-To: <cover.1562087728.git.sergepetrenko@tarantool.org> A ffi metatype has a CTypeID, which can be used to push cdata of the type on the lua stack, and has an associated metatable, automatically applied to every created member of the type. This allows the behavior similar to pushing userdata and assigning a metatable to it. Needed for #692 --- src/lua/utils.c | 28 ++++++++++++++++++++++++++++ src/lua/utils.h | 13 +++++++++++++ 2 files changed, 41 insertions(+) diff --git a/src/lua/utils.c b/src/lua/utils.c index 01a0cd894..0a4bcf517 100644 --- a/src/lua/utils.c +++ b/src/lua/utils.c @@ -141,6 +141,34 @@ luaL_ctypeid(struct lua_State *L, const char *ctypename) return ctypeid; } +uint32_t +luaL_metatype(struct lua_State *L, const char *ctypename, + const struct luaL_Reg *methods) +{ + /* Create a metatable for our ffi metatype. */ + luaL_register_type(L, ctypename, methods); + int idx = lua_gettop(L); + /* + * Get ffi.metatype function. It is like typeof with + * an additional effect of registering a metatable for + * all the cdata objects of the type. + */ + luaL_loadstring(L, "return require('ffi').metatype"); + lua_call(L, 0, 1); + assert(lua_gettop(L) == idx + 1 && lua_isfunction(L, idx + 1)); + lua_pushstring(L, ctypename); + /* Push the freshly created metatable as the second parameter. */ + luaL_getmetatable(L, ctypename); + assert(lua_gettop(L) == idx + 3 && lua_istable(L, idx + 3)); + lua_call(L, 2, 1); + uint32_t ctypetypeid; + CTypeID ctypeid = *(CTypeID *)luaL_checkcdata(L, idx + 1, &ctypetypeid); + assert(ctypetypeid == CTID_CTYPEID); + + lua_settop(L, idx); + return ctypeid; +} + int luaL_cdef(struct lua_State *L, const char *what) { diff --git a/src/lua/utils.h b/src/lua/utils.h index 943840ec0..7e7cdc0c6 100644 --- a/src/lua/utils.h +++ b/src/lua/utils.h @@ -129,6 +129,19 @@ luaL_cdef(struct lua_State *L, const char *ctypename); /** \endcond public */ +/** + * @brief Return CTypeID (FFI) of given CDATA type, + * register a metatable with \a methods to be + * associated with every value of the given + * type on its creation iva FFI. + * @sa luaL_register_type + * @sa luaL_ctypeid + * @return CTypeID + */ +uint32_t +luaL_metatype(struct lua_State *L, const char *ctypename, + const struct luaL_Reg *methods); + static inline lua_Integer luaL_arrlen(struct lua_State *L, int idx) { -- 2.20.1 (Apple Git-117)
next prev parent reply other threads:[~2019-07-02 17:27 UTC|newest] Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top 2019-07-02 17:27 [PATCH v3 0/5] decimal: expose decimal module to Lua Serge Petrenko 2019-07-02 17:27 ` [PATCH v3 1/5] decimal: fix ln hang on values between ~ 0.9 and 1.1 Serge Petrenko 2019-07-02 17:27 ` [PATCH v3 2/5] decimal: diallow infinity and NaN entirely Serge Petrenko 2019-07-02 17:27 ` [PATCH v3 3/5] decimal: fix string formatting on construction from double Serge Petrenko 2019-07-02 17:27 ` Serge Petrenko [this message] 2019-07-02 17:27 ` [PATCH v3 5/5] decimal: expose decimal type to lua Serge Petrenko 2019-07-03 6:50 ` [tarantool-patches] " Serge Petrenko 2019-07-08 14:25 ` [PATCH v3 0/5] decimal: expose decimal module to Lua Vladimir Davydov
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=5530a0090722e5a1e44e0f680ed643ee6efee2b2.1562087728.git.sergepetrenko@tarantool.org \ --to=sergepetrenko@tarantool.org \ --cc=tarantool-patches@freelists.org \ --cc=vdavydov.dev@gmail.com \ --subject='Re: [PATCH v3 4/5] lua/utils: add a function to register FFI metatypes.' \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: link
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox