lua-users home
lua-l archive

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


The C-file below registers a library "app" containing
one function which does nothing but calls luaL_argerror.
Then an embedded Lua script is executed, which calls that
library function.

The output is:
 [string " local Var = 0 local function main()   app...."]:1:
 bad argument #1 to 'Var' (unknown event name)

When it should be (IMHO):
 [string " local Var = 0 local function main()   app...."]:1:
 bad argument #1 to 'RegisterEvent' (unknown event name)

Could somebody explain that?
Thank you.
--
Shmuel


/* test1.c */
#include <stdio.h>

#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"

const char* script =
  " local Var = 0"
  " local function main()"
  "   app.RegisterEvent(function() V = Var end)"
  " end"
  " main()";

static int iRegisterEvent(lua_State* L)
{
  return luaL_argerror(L, 1, "unknown event name");
}

static const struct luaL_reg MainLib[] = {
  { "RegisterEvent",  iRegisterEvent },
  { NULL, NULL }
};

int main (void)
{
  lua_State* L = lua_open();
  luaL_register(L, "app", MainLib);
  lua_pop(L,1);

  if(luaL_loadstring (L, script) || lua_pcall(L, 0, 0, 0))
    printf("%s\n", lua_tostring(L, -1));

  lua_close(L);
  return 0;
}
/* end of test1.c */