[PATCH 1/2] lua/utils: add a function to register FFI metatypes.

Serge Petrenko sergepetrenko at tarantool.org
Wed Jun 19 18:58:04 MSK 2019


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)




More information about the Tarantool-patches mailing list