Tarantool development patches archive
 help / color / mirror / Atom feed
From: Vladislav Shpilevoy via Tarantool-patches <tarantool-patches@dev.tarantool.org>
To: Cyrill Gorcunov <gorcunov@gmail.com>,
	tml <tarantool-patches@dev.tarantool.org>
Subject: Re: [Tarantool-patches] [PATCH v21 5/6] box: implement box.lib module
Date: Sun, 11 Apr 2021 17:38:51 +0200	[thread overview]
Message-ID: <0868de76-3941-ed34-3d7e-2f7328e30934@tarantool.org> (raw)
In-Reply-To: <20210408164151.1759348-6-gorcunov@gmail.com>

Thanks for the patch!

Please, try to respond to my comments in the original email,
explicitly. When and how you fixed them, or why did not. Not
with promises like "will do", "ok", and so on, they don't help
much.

Otherwise I see you tend to miss some of the comments. Also I
don't remember all of my comments, so when you don't respond to
them, I need to go to my old email, and match the comments
with the places you was supposed to fix, and it takes time.

See how Sergey and Mergen send their patches for examples.

https://github.com/tarantool/tarantool/wiki/Code-review-procedure#during-the-review

See 4 comments below.

> module:load(name) -> obj | error`

1. In the previous review I said you missed ` in the beginning
of the line here. Please, add it. Otherwise the formatting might
be broken.

> ---------------------------------
> 
> Loads a new function with name `name` from the previously
> loaded `module` and return a callable object instance
> associated with the function. On failure an error is thrown.
> 
> Possible errors:
>  - IllegalParams: function name is either not supplied
>    or not a string.
>  - IllegalParams: attempt to load a function but module
>    has been unloaded already.
>  - ClientError: no such function in the module.
>  - OutOfMemory: unable to allocate a function.
> 
> Example:
> 
> ``` Lua
> -- Load a module if not been loaded yet.
> m = box.lib.load('path/to/library')
> -- Load a function with the `foo` name from the module `m`.
> func = m:load('foo')
> ```
> 
> diff --git a/src/box/lua/lib.c b/src/box/lua/lib.c
> new file mode 100644
> index 000000000..78aee37b5
> --- /dev/null
> +++ b/src/box/lua/lib.c
> @@ -0,0 +1,606 @@
> +
> +static int
> +cache_put(struct box_module_func *cf)
> +{
> +	const struct mh_strnptr_node_t nd = {
> +		.str	= cf->key,
> +		.len	= cf->len,
> +		.hash	= mh_strn_hash(cf->key, cf->len),
> +		.val	= cf,
> +	};
> +	mh_int_t e = mh_strnptr_put(func_hash, &nd, NULL, NULL);

2. As I said in the previous review, it would be good to get the
old value and ensure it is NULL. So as not to replace something
and not notice it.

> +	if (e == mh_end(func_hash)) {
> +		diag_set(OutOfMemory, sizeof(nd), "malloc",
> +			 "box.lib: hash node");
> +		return -1;
> +	}
> +	return 0;
> +}

<...>

> +static int
> +lbox_module_load_func(struct lua_State *L)
> +{
> +	const char *method = "function = module:load";
> +	const char *fmt_noname = "Expects %s(\'name\') but no name passed";
> +
> +	if (lua_gettop(L) != 2 || !lua_isstring(L, 2)) {
> +		diag_set(IllegalParams, fmt_noname, method);
> +		return luaT_error(L);
> +	}
> +
> +	struct module *m = get_udata(L, uname_lib);
> +	if (m == NULL) {
> +		const char *fmt =
> +			"Expects %s(\'name\') but not module object passed";
> +		diag_set(IllegalParams, fmt, method);
> +		return luaT_error(L);
> +	}
> +
> +	size_t sym_len;
> +	const char *sym = lua_tolstring(L, 2, &sym_len);
> +	const size_t max_sym_len = 512;
> +
> +	if (sym_len < 1) {
> +		diag_set(IllegalParams, fmt_noname, method);
> +		return luaT_error(L);
> +	} else if (sym_len > max_sym_len) {
> +		diag_set(IllegalParams, "Symbol \'%s\' is too long (max %zd)",
> +			 sym, max_sym_len);
> +		return luaT_error(L);
> +	}
> +
> +	/*
> +	 * Functions are bound to a module symbols, thus
> +	 * since the hash is global it should be unique
> +	 * per module. The symbol (function name) is the
> +	 * last part of the hash key.
> +	 *
> +	 * The key's buffer should be big enough to keep
> +	 * the longest package path plus symbol name and
> +	 * the pointer.
> +	 */
> +	char key[PATH_MAX + 2 * max_sym_len];

3. +3 for the dots you added below.

> +	snprintf(key, sizeof(key), "%p.%s.%s",
> +		 (void *)m, m->package, sym);

4. Is '(void *)' cast needed? It should work implicitly, no?

> +	size_t len = strlen(key);
> +
> +	struct box_module_func *cf = cache_find(key, len);
> +	if (cf == NULL) {
> +		cf = box_module_func_new(m, key, len, sym_len);
> +		if (cf == NULL)
> +			return luaT_error(L);
> +	} else {
> +		box_module_func_ref(cf);
> +	}
> +
> +	new_udata(L, uname_func, cf);
> +	return 1;
> +}

  reply	other threads:[~2021-04-11 15:38 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-04-08 16:41 [Tarantool-patches] [PATCH v21 0/6] box: implement box.lib Lua module Cyrill Gorcunov via Tarantool-patches
2021-04-08 16:41 ` [Tarantool-patches] [PATCH v21 1/6] box/func: fix modules functions restore Cyrill Gorcunov via Tarantool-patches
2021-04-09 23:31   ` Vladislav Shpilevoy via Tarantool-patches
2021-04-10 15:02     ` Cyrill Gorcunov via Tarantool-patches
2021-04-08 16:41 ` [Tarantool-patches] [PATCH v21 2/6] box/func: module_reload -- drop redundant argument Cyrill Gorcunov via Tarantool-patches
2021-04-08 16:41 ` [Tarantool-patches] [PATCH v21 3/6] box/module_cache: introduce modules subsystem Cyrill Gorcunov via Tarantool-patches
2021-04-09 23:54   ` Vladislav Shpilevoy via Tarantool-patches
2021-04-10 14:59     ` Cyrill Gorcunov via Tarantool-patches
2021-04-08 16:41 ` [Tarantool-patches] [PATCH v21 4/6] box/schema.func: switch to new module api Cyrill Gorcunov via Tarantool-patches
2021-04-09 23:55   ` Vladislav Shpilevoy via Tarantool-patches
2021-04-10 15:00     ` Cyrill Gorcunov via Tarantool-patches
2021-04-08 16:41 ` [Tarantool-patches] [PATCH v21 5/6] box: implement box.lib module Cyrill Gorcunov via Tarantool-patches
2021-04-11 15:38   ` Vladislav Shpilevoy via Tarantool-patches [this message]
2021-04-11 22:38     ` Cyrill Gorcunov via Tarantool-patches
2021-04-12 22:08       ` Vladislav Shpilevoy via Tarantool-patches
2021-04-12 22:34         ` Cyrill Gorcunov via Tarantool-patches
2021-04-08 16:41 ` [Tarantool-patches] [PATCH v21 6/6] test: add box.lib test Cyrill Gorcunov via Tarantool-patches
2021-04-08 18:53   ` Cyrill Gorcunov via Tarantool-patches
2021-04-11 15:43     ` Vladislav Shpilevoy via Tarantool-patches
2021-04-11 21:56       ` Cyrill Gorcunov via Tarantool-patches
2021-04-11 22:36       ` Cyrill Gorcunov via Tarantool-patches
2021-04-12 22:08         ` Vladislav Shpilevoy via Tarantool-patches
2021-04-13  7:10           ` Cyrill Gorcunov via Tarantool-patches
2021-04-13 21:53 ` [Tarantool-patches] [PATCH v21 0/6] box: implement box.lib Lua module Vladislav Shpilevoy via Tarantool-patches
2021-04-14  8:07 ` Kirill Yukhin via Tarantool-patches

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=0868de76-3941-ed34-3d7e-2f7328e30934@tarantool.org \
    --to=tarantool-patches@dev.tarantool.org \
    --cc=gorcunov@gmail.com \
    --cc=v.shpilevoy@tarantool.org \
    --subject='Re: [Tarantool-patches] [PATCH v21 5/6] box: implement box.lib module' \
    /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