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