summaryrefslogtreecommitdiffstats
path: root/src/if_lua.c
diff options
context:
space:
mode:
authorYegappan Lakshmanan <yegappan@yahoo.com>2023-01-12 12:33:30 +0000
committerBram Moolenaar <Bram@vim.org>2023-01-12 12:33:30 +0000
commit0233bdfa2b487c392dc4fd1a29113e08fbace334 (patch)
tree83551e09979e7baa762cd11a65ddff2153883e4e /src/if_lua.c
parent043d7b2c84cda275354aa023b5769660ea70a168 (diff)
patch 9.0.1183: code is indented more than necessaryv9.0.1183
Problem: Code is indented more than necessary. Solution: Use an early return where it makes sense. (Yegappan Lakshmanan, closes #11805)
Diffstat (limited to 'src/if_lua.c')
-rw-r--r--src/if_lua.c258
1 files changed, 139 insertions, 119 deletions
diff --git a/src/if_lua.c b/src/if_lua.c
index 78bc190950..6941789b91 100644
--- a/src/if_lua.c
+++ b/src/if_lua.c
@@ -505,18 +505,20 @@ luaV_toudata(lua_State *L, int ud, const char *tname)
{
void *p = lua_touserdata(L, ud);
- if (p != NULL) // value is userdata?
+ if (p == NULL)
+ return NULL;
+
+ // value is userdata
+ if (lua_getmetatable(L, ud)) // does it have a metatable?
{
- if (lua_getmetatable(L, ud)) // does it have a metatable?
+ luaV_getfield(L, tname); // get metatable
+ if (lua_rawequal(L, -1, -2)) // MTs match?
{
- luaV_getfield(L, tname); // get metatable
- if (lua_rawequal(L, -1, -2)) // MTs match?
- {
- lua_pop(L, 2); // MTs
- return p;
- }
+ lua_pop(L, 2); // MTs
+ return p;
}
}
+
return NULL;
}
@@ -1090,17 +1092,19 @@ luaV_dict_index(lua_State *L)
dictitem_T *di = dict_find(d, key, -1);
if (di == NULL)
+ {
lua_pushnil(L);
- else
+ return 1;
+ }
+
+ luaV_pushtypval(L, &di->di_tv);
+ if (di->di_tv.v_type == VAR_FUNC) // funcref?
{
- luaV_pushtypval(L, &di->di_tv);
- if (di->di_tv.v_type == VAR_FUNC) // funcref?
- {
- luaV_Funcref *f = (luaV_Funcref *) lua_touserdata(L, -1);
- f->self = d; // keep "self" reference
- d->dv_refcount++;
- }
+ luaV_Funcref *f = (luaV_Funcref *) lua_touserdata(L, -1);
+ f->self = d; // keep "self" reference
+ d->dv_refcount++;
}
+
return 1;
}
@@ -1235,20 +1239,22 @@ luaV_blob_newindex(lua_State *L)
blob_T *b = luaV_unbox(L, luaV_Blob, 1);
if (b->bv_lock)
luaL_error(L, "blob is locked");
- if (lua_isnumber(L, 2))
+
+ if (!lua_isnumber(L, 2))
+ return 0;
+
+ long len = blob_len(b);
+ int idx = luaL_checkinteger(L, 2);
+ int val = luaL_checkinteger(L, 3);
+ if (idx < len || (idx == len && ga_grow(&b->bv_ga, 1) == OK))
{
- long len = blob_len(b);
- int idx = luaL_checkinteger(L, 2);
- int val = luaL_checkinteger(L, 3);
- if (idx < len || (idx == len && ga_grow(&b->bv_ga, 1) == OK))
- {
- blob_set(b, idx, (char_u) val);
- if (idx == len)
- ++b->bv_ga.ga_len;
- }
- else
- luaL_error(L, "index out of range");
+ blob_set(b, idx, (char_u) val);
+ if (idx == len)
+ ++b->bv_ga.ga_len;
}
+ else
+ luaL_error(L, "index out of range");
+
return 0;
}
@@ -1943,30 +1949,34 @@ luaV_list(lua_State *L)
if (initarg && lua_type(L, 1) != LUA_TTABLE)
luaL_error(L, "table expected, got %s", luaL_typename(L, 1));
+
l = list_alloc();
if (l == NULL)
+ {
lua_pushnil(L);
- else
+ return 1;
+ }
+
+ luaV_newlist(L, l);
+ if (!initarg)
+ return 1;
+
+ // traverse table to init list
+ int notnil, i = 0;
+ typval_T v;
+ do
{
- luaV_newlist(L, l);
- if (initarg) // traverse table to init list
+ lua_rawgeti(L, 1, ++i);
+ notnil = !lua_isnil(L, -1);
+ if (notnil)
{
- int notnil, i = 0;
- typval_T v;
- do
- {
- lua_rawgeti(L, 1, ++i);
- notnil = !lua_isnil(L, -1);
- if (notnil)
- {
- luaV_checktypval(L, -1, &v, "vim.list");
- list_append_tv(l, &v);
- clear_tv(&v);
- }
- lua_pop(L, 1); // value
- } while (notnil);
+ luaV_checktypval(L, -1, &v, "vim.list");
+ list_append_tv(l, &v);
+ clear_tv(&v);
}
- }
+ lua_pop(L, 1); // value
+ } while (notnil);
+
return 1;
}
@@ -1978,43 +1988,47 @@ luaV_dict(lua_State *L)
if (initarg && lua_type(L, 1) != LUA_TTABLE)
luaL_error(L, "table expected, got %s", luaL_typename(L, 1));
+
d = dict_alloc();
if (d == NULL)
+ {
lua_pushnil(L);
- else
+ return 1;
+ }
+
+ luaV_newdict(L, d);
+ if (!initarg)
+ return 1;
+
+ // traverse table to init dict
+ lua_pushnil(L);
+ while (lua_next(L, 1))
{
- luaV_newdict(L, d);
- if (initarg) // traverse table to init dict
+ char_u *key;
+ dictitem_T *di;
+ typval_T v;
+
+ lua_pushvalue(L, -2); // dup key in case it's a number
+ key = (char_u *) lua_tostring(L, -1);
+ if (key == NULL)
{
lua_pushnil(L);
- while (lua_next(L, 1))
- {
- char_u *key;
- dictitem_T *di;
- typval_T v;
-
- lua_pushvalue(L, -2); // dup key in case it's a number
- key = (char_u *) lua_tostring(L, -1);
- if (key == NULL)
- {
- lua_pushnil(L);
- return 1;
- }
- if (*key == NUL)
- luaL_error(L, "table has empty key");
- luaV_checktypval(L, -2, &v, "vim.dict"); // value
- di = dictitem_alloc(key);
- if (di == NULL || dict_add(d, di) == FAIL)
- {
- vim_free(di);
- lua_pushnil(L);
- return 1;
- }
- di->di_tv = v;
- lua_pop(L, 2); // key copy and value
- }
+ return 1;
}
+ if (*key == NUL)
+ luaL_error(L, "table has empty key");
+ luaV_checktypval(L, -2, &v, "vim.dict"); // value
+ di = dictitem_alloc(key);
+ if (di == NULL || dict_add(d, di) == FAIL)
+ {
+ vim_free(di);
+ lua_pushnil(L);
+ return 1;
+ }
+ di->di_tv = v;
+ lua_pop(L, 2); // key copy and value
}
+
return 1;
}
@@ -2026,22 +2040,26 @@ luaV_blob(lua_State *L)
if (initarg && !lua_isstring(L, 1))
luaL_error(L, "string expected, got %s", luaL_typename(L, 1));
+
b = blob_alloc();
if (b == NULL)
- lua_pushnil(L);
- else
{
- luaV_newblob(L, b);
- if (initarg)
- {
- size_t i, l = 0;
- const char *s = lua_tolstring(L, 1, &l);
-
- if (ga_grow(&b->bv_ga, (int)l) == OK)
- for (i = 0; i < l; ++i)
- ga_append(&b->bv_ga, s[i]);
- }
+ lua_pushnil(L);
+ return 1;
}
+
+ luaV_newblob(L, b);
+ if (!initarg)
+ return 1;
+
+ // traverse table to init blob
+ size_t i, l = 0;
+ const char *s = lua_tolstring(L, 1, &l);
+
+ if (ga_grow(&b->bv_ga, (int)l) == OK)
+ for (i = 0; i < l; ++i)
+ ga_append(&b->bv_ga, s[i]);
+
return 1;
}
@@ -2548,28 +2566,29 @@ lua_isopen(void)
static int
lua_init(void)
{
- if (!lua_isopen())
- {
+ if (lua_isopen())
+ return OK;
+
#ifdef DYNAMIC_LUA
- if (!lua_enabled(TRUE))
- {
- emsg(_("Lua library cannot be loaded."));
- return FAIL;
- }
-#endif
- L = luaV_newstate();
+ if (!lua_enabled(TRUE))
+ {
+ emsg(_("Lua library cannot be loaded."));
+ return FAIL;
}
+#endif
+ L = luaV_newstate();
+
return OK;
}
void
lua_end(void)
{
- if (lua_isopen())
- {
- lua_close(L);
- L = NULL;
- }
+ if (!lua_isopen())
+ return;
+
+ lua_close(L);
+ L = NULL;
}
/*
@@ -2698,31 +2717,32 @@ set_ref_in_lua(int copyID)
{
int aborted = 0;
- if (lua_isopen())
- {
- luaV_getfield(L, LUAVIM_SETREF);
- // call the function with 1 arg, getting 1 result back
- lua_pushinteger(L, copyID);
- lua_call(L, 1, 1);
- // get the result
- aborted = lua_tointeger(L, -1);
- // pop result off the stack
- lua_pop(L, 1);
- }
+ if (!lua_isopen())
+ return 0;
+
+ luaV_getfield(L, LUAVIM_SETREF);
+ // call the function with 1 arg, getting 1 result back
+ lua_pushinteger(L, copyID);
+ lua_call(L, 1, 1);
+ // get the result
+ aborted = lua_tointeger(L, -1);
+ // pop result off the stack
+ lua_pop(L, 1);
+
return aborted;
}
void
update_package_paths_in_lua()
{
- if (lua_isopen())
- {
- lua_getglobal(L, "vim");
- lua_getfield(L, -1, "_update_package_paths");
+ if (!lua_isopen())
+ return;
- if (lua_pcall(L, 0, 0, 0))
- luaV_emsg(L);
- }
+ lua_getglobal(L, "vim");
+ lua_getfield(L, -1, "_update_package_paths");
+
+ if (lua_pcall(L, 0, 0, 0))
+ luaV_emsg(L);
}
/*