Tarantool development patches archive
 help / color / mirror / Atom feed
From: Serge Petrenko <sergepetrenko@tarantool.org>
To: vdavydov.dev@gmail.com
Cc: tarantool-patches@freelists.org,
	Serge Petrenko <sergepetrenko@tarantool.org>
Subject: [PATCH v2 1/2] lua/utils: add a function to register FFI metatypes.
Date: Fri, 28 Jun 2019 17:36:38 +0300	[thread overview]
Message-ID: <af0a8b76e8205d0067f7a3d300a68f5328313dc1.1561731259.git.sergepetrenko@tarantool.org> (raw)
In-Reply-To: <cover.1561731259.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)

  reply	other threads:[~2019-06-28 14:36 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-06-28 14:36 [PATCH v2 0/2] decimal: expose decimal module to Lua Serge Petrenko
2019-06-28 14:36 ` Serge Petrenko [this message]
2019-06-28 14:36 ` [PATCH v2 2/2] decimal: expose decimal type to lua Serge Petrenko
2019-06-28 15:32   ` 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=af0a8b76e8205d0067f7a3d300a68f5328313dc1.1561731259.git.sergepetrenko@tarantool.org \
    --to=sergepetrenko@tarantool.org \
    --cc=tarantool-patches@freelists.org \
    --cc=vdavydov.dev@gmail.com \
    --subject='Re: [PATCH v2 1/2] 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