// 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>
#include <lualib.h>
#include <stdbool.h>
#include <stdint.h>
#include "nvim/api/private/defs.h"
#include "nvim/api/private/helpers.h"
#include "nvim/assert.h"
#include "nvim/func_attr.h"
#include "nvim/memory.h"
// FIXME: vim.h is not actually needed, but otherwise it states MAXPATHL is
// redefined
#include "nvim/ascii.h"
#include "nvim/eval/decode.h"
#include "nvim/eval/typval.h"
#include "nvim/eval/userfunc.h"
#include "nvim/globals.h"
#include "nvim/lib/kvec.h"
#include "nvim/lua/converter.h"
#include "nvim/lua/executor.h"
#include "nvim/macros.h"
#include "nvim/message.h"
#include "nvim/vim.h"
/// Determine, which keys lua table contains
typedef struct {
size_t maxidx; ///< Maximum positive integral value found.
size_t string_keys_num; ///< Number of string keys.
bool has_string_with_nul; ///< True if there is string key with NUL byte.
ObjectType type; ///< If has_type_key is true then attached value. Otherwise
///< either kObjectTypeNil, kObjectTypeDictionary or
///< kObjectTypeArray, depending on other properties.
lua_Number val; ///< If has_val_key and val_type == LUA_TNUMBER: value.
bool has_type_key; ///< True if type key is present.
} LuaTableProps;
#ifdef INCLUDE_GENERATED_DECLARATIONS
# include "lua/converter.c.generated.h"
#endif
#define TYPE_IDX_VALUE true
#define VAL_IDX_VALUE false
#define LUA_PUSH_STATIC_STRING(lstate, s) \
lua_pushlstring(lstate, s, sizeof(s) - 1)
static LuaTableProps nlua_traverse_table(lua_State *const lstate)
FUNC_ATTR_NONNULL_ALL FUNC_ATTR_WARN_UNUSED_RESULT
{
size_t tsize = 0; // Total number of keys.
int val_type = 0; // If has_val_key: lua type of the value.
bool has_val_key = false; // True if val key was found,
// @see nlua_push_val_idx().
size_t other_keys_num = 0; // Number of keys that are not string, integral
// or type keys.
LuaTableProps ret;
CLEAR_FIELD(ret);
if (!lua_checkstack(lstate, lua_gettop(lstate) + 3)) {
semsg(_("E1502: Lua failed to grow stack to %i"), lua_gettop(lstate) + 2);
ret.type = kObjectTypeNil;
return ret;
}
lua_pushnil(lstate);
while (lua_next(lstate, -2)) {
switch (lua_type(lstate, -2)) {
case LUA_TSTRING: {
size_t len;
const char *s = lua_tolstring(lstate, -2, &len);
if (memchr(s, NUL, len) != NULL) {
ret.has_string_with_nul = true;
}
ret.string_keys_num++;
break;
}
case LUA_TNUMBER: {
const lua_Number n = lua_tonumber(lstate, -2);
if (n > (lua_Number)SIZE_MAX || n <= 0
|| ((lua_Number)((size_t)n)) != n) {
other_keys_num++;
} else {
const size_t idx = (size_t)n;
if (idx > ret.maxidx) {
ret.maxidx = idx;
}
}
break;
}
case LUA_TBOOLEAN: {
const bool b = lua_toboolean(lstate, -2);
if (b == TYPE_IDX_VALUE) {
if (lua_type(lstate, -1) == LUA_TNUMBER) {
lua_Number n = lua_tonumber(lstate, -1);
if (n == (lua_Number)kObjectTypeFloat
|| n == (lua_Number)kObjectTypeArray
|| n == (lua_Number)kObjectTypeDictionary) {
ret.has_type_key = true;
ret.type = (ObjectType)n;
} else {
other_keys_num++;
}
} else {
other_keys_num++;
}
} else {
has_val_key = true;
val_type = lua_type(lstate, -1);
if (val_type == LUA_TNUMBER) {
ret.val = lua_tonumber(lstate, -1);
}
}
break;
}
default:
other_keys_num++;
break;
}
tsize++;
lua_pop(lstate, 1);
}
if (ret.has_type_key) {
if (ret.type == kObjectTypeFloat
&& (!has_val_key || val_type != LUA_TNUMBER)) {
ret.type = kObjectTypeNil;
} else if (ret.type == kObjectTypeArray) {
// Determine what is the last number in a *sequence* of keys.
// This condition makes sure that Neovim will not crash when it gets table
// {[vim.type_idx]=vim.types.array, [SIZE_MAX]=1}: without it maxidx will
// be SIZE_MAX, with this condition it should be zero and [SIZE_MAX] key
// should be ignored.
if (ret.maxidx != 0
&& ret.maxidx != (tsize
- ret.has_type_key
- other_keys_num
- has_val_key
- ret.string_keys_num)) {
for (ret.maxidx = 0;; ret.maxidx++) {
lua_rawgeti(lstate, -1
|