lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Paul Dixon [SMTP:pauld@yitm.com] wrote:

> After a call to lua_dofile on a script with no body, can I use 
lua_callfunction to select the desired function?

When I was experimenting with Lua, this is exactly what I did.  Any code 
outside of the functions was executed during the lua_dofile(), while the 
functions were simply defined for later reference via lua_callfunction().

Here is the main() driver source from my initial prototype -- the output 
that is generated demonstrates how Lua's global (outside function) 
statements are executed during the lua_dofile() call, but the code inside 
function definitions is not:

#include <stdio.h>
#include "lua.h"

int main(int argc, char* argv[])
{
   int ret;
   lua_Object testFunction;

   ret = lua_dofile("test.lua");
   printf("Back from lua_dofile(), ret = %d\n", ret	);

   printf("Getting TestFunction()\n");
   testFunction = lua_getglobal("TestFunction");
   if (testFunction)
   {
      printf("Going to lua_callfunction(TestFunction)\n");
      ret = lua_callfunction(testFunction);
      printf("Back from lua_callfunction(TestFunction), ret = %d\n", ret);
   }

   return 0;
}

Here is my test Lua script:

--
-- test.lua
--
-- A program written to test Lua and figure out it works.
--

print('Starting global execution space.');

function TestFunction()
   print('--> TestFunction');
   print('<-- TestFunction');
end

print('Back to global execution space.');