* [Tarantool-patches] [PATCH 1.10 v3 1/4] module api: export box_tuple_validate
2020-10-12 0:50 [Tarantool-patches] [PATCH 1.10 v3 0/4] module api: extend for external merger Lua module Timur Safin
@ 2020-10-12 0:50 ` Timur Safin
2020-10-12 0:50 ` [Tarantool-patches] [PATCH 1.10 v3 2/4] module api: export box_key_def_dup Timur Safin
` (3 subsequent siblings)
4 siblings, 0 replies; 8+ messages in thread
From: Timur Safin @ 2020-10-12 0:50 UTC (permalink / raw)
To: v.shpilevoy, alexander.turenko; +Cc: tarantool-patches
For external merger we need means to validate tuple data,
thus exporting `box_tuple_validate` which is wrapper around
`tuple_validate_raw` without revealing access to tuple
internals.
Part of #5384
---
| 1 +
src/box/tuple.c | 8 ++++++++
src/box/tuple.h | 11 +++++++++++
test/app-tap/module_api.c | 16 ++++++++++++++++
test/app-tap/module_api.test.lua | 25 ++++++++++++++++++++++++-
5 files changed, 60 insertions(+), 1 deletion(-)
--git a/extra/exports b/extra/exports
index d99778e56..aa9f5defe 100644
--- a/extra/exports
+++ b/extra/exports
@@ -177,6 +177,7 @@ box_tuple_upsert
box_tuple_extract_key
box_tuple_compare
box_tuple_compare_with_key
+box_tuple_validate
box_return_tuple
box_space_id_by_name
box_index_id_by_name
diff --git a/src/box/tuple.c b/src/box/tuple.c
index 1db69e414..ab3990ac4 100644
--- a/src/box/tuple.c
+++ b/src/box/tuple.c
@@ -576,6 +576,14 @@ box_tuple_new(box_tuple_format_t *format, const char *data, const char *end)
return tuple_bless(ret);
}
+int
+box_tuple_validate(box_tuple_t *tuple, box_tuple_format_t *format)
+{
+ return tuple_validate_raw(format, tuple_data(tuple));
+}
+
+/* }}} box_tuple_* */
+
int
tuple_snprint(char *buf, int size, const struct tuple *tuple)
{
diff --git a/src/box/tuple.h b/src/box/tuple.h
index 5c6dc6b8c..217c438c5 100644
--- a/src/box/tuple.h
+++ b/src/box/tuple.h
@@ -284,6 +284,17 @@ box_tuple_t *
box_tuple_upsert(const box_tuple_t *tuple, const char *expr, const
char *expr_end);
+/**
+ * Check tuple data correspondence to the space format.
+ * @param tuple Tuple to validate.
+ * @param format Format to which the tuple must match.
+ *
+ * @retval 0 The tuple is valid.
+ * @retval -1 The tuple is invalid.
+ */
+int
+box_tuple_validate(box_tuple_t *tuple, box_tuple_format_t *format);
+
/** \endcond public */
/**
diff --git a/test/app-tap/module_api.c b/test/app-tap/module_api.c
index 4aaf985fa..74a4c4d54 100644
--- a/test/app-tap/module_api.c
+++ b/test/app-tap/module_api.c
@@ -556,6 +556,21 @@ test_luaT_tuple_encode(struct lua_State *L)
/* }}} test_luaT_tuple_encode */
+static int
+test_tuple_validate(lua_State *L)
+{
+ int valid = 0;
+ box_tuple_t *tuple = luaT_istuple(L, -1);
+
+ if (tuple != NULL) {
+ box_tuple_format_t *format = box_tuple_format_default();
+ valid = box_tuple_validate(tuple, format) == 0;
+ }
+ lua_pushboolean(L, valid);
+
+ return 1;
+}
+
LUA_API int
luaopen_module_api(lua_State *L)
{
@@ -584,6 +599,7 @@ luaopen_module_api(lua_State *L)
{"test_state", test_state},
{"test_tostring", test_tostring},
{"test_luaT_tuple_encode", test_luaT_tuple_encode},
+ {"tuple_validate", test_tuple_validate},
{NULL, NULL}
};
luaL_register(L, "module_api", lib);
diff --git a/test/app-tap/module_api.test.lua b/test/app-tap/module_api.test.lua
index 467000c98..06ecb5470 100755
--- a/test/app-tap/module_api.test.lua
+++ b/test/app-tap/module_api.test.lua
@@ -36,8 +36,30 @@ local function test_pushcdata(test, module)
test:is(gc_counter, 1, 'pushcdata gc')
end
+local function test_tuples(test, module)
+ test:plan(8)
+
+ local nottuple1 = {}
+ local nottuple2 = {1, 2}
+ local nottuple3 = {1, nil, 2}
+ local nottuple4 = {1, box.NULL, 2, 3}
+ local tuple1 = box.tuple.new(nottuple1)
+ local tuple2 = box.tuple.new(nottuple2)
+ local tuple3 = box.tuple.new(nottuple3)
+ local tuple4 = box.tuple.new(nottuple4)
+
+ test:ok(not module.tuple_validate(nottuple1), "not tuple 1")
+ test:ok(not module.tuple_validate(nottuple2), "not tuple 2")
+ test:ok(not module.tuple_validate(nottuple3), "not tuple 3")
+ test:ok(not module.tuple_validate(nottuple4), "not tuple 4")
+ test:ok(module.tuple_validate(tuple1), "tuple 1")
+ test:ok(module.tuple_validate(tuple2), "tuple 2")
+ test:ok(module.tuple_validate(tuple3), "tuple 3")
+ test:ok(module.tuple_validate(tuple4), "tuple 4")
+end
+
local test = require('tap').test("module_api", function(test)
- test:plan(24)
+ test:plan(25)
local status, module = pcall(require, 'module_api')
test:is(status, true, "module")
test:ok(status, "module is loaded")
@@ -62,6 +84,7 @@ local test = require('tap').test("module_api", function(test)
test:like(msg, 'luaT_error', 'luaT_error')
test:test("pushcdata", test_pushcdata, module)
+ test:test("validate", test_tuples, module)
space:drop()
end)
--
2.20.1
^ permalink raw reply [flat|nested] 8+ messages in thread
* [Tarantool-patches] [PATCH 1.10 v3 2/4] module api: export box_key_def_dup
2020-10-12 0:50 [Tarantool-patches] [PATCH 1.10 v3 0/4] module api: extend for external merger Lua module Timur Safin
2020-10-12 0:50 ` [Tarantool-patches] [PATCH 1.10 v3 1/4] module api: export box_tuple_validate Timur Safin
@ 2020-10-12 0:50 ` Timur Safin
2020-10-12 0:50 ` [Tarantool-patches] [PATCH 1.10 v3 3/4] module api: luaL_checkibuf Timur Safin
` (2 subsequent siblings)
4 siblings, 0 replies; 8+ messages in thread
From: Timur Safin @ 2020-10-12 0:50 UTC (permalink / raw)
To: v.shpilevoy, alexander.turenko; +Cc: tarantool-patches
Exporting `box_key_def_dup` as accessor to the internal `key_def_dup`
Part of #5384
---
| 1 +
src/box/key_def.c | 6 ++++++
src/box/key_def.h | 10 ++++++++++
3 files changed, 17 insertions(+)
--git a/extra/exports b/extra/exports
index aa9f5defe..9e2023685 100644
--- a/extra/exports
+++ b/extra/exports
@@ -149,6 +149,7 @@ box_key_def_new
box_key_def_delete
box_key_def_dump_parts
box_key_def_new_v2
+box_key_def_dup
box_key_def_merge
box_key_def_validate_tuple
box_key_def_extract_key
diff --git a/src/box/key_def.c b/src/box/key_def.c
index d118c5802..35e836c8c 100644
--- a/src/box/key_def.c
+++ b/src/box/key_def.c
@@ -333,6 +333,12 @@ box_key_def_new_v2(box_key_part_def_t *parts, uint32_t part_count)
return key_def;
}
+box_key_def_t *
+box_key_def_dup(const box_key_def_t *key_def)
+{
+ return key_def_dup(key_def);
+}
+
void
box_key_def_delete(box_key_def_t *key_def)
{
diff --git a/src/box/key_def.h b/src/box/key_def.h
index 32e722f36..9e9e5c85b 100644
--- a/src/box/key_def.h
+++ b/src/box/key_def.h
@@ -281,6 +281,16 @@ box_key_part_def_create(box_key_part_def_t *part);
API_EXPORT box_key_def_t *
box_key_def_new_v2(box_key_part_def_t *parts, uint32_t part_count);
+/**
+ * Duplicate key_def.
+ * \param key_def Original key_def.
+ *
+ * \retval not NULL Duplicate of src.
+ * \retval NULL Memory error.
+ */
+API_EXPORT box_key_def_t *
+box_key_def_dup(const box_key_def_t *key_def);
+
/**
* Delete key definition
*
--
2.20.1
^ permalink raw reply [flat|nested] 8+ messages in thread
* [Tarantool-patches] [PATCH 1.10 v3 3/4] module api: luaL_checkibuf
2020-10-12 0:50 [Tarantool-patches] [PATCH 1.10 v3 0/4] module api: extend for external merger Lua module Timur Safin
2020-10-12 0:50 ` [Tarantool-patches] [PATCH 1.10 v3 1/4] module api: export box_tuple_validate Timur Safin
2020-10-12 0:50 ` [Tarantool-patches] [PATCH 1.10 v3 2/4] module api: export box_key_def_dup Timur Safin
@ 2020-10-12 0:50 ` Timur Safin
2020-10-12 7:43 ` Timur Safin
2020-10-12 0:50 ` [Tarantool-patches] [PATCH 1.10 v3 4/4] module api: add luaL_iscallable with support of cdata metatype Timur Safin
2020-10-12 9:19 ` [Tarantool-patches] [PATCH 1.10 v3 0/4] module api: extend for external merger Lua module Timur Safin
4 siblings, 1 reply; 8+ messages in thread
From: Timur Safin @ 2020-10-12 0:50 UTC (permalink / raw)
To: v.shpilevoy, alexander.turenko; +Cc: tarantool-patches
Moved `luaL_checkibuf` to the public part of module api.
Part of #5384
---
| 2 ++
src/lua/utils.h | 44 ++++++++++++++++++++------------
test/app-tap/module_api.c | 10 ++++++++
test/app-tap/module_api.test.lua | 20 ++++++++++++++-
4 files changed, 58 insertions(+), 18 deletions(-)
--git a/extra/exports b/extra/exports
index 9e2023685..abe9eded4 100644
--- a/extra/exports
+++ b/extra/exports
@@ -118,8 +118,10 @@ coio_call
coio_getaddrinfo
luaL_pushcdata
luaL_iscdata
+luaL_iscallable
luaL_checkcdata
luaL_setcdatagc
+luaL_checkibuf
luaL_ctypeid
luaL_cdef
luaL_pushuint64
diff --git a/src/lua/utils.h b/src/lua/utils.h
index d6fbd0e48..92bf88900 100644
--- a/src/lua/utils.h
+++ b/src/lua/utils.h
@@ -505,6 +505,24 @@ 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.
+ * Returns NULL, if can't convert - not an ibuf object.
+ */
+LUA_API struct ibuf *
+luaL_checkibuf(struct lua_State *L, int idx);
+
/** \endcond public */
void
@@ -588,23 +606,6 @@ luaL_checkfinite(struct lua_State *L, struct luaL_serializer *cfg,
luaL_error(L, "number must not be NaN or Inf");
}
-/**
- * Check if a value on @a L stack by index @a idx is an ibuf
- * object. Both 'struct ibuf' and 'struct ibuf *' are accepted.
- * Returns NULL, if can't convert - not an ibuf object.
- */
-struct ibuf *
-luaL_checkibuf(struct lua_State *L, int idx);
-
-/**
- * Check if a value on @a L stack by index @a idx is pointer at
- * char or const char. '(char *)NULL' is also considered a valid
- * char pointer.
- */
-int
-luaL_checkconstchar(struct lua_State *L, int idx, const char **res,
- uint32_t *cdata_type_p);
-
/**
* @brief Creates a new Lua coroutine in a protected frame. If
* <lua_newthread> call underneath succeeds, the created Lua state
@@ -617,6 +618,15 @@ luaL_checkconstchar(struct lua_State *L, int idx, const char **res,
struct lua_State *
luaT_newthread(struct lua_State *L);
+/**
+ * Check if a value on @a L stack by index @a idx is pointer at
+ * char or const char. '(char *)NULL' is also considered a valid
+ * char pointer.
+ */
+int
+luaL_checkconstchar(struct lua_State *L, int idx, const char **res,
+ uint32_t *cdata_type_p);
+
int
tarantool_lua_utils_init(struct lua_State *L);
diff --git a/test/app-tap/module_api.c b/test/app-tap/module_api.c
index 74a4c4d54..2c4ca710d 100644
--- a/test/app-tap/module_api.c
+++ b/test/app-tap/module_api.c
@@ -150,6 +150,15 @@ test_checkint64(lua_State *L)
return 1;
}
+static int
+test_checkibuf(lua_State *L)
+{
+ struct ibuf *buf;
+ buf = luaL_checkibuf(L, -1);
+ lua_pushboolean(L, buf != NULL);
+ return 1;
+}
+
static int
test_touint64(lua_State *L)
{
@@ -584,6 +593,7 @@ luaopen_module_api(lua_State *L)
{"test_pushint64", test_pushint64 },
{"test_checkuint64", test_checkuint64 },
{"test_checkint64", test_checkint64 },
+ {"checkibuf", test_checkibuf},
{"test_touint64", test_touint64 },
{"test_toint64", test_toint64 },
{"test_fiber", test_fiber },
diff --git a/test/app-tap/module_api.test.lua b/test/app-tap/module_api.test.lua
index 06ecb5470..2b28bdaff 100755
--- a/test/app-tap/module_api.test.lua
+++ b/test/app-tap/module_api.test.lua
@@ -36,6 +36,23 @@ local function test_pushcdata(test, module)
test:is(gc_counter, 1, 'pushcdata gc')
end
+local function test_buffers(test, module)
+ test:plan(7)
+ local ffi = require('ffi')
+ local buffer = require('buffer')
+
+ local ibuf = buffer.ibuf()
+ local pbuf = ibuf:alloc(128)
+
+ test:ok(not module.checkibuf(nil), 'checkibuf of nil')
+ test:ok(not module.checkibuf({}), 'checkibuf of {}')
+ test:ok(not module.checkibuf(1LL), 'checkibuf of 1LL')
+ test:ok(not module.checkibuf(box.NULL), 'checkibuf of box.NULL')
+ test:ok(not module.checkibuf(buffer.reg1), 'checkibuf of reg1')
+ test:ok(module.checkibuf(ibuf), 'checkibuf of ibuf')
+ test:ok(not module.checkibuf(pbuf), 'checkibuf of pointer to ibuf data')
+end
+
local function test_tuples(test, module)
test:plan(8)
@@ -59,7 +76,7 @@ local function test_tuples(test, module)
end
local test = require('tap').test("module_api", function(test)
- test:plan(25)
+ test:plan(26)
local status, module = pcall(require, 'module_api')
test:is(status, true, "module")
test:ok(status, "module is loaded")
@@ -84,6 +101,7 @@ local test = require('tap').test("module_api", function(test)
test:like(msg, 'luaT_error', 'luaT_error')
test:test("pushcdata", test_pushcdata, module)
+ test:test("buffers", test_buffers, module)
test:test("validate", test_tuples, module)
space:drop()
--
2.20.1
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [Tarantool-patches] [PATCH 1.10 v3 3/4] module api: luaL_checkibuf
2020-10-12 0:50 ` [Tarantool-patches] [PATCH 1.10 v3 3/4] module api: luaL_checkibuf Timur Safin
@ 2020-10-12 7:43 ` Timur Safin
0 siblings, 0 replies; 8+ messages in thread
From: Timur Safin @ 2020-10-12 7:43 UTC (permalink / raw)
To: v.shpilevoy, alexander.turenko; +Cc: tarantool-patches
: From: Timur Safin <tsafin@tarantool.org>
: Subject: [PATCH 1.10 v3 3/4] module api: luaL_checkibuf
:
: Moved `luaL_checkibuf` to the public part of module api.
:
: Part of #5384
: ---
: extra/exports | 2 ++
: src/lua/utils.h | 44 ++++++++++++++++++++------------
: test/app-tap/module_api.c | 10 ++++++++
: test/app-tap/module_api.test.lua | 20 ++++++++++++++-
: 4 files changed, 58 insertions(+), 18 deletions(-)
:
: diff --git a/extra/exports b/extra/exports
: index 9e2023685..abe9eded4 100644
: --- a/extra/exports
: +++ b/extra/exports
: @@ -118,8 +118,10 @@ coio_call
: coio_getaddrinfo
: luaL_pushcdata
: luaL_iscdata
: +luaL_iscallable
Oops, that's an artifact of moving patches around. Would need
to reshuffle this patch again. And split/remove irrelevant parts.
: luaL_checkcdata
: luaL_setcdatagc
: +luaL_checkibuf
: luaL_ctypeid
: luaL_cdef
: luaL_pushuint64
: diff --git a/src/lua/utils.h b/src/lua/utils.h
: index d6fbd0e48..92bf88900 100644
: --- a/src/lua/utils.h
: +++ b/src/lua/utils.h
: @@ -505,6 +505,24 @@ 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.
: + * Returns NULL, if can't convert - not an ibuf object.
: + */
: +LUA_API struct ibuf *
: +luaL_checkibuf(struct lua_State *L, int idx);
: +
: /** \endcond public */
:
: void
: @@ -588,23 +606,6 @@ luaL_checkfinite(struct lua_State *L, struct
: luaL_serializer *cfg,
: luaL_error(L, "number must not be NaN or Inf");
: }
:
: -/**
: - * Check if a value on @a L stack by index @a idx is an ibuf
: - * object. Both 'struct ibuf' and 'struct ibuf *' are accepted.
: - * Returns NULL, if can't convert - not an ibuf object.
: - */
: -struct ibuf *
: -luaL_checkibuf(struct lua_State *L, int idx);
: -
: -/**
: - * Check if a value on @a L stack by index @a idx is pointer at
: - * char or const char. '(char *)NULL' is also considered a valid
: - * char pointer.
: - */
: -int
: -luaL_checkconstchar(struct lua_State *L, int idx, const char **res,
: - uint32_t *cdata_type_p);
: -
: /**
: * @brief Creates a new Lua coroutine in a protected frame. If
: * <lua_newthread> call underneath succeeds, the created Lua state
: @@ -617,6 +618,15 @@ luaL_checkconstchar(struct lua_State *L, int idx,
: const char **res,
: struct lua_State *
: luaT_newthread(struct lua_State *L);
:
: +/**
: + * Check if a value on @a L stack by index @a idx is pointer at
: + * char or const char. '(char *)NULL' is also considered a valid
: + * char pointer.
: + */
: +int
: +luaL_checkconstchar(struct lua_State *L, int idx, const char **res,
: + uint32_t *cdata_type_p);
: +
: int
: tarantool_lua_utils_init(struct lua_State *L);
:
: diff --git a/test/app-tap/module_api.c b/test/app-tap/module_api.c
: index 74a4c4d54..2c4ca710d 100644
: --- a/test/app-tap/module_api.c
: +++ b/test/app-tap/module_api.c
: @@ -150,6 +150,15 @@ test_checkint64(lua_State *L)
: return 1;
: }
:
: +static int
: +test_checkibuf(lua_State *L)
: +{
: + struct ibuf *buf;
: + buf = luaL_checkibuf(L, -1);
: + lua_pushboolean(L, buf != NULL);
: + return 1;
: +}
: +
: static int
: test_touint64(lua_State *L)
: {
: @@ -584,6 +593,7 @@ luaopen_module_api(lua_State *L)
: {"test_pushint64", test_pushint64 },
: {"test_checkuint64", test_checkuint64 },
: {"test_checkint64", test_checkint64 },
: + {"checkibuf", test_checkibuf},
: {"test_touint64", test_touint64 },
: {"test_toint64", test_toint64 },
: {"test_fiber", test_fiber },
: diff --git a/test/app-tap/module_api.test.lua b/test/app-
: tap/module_api.test.lua
: index 06ecb5470..2b28bdaff 100755
: --- a/test/app-tap/module_api.test.lua
: +++ b/test/app-tap/module_api.test.lua
: @@ -36,6 +36,23 @@ local function test_pushcdata(test, module)
: test:is(gc_counter, 1, 'pushcdata gc')
: end
:
: +local function test_buffers(test, module)
: + test:plan(7)
: + local ffi = require('ffi')
: + local buffer = require('buffer')
: +
: + local ibuf = buffer.ibuf()
: + local pbuf = ibuf:alloc(128)
: +
: + test:ok(not module.checkibuf(nil), 'checkibuf of nil')
: + test:ok(not module.checkibuf({}), 'checkibuf of {}')
: + test:ok(not module.checkibuf(1LL), 'checkibuf of 1LL')
: + test:ok(not module.checkibuf(box.NULL), 'checkibuf of box.NULL')
: + test:ok(not module.checkibuf(buffer.reg1), 'checkibuf of reg1')
: + test:ok(module.checkibuf(ibuf), 'checkibuf of ibuf')
: + test:ok(not module.checkibuf(pbuf), 'checkibuf of pointer to ibuf
: data')
: +end
: +
: local function test_tuples(test, module)
: test:plan(8)
:
: @@ -59,7 +76,7 @@ local function test_tuples(test, module)
: end
:
: local test = require('tap').test("module_api", function(test)
: - test:plan(25)
: + test:plan(26)
: local status, module = pcall(require, 'module_api')
: test:is(status, true, "module")
: test:ok(status, "module is loaded")
: @@ -84,6 +101,7 @@ local test = require('tap').test("module_api",
: function(test)
: test:like(msg, 'luaT_error', 'luaT_error')
:
: test:test("pushcdata", test_pushcdata, module)
: + test:test("buffers", test_buffers, module)
: test:test("validate", test_tuples, module)
:
: space:drop()
: --
: 2.20.1
^ permalink raw reply [flat|nested] 8+ messages in thread
* [Tarantool-patches] [PATCH 1.10 v3 4/4] module api: add luaL_iscallable with support of cdata metatype
2020-10-12 0:50 [Tarantool-patches] [PATCH 1.10 v3 0/4] module api: extend for external merger Lua module Timur Safin
` (2 preceding siblings ...)
2020-10-12 0:50 ` [Tarantool-patches] [PATCH 1.10 v3 3/4] module api: luaL_checkibuf Timur Safin
@ 2020-10-12 0:50 ` Timur Safin
2020-10-13 11:58 ` Alexander Turenko
2020-10-12 9:19 ` [Tarantool-patches] [PATCH 1.10 v3 0/4] module api: extend for external merger Lua module Timur Safin
4 siblings, 1 reply; 8+ messages in thread
From: Timur Safin @ 2020-10-12 0:50 UTC (permalink / raw)
To: v.shpilevoy, alexander.turenko; +Cc: tarantool-patches
From: Alexander Turenko <alexander.turenko@tarantool.org>
Part of #5273.
---
| 1 +
src/lua/utils.c | 43 ++++++++++++++++
test/app-tap/module_api.c | 9 ++++
test/app-tap/module_api.test.lua | 85 +++++++++++++++++++++++++++++++-
4 files changed, 136 insertions(+), 2 deletions(-)
--git a/extra/exports b/extra/exports
index abe9eded4..7a4503578 100644
--- a/extra/exports
+++ b/extra/exports
@@ -139,6 +139,7 @@ luaT_state
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 318715960..c44f680d9 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/test/app-tap/module_api.c b/test/app-tap/module_api.c
index 2c4ca710d..16707f4ed 100644
--- a/test/app-tap/module_api.c
+++ b/test/app-tap/module_api.c
@@ -576,7 +576,15 @@ test_tuple_validate(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;
}
@@ -610,6 +618,7 @@ luaopen_module_api(lua_State *L)
{"test_tostring", test_tostring},
{"test_luaT_tuple_encode", test_luaT_tuple_encode},
{"tuple_validate", test_tuple_validate},
+ {"iscallable", test_iscallable},
{NULL, NULL}
};
luaL_register(L, "module_api", lib);
diff --git a/test/app-tap/module_api.test.lua b/test/app-tap/module_api.test.lua
index 2b28bdaff..82ee81378 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
@@ -75,8 +77,86 @@ local function test_tuples(test, module)
test:ok(module.tuple_validate(tuple4), "tuple 4")
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(26)
+ test:plan(27)
local status, module = pcall(require, 'module_api')
test:is(status, true, "module")
test:ok(status, "module is loaded")
@@ -101,6 +181,7 @@ local test = require('tap').test("module_api", function(test)
test:like(msg, 'luaT_error', 'luaT_error')
test:test("pushcdata", test_pushcdata, module)
+ test:test("iscallable", test_iscallable, module)
test:test("buffers", test_buffers, module)
test:test("validate", test_tuples, module)
--
2.20.1
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [Tarantool-patches] [PATCH 1.10 v3 0/4] module api: extend for external merger Lua module
2020-10-12 0:50 [Tarantool-patches] [PATCH 1.10 v3 0/4] module api: extend for external merger Lua module Timur Safin
` (3 preceding siblings ...)
2020-10-12 0:50 ` [Tarantool-patches] [PATCH 1.10 v3 4/4] module api: add luaL_iscallable with support of cdata metatype Timur Safin
@ 2020-10-12 9:19 ` Timur Safin
4 siblings, 0 replies; 8+ messages in thread
From: Timur Safin @ 2020-10-12 9:19 UTC (permalink / raw)
To: v.shpilevoy, alexander.turenko; +Cc: tarantool-patches
I've updated https://github.com/tarantool/tarantool/tree/tsafin/gh-5273-expand-module-api-1.10-v3
branch to not contain old, unnecessary cruft (like luaL_checkconstchar), and
having all iscallable code at the single commit.
Do not sending updated patchset to maillist - to reduce noise level these days.
Please give me know if you still prefer to see updated patchset.
Timur
: -----Original Message-----
: From: Timur Safin <tsafin@tarantool.org>
: Sent: Monday, October 12, 2020 3:51 AM
: To: v.shpilevoy@tarantool.org; alexander.turenko@tarantool.org
: Cc: Timur Safin <tsafin@tarantool.org>; tarantool-
: patches@dev.tarantool.org
: Subject: [PATCH 1.10 v3 0/4] module api: extend for external merger Lua
: module
:
: This patchset is a 1.10 backport of a 2.X series which was in turn
: continuation of patch series which Alexander Turenko has sent before.
:
: The major difference here - is the way how we export symbols:
: - in 1.10 there was no `src/exports.h` existing, and we were using
: old good `extra/exports` instead.
:
: Changelog of v2 to v1
: ---------------------
: Please do not be surprised to not seen v2 in your inbox - it was cancelled
: after discussion with Alexander Turenko (see v2.1 below with descriptions)
:
: - Unpublished again `lauT_temp_state`, `luaT_release_temp_state` - they
: are
: performance wrappers around creation of Lua thread states. Agreed that
: performance impact not that big, thus created similar compatibility
: wrappers
: on the module side;
: - unpublished `luaT_newthread`, because for module usage `lua_newthread`
: is
: enough;
: - unpublished `luaL_register_module` & `luaL_register_type` and reworked
: module
: code with `luaL_register`.
:
: Smallish changelog of v2.1 to v2
: --------------------------------
: Alexander Turenko has provided valuable feedback to the v2 branch I've
: shown,
: so we have yet more reduced this patchset
: - Unpublished `luaL_cdata_iscallable`, it's unnecessary for external
: module
: which is using now public `luaL_iscallable` module api call, instead
: of compatibility layer in module;
: - Unpublished `luaL_checkconstchar` because it's not yet needed anywhere
: externally;
: - Accordingly to those changes above we have reduced code in module_api
: test files.
:
: Changelog for v3
: ----------------
: - Have removed all extra msgpuck symbols from extra/exports;
: - Reshuffled patchset to merge tests with their corresponding code;
:
: I'm sending this version because it's too late already and I need to
: sleep...
:
: Plans for v3.1
: --------------
: - Add ibuf wrapper, despite all controversies
: it's coming soon...
:
:
: Issue:
: * https://github.com/tarantool/tarantool/issues/5384
: ('module api: expose everything that is needed for external merger
: module')
:
: Branches:
: * https://github.com/tarantool/tarantool/tree/tsafin/gh-5273-expand-
: module-api-1.10-v3
: (last 4 commits above of a bunch of @Totktonada's commits)
:
: == External merger module
:
: Reminder - current external merger is residing here
: https://github.com/tsafin/tarantool-merge
: CI is green again!
:
:
:
: Alexander Turenko (1):
: module api: add luaL_iscallable with support of cdata metatype
:
: Timur Safin (3):
: module api: export box_tuple_validate
: module api: export box_key_def_dup
: module api: luaL_checkibuf
:
: extra/exports | 5 ++
: src/box/key_def.c | 6 ++
: src/box/key_def.h | 10 +++
: src/box/tuple.c | 8 ++
: src/box/tuple.h | 11 +++
: src/lua/utils.c | 43 +++++++++++
: src/lua/utils.h | 44 ++++++-----
: test/app-tap/module_api.c | 35 +++++++++
: test/app-tap/module_api.test.lua | 126 ++++++++++++++++++++++++++++++-
: 9 files changed, 269 insertions(+), 19 deletions(-)
:
: --
: 2.20.1
^ permalink raw reply [flat|nested] 8+ messages in thread