[PATCH 1/3] Add luaT_iscallable with support of cdata metatype

Vladimir Davydov vdavydov.dev at gmail.com
Wed Dec 26 21:35:56 MSK 2018


On Sun, Dec 16, 2018 at 11:17:24PM +0300, Alexander Turenko wrote:
> Needed for #3276.
> ---
>  extra/exports                    |  1 +
>  src/lua/utils.c                  | 43 ++++++++++++++++
>  src/lua/utils.h                  | 10 ++++
>  test/app-tap/module_api.c        | 10 ++++
>  test/app-tap/module_api.test.lua | 85 +++++++++++++++++++++++++++++++-
>  5 files changed, 147 insertions(+), 2 deletions(-)
> 
> diff --git a/extra/exports b/extra/exports
> index 5f69e0730..52f0b2378 100644
> --- a/extra/exports
> +++ b/extra/exports
> @@ -134,6 +134,7 @@ luaT_call
>  luaT_cpcall
>  luaT_state
>  luaT_tolstring
> +luaT_iscallable

Do we really need to export it?

>  box_txn
>  box_txn_begin
>  box_txn_commit
> diff --git a/src/lua/utils.c b/src/lua/utils.c
> index 978fe61f1..7a6069fbb 100644
> --- a/src/lua/utils.c
> +++ b/src/lua/utils.c
> @@ -920,6 +920,49 @@ luaT_tolstring(lua_State *L, int idx, size_t *len)
>  	return lua_tolstring(L, -1, len);
>  }
>  
> +/* Based on ffi_meta___call() from luajit/src/lib_ffi.c. */
> +static int
> +luaT_cdata_iscallable(lua_State *L, int idx)

I think this function should have prefix luaL_ rather than luaT_.
Other than that, this particular patch looks OK to me.

> +{
> +	/* 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;
> +}



More information about the Tarantool-patches mailing list