4

I am embedding a Lua interpreter in a C++ application.

I want to setup an 'environment' for running scripts, such that certain variables are made available to all scripts.

For example, I want to expose READ ONLY objects Foo and FooBar to scripts, such that Foo and FooBar are available to all running scripts.

Does anyone know how I can do this?. A snippet to show how to so this would be very useful.

2 Answers 2

2

I haven't heard of read-only variables in Lua but you can prevent modification by making the environment available via a function call instead.

If the C++ application is large, you will probably want to use a tool to generate an interface you can call from Lua. I have used tolua++ in the past with some luck:

Suppose demo.hpp is a header file of the C++ application:

#ifndef SO_DEMO_HPP
#define SO_DEMO_HPP

namespace demo
{
    class Foo
    {
        double x;

    public:
        Foo(double x) : x(x) {}
        double getX() const { return x; }
    };

    Foo* getFoo();
}

#endif

Implement demo::getFoo() in demo.cpp.

demo.pkg lists the things that should be callable from Lua:

$#include "demo.hpp"

namespace demo
{
    class Foo
    {
        double getX() const;
    };

    Foo* getFoo();
}

Generate demo_stub.cpp and demo_stub.hpp files containing our Lua module:

$ tolua++5.1 -o demo_stub.cpp -H demo_stub.hpp demo.pkg

main.cpp is a Lua interpreter that loads the demo module:

#include "demo.hpp"

extern "C" {
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
#include <tolua++.h>
}
#include "demo_stub.hpp"

int main()
{
    lua_State *L = lua_open();

    luaL_openlibs(L);
    tolua_demo_open(L);

    if (luaL_dofile(L, NULL) != 0)
        fprintf(stderr, "%s\n", lua_tostring(L, -1));

    lua_close(L);
    return 0;
}

The tolua_demo_open() function was generated by tolua++ and is declared in demo_stub.hpp.

Build a Lua interpreter named demo:

$ g++ -I/usr/include/lua5.1 demo.cpp demo_stub.cpp main.cpp -ltolua++5.1 -llua5.1 -o demo

Construct a demo.lua script

print("Hello, world!")
print(tolua.type(demo.getFoo()))
print(demo.getFoo():getX())

and feed the script to the interpreter:

$ ./demo < demo.lua
Hello, world!
demo::Foo
42
Sign up to request clarification or add additional context in comments.

1 Comment

@autonakos: Thanks for the code. It will definitely help set me on the right path (I still need to experiment some more).
0

lua_setglobal.

As for 'read only', do you mean that that Foo will not be overwritable (e.g. Foo = 10), or that the value Foo reverse will be immutable (e.g. Foo.x = 10)?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.