Skip to content

Commit 357e459

Browse files
author
BirdeeHub
committed
fix(headings): tricky case of lua table arrays
#21
1 parent f8fd38d commit 357e459

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

src/decode.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ static bool heading_nav(lua_State *L, int keys_len, bool array_type) {
7777
if (keys_len <= 0) return set_tmlerr(new_tmlerr(L, DECODE_DEFINED_IDX), false, 28, "no keys provided to navigate");
7878
int keys_start = absindex(lua_gettop(L), -keys_len);
7979
lua_pushvalue(L, DECODE_RESULT_IDX);
80+
bool is_new = false;
8081
for (int key_idx = keys_start; key_idx < keys_start + keys_len; key_idx++) {
8182
int parent_idx = lua_gettop(L);
8283
lua_pushvalue(L, key_idx);
@@ -88,6 +89,7 @@ static bool heading_nav(lua_State *L, int keys_len, bool array_type) {
8889
lua_pushvalue(L, key_idx);
8990
lua_pushvalue(L, -2);
9091
lua_rawset(L, parent_idx); // t[key] = new table
92+
is_new = true;
9193
} else if (vtype != LUA_TTABLE) {
9294
TMLErr *err = new_tmlerr(L, DECODE_DEFINED_IDX);
9395
set_tmlerr(err, false, 44, "cannot navigate through non-table! Key was: ");
@@ -101,7 +103,7 @@ static bool heading_nav(lua_State *L, int keys_len, bool array_type) {
101103
lua_Integer len = lua_tointeger(L, -1);
102104
lua_pop(L, 1);
103105
if (key_idx == keys_start + keys_len - 1) {
104-
if (len >= 0 && array_type) {
106+
if ((len > 0 || is_new) && array_type) {
105107
len++;
106108
parent_idx = lua_gettop(L);
107109
lua_pushvalue(L, parent_idx);
@@ -111,7 +113,7 @@ static bool heading_nav(lua_State *L, int keys_len, bool array_type) {
111113
lua_pushvalue(L, -1);
112114
lua_rawseti(L, parent_idx, len);
113115
lua_remove(L, parent_idx); // remove parent table, keep child on top
114-
} else if (len != 0) {
116+
} else if (!is_new) {
115117
TMLErr *err = new_tmlerr(L, DECODE_DEFINED_IDX);
116118
set_tmlerr(err, false, 32, "table already defined! Key was: ");
117119
return err_push_keys(L, err, keys_start, keys_start + keys_len - 1);

tests/decode_tests.lua

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,14 @@ key = 2
132132
]]
133133
local data, err = tomlua_default.decode(toml_str)
134134
it(err ~= nil, "should error on duplicate table")
135+
toml_str = [=[
136+
[fruit.oops]
137+
oops = "hmmmm"
138+
[[fruit]]
139+
name = "banana"
140+
]=]
141+
data, err = tomlua_default.decode(toml_str)
142+
it(err ~= nil, "should error on duplicate table defined in a tricky way with headings")
135143
end)
136144

137145

tomlua-scm-1.rockspec

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,17 @@ build = {
2727
LUADIR="$(LUADIR)",
2828
},
2929
}
30+
-- -- How do I specify CFLAGS and make them compile in 1 command?
31+
-- build = {
32+
-- type = "builtin",
33+
-- modules = {
34+
-- tomlua = {
35+
-- "./src/tomlua.c",
36+
-- "./src/decode_str.c",
37+
-- "./src/decode_inline_value.c",
38+
-- "./src/decode.c",
39+
-- "./src/encode.c",
40+
-- "./src/dates.c",
41+
-- },
42+
-- }
43+
-- }

0 commit comments

Comments
 (0)