lua-users home
lua-l archive

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


Hello Lua list,

I am preparing to use Lua 5.3.3 in environments that include Microsoft
.Net C# (www.microsoft.com/net/) and PowerBASIC (http://powerbasic.com/)
under Microsoft Windows. In this process, I notice that lua_remove() and
other C API functions are defined as macros, and therefore not proper
Lua library entries. Of course, we are duly prepared for this, as
explained in the Lua 5.3 reference manual
(https://www.lua.org/manual/5.3/manual.html#4):

  Even when we use the term "function", any facility in the API may be
  provided as a macro instead.

In practice, such a macro-defined C API "function" cannot really be
called from an environment that is not C/C++ and includes the customary
C language preprocessor. The error reaction that I get is

  GetLastError()=127
  Message: The specified procedure could not be found.

when calling the Windows API GetProcAddress() to find the address of
lua_remove() in the Lua library. (Which was built using the Microsoft
Visual Studio tool with -DLUA_BUILD_AS_DLL.)

For a fix to this problem, we note that lua-5.3.3/src/lua.h contains

  #define lua_remove(L,idx)       (lua_rotate(L, (idx), -1), lua_pop(L, 1))

thus telling us how to re-implement lua_remove() in the environments
where it is missing. (As a side remark, lua_pop() is also a macro,
requiring similar treatment as lua_remove().)

Another solution is to adjust Lua itself: For example, we could replace

  #define lua_remove(L,idx)     (lua_rotate(L, (idx), -1), lua_pop(L, 1))

in lua-5.3.3/src/lua.h by

  #define lua_removeMACRO(L,idx) (lua_rotate(L, (idx), -1), lua_pop(L, 1))
  #if !defined(LUA_REMOVE_AS_ENTRY)
  #define lua_remove(L,idx)     lua_removeMACRO(L, idx)
  #endif

then add

  #if !defined(lua_remove)
  LUA_API void lua_remove (lua_State *L, int idx) {
    lua_removeMACRO(L, idx);
  }
  #endif

to lua-5.3.3/src/lapi.c so that

  #define LUA_REMOVE_AS_ENTRY

in lua-5.3.3/src/luaconf.h causes a suitable library entry for
lua_remove() to be defined.

The problem had actually become apparent at an earlier time, on finding
that lua_pcall() was a macro (lua-5.3.3/src/lua.h):

  #define lua_pcall(L,n,r,f)  lua_pcallk(L, (n), (r), (f), 0, NULL)

But this seemed less of a problem, since the required re-implementation
consisted of supplying constant parameter values only.

I have seached in vain for possible other solutions to this problem: The
missing lua_remove() library entry for recent (5.3.0 and later) Lua
releases has been reported by others, but the solution apparently only
involved re-building the calling (presumably C/C++) program.

Are there, perhaps, other solutions to this problem?

Many thanks and best regards
Thorkil Naur