[Tarantool-patches] [PATCH v2 08/10] box: add param_list to 'struct func'

imeevma at tarantool.org imeevma at tarantool.org
Fri Aug 14 18:05:05 MSK 2020


This is needed to create an uniform way to check the types of arguments
of SQL built-in functions.

Part of #4159
---
 src/box/alter.cc   | 12 ++++++++++--
 src/box/func.c     |  1 +
 src/box/func_def.h |  2 ++
 src/box/lua/call.c |  2 ++
 src/box/sql/func.c |  1 +
 5 files changed, 16 insertions(+), 2 deletions(-)

diff --git a/src/box/alter.cc b/src/box/alter.cc
index ba96d9c62..0914a7615 100644
--- a/src/box/alter.cc
+++ b/src/box/alter.cc
@@ -3293,7 +3293,11 @@ func_def_new_from_tuple(struct tuple *tuple)
 		diag_set(OutOfMemory, def_sz, "malloc", "def");
 		return NULL;
 	}
-	auto def_guard = make_scoped_guard([=] { free(def); });
+	def->param_list = NULL;
+	auto def_guard = make_scoped_guard([=] {
+		free(def->param_list);
+		free(def);
+	});
 	if (func_def_get_ids_from_tuple(tuple, &def->fid, &def->uid) != 0)
 		return NULL;
 	if (def->fid > BOX_FUNCTION_MAX) {
@@ -3403,6 +3407,8 @@ func_def_new_from_tuple(struct tuple *tuple)
 		if (param_list == NULL)
 			return NULL;
 		uint32_t argc = mp_decode_array(&param_list);
+		uint32_t size = sizeof(enum field_type) * argc;
+		def->param_list = (enum field_type *)malloc(size);
 		for (uint32_t i = 0; i < argc; i++) {
 			 if (mp_typeof(*param_list) != MP_STR) {
 				diag_set(ClientError, ER_FIELD_TYPE,
@@ -3412,7 +3418,8 @@ func_def_new_from_tuple(struct tuple *tuple)
 			}
 			uint32_t len;
 			const char *str = mp_decode_str(&param_list, &len);
-			if (STRN2ENUM(field_type, str, len) == field_type_MAX) {
+			def->param_list[i] = STRN2ENUM(field_type, str, len);
+			if (def->param_list[i] == field_type_MAX) {
 				diag_set(ClientError, ER_CREATE_FUNCTION,
 					  def->name, "invalid argument type");
 				return NULL;
@@ -3433,6 +3440,7 @@ func_def_new_from_tuple(struct tuple *tuple)
 		/* By default export to Lua, but not other frontends. */
 		def->exports.lua = true;
 		def->param_count = 0;
+		assert(def->param_list == NULL);
 	}
 	if (func_def_check(def) != 0)
 		return NULL;
diff --git a/src/box/func.c b/src/box/func.c
index 8087c953f..1d20f8872 100644
--- a/src/box/func.c
+++ b/src/box/func.c
@@ -510,6 +510,7 @@ func_c_destroy(struct func *base)
 {
 	assert(base->vtab == &func_c_vtab);
 	assert(base != NULL && base->def->language == FUNC_LANGUAGE_C);
+	free(base->def->param_list);
 	struct func_c *func = (struct func_c *) base;
 	func_c_unload(func);
 	TRASH(base);
diff --git a/src/box/func_def.h b/src/box/func_def.h
index bab2186da..712a5123b 100644
--- a/src/box/func_def.h
+++ b/src/box/func_def.h
@@ -129,6 +129,8 @@ struct func_def {
 	bool is_sandboxed;
 	/** The count of function's input arguments. */
 	int param_count;
+	/** List of input arguments to the function. */
+	enum field_type *param_list;
 	/** The type of the value returned by function. */
 	enum field_type returns;
 	/** Function aggregate option. */
diff --git a/src/box/lua/call.c b/src/box/lua/call.c
index 0315e720c..1dc4589dd 100644
--- a/src/box/lua/call.c
+++ b/src/box/lua/call.c
@@ -783,6 +783,7 @@ func_lua_destroy(struct func *func)
 {
 	assert(func != NULL && func->def->language == FUNC_LANGUAGE_LUA);
 	assert(func->vtab == &func_lua_vtab);
+	free(func->def->param_list);
 	TRASH(func);
 	free(func);
 }
@@ -812,6 +813,7 @@ func_persistent_lua_destroy(struct func *base)
 	assert(base != NULL && base->def->language == FUNC_LANGUAGE_LUA &&
 	       base->def->body != NULL);
 	assert(base->vtab == &func_persistent_lua_vtab);
+	free(base->def->param_list);
 	struct func_lua *func = (struct func_lua *) base;
 	func_persistent_lua_unload(func);
 	free(func);
diff --git a/src/box/sql/func.c b/src/box/sql/func.c
index 1fbffa535..c289f2de0 100644
--- a/src/box/sql/func.c
+++ b/src/box/sql/func.c
@@ -2979,6 +2979,7 @@ func_sql_builtin_destroy(struct func *func)
 {
 	assert(func->vtab == &func_sql_builtin_vtab);
 	assert(func->def->language == FUNC_LANGUAGE_SQL_BUILTIN);
+	free(func->def->param_list);
 	free(func);
 }
 
-- 
2.25.1



More information about the Tarantool-patches mailing list