From: Vladimir Davydov <vdavydov.dev@gmail.com> To: Alexander Turenko <alexander.turenko@tarantool.org> Cc: tarantool-patches@freelists.org Subject: Re: [PATCH v2 3/6] lua: add luaT_newtuple() Date: Wed, 23 Jan 2019 19:12:26 +0300 [thread overview] Message-ID: <20190123161226.fbarwvyjpcecg2hl@esperanza> (raw) In-Reply-To: <20190118215820.royn7thqv4xuflb4@tkn_work_nb> On Sat, Jan 19, 2019 at 12:58:20AM +0300, Alexander Turenko wrote: > > > diff --git a/src/box/lua/tuple.c b/src/box/lua/tuple.c > > > index 1867f810f..7e9ad89fe 100644 > > > --- a/src/box/lua/tuple.c > > > +++ b/src/box/lua/tuple.c > > > @@ -92,6 +92,65 @@ luaT_istuple(struct lua_State *L, int narg) > > > return *(struct tuple **) data; > > > } > > > > > > +struct tuple * > > > +luaT_newtuple(struct lua_State *L, int idx, box_tuple_format_t *format) > > > > I looked at the Lua reference manual and realized that they usually call > > a function lua_newsomething if it creates an object on Lua stack. So I > > guess we'd better rename it to luaT_tuple_new() to avoid confusion. > > Renamed, but I'm tentative about this change. > > We'll have the following convention: > > * lua{,L,T}_foo_new(): read from a Lua stack, return a pointer, push an > error to a Lua stack (errors is the open question, see below); > * lua{,L,T}_newfoo(): read from a Lua stack, push on a Lua stack, raise > an error; > * lbox_foo_new(): the same as previous, but exposed into Lua via a > module / instance fields. > > Don't sure I'll able to remember this convention: a name does not much > say about a behaviour. I would stick with luaT_new* or introduce some luaT_newkey_def would look awful, wouldn't it? > more explicit naming for a function to grab a definition from lua and > create an object in C. > > Check / to does not fit good: > > * lua{L,T}_checkfoo() -> struct foo * > - Like luaL_checkstring(). > - But it looks as if it can raise an exception. > - But it looks as if we'll unwrap a pointer from a cdata. > * lua{L,T}_tofoo() -> struct foo * > - Like lua_tolstring(). > - But it looks as if we'll unwrap a pointer from a cdata. > > Another possible variants (in order of increasing weirdness for me): > > * lua{L,T}_makefoo(); > * lua{L,T}_createfoo(); > * lua{L,T}_parsefoo(); > * lua{L,T}_newfoo_c(); > * lua{L,T}_constructfoo(); > * lua{L,T}_foo_new_fromlua(); > > BTW, '_c' postfix can suggest that a function cannot raise an error. Better not (why _c?) > > There are lua{,L}_new* functions that returns a pointer: > > * luaL_newstate(void) -> struct lua_State * > * lua_newthread(struct lua_State *) -> struct lua_State * > * lua_newuserdata(struct lua_State *, size_t) -> void * These function don't use Lua stack as input AFAIK so they are different. > > Maybe it is not criminal to do the same under luaT_newtuple()? > > We also can use luaT_create*, I found only one function with such > naming: lua_createtable(). > > I think the general approach could be the following: use > lua{L,T}_newfoo() or lua{L,T}_createfoo() if the former one is busy. > > luaT_maketuple() and lua{L,T}_newtuple_c() looks appropriate for me too. > Latter a bit ugly, but '_c' is informative (no exceptions). I don't know what to say, really. It's overwhelming. Let's just call this function luaT_tuple_new ;-) > > > > > > +{ > > > + struct tuple *tuple; > > > + > > > + if (idx == 0 || lua_istable(L, idx)) { > > > + struct ibuf *buf = tarantool_lua_ibuf; > > > + ibuf_reset(buf); > > > + struct mpstream stream; > > > + mpstream_init(&stream, buf, ibuf_reserve_cb, ibuf_alloc_cb, > > > + luamp_error, L); > > > + if (idx == 0) { > > > + /* > > > + * Create the tuple from lua stack > > > + * objects. > > > + */ > > > + int argc = lua_gettop(L); > > > + mpstream_encode_array(&stream, argc); > > > + for (int k = 1; k <= argc; ++k) { > > > + luamp_encode(L, luaL_msgpack_default, &stream, > > > + k); > > > + } > > > + } else { > > > + /* Create the tuple from a Lua table. */ > > > + luamp_encode_tuple(L, luaL_msgpack_default, &stream, > > > + idx); > > > + } > > > + mpstream_flush(&stream); > > > + tuple = box_tuple_new(format, buf->buf, > > > + buf->buf + ibuf_used(buf)); > > > + if (tuple == NULL) { > > > + luaT_pusherror(L, diag_last_error(diag_get())); > > > > Why not simply throw the error with luaT_error()? Other similar > > functions throw an error, not just push it to the stack. > > Because in a caller I need to perform clean up before reraise it. > lua_pcall() would be expensive. > > luaT_tuple_new() is used in a table and an iterator source in next(). > next() is used in two places: merger_next() where I just pop and raise > the error and in merger_source_new() to acquire a first tuple. If an Why do you need to acquire the first tuple in merger_source_new()? Can't you do that in next()? > error occurs here I need to free the newly created source, then in > merger_state_new() free newly created merger_state with all successfully > created sources (it also perform needed unref's). I cannot allow any > function called on this path to raise an error. > > I can implement reference counting of all that objects and free them in > the next call to merger (some kind of simple gc), but this way looks as > overengineered. > > Mine errors are strings and it is convenient to create them with > lua_pushfstring() or push memory errors with luaT_pusherror(). > > There are two variants how to avoid raising an error: > > * lua_pushfstring(); > * diag_set(). > > Latter seems to be more native for tarantool. I would use something like > XlogError: printf-style format string + vararg. But I doubt how should I > name such class? ContractError (most of them are about bad args)? There > is also unexpected buffer end, it is more RuntimeError. Use IllegalParams (already exists)? > > I dislike the idea to copy XlogError code under another name. Maybe we > can implement a general class for such errors and inherit it in > XlogError, ContractError and RuntimeError? > > I choose pushing to stack, because it is the most simple solution, and > forget to discuss it with you. My bad. > > Please, give me some pointer here. I'd prefer to either throw an error (if the merger can handle it) or set diag to IllegalParams.
next prev parent reply other threads:[~2019-01-23 16:12 UTC|newest] Thread overview: 28+ messages / expand[flat|nested] mbox.gz Atom feed top 2019-01-09 20:20 [PATCH v2 0/6] Merger Alexander Turenko 2019-01-09 20:20 ` [PATCH v2 1/6] Add luaL_iscallable with support of cdata metatype Alexander Turenko 2019-01-10 12:21 ` Vladimir Davydov 2019-01-09 20:20 ` [PATCH v2 2/6] Add functions to ease using Lua iterators from C Alexander Turenko 2019-01-10 12:29 ` Vladimir Davydov 2019-01-15 23:26 ` Alexander Turenko 2019-01-16 8:18 ` Vladimir Davydov 2019-01-16 11:40 ` Alexander Turenko 2019-01-16 12:20 ` Vladimir Davydov 2019-01-17 1:20 ` Alexander Turenko 2019-01-28 18:17 ` Alexander Turenko 2019-03-01 4:04 ` Alexander Turenko 2019-01-09 20:20 ` [PATCH v2 3/6] lua: add luaT_newtuple() Alexander Turenko 2019-01-10 12:44 ` Vladimir Davydov 2019-01-18 21:58 ` Alexander Turenko 2019-01-23 16:12 ` Vladimir Davydov [this message] 2019-01-28 16:51 ` Alexander Turenko 2019-03-01 4:08 ` Alexander Turenko 2019-01-09 20:20 ` [PATCH v2 4/6] lua: add luaT_new_key_def() Alexander Turenko 2019-01-10 13:07 ` Vladimir Davydov 2019-01-29 18:52 ` Alexander Turenko 2019-01-30 10:58 ` Alexander Turenko 2019-03-01 4:10 ` Alexander Turenko 2019-01-09 20:20 ` [PATCH v2 5/6] net.box: add helpers to decode msgpack headers Alexander Turenko 2019-01-10 17:29 ` Vladimir Davydov 2019-02-01 15:11 ` Alexander Turenko 2019-03-05 12:00 ` Alexander Turenko 2019-01-09 20:20 ` [PATCH v2 6/6] Add merger for tuple streams 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=20190123161226.fbarwvyjpcecg2hl@esperanza \ --to=vdavydov.dev@gmail.com \ --cc=alexander.turenko@tarantool.org \ --cc=tarantool-patches@freelists.org \ --subject='Re: [PATCH v2 3/6] lua: add luaT_newtuple()' \ /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