lua-users home
lua-l archive

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


I’m in the process of developing an automated test harness for a product we are developing.  The test harness will use a combination of Lua and C.   One of the goals is to “hide” all the details so that when the software developers want to test their code modules, they can write script files which are used as input to the test harness.

 

I’ve been  having some issues calling C functions from Lua 5.2

 

Here’s a portion of the C file (and ignore the fact that it is just returning dummy values):

 

#include <stdio.h>

 

/* include Lua libraries */

 

#include "lua.h"

#include "lauxlib.h"

#include "lualib.h"

 

static int create_message(lua_State *L)

{

int dest;

int payload;

int numops;

int msg_ID;

 

  /* get number of arguments */

 

  numops = lua_gettop(L);

 

  /* get destination ID */

 

  dest = lua_tonumber(L,1);

 

  /* get payload */

 

  payload = lua_tonumber(L,2);

 

  /* call function to generate message ID */

 

  msg_ID = payload * 10;

 

  /* push message ID onto stack */

  lua_pushnumber(L,msg_ID);

 

  /* return the number of results */

 

  return 1;

}

 

/* table of functions accessible from Lua */

 

static const struct luaL_Reg testHarness [] = {

  {"buildMessage", create_message},

  {NULL,NULL}

};

 

int luaopen_testHarness(lua_State *L)

{

  luaL_newlib (L, testHarness);   /* register C functions with Lua */

  return 1;

}

 

I compiled the C file and created a Linux dynamic library named “testHarness.so”.  Here’s a simple Lua file (named “lua_test.lua”) that tests the Lua to C interface:

 

#!/usr/local/bin/lua

 

-- Test harness script file

 

require("testHarness") -- link in testHarness C library

 

-- call C routine

 

message_ID = buildMessage(10, 20)

print("Returned message_ID = " .. message_ID .. "\n")

 

When I run run lua_test.lua from the Linux command line, I get an error message that says “attempt to call global ‘buildMessage’ (a nil value)”.

 

If I rewrite the require statement as:

 

th = require(“testHarness”)

 

and change the function call to:

 

message_ID = th.buildMessage(10,20)

 

everything then works fine.  No error occurs and the print statement is executed.

 

Is there something I can do so that I don’t need to have the “th.” In front of the call to the buildMessage function in the C library?  Eventually, I plan to take the function call out of here and pass in an input file name as a command line parameter.  The input file will be the script file written by the software developers and it will simply contain a series of buildMessage() calls and calls to other soon-to-be-developed functions.

 

For simplicity, I would prefer that the developers not have to put “th.” In front of every function call.  I’ve Googled for answers but haven’t really found any.  One webpage I found had an example that seemed to do what I wanted, but it was written in Lua 5.1 and used some deprecated functions.  Any suggestions to solve this issue would be appreciated.

 

Thanks,

 

Jim Urso