[Tarantool-patches] [PATCH 1.10 v4 5/5] module api: add luaL_iscallable with support of cdata metatype
Timur Safin
tsafin at tarantool.org
Wed Oct 14 03:15:44 MSK 2020
From: Alexander Turenko <alexander.turenko at tarantool.org>
Part of #5384.
(cherry picked from 82dc8c7110f744752dd81a975d51ac97398d120d)
Co-authored-by: Timur Safin <tsafin at tarantool.org>
---
extra/exports | 1 +
src/lua/utils.c | 43 ++++++++++++++++
src/lua/utils.h | 10 ++++
test/app-tap/module_api.c | 9 ++++
test/app-tap/module_api.test.lua | 85 +++++++++++++++++++++++++++++++-
5 files changed, 146 insertions(+), 2 deletions(-)
diff --git a/extra/exports b/extra/exports
index d918b7e59..94e04264e 100644
--- a/extra/exports
+++ b/extra/exports
@@ -138,6 +138,7 @@ luaT_toibuf
luaT_tolstring
luaT_tuple_encode
luaT_tuple_new
+luaL_iscallable
box_txn
box_txn_begin
box_txn_commit
diff --git a/src/lua/utils.c b/src/lua/utils.c
index 90515affc..e9b783afb 100644
--- a/src/lua/utils.c
+++ b/src/lua/utils.c
@@ -1058,6 +1058,49 @@ luaL_checkconstchar(struct lua_State *L, int idx, const char **res,
return 0;
}
+/* Based on ffi_meta___call() from luajit/src/lib_ffi.c. */
+static int
+luaL_cdata_iscallable(lua_State *L, int idx)
+{
+ /* Calculate absolute value in the stack. */
+ if (idx < 0)
+ idx = lua_gettop(L) + idx + 1;
+
+ /* Get cdata from the stack. */
+ assert(lua_type(L, idx) == LUA_TCDATA);
+ GCcdata *cd = cdataV(L->base + idx - 1);
+
+ CTState *cts = ctype_cts(L);
+ CTypeID id = cd->ctypeid;
+ CType *ct = ctype_raw(cts, id);
+ if (ctype_isptr(ct->info))
+ id = ctype_cid(ct->info);
+
+ /* Get ctype metamethod. */
+ cTValue *tv = lj_ctype_meta(cts, id, MM_call);
+
+ return tv != NULL;
+}
+
+int
+luaL_iscallable(lua_State *L, int idx)
+{
+ /* Whether it is function. */
+ int res = lua_isfunction(L, idx);
+ if (res == 1)
+ return 1;
+
+ /* Whether it is cdata with metatype with __call field. */
+ if (lua_type(L, idx) == LUA_TCDATA)
+ return luaL_cdata_iscallable(L, idx);
+
+ /* Whether it has metatable with __call field. */
+ res = luaL_getmetafield(L, idx, "__call");
+ if (res == 1)
+ lua_pop(L, 1); /* Pop __call value. */
+ return res;
+}
+
lua_State *
luaT_state(void)
{
diff --git a/src/lua/utils.h b/src/lua/utils.h
index a44c419d3..3eaf1e417 100644
--- a/src/lua/utils.h
+++ b/src/lua/utils.h
@@ -505,6 +505,16 @@ luaT_state(void);
LUA_API const char *
luaT_tolstring(lua_State *L, int idx, size_t *ssize);
+/**
+ * Check whether a Lua object is a function or has
+ * metatable/metatype with a __call field.
+ *
+ * Note: It does not check type of __call metatable/metatype
+ * field.
+ */
+LUA_API int
+luaL_iscallable(lua_State *L, int idx);
+
/**
* Check if a value on @a L stack by index @a idx is an ibuf
* object. Both 'struct ibuf' and 'struct ibuf *' are accepted.
diff --git a/test/app-tap/module_api.c b/test/app-tap/module_api.c
index 35df2403b..cc3475d60 100644
--- a/test/app-tap/module_api.c
+++ b/test/app-tap/module_api.c
@@ -1895,7 +1895,15 @@ test_tuple_validate_default(lua_State *L)
valid = box_tuple_validate(tuple, format) == 0;
}
lua_pushboolean(L, valid);
+ return 1;
+}
+static int
+test_iscallable(lua_State *L)
+{
+ int exp = lua_toboolean(L, 2);
+ int res = luaL_iscallable(L, 1);
+ lua_pushboolean(L, res == exp);
return 1;
}
@@ -1954,6 +1962,7 @@ luaopen_module_api(lua_State *L)
{"test_cpcall", test_cpcall},
{"test_state", test_state},
{"test_tostring", test_tostring},
+ {"iscallable", test_iscallable},
{"iscdata", test_iscdata},
{"test_box_region", test_box_region},
{"test_tuple_encode", test_tuple_encode},
diff --git a/test/app-tap/module_api.test.lua b/test/app-tap/module_api.test.lua
index b8c10a310..e5dcea55a 100755
--- a/test/app-tap/module_api.test.lua
+++ b/test/app-tap/module_api.test.lua
@@ -3,7 +3,9 @@
local fio = require('fio')
box.cfg{log = "tarantool.log"}
-build_path = os.getenv("BUILDDIR")
+-- Use BUILDDIR passed from test-run or cwd when run w/o
+-- test-run to find test/app-tap/module_api.{so,dylib}.
+build_path = os.getenv("BUILDDIR") or '.'
package.cpath = fio.pathjoin(build_path, 'test/app-tap/?.so' ) .. ';' ..
fio.pathjoin(build_path, 'test/app-tap/?.dylib') .. ';' ..
package.cpath
@@ -140,8 +142,86 @@ local function test_iscdata(test, module)
end
end
+local function test_iscallable(test, module)
+ local ffi = require('ffi')
+
+ ffi.cdef([[
+ struct cdata_1 { int foo; };
+ struct cdata_2 { int foo; };
+ ]])
+
+ local cdata_1 = ffi.new('struct cdata_1')
+ local cdata_1_ref = ffi.new('struct cdata_1 &')
+ local cdata_2 = ffi.new('struct cdata_2')
+ local cdata_2_ref = ffi.new('struct cdata_2 &')
+
+ local nop = function() end
+
+ ffi.metatype('struct cdata_2', {
+ __call = nop,
+ })
+
+ local cases = {
+ {
+ obj = nop,
+ exp = true,
+ description = 'function',
+ },
+ {
+ obj = nil,
+ exp = false,
+ description = 'nil',
+ },
+ {
+ obj = 1,
+ exp = false,
+ description = 'number',
+ },
+ {
+ obj = {},
+ exp = false,
+ description = 'table without metatable',
+ },
+ {
+ obj = setmetatable({}, {}),
+ exp = false,
+ description = 'table without __call metatable field',
+ },
+ {
+ obj = setmetatable({}, {__call = nop}),
+ exp = true,
+ description = 'table with __call metatable field'
+ },
+ {
+ obj = cdata_1,
+ exp = false,
+ description = 'cdata without __call metatable field',
+ },
+ {
+ obj = cdata_1_ref,
+ exp = false,
+ description = 'cdata reference without __call metatable field',
+ },
+ {
+ obj = cdata_2,
+ exp = true,
+ description = 'cdata with __call metatable field',
+ },
+ {
+ obj = cdata_2_ref,
+ exp = true,
+ description = 'cdata reference with __call metatable field',
+ },
+ }
+
+ test:plan(#cases)
+ for _, case in ipairs(cases) do
+ test:ok(module.iscallable(case.obj, case.exp), case.description)
+ end
+end
+
local test = require('tap').test("module_api", function(test)
- test:plan(37)
+ test:plan(38)
local status, module = pcall(require, 'module_api')
test:is(status, true, "module")
test:ok(status, "module is loaded")
@@ -167,6 +247,7 @@ local test = require('tap').test("module_api", function(test)
test:test("pushcdata", test_pushcdata, module)
test:test("iscdata", test_iscdata, module)
+ test:test("iscallable", test_iscallable, module)
test:test("buffers", test_buffers, module)
test:test("tuple_validate", test_tuple_validate, module)
--
2.20.1
More information about the Tarantool-patches
mailing list