aboutsummaryrefslogtreecommitdiff
path: root/src/nvim/lua/stdlib.c
diff options
context:
space:
mode:
authorJosh Rahm <joshuarahm@gmail.com>2023-11-29 22:39:54 +0000
committerJosh Rahm <joshuarahm@gmail.com>2023-11-29 22:39:54 +0000
commit21cb7d04c387e4198ca8098a884c78b56ffcf4c2 (patch)
tree84fe5690df1551f0bb2bdfe1a13aacd29ebc1de7 /src/nvim/lua/stdlib.c
parentd9c904f85a23a496df4eb6be42aa43f007b22d50 (diff)
parent4a8bf24ac690004aedf5540fa440e788459e5e34 (diff)
downloadrneovim-colorcolchar.tar.gz
rneovim-colorcolchar.tar.bz2
rneovim-colorcolchar.zip
Merge remote-tracking branch 'upstream/master' into colorcolcharcolorcolchar
Diffstat (limited to 'src/nvim/lua/stdlib.c')
-rw-r--r--src/nvim/lua/stdlib.c134
1 files changed, 95 insertions, 39 deletions
diff --git a/src/nvim/lua/stdlib.c b/src/nvim/lua/stdlib.c
index 6ebca6d97e..33770b2e62 100644
--- a/src/nvim/lua/stdlib.c
+++ b/src/nvim/lua/stdlib.c
@@ -1,6 +1,3 @@
-// This is an open source non-commercial project. Dear PVS-Studio, please check
-// it. PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
-
#include <assert.h>
#include <lauxlib.h>
#include <lua.h>
@@ -11,30 +8,36 @@
#include <string.h>
#include <sys/types.h>
-#include "auto/config.h"
+#ifdef NVIM_VENDOR_BIT
+# include "bit.h"
+#endif
+
#include "cjson/lua_cjson.h"
#include "mpack/lmpack.h"
#include "nvim/api/private/defs.h"
#include "nvim/api/private/helpers.h"
-#include "nvim/ascii.h"
+#include "nvim/ascii_defs.h"
#include "nvim/buffer_defs.h"
-#include "nvim/eval.h"
#include "nvim/eval/typval.h"
-#include "nvim/eval/typval_defs.h"
+#include "nvim/eval/vars.h"
#include "nvim/ex_eval.h"
+#include "nvim/fold.h"
+#include "nvim/func_attr.h"
#include "nvim/globals.h"
+#include "nvim/lua/base64.h"
#include "nvim/lua/converter.h"
#include "nvim/lua/spell.h"
#include "nvim/lua/stdlib.h"
#include "nvim/lua/xdiff.h"
-#include "nvim/map.h"
+#include "nvim/map_defs.h"
#include "nvim/mbyte.h"
#include "nvim/memline.h"
#include "nvim/memory.h"
-#include "nvim/pos.h"
+#include "nvim/pos_defs.h"
#include "nvim/regexp.h"
-#include "nvim/types.h"
-#include "nvim/vim.h"
+#include "nvim/runtime.h"
+#include "nvim/strings.h"
+#include "nvim/types_defs.h"
#ifdef INCLUDE_GENERATED_DECLARATIONS
# include "lua/stdlib.c.generated.h"
@@ -78,20 +81,20 @@ static int regex_match_line(lua_State *lstate)
return luaL_error(lstate, "not enough args");
}
- long bufnr = luaL_checkinteger(lstate, 2);
+ handle_T bufnr = (handle_T)luaL_checkinteger(lstate, 2);
linenr_T rownr = (linenr_T)luaL_checkinteger(lstate, 3);
- long start = 0, end = -1;
+ int start = 0, end = -1;
if (narg >= 4) {
- start = luaL_checkinteger(lstate, 4);
+ start = (int)luaL_checkinteger(lstate, 4);
}
if (narg >= 5) {
- end = luaL_checkinteger(lstate, 5);
+ end = (int)luaL_checkinteger(lstate, 5);
if (end < 0) {
return luaL_error(lstate, "invalid end");
}
}
- buf_T *buf = bufnr ? handle_get_buffer((int)bufnr) : curbuf;
+ buf_T *buf = bufnr ? handle_get_buffer(bufnr) : curbuf;
if (!buf || buf->b_ml.ml_mfp == NULL) {
return luaL_error(lstate, "invalid buffer");
}
@@ -100,7 +103,7 @@ static int regex_match_line(lua_State *lstate)
return luaL_error(lstate, "invalid row");
}
- char *line = ml_get_buf(buf, rownr + 1, false);
+ char *line = ml_get_buf(buf, rownr + 1);
size_t len = strlen(line);
if (start < 0 || (size_t)start > len) {
@@ -178,8 +181,8 @@ int nlua_str_utfindex(lua_State *const lstate) FUNC_ATTR_NONNULL_ALL
size_t codepoints = 0, codeunits = 0;
mb_utflen(s1, (size_t)idx, &codepoints, &codeunits);
- lua_pushinteger(lstate, (long)codepoints);
- lua_pushinteger(lstate, (long)codeunits);
+ lua_pushinteger(lstate, (lua_Integer)codepoints);
+ lua_pushinteger(lstate, (lua_Integer)codeunits);
return 2;
}
@@ -199,7 +202,7 @@ static int nlua_str_utf_pos(lua_State *const lstate) FUNC_ATTR_NONNULL_ALL
size_t clen;
for (size_t i = 0; i < s1_len && s1[i] != NUL; i += clen) {
clen = (size_t)utf_ptr2len_len(s1 + i, (int)(s1_len - i));
- lua_pushinteger(lstate, (long)i + 1);
+ lua_pushinteger(lstate, (lua_Integer)i + 1);
lua_rawseti(lstate, -2, (int)idx);
idx++;
}
@@ -218,11 +221,11 @@ static int nlua_str_utf_start(lua_State *const lstate) FUNC_ATTR_NONNULL_ALL
{
size_t s1_len;
const char *s1 = luaL_checklstring(lstate, 1, &s1_len);
- long offset = luaL_checkinteger(lstate, 2);
+ ptrdiff_t offset = luaL_checkinteger(lstate, 2);
if (offset < 0 || offset > (intptr_t)s1_len) {
return luaL_error(lstate, "index out of range");
}
- int head_offset = utf_cp_head_off((char_u *)s1, (char_u *)s1 + offset - 1);
+ int head_offset = -utf_cp_head_off(s1, s1 + offset - 1);
lua_pushinteger(lstate, head_offset);
return 1;
}
@@ -238,7 +241,7 @@ static int nlua_str_utf_end(lua_State *const lstate) FUNC_ATTR_NONNULL_ALL
{
size_t s1_len;
const char *s1 = luaL_checklstring(lstate, 1, &s1_len);
- long offset = luaL_checkinteger(lstate, 2);
+ ptrdiff_t offset = luaL_checkinteger(lstate, 2);
if (offset < 0 || offset > (intptr_t)s1_len) {
return luaL_error(lstate, "index out of range");
}
@@ -271,7 +274,7 @@ int nlua_str_byteindex(lua_State *const lstate) FUNC_ATTR_NONNULL_ALL
return luaL_error(lstate, "index out of range");
}
- lua_pushinteger(lstate, (long)byteidx);
+ lua_pushinteger(lstate, (lua_Integer)byteidx);
return 1;
}
@@ -282,10 +285,8 @@ int nlua_regex(lua_State *lstate)
const char *text = luaL_checkstring(lstate, 1);
regprog_T *prog = NULL;
- TRY_WRAP({
- try_start();
- prog = vim_regcomp((char *)text, RE_AUTO | RE_MAGIC | RE_STRICT);
- try_end(&err);
+ TRY_WRAP(&err, {
+ prog = vim_regcomp(text, RE_AUTO | RE_MAGIC | RE_STRICT);
});
if (ERROR_SET(&err)) {
@@ -356,6 +357,9 @@ int nlua_setvar(lua_State *lstate)
Error err = ERROR_INIT;
dictitem_T *di = dict_check_writable(dict, key, del, &err);
if (ERROR_SET(&err)) {
+ nlua_push_errstr(lstate, "%s", err.msg);
+ api_clear_error(&err);
+ lua_error(lstate);
return 0;
}
@@ -391,6 +395,15 @@ int nlua_setvar(lua_State *lstate)
di = tv_dict_item_alloc_len(key.data, key.size);
tv_dict_add(dict, di);
} else {
+ bool type_error = false;
+ if (dict == &vimvardict
+ && !before_set_vvar(key.data, di, &tv, true, watched, &type_error)) {
+ tv_clear(&tv);
+ if (type_error) {
+ return luaL_error(lstate, "Setting v:%s to value with wrong type", key.data);
+ }
+ return 0;
+ }
if (watched) {
tv_copy(&di->di_tv, &oldtv);
}
@@ -452,7 +465,7 @@ static int nlua_stricmp(lua_State *const lstate) FUNC_ATTR_NONNULL_ALL
int ret = 0;
assert(s1[s1_len] == NUL);
assert(s2[s2_len] == NUL);
- do {
+ while (true) {
nul1 = memchr(s1, NUL, s1_len);
nul2 = memchr(s2, NUL, s2_len);
ret = STRICMP(s1, s2);
@@ -476,7 +489,7 @@ static int nlua_stricmp(lua_State *const lstate) FUNC_ATTR_NONNULL_ALL
} else {
break;
}
- } while (true);
+ }
lua_pop(lstate, 2);
lua_pushnumber(lstate, (lua_Number)((ret > 0) - (ret < 0)));
return 1;
@@ -501,7 +514,7 @@ static int nlua_iconv(lua_State *lstate)
const char *str = lua_tolstring(lstate, 1, &str_len);
char *from = enc_canonize(enc_skip((char *)lua_tolstring(lstate, 2, NULL)));
- char *to = enc_canonize(enc_skip((char *)lua_tolstring(lstate, 3, NULL)));
+ char *to = enc_canonize(enc_skip((char *)lua_tolstring(lstate, 3, NULL)));
vimconv_T vimconv;
vimconv.vc_type = CONV_NONE;
@@ -524,6 +537,31 @@ static int nlua_iconv(lua_State *lstate)
return 1;
}
+// Like 'zx' but don't call newFoldLevel()
+static int nlua_foldupdate(lua_State *lstate)
+{
+ curwin->w_foldinvalid = true; // recompute folds
+ foldOpenCursor();
+
+ return 0;
+}
+
+// Access to internal functions. For use in runtime/
+static void nlua_state_add_internal(lua_State *const lstate)
+{
+ // _getvar
+ lua_pushcfunction(lstate, &nlua_getvar);
+ lua_setfield(lstate, -2, "_getvar");
+
+ // _setvar
+ lua_pushcfunction(lstate, &nlua_setvar);
+ lua_setfield(lstate, -2, "_setvar");
+
+ // _updatefolds
+ lua_pushcfunction(lstate, &nlua_foldupdate);
+ lua_setfield(lstate, -2, "_foldupdate");
+}
+
void nlua_state_add_stdlib(lua_State *const lstate, bool is_thread)
{
if (!is_thread) {
@@ -558,14 +596,6 @@ void nlua_state_add_stdlib(lua_State *const lstate, bool is_thread)
lua_setfield(lstate, -2, "__index"); // [meta]
lua_pop(lstate, 1); // don't use metatable now
- // _getvar
- lua_pushcfunction(lstate, &nlua_getvar);
- lua_setfield(lstate, -2, "_getvar");
-
- // _setvar
- lua_pushcfunction(lstate, &nlua_setvar);
- lua_setfield(lstate, -2, "_setvar");
-
// vim.spell
luaopen_spell(lstate);
lua_setfield(lstate, -2, "spell");
@@ -574,6 +604,12 @@ void nlua_state_add_stdlib(lua_State *const lstate, bool is_thread)
// depends on p_ambw, p_emoji
lua_pushcfunction(lstate, &nlua_iconv);
lua_setfield(lstate, -2, "iconv");
+
+ // vim.base64
+ luaopen_base64(lstate);
+ lua_setfield(lstate, -2, "base64");
+
+ nlua_state_add_internal(lstate);
}
// vim.mpack
@@ -589,6 +625,19 @@ void nlua_state_add_stdlib(lua_State *const lstate, bool is_thread)
lua_setfield(lstate, -2, "mpack");
lua_pop(lstate, 3);
+ // vim.lpeg
+ int luaopen_lpeg(lua_State *);
+ luaopen_lpeg(lstate);
+ lua_pushvalue(lstate, -1);
+ lua_setfield(lstate, -4, "lpeg");
+
+ // package.loaded.lpeg = vim.lpeg
+ lua_getglobal(lstate, "package");
+ lua_getfield(lstate, -1, "loaded");
+ lua_pushvalue(lstate, -3);
+ lua_setfield(lstate, -2, "lpeg");
+ lua_pop(lstate, 4);
+
// vim.diff
lua_pushcfunction(lstate, &nlua_xdl_diff);
lua_setfield(lstate, -2, "diff");
@@ -596,6 +645,13 @@ void nlua_state_add_stdlib(lua_State *const lstate, bool is_thread)
// vim.json
lua_cjson_new(lstate);
lua_setfield(lstate, -2, "json");
+
+#ifdef NVIM_VENDOR_BIT
+ // if building with puc lua, use internal fallback for require'bit'
+ int top = lua_gettop(lstate);
+ luaopen_bit(lstate);
+ lua_settop(lstate, top);
+#endif
}
/// like luaL_error, but allow cleanup