Data Structures | Macros | Functions
converter.c File Reference
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
#include <assert.h>
#include <stdint.h>
#include <stdbool.h>
#include "nvim/api/private/defs.h"
#include "nvim/api/private/helpers.h"
#include "nvim/func_attr.h"
#include "nvim/memory.h"
#include "nvim/vim.h"
#include "nvim/globals.h"
#include "nvim/message.h"
#include "nvim/eval/typval.h"
#include "nvim/ascii.h"
#include "nvim/macros.h"
#include "nvim/lib/kvec.h"
#include "nvim/eval/decode.h"
#include "nvim/lua/converter.h"
#include "nvim/lua/executor.h"
#include "nvim/eval/typval_encode.c.h"

Data Structures

struct  LuaTableProps
 Determine, which keys lua table contains. More...
 
struct  TVPopStackItem
 Helper structure for nlua_pop_typval. More...
 
struct  ObjPopStackItem
 Helper structure for nlua_pop_Object. More...
 

Macros

#define TYPE_IDX_VALUE   true
 
#define VAL_IDX_VALUE   false
 
#define LUA_PUSH_STATIC_STRING(lstate, s)   lua_pushlstring(lstate, s, sizeof(s) - 1)
 
#define TYPVAL_ENCODE_ALLOW_SPECIALS   true
 
#define TYPVAL_ENCODE_CONV_NIL(tv)
 
#define TYPVAL_ENCODE_CONV_BOOL(tv, num)   lua_pushboolean(lstate, (bool)(num))
 
#define TYPVAL_ENCODE_CONV_NUMBER(tv, num)   lua_pushnumber(lstate, (lua_Number)(num))
 
#define TYPVAL_ENCODE_CONV_UNSIGNED_NUMBER   TYPVAL_ENCODE_CONV_NUMBER
 
#define TYPVAL_ENCODE_CONV_FLOAT(tv, flt)   TYPVAL_ENCODE_CONV_NUMBER(tv, flt)
 
#define TYPVAL_ENCODE_CONV_STRING(tv, str, len)   lua_pushlstring(lstate, (const char *)(str), (len))
 
#define TYPVAL_ENCODE_CONV_STR_STRING   TYPVAL_ENCODE_CONV_STRING
 
#define TYPVAL_ENCODE_CONV_EXT_STRING(tv, str, len, type)   TYPVAL_ENCODE_CONV_NIL(tv)
 
#define TYPVAL_ENCODE_CONV_FUNC_START(tv, fun)
 
#define TYPVAL_ENCODE_CONV_FUNC_BEFORE_ARGS(tv, len)
 
#define TYPVAL_ENCODE_CONV_FUNC_BEFORE_SELF(tv, len)
 
#define TYPVAL_ENCODE_CONV_FUNC_END(tv)
 
#define TYPVAL_ENCODE_CONV_EMPTY_LIST(tv)   lua_createtable(lstate, 0, 0)
 
#define TYPVAL_ENCODE_CONV_EMPTY_DICT(tv, dict)
 
#define TYPVAL_ENCODE_CONV_LIST_START(tv, len)
 
#define TYPVAL_ENCODE_CONV_REAL_LIST_AFTER_START(tv, mpsv)
 
#define TYPVAL_ENCODE_CONV_LIST_BETWEEN_ITEMS(tv)
 
#define TYPVAL_ENCODE_CONV_LIST_END(tv)   lua_rawset(lstate, -3)
 
#define TYPVAL_ENCODE_CONV_DICT_START(tv, dict, len)
 
#define TYPVAL_ENCODE_SPECIAL_DICT_KEY_CHECK(label, kv_pair)
 
#define TYPVAL_ENCODE_CONV_REAL_DICT_AFTER_START(tv, dict, mpsv)
 
#define TYPVAL_ENCODE_CONV_DICT_AFTER_KEY(tv, dict)
 
#define TYPVAL_ENCODE_CONV_DICT_BETWEEN_ITEMS(tv, dict)   lua_rawset(lstate, -3)
 
#define TYPVAL_ENCODE_CONV_DICT_END(tv, dict)   TYPVAL_ENCODE_CONV_DICT_BETWEEN_ITEMS(tv, dict)
 
#define TYPVAL_ENCODE_CONV_RECURSE(val, conv_type)
 
#define TYPVAL_ENCODE_SCOPE   static
 
#define TYPVAL_ENCODE_NAME   lua
 
#define TYPVAL_ENCODE_FIRST_ARG_TYPE   lua_State *const
 
#define TYPVAL_ENCODE_FIRST_ARG_NAME   lstate
 
#define GENERATE_INDEX_FUNCTION(type)
 
#define ADD_TYPE(type, data_key)
 
#define ADD_REMOTE_TYPE(type)
 
#define GENERATE_INDEX_FUNCTION(type)
 

Functions

bool nlua_pop_typval (lua_State *lstate, typval_T *ret_tv)
 
bool nlua_push_typval (lua_State *lstate, typval_T *const tv, bool special)
 
void nlua_push_String (lua_State *lstate, const String s, bool special) FUNC_ATTR_NONNULL_ALL
 
void nlua_push_Integer (lua_State *lstate, const Integer n, bool special) FUNC_ATTR_NONNULL_ALL
 
void nlua_push_Float (lua_State *lstate, const Float f, bool special) FUNC_ATTR_NONNULL_ALL
 
void nlua_push_Boolean (lua_State *lstate, const Boolean b, bool special) FUNC_ATTR_NONNULL_ALL
 
void nlua_push_Dictionary (lua_State *lstate, const Dictionary dict, bool special) FUNC_ATTR_NONNULL_ALL
 
void nlua_push_Array (lua_State *lstate, const Array array, bool special) FUNC_ATTR_NONNULL_ALL
 
void nlua_push_Object (lua_State *lstate, const Object obj, bool special) FUNC_ATTR_NONNULL_ALL
 
String nlua_pop_String (lua_State *lstate, Error *err) FUNC_ATTR_NONNULL_ALL FUNC_ATTR_WARN_UNUSED_RESULT
 
Integer nlua_pop_Integer (lua_State *lstate, Error *err) FUNC_ATTR_NONNULL_ALL FUNC_ATTR_WARN_UNUSED_RESULT
 
Boolean nlua_pop_Boolean (lua_State *lstate, Error *err) FUNC_ATTR_NONNULL_ALL FUNC_ATTR_WARN_UNUSED_RESULT
 
Float nlua_pop_Float (lua_State *lstate, Error *err) FUNC_ATTR_NONNULL_ALL FUNC_ATTR_WARN_UNUSED_RESULT
 
Array nlua_pop_Array (lua_State *lstate, Error *err) FUNC_ATTR_NONNULL_ALL FUNC_ATTR_WARN_UNUSED_RESULT
 
Dictionary nlua_pop_Dictionary (lua_State *lstate, bool ref, Error *err) FUNC_ATTR_NONNULL_ALL FUNC_ATTR_WARN_UNUSED_RESULT
 
Object nlua_pop_Object (lua_State *const lstate, bool ref, Error *const err)
 
void nlua_init_types (lua_State *const lstate)
 

Macro Definition Documentation

#define ADD_REMOTE_TYPE (   type)
Value:
case kObjectType##type: { \
nlua_push_##type(lstate, (type)obj.data.integer, special); \
break; \
}
err type
Definition: helpers.c:1470
#define ADD_TYPE (   type,
  data_key 
)
Value:
case kObjectType##type: { \
nlua_push_##type(lstate, obj.data.data_key, special); \
break; \
}
err type
Definition: helpers.c:1470
#define GENERATE_INDEX_FUNCTION (   type)
Value:
void nlua_push_##type(lua_State *lstate, const type item, bool special) \
{ \
lua_pushnumber(lstate, (lua_Number)(item)); \
}
int item
Definition: edit.c:216
err type
Definition: helpers.c:1470
#define FUNC_ATTR_NONNULL_ALL
Definition: func_attr.h:243
#define GENERATE_INDEX_FUNCTION (   type)
Value:
type nlua_pop_##type(lua_State *lstate, Error *err) \
{ \
ret = (type)lua_tonumber(lstate, -1); \
lua_pop(lstate, 1); \
}
const int ret
Definition: eval.c:1005
Definition: defs.h:61
#define FUNC_ATTR_WARN_UNUSED_RESULT
Definition: func_attr.h:240
return
Definition: eval.c:15807
err type
Definition: helpers.c:1470
#define FUNC_ATTR_NONNULL_ALL
Definition: func_attr.h:243
#define LUA_PUSH_STATIC_STRING (   lstate,
  s 
)    lua_pushlstring(lstate, s, sizeof(s) - 1)
#define TYPE_IDX_VALUE   true
#define TYPVAL_ENCODE_ALLOW_SPECIALS   true
#define TYPVAL_ENCODE_CONV_BOOL (   tv,
  num 
)    lua_pushboolean(lstate, (bool)(num))
#define TYPVAL_ENCODE_CONV_DICT_AFTER_KEY (   tv,
  dict 
)
#define TYPVAL_ENCODE_CONV_DICT_BETWEEN_ITEMS (   tv,
  dict 
)    lua_rawset(lstate, -3)
#define TYPVAL_ENCODE_CONV_DICT_END (   tv,
  dict 
)    TYPVAL_ENCODE_CONV_DICT_BETWEEN_ITEMS(tv, dict)
#define TYPVAL_ENCODE_CONV_DICT_START (   tv,
  dict,
  len 
)
Value:
do { \
if (!lua_checkstack(lstate, lua_gettop(lstate) + 3)) { \
emsgf(_("E5102: Lua failed to grow stack to %i"), \
lua_gettop(lstate) + 3); \
return false; \
} \
lua_createtable(lstate, 0, (int)(len)); \
} while (0)
#define _(x)
Definition: gettext.h:20
if(len)
Definition: encode.c:222
int len
Definition: helpers.c:1461
bool emsgf(const char *const fmt,...)
Print an error message with unknown number of arguments.
return
Definition: eval.c:15807
#define TYPVAL_ENCODE_CONV_EMPTY_DICT (   tv,
  dict 
)
Value:
do { \
if (typval_conv_special) { \
nlua_create_typed_table(lstate, 0, 0, kObjectTypeDictionary); \
} else { \
lua_createtable(lstate, 0, 0); \
} \
} while (0)
Definition: defs.h:106
if(len)
Definition: encode.c:222
#define TYPVAL_ENCODE_CONV_EMPTY_LIST (   tv)    lua_createtable(lstate, 0, 0)
#define TYPVAL_ENCODE_CONV_EXT_STRING (   tv,
  str,
  len,
  type 
)    TYPVAL_ENCODE_CONV_NIL(tv)
#define TYPVAL_ENCODE_CONV_FLOAT (   tv,
  flt 
)    TYPVAL_ENCODE_CONV_NUMBER(tv, flt)
#define TYPVAL_ENCODE_CONV_FUNC_BEFORE_ARGS (   tv,
  len 
)
#define TYPVAL_ENCODE_CONV_FUNC_BEFORE_SELF (   tv,
  len 
)
#define TYPVAL_ENCODE_CONV_FUNC_END (   tv)
#define TYPVAL_ENCODE_CONV_FUNC_START (   tv,
  fun 
)
Value:
do { \
goto typval_encode_stop_converting_one_item; \
} while (0)
typval_T tv
Definition: typval.c:1544
#define TYPVAL_ENCODE_CONV_NIL(tv)
Definition: converter.c:421
#define TYPVAL_ENCODE_CONV_LIST_BETWEEN_ITEMS (   tv)
Value:
do { \
lua_Number idx = lua_tonumber(lstate, -2); \
lua_rawset(lstate, -3); \
lua_pushnumber(lstate, idx + 1); \
} while (0)
#define TYPVAL_ENCODE_CONV_LIST_END (   tv)    lua_rawset(lstate, -3)
#define TYPVAL_ENCODE_CONV_LIST_START (   tv,
  len 
)
Value:
do { \
if (!lua_checkstack(lstate, lua_gettop(lstate) + 3)) { \
emsgf(_("E5102: Lua failed to grow stack to %i"), \
lua_gettop(lstate) + 3); \
return false; \
} \
lua_createtable(lstate, (int)(len), 0); \
lua_pushnumber(lstate, 1); \
} while (0)
#define _(x)
Definition: gettext.h:20
if(len)
Definition: encode.c:222
int len
Definition: helpers.c:1461
bool emsgf(const char *const fmt,...)
Print an error message with unknown number of arguments.
return
Definition: eval.c:15807
#define TYPVAL_ENCODE_CONV_NIL (   tv)
Value:
do { \
if (typval_conv_special) { \
lua_pushnil(lstate); \
} else { \
nlua_pushref(lstate, nlua_nil_ref); \
} \
} while (0)
if(len)
Definition: encode.c:222
void nlua_pushref(lua_State *lstate, LuaRef ref)
push a value referenced in the regirstry
Definition: executor.c:704
#define TYPVAL_ENCODE_CONV_NUMBER (   tv,
  num 
)    lua_pushnumber(lstate, (lua_Number)(num))
#define TYPVAL_ENCODE_CONV_REAL_DICT_AFTER_START (   tv,
  dict,
  mpsv 
)
#define TYPVAL_ENCODE_CONV_REAL_LIST_AFTER_START (   tv,
  mpsv 
)
#define TYPVAL_ENCODE_CONV_RECURSE (   val,
  conv_type 
)
Value:
do { \
for (size_t backref = kv_size(*mpstack); backref; backref--) { \
const MPConvStackVal mpval = kv_A(*mpstack, backref - 1); \
if (mpval.type == conv_type) { \
if (conv_type == kMPConvDict \
? (void *)mpval.data.d.dict == (void *)(val) \
: (void *)mpval.data.l.list == (void *)(val)) { \
lua_pushvalue(lstate, \
-((int)((kv_size(*mpstack) - backref + 1) * 2))); \
break; \
} \
} \
} \
} while (0)
if(len)
Definition: encode.c:222
#define kv_size(v)
Definition: kvec.h:68
Structure representing current VimL to messagepack conversion state.
Definition: typval_encode.h:34
for(size_t i=1;i< ARRAY_SIZE(argv);i++)
Definition: typval.c:1217
#define kv_A(v, i)
Definition: kvec.h:66
Convert dict_T *dictionary.
Definition: typval_encode.h:19
#define TYPVAL_ENCODE_CONV_STR_STRING   TYPVAL_ENCODE_CONV_STRING
#define TYPVAL_ENCODE_CONV_STRING (   tv,
  str,
  len 
)    lua_pushlstring(lstate, (const char *)(str), (len))
#define TYPVAL_ENCODE_CONV_UNSIGNED_NUMBER   TYPVAL_ENCODE_CONV_NUMBER
#define TYPVAL_ENCODE_FIRST_ARG_NAME   lstate
#define TYPVAL_ENCODE_FIRST_ARG_TYPE   lua_State *const
#define TYPVAL_ENCODE_NAME   lua
#define TYPVAL_ENCODE_SCOPE   static
#define TYPVAL_ENCODE_SPECIAL_DICT_KEY_CHECK (   label,
  kv_pair 
)
#define VAL_IDX_VALUE   false

Function Documentation

void nlua_init_types ( lua_State *const  lstate)

Record some auxiliary values in vim module

Assumes that module table is on top of the stack.

Recorded values:

vim.type_idx:

See also
nlua_push_type_idx() vim.val_idx:
nlua_push_val_idx() vim.types: table mapping possible values of vim.type_idx to string names (i.e. array, float, dictionary) and back.
Array nlua_pop_Array ( lua_State *  lstate,
Error err 
)

Convert lua table to array

Always pops one value from the stack.

Boolean nlua_pop_Boolean ( lua_State *  lstate,
Error err 
)

Convert lua value to boolean

Always pops one value from the stack.

Dictionary nlua_pop_Dictionary ( lua_State *  lstate,
bool  ref,
Error err 
)

Convert lua table to dictionary

Always pops one value from the stack.

Float nlua_pop_Float ( lua_State *  lstate,
Error err 
)

Convert lua table to float

Always pops one value from the stack.

Integer nlua_pop_Integer ( lua_State *  lstate,
Error err 
)

Convert lua value to integer

Always pops one value from the stack.

Object nlua_pop_Object ( lua_State *const  lstate,
bool  ref,
Error *const  err 
)

Convert lua table to object

Always pops one value from the stack.

String nlua_pop_String ( lua_State *  lstate,
Error err 
)

Convert lua value to string

Always pops one value from the stack.

bool nlua_pop_typval ( lua_State *  lstate,
typval_T ret_tv 
)

Convert lua object to VimL typval_T

Should pop exactly one value from lua stack.

Parameters
lstateLua state.
[out]ret_tvWhere to put the result.
Returns
true in case of success, false in case of failure. Error is reported automatically.
void nlua_push_Array ( lua_State *  lstate,
const Array  array,
bool  special 
)

Convert given Array to lua table

Leaves converted table on top of the stack.

void nlua_push_Boolean ( lua_State *  lstate,
const Boolean  b,
bool  special 
)

Convert given Float to lua boolean

Leaves converted value on top of the stack.

void nlua_push_Dictionary ( lua_State *  lstate,
const Dictionary  dict,
bool  special 
)

Convert given Dictionary to lua table

Leaves converted table on top of the stack.

void nlua_push_Float ( lua_State *  lstate,
const Float  f,
bool  special 
)

Convert given Float to lua table

Leaves converted table on top of the stack.

void nlua_push_Integer ( lua_State *  lstate,
const Integer  n,
bool  special 
)

Convert given Integer to lua number

Leaves converted number on top of the stack.

void nlua_push_Object ( lua_State *  lstate,
const Object  obj,
bool  special 
)

Convert given Object to lua value

Leaves converted value on top of the stack.

void nlua_push_String ( lua_State *  lstate,
const String  s,
bool  special 
)

Convert given String to lua string

Leaves converted string on top of the stack.

bool nlua_push_typval ( lua_State *  lstate,
typval_T *const  tv,
bool  special 
)

Convert VimL typval_T to lua value

Should leave single value in lua stack. May only fail if lua failed to grow stack.

Parameters
lstateLua interpreter state.
[in]tvtypval_T to convert.
Returns
true in case of success, false otherwise.