Files
neovim/src/nvim/api/private/validate.h
Justin M. Keyes 09b3432eaf fix(api): allow empty Lua table for nested dicts #22268
Problem:
The Lua-API bridge allows Dict params to be empty Lua (list) tables at
the function-signature level. But not for _nested_ Dicts, because they
are not modeled:
fae7540732/src/nvim/api/keysets.lua (L184)
Some API functions like nvim_cmd check for kObjectTypeDictionary and
don't handle the case of empty Lua tables (treated as "Array").

Solution:
Introduce VALIDATE_T_DICT and use it in places where
kObjectTypeDictionary was being checked directly.

fixes #21005
2023-02-16 07:07:18 -08:00

86 lines
2.3 KiB
C

#ifndef NVIM_API_PRIVATE_VALIDATE_H
#define NVIM_API_PRIVATE_VALIDATE_H
#include "nvim/api/private/defs.h"
#include "nvim/api/private/helpers.h"
#define VALIDATE(cond, fmt_, fmt_arg1, code) \
do { \
if (!(cond)) { \
api_set_error(err, kErrorTypeValidation, fmt_, fmt_arg1); \
code; \
} \
} while (0)
#define VALIDATE_INT(cond, name, val_, code) \
do { \
if (!(cond)) { \
api_err_invalid(err, name, NULL, val_, false); \
code; \
} \
} while (0)
#define VALIDATE_S(cond, name, val_, code) \
do { \
if (!(cond)) { \
api_err_invalid(err, name, val_, 0, true); \
code; \
} \
} while (0)
#define VALIDATE_EXP(cond, name, expected, actual, code) \
do { \
if (!(cond)) { \
api_err_exp(err, name, expected, actual); \
code; \
} \
} while (0)
#define VALIDATE_T(name, expected_t, actual_t, code) \
do { \
STATIC_ASSERT(expected_t != kObjectTypeDictionary, "use VALIDATE_T_DICT"); \
if (expected_t != actual_t) { \
api_err_exp(err, name, api_typename(expected_t), api_typename(actual_t)); \
code; \
} \
} while (0)
/// Checks that `obj_` has type `expected_t`.
#define VALIDATE_T2(obj_, expected_t, code) \
do { \
STATIC_ASSERT(expected_t != kObjectTypeDictionary, "use VALIDATE_T_DICT"); \
if ((obj_).type != expected_t) { \
api_err_exp(err, STR(obj_), api_typename(expected_t), api_typename((obj_).type)); \
code; \
} \
} while (0)
/// Checks that `obj_` has Dict type. Also allows empty Array in a Lua context.
#define VALIDATE_T_DICT(name, obj_, code) \
do { \
if ((obj_).type != kObjectTypeDictionary \
&& !(channel_id == LUA_INTERNAL_CALL \
&& (obj_).type == kObjectTypeArray \
&& (obj_).data.array.size == 0)) { \
api_err_exp(err, name, api_typename(kObjectTypeDictionary), api_typename((obj_).type)); \
code; \
} \
} while (0)
#define VALIDATE_RANGE(cond, name, code) \
do { \
if (!(cond)) { \
api_err_invalid(err, name, "out of range", 0, false); \
code; \
} \
} while (0)
#define VALIDATE_R(cond, name, code) \
VALIDATE(cond, "Required: '%s'", name, code);
#ifdef INCLUDE_GENERATED_DECLARATIONS
# include "api/private/validate.h.generated.h"
#endif
#endif // NVIM_API_PRIVATE_VALIDATE_H