Skip to content

Commit

Permalink
Make numerical keys an option for set/init global
Browse files Browse the repository at this point in the history
  • Loading branch information
CannibalVox committed Nov 2, 2019
1 parent f669b86 commit d25748d
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 5 deletions.
23 changes: 18 additions & 5 deletions go_luainterface.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,19 @@ lua_err *create_walk_error(const char *fullPath, const char *path, const char *e
return create_lua_error(fullError);
}

void push_walk_key(lua_State *_L, const char *path, int segLen) {
char *outPath;
char *expectedFinishPath = (char*)(path+segLen);

int result = strtol(path, &outPath, 10);
if (result != 0 || errno == 0 && expectedFinishPath == outPath) {
lua_pushinteger(_L, result);
return;
}

lua_pushlstring(_L, path, (size_t)segLen);
}

lua_result walk_next_segment(lua_State *_L, int depth, const char *fullPath, const char *path, last_segment_handler handler, _Bool fillIntermediateTables) {
lua_result retVal = {};
if (lua_isnil(_L, -1)) {
Expand All @@ -62,20 +75,20 @@ lua_result walk_next_segment(lua_State *_L, int depth, const char *fullPath, con
}

if (path[segLen] == '.') {
lua_pushlstring(_L, path, (size_t)segLen);
push_walk_key(_L, path, segLen);
lua_gettable(_L, -2);
if (fillIntermediateTables && lua_isnil(_L, -1)) {
//Remove nil from the stack
lua_pop(_L, 1);

//Push key & new table
lua_pushlstring(_L, path, (size_t)segLen);
push_walk_key(_L, path, segLen);
lua_newtable(_L);
//Put new table into old one
lua_settable(_L, -3);

//Retry get now that the new table is in there
lua_pushlstring(_L, path, (size_t)segLen);
push_walk_key(_L, path, segLen);
lua_gettable(_L, -2);
}
retVal = walk_next_segment(_L, depth+1, fullPath, path+segLen+1, handler, fillIntermediateTables);
Expand All @@ -94,7 +107,7 @@ lua_result walk_table_path(lua_State *_L, int valueIndex, const char *path, last
}

lua_result get_global_handler(lua_State *_L, int depth, const char *fullPath, const char *path) {
lua_pushlstring(_L, path, strlen(path));
push_walk_key(_L, path, strlen(path));
lua_gettable(_L, -2);
return convert_stack_value(_L);
}
Expand All @@ -105,7 +118,7 @@ lua_result get_global(lua_State *_L, const char *path, _Bool fillIntermediateTab
}

lua_result set_global_handler(lua_State *_L, int depth, const char *fullPath, const char *path) {
lua_pushlstring(_L, path, strlen(path));
push_walk_key(_L, path, strlen(path));
lua_pushvalue(_L, -2-depth);
lua_settable(_L, -3);
lua_result res = {};
Expand Down
1 change: 1 addition & 0 deletions go_luajit.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <lauxlib.h>
#include <lualib.h>
#include <string.h>
#include <errno.h>

#define MT_GOCALLBACK "GO_CALLBACK"

Expand Down
31 changes: 31 additions & 0 deletions lua_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,37 @@ func TestInitGlobal(t *testing.T) {
require.Equal(t, 0, outlyingAllocs())
}

func TestInitGlobalNumIndex(t *testing.T) {
clearAllocs()
vm := NewState()
defer closeVM(t, vm)

err := vm.InitGlobal("test.test2.1", "This ")
require.Nil(t, err)

err = vm.SetGlobal("test.test2.2", "is")
require.Nil(t, err)

err = vm.SetGlobal("test.test2.3", " good")
require.Nil(t, err)

err = vm.DoString("function getVal() return table.concat(test.test2) end")
require.Nil(t, err)

funcObj, err := vm.GetGlobal("getVal")
require.Nil(t, err)

f := funcObj.(*LocalLuaFunction)
outVal, err := f.Call()
require.Nil(t, err)
require.Len(t, outVal, 1)
require.Equal(t, "This is good", outVal[0])
err = f.Close()
require.Nil(t, err)

require.Equal(t, 0, outlyingAllocs())
}

const fibo string = `
function fib(val)
if val < 2 then
Expand Down

0 comments on commit d25748d

Please sign in to comment.