diff --git a/LUA_API.rst b/LUA_API.rst index 1a38c4edc..050714c72 100644 --- a/LUA_API.rst +++ b/LUA_API.rst @@ -209,7 +209,7 @@ Implemented features: * ``ref:insert(index,item)`` Inserts a new item at the specified index. To add at the end, - use ``#ref`` as index. + use ``#ref``, or just ``'#'`` as index. * ``ref:erase(index)`` diff --git a/Lua API.html b/Lua API.html index d5d849d91..cbf35f0eb 100644 --- a/Lua API.html +++ b/Lua API.html @@ -516,7 +516,7 @@ possible.

  • ref:insert(index,item)

    Inserts a new item at the specified index. To add at the end, -use #ref as index.

    +use #ref, or just '#' as index.

  • ref:erase(index)

    Removes the element at the given valid index.

    diff --git a/library/LuaTypes.cpp b/library/LuaTypes.cpp index c5ffe0f6b..2cfe8a84e 100644 --- a/library/LuaTypes.cpp +++ b/library/LuaTypes.cpp @@ -733,8 +733,18 @@ static int lookup_container_field(lua_State *state, int field, const char *mode * Index verification: number and in range. */ static int check_container_index(lua_State *state, int len, - int fidx, int iidx, const char *mode) + int fidx, int iidx, const char *mode, + bool is_insert = false) { + if (is_insert && len >= 0) + { + if (lua_type(state, iidx) == LUA_TSTRING + && strcmp(lua_tostring(state, iidx), "#") == 0) + return len; + + len++; + } + if (!lua_isnumber(state, iidx)) field_error(state, fidx, "invalid index", mode); @@ -867,8 +877,7 @@ static int method_container_insert(lua_State *state) auto id = (container_identity*)lua_touserdata(state, UPVAL_CONTAINER_ID); int len = id->lua_item_count(state, ptr, container_identity::COUNT_LEN); - if (len >= 0) len++; - int idx = check_container_index(state, len, UPVAL_METHOD_NAME, 2, "call"); + int idx = check_container_index(state, len, UPVAL_METHOD_NAME, 2, "call", true); if (!id->lua_insert(state, UPVAL_METHOD_NAME, ptr, idx, 3)) field_error(state, UPVAL_METHOD_NAME, "not supported", "call");