2

Using the Lua 5.3.4 C API, this works:

luaL_dostring(lua, "dofile('mnemonics.lua')");

But this fails to execute the file:

luaL_dofile(lua, "mnemonics.lua");

Instead, it reports "attempt to call a string value".

When I replace it with

luaL_loadfile(lua, "mnemonics.lua");

it returns LUA_OK, but it doesn't push a function on the stack like it's supposed to. In fact, the stack is empty (gettop reports 0) afterwards.

I can use the dostring workaround, but either I'm missing something, or there's a bug.

greatwolf
  • 20,287
  • 13
  • 71
  • 105
Octa9on
  • 183
  • 8
  • 3
    This is really weird. A wild guess: something that happened earlier in your C code corrupted your `lua` state? Try running the program with Valgrind. – Hisham H M Mar 01 '17 at 22:47
  • 6
    Can you show us a minimal example that reproduces the problem you're seeing because I can't. – greatwolf Mar 02 '17 at 05:09
  • In searching for a minimal example I found that the problem was caused by trying to pop an empty stack. I had misunderstood the behavior of a call to `lua_settable`. I believe the reason the Lua command worked when the C API command didn't is that the stack gets cleaned up at the transition between C code and Lua code. – Octa9on Mar 02 '17 at 19:33

1 Answers1

0

Popping from an empty stack will cause unexpected behavior. The Lua interpreter cleans up the stack between executing C code and executing Lua code, so the messed up stack didn't affect the dofile in Lua.

Octa9on
  • 183
  • 8