Tarantool development patches archive
 help / color / mirror / Atom feed
From: Timur Safin <tsafin@tarantool.org>
To: v.shpilevoy@tarantool.org, alexander.turenko@tarantool.org
Cc: tarantool-patches@dev.tarantool.org
Subject: [Tarantool-patches] [PATCH 2.X v4 3/4] module api: introduced luaT_toibuf instead of luaL_checkibuf
Date: Wed, 14 Oct 2020 02:01:11 +0300	[thread overview]
Message-ID: <fefe53d5e5c88be9e6762783003a1213b2d67cda.1602629628.git.tsafin@tarantool.org> (raw)
In-Reply-To: <cover.1602629628.git.tsafin@tarantool.org>

* made `luaL_checkibuf` public;
* renamed it to `luaT_toibuf` to follow naming convention
  (it's not raising error, and is casting to ibuf type).

Part of #5384
---
 src/box/lua/merger.c             |  4 ++--
 src/exports.h                    |  1 +
 src/lua/msgpack.c                |  2 +-
 src/lua/utils.c                  |  2 +-
 src/lua/utils.h                  | 16 ++++++++--------
 test/app-tap/module_api.c        | 10 ++++++++++
 test/app-tap/module_api.test.lua | 23 ++++++++++++++++++++++-
 7 files changed, 45 insertions(+), 13 deletions(-)

diff --git a/src/box/lua/merger.c b/src/box/lua/merger.c
index 583946c4c..17e237902 100644
--- a/src/box/lua/merger.c
+++ b/src/box/lua/merger.c
@@ -503,7 +503,7 @@ luaL_merge_source_buffer_fetch_impl(struct merge_source_buffer *source,
 		source->ref = 0;
 	}
 	lua_pushvalue(L, -nresult + 1); /* Popped by luaL_ref(). */
-	source->buf = luaL_checkibuf(L, -1);
+	source->buf = luaT_toibuf(L, -1);
 	if (source->buf == NULL) {
 		diag_set(IllegalParams, "Expected <state>, <buffer>");
 		return -1;
@@ -1225,7 +1225,7 @@ lbox_merge_source_select(struct lua_State *L)
 		lua_pushstring(L, "buffer");
 		lua_gettable(L, 2);
 		if (!lua_isnil(L, -1)) {
-			if ((output_buffer = luaL_checkibuf(L, -1)) == NULL)
+			if ((output_buffer = luaT_toibuf(L, -1)) == NULL)
 				return lbox_merge_source_select_usage(L,
 					"buffer");
 		}
diff --git a/src/exports.h b/src/exports.h
index ff0660e24..9c56686db 100644
--- a/src/exports.h
+++ b/src/exports.h
@@ -415,6 +415,7 @@ EXPORT(luaT_istuple)
 EXPORT(luaT_pushtuple)
 EXPORT(luaT_state)
 EXPORT(luaT_tolstring)
+EXPORT(luaT_toibuf)
 EXPORT(luaT_tuple_encode)
 EXPORT(luaT_tuple_new)
 EXPORT(mp_char2escape)
diff --git a/src/lua/msgpack.c b/src/lua/msgpack.c
index 4c4ada39c..24b0d2ccd 100644
--- a/src/lua/msgpack.c
+++ b/src/lua/msgpack.c
@@ -356,7 +356,7 @@ lua_msgpack_encode(lua_State *L)
 
 	struct ibuf *buf;
 	if (index > 1) {
-		buf = luaL_checkibuf(L, 2);
+		buf = luaT_toibuf(L, 2);
 		if (buf == NULL) {
 			return luaL_error(L, "msgpack.encode: argument 2 "
 					  "must be of type 'struct ibuf'");
diff --git a/src/lua/utils.c b/src/lua/utils.c
index 3e1c491f8..004ea8ba5 100644
--- a/src/lua/utils.c
+++ b/src/lua/utils.c
@@ -1107,7 +1107,7 @@ luaL_iscallable(lua_State *L, int idx)
 }
 
 struct ibuf *
-luaL_checkibuf(struct lua_State *L, int idx)
+luaT_toibuf(struct lua_State *L, int idx)
 {
 	if (lua_type(L, idx) != LUA_TCDATA)
 		return NULL;
diff --git a/src/lua/utils.h b/src/lua/utils.h
index e80e2b1a2..5e902b94e 100644
--- a/src/lua/utils.h
+++ b/src/lua/utils.h
@@ -539,6 +539,14 @@ luaT_tolstring(lua_State *L, int idx, size_t *ssize);
 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.
+ */
+struct ibuf *
+luaT_toibuf(struct lua_State *L, int idx);
+
 /** \endcond public */
 
 /**
@@ -628,14 +636,6 @@ luaL_checkfinite(struct lua_State *L, struct luaL_serializer *cfg,
 struct lua_State *
 luaT_newthread(struct lua_State *L);
 
-/**
- * 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
diff --git a/test/app-tap/module_api.c b/test/app-tap/module_api.c
index 17048081f..a095416fe 100644
--- a/test/app-tap/module_api.c
+++ b/test/app-tap/module_api.c
@@ -155,6 +155,15 @@ test_checkint64(lua_State *L)
 	return 1;
 }
 
+static int
+test_checkibuf(lua_State *L)
+{
+	struct ibuf *buf;
+	buf = luaT_toibuf(L, -1);
+	lua_pushboolean(L, buf != NULL);
+	return 1;
+}
+
 static int
 test_touint64(lua_State *L)
 {
@@ -1242,6 +1251,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 ab5c89c8c..ff251b338 100755
--- a/test/app-tap/module_api.test.lua
+++ b/test/app-tap/module_api.test.lua
@@ -38,6 +38,26 @@ local function test_pushcdata(test, module)
     test:is(gc_counter, 1, 'pushcdata gc')
 end
 
+local function test_buffers(test, module)
+    test:plan(9)
+    local ffi = require('ffi')
+    local buffer = require('buffer')
+
+    local bufalloc = buffer.static_alloc("char", 128)
+    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(not module.checkibuf(bufalloc), 'checkibuf of allocated buffer')
+    test:ok(module.checkibuf(buffer.IBUF_SHARED), "checkibuf of ibuf*")
+    test:ok(module.checkibuf(ibuf), 'checkibuf of ibuf')
+    test:ok(not module.checkibuf(pbuf), 'checkibuf of pointer to ibuf data')
+end
+
 local function test_tuple_validate(test, module)
     test:plan(12)
 
@@ -198,7 +218,7 @@ local function test_iscdata(test, module)
 end
 
 local test = require('tap').test("module_api", function(test)
-    test:plan(33)
+    test:plan(34)
     local status, module = pcall(require, 'module_api')
     test:is(status, true, "module")
     test:ok(status, "module is loaded")
@@ -225,6 +245,7 @@ local test = require('tap').test("module_api", function(test)
     test:test("pushcdata", test_pushcdata, module)
     test:test("iscallable", test_iscallable, module)
     test:test("iscdata", test_iscdata, module)
+    test:test("buffers", test_buffers, module)
     test:test("tuple_validate", test_tuple_validate, module)
 
     space:drop()
-- 
2.20.1

  parent reply	other threads:[~2020-10-13 23:01 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-10-13 23:01 [Tarantool-patches] [PATCH 2.X v4 0/4] module api: extend for external merger Lua module Timur Safin
2020-10-13 23:01 ` [Tarantool-patches] [PATCH 2.X v4 1/4] module api: export box_tuple_validate Timur Safin
2020-10-15 21:38   ` Alexander Turenko
2020-10-15 21:47     ` Timur Safin
2020-10-15 22:03   ` Vladislav Shpilevoy
2020-10-13 23:01 ` [Tarantool-patches] [PATCH 2.X v4 2/4] module api: export box_key_def_dup Timur Safin
2020-10-13 23:01 ` Timur Safin [this message]
2020-10-14  3:50   ` [Tarantool-patches] [PATCH 2.X v4 3/4] module api: introduced luaT_toibuf instead of luaL_checkibuf Alexander Turenko
2020-10-15 21:07     ` Timur Safin
2020-10-15 22:04   ` Vladislav Shpilevoy
2020-10-13 23:01 ` [Tarantool-patches] [PATCH 2.X v4 4/4] module api: box_ibuf_* wrappers Timur Safin
2020-10-14  3:31   ` Alexander Turenko
2020-10-15 21:35     ` Timur Safin
2020-10-15 21:42       ` Alexander Turenko
2020-10-15 21:44         ` Timur Safin
2020-10-15 21:52         ` Alexander Turenko
2020-10-15 22:07   ` Vladislav Shpilevoy
2020-10-15 22:20     ` Alexander Turenko
2020-10-15 22:27     ` Timur Safin
2020-10-15 22:47       ` Alexander Turenko
2020-10-15 22:37   ` Alexander Turenko
2020-10-15 22:48   ` Alexander Turenko
2020-10-15 22:39 ` [Tarantool-patches] [PATCH 2.X v4.1] " Timur Safin
2020-10-16  6:10 ` [Tarantool-patches] [PATCH 2.X v4 0/4] module api: extend for external merger Lua module Alexander Turenko

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=fefe53d5e5c88be9e6762783003a1213b2d67cda.1602629628.git.tsafin@tarantool.org \
    --to=tsafin@tarantool.org \
    --cc=alexander.turenko@tarantool.org \
    --cc=tarantool-patches@dev.tarantool.org \
    --cc=v.shpilevoy@tarantool.org \
    --subject='Re: [Tarantool-patches] [PATCH 2.X v4 3/4] module api: introduced luaT_toibuf instead of luaL_checkibuf' \
    /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