Search code examples
clualua-api

What's the difference between "dofile" in lua and "luaL_dofile" in the C API?


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.


Solution

  • 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.