这样注册后,我们可以在Lua中使用test, test.detail,和test.utility。这里的引入的endNamespace函数,也会返回一个对象(实质也是table),该对象实质是上一层namespace,表示当前namespace注册完成。 All LuaBridge functions which create registrations return an object upon which subsequent registrations can be made,allowing for an unlimited number of registrations to be chained together using the dot operator。在一个namespace中,注册相同命名的对象,对于LuaBridge来说是没有定义的行为。一个namespace可以多次使用增加更多的成员。比如下面两段代码是等价的:
<span style="font-size:14px;">    getGlobalNamespace (L)
.beginNamespace ("test")
.addFunction ("foo", foo)
.endNamespace ();
getGlobalNamespace (L)
.beginNamespace ("test")
.addFunction ("bar", bar)
.endNamespace ();</span>
  和
<span style="font-size:14px;">    getGlobalNamespace (L)
.beginNamespace ("test")
.addFunction ("foo", foo)
.addFunction ("bar", bar)
.endNamespace ();</span>
Data, Properties, Functions, and CFunctions
  Data, Properties, Functions, and CFunctions可以依次使用addVariable,, addProperty, addFunction, and addCFunction来注册。在Lua脚本中调用注册的函数时,LuaBridge会自动地传入相应的参数,并对参数类型转和检查。同样,函数的返回值也会自动处理。当前LuaBridge多可处理8个参数。Pointers, references, and objectsof class type as parameters are treated specially。如果我们在C++中有以下定义:
<span style="font-size:14px;">    int globalVar;
static float staticVar;
std::string stringProperty;
std::string getString () { return stringProperty; }
void setString (std::string s) { stringProperty = s; }
int foo () { return 42; }
void bar (char const*) { }
int cFunc (lua_State* L) { return 0; }</span>
  为了在Lua使用这些变量和函数,我们可以按以下方式注册它们:
<span style="font-size:14px;">    getGlobalNamespace (L)
.beginNamespace ("test")
.addVariable ("var1", &globalVar)
.addVariable ("var2", &staticVar, false)     // read-only
.addProperty ("prop1", getString, setString)
.addProperty ("prop2", getString)            // read only
.addFunction ("foo", foo)
.addFunction ("bar", bar)
.addCFunction ("cfunc", cFunc)
.endNamespace ();
</span>
  Variables在注册时,可以通过传递第二个参数为false,确保Variables不会在Lua被修改,默认第二个参数是true。Properties在注册时,若不传递set函数,则在脚本中是read-only。
  通过上面注册后,则下面表达式在Lua是有效的:
  <span style="font-size:14px;">    test        -- a namespace,实质是一个table,下面都是table中的成员
test.var1   -- a lua_Number variable
test.var2   -- a read-only lua_Number variable
test.prop1  -- a lua_String property
test.prop2  -- a read-only lua_String property
test.foo    -- a function returning a lua_Number
test.bar    -- a function taking a lua_String as a parameter
test.cfunc  -- a function with a variable argument list and multi-return</span>
  注意test.prop1和test.prop2引用的C++中同一个变量,然后test.prop2是read-only,因此在脚本中对test.prop2赋值,会导致运行时错误(run-time error)。在Lua按以下方式使用:
<span style="font-size:14px;">    test.var1 = 5         -- okay
test.var2 = 6         -- error: var2 is not writable
test.prop1 = "Hello"  -- okay
test.prop1 = 68       -- okay, Lua converts the number to a string.
test.prop2 = "bar"    -- error: prop2 is not writable
test.foo ()           -- calls foo and discards the return value
test.var1 = foo ()    -- calls foo and stores the result in var1
test.bar ("Employee") -- calls bar with a string
test.bar (test)       -- error: bar expects a string not a table</span>
Class Objects
  类的注册是以beginClass或deriveClass开始,以endClass结束。一个类注册完后,还可以使用beginClass重新注册更多的信息,但是deriveClass只能被使用一次。为了给已经用deriveClass注册的类,注册更多的信息,可以使用beginClass。
<span style="font-size:14px;">  class A {
public:
A() { printf("A constructor ");}
static int staticData;
static int getStaticData() {return staticData;}
static float staticProperty;
static float getStaticProperty () { return staticProperty; }
static void setStaticProperty (float f) { staticProperty = f; }
static int staticCFunc (lua_State *L) { return 0; }
std::string dataMember;
char dataProperty;
char getProperty () const { return dataProperty; }
void setProperty (char v) { dataProperty = v; }
void func1 () {printf("func1 In Class A "); }
virtual void virtualFunc () {printf("virtualFunc In Class A ");  }
int cfunc (lua_State* L) { printf("cfunc In Class A ");  return 0; }
};
class B : public A {
public:
B() { printf("B constructor ");}
double dataMember2;
void func1 () {printf("func1 In Class B "); }
void func2 () { printf("func2 In Class B "); }
void virtualFunc () {printf("virtualFunc In Class B ");  }
};
int A::staticData = 3;
float A::staticProperty = 0.5;</span>