Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do I extend Lua with a static c++ library?

Tags:

c++

lua

I have a Visual Studio 2008 C++03 application that uses Lua 5.2.1. I would like to extend Lua with a module called "foo", but when I call require("foo") in my Lua script, I get the error:

foo_test.lua:1: module 'foo' not found:
    no field package.preload['process']
    no file '!\lua\process.lua'
    no file '!\lua\process\init.lua'
    no file '!\process.lua'
    no file '!\process\

My Lua script:

foo.bar()

My lua_foo.h file:

#include <lua.h>
extern "C" int luaopen_foo( lua_State* L );

My lua_foo.cpp file:

#include "lua_foo.h"
#include <lua.hpp>

static int l_bar( lua_State *L )
{
    puts( "in bar()" );
    return 1;
}

int luaopen_foo( lua_State *L ) 
{
    static const luaL_Reg foo[] = {
        { "bar", l_bar },
        { NULL, NULL }
    };

    luaL_newlib( L, foo );
    return 1;
}

These are compiled in to a static library "lua_foo.lib" which is statically linked to my main Lua executable.

Can anybody help me understand where I'm going wrong? thanks. I would prefer to avoid c++ wrappers (for now) and I do not want to package this library as a separate DLL from the main Lua engine.


EDIT

The issue was in the lua engine code. I added the luaL_requiref per @NicolBolas 's suggestion.

lua_State* L = luaL_newstate();
if( NULL != L )
{
    luaL_openlibs( L );
    luaL_requiref( token.get(), "foo", luaopen_foo, 1 );
    luaL_dofile( L, "foo_test.lua" );
    lua_close( L );
}
like image 655
PaulH Avatar asked Aug 21 '12 15:08

PaulH


2 Answers

It's important to understand how the require machinery works and therefore why your code doesn't.

require is designed to look for Lua scripts in the file system and DLLs. Static libraries are not DLLs; indeed, as far as C/C++ is concerned, once you've finished linking, static libraries are no different than compiling those .c/.cpp files into your application directly.

When require finds a DLL with the appropriate name, it loads it and attempts to find a function named luaopen_<modname>, where <modname> is the name of the module. When it does, it will execute this function and store the value it returns in an internal database of loaded modules.

Calling require for a module will return whatever this function returned; if the module has already been loaded, then the return value is pulled from the database and returned directly.

Simply calling luaopen_foo will not do any of this. Indeed, simply calling this function is a bad idea; it is a Lua function and needs to be called as a Lua function (ie: you need to push it onto the Lua stack with lua_pushcfunction and call it with lua_call and so forth).

If you want to create a local module (one not in a Lua script or DLL, but exposed from your code), then you need to use the Lua facilities to do that. Specifically, use luaL_requiref:

luaL_requiref(L, "foo", luaopen_foo, 0);

Call this instead of calling luaopen_foo directly. This will automatically register the return value from luaopen_foo with require's internal database of loaded modules. Thus, subsequent calls to require "foo" will return this table.

One more thing: do is a keyword in Lua; you should not use keywords for Lua table key names. You can, but you always have to quote them (ie: your script must do foo["do"](...) to call it).

like image 126
Nicol Bolas Avatar answered Sep 21 '22 00:09

Nicol Bolas


  • luaopen_foo creates a table with one function in it, but it doesn't expose it to Lua in any way. You need to assign it to something your scripts can access if you want to access it. You can do this with the package mechanism, or just assign it to a global (which is what Lua's built-in libraries do).
  • You have a field named do, which is problematic if you want to use foo.do syntax, because do is a keyword.
  • The return value of a Lua function tells Lua how many values you left on the stack. Your l_do function lies with its return value.
  • In the case of luaopen_foo, since you're calling it directly and ignoring it's return value, there's no need for it to return anything at all.

Change your code to this:

static int l_bar( lua_State *L )
{
   puts("l_bar called.");
   return 0;
}

void luaopen_foo( lua_State *L ) 
{
   static const struct luaL_Reg foo[] = {
      { "bar", l_bar },
      { NULL, NULL }
   };
   luaL_newlib( L, foo );   // create table containing `bar`
   lua_setglobal(L, "foo"); // assign that table to global `foo`
}

And change your script to this:

foo.bar()
like image 36
Mud Avatar answered Sep 20 '22 00:09

Mud