Tuesday, October 28, 2008

Lua binding (manually)

almost three weeks has gone, my scripting framework is still in trial and error stage. the framework i have in mind is similar to torquescript design. class inherited from ScriptObject are able to expose properties and methods to script as global or class. after same testing i come out with this design

1) Scope, global or class
only manager/singleton class can declare global scope properties and methods, it'll be meaningless otherwise.
to declare properties
registerVar(name, class, variable);
to decalre methods
registerFunc(name, class, function);
to declare class scope 
startClass(name, class);
registerVar(...);
registerFunc(...);
endClass();

2) Registration propagation
instead of using lua example's method, create a proxy class (a static class) and add the static class to script manager. my framework will use torque method instead. torque engine uses ConcreteClassVariable static variable to represent each class datatype. and the constructor for ConcreateClassVariable will register itself to ScriptMgr. when ScriptMgr open libraries, it'll call all registered ConcreateClassVariable, and ConcreateClassVariable will call T::installScript in turn, installScript() is the one contained registerVar and registerFunc.

3) Type
the expose class has 2 different type, script class or Script object. Script object == C++ manager/singleton class, script class == C++ normal class. lua script can use script object to instantiate script class and script class destruction is maintain by lua GC.

there few lua tricks i'd learn from these few weeks. 
1) LUA_ENVIRONINDEX pseudo table is only available when lua call c++ function, hence if this table are required during C++ call C++ function, then just make the later one as lua closure then call it by lua_pcall, then LUA_ENVIRONINDEX table is available to later c++ function

2) when overwriting __newindex and __index, is good to save exisiting default newindex and index function, then set them as closure parameter to new newindex and index closure.

hopefully i could implement all this wihtin 1 week time. my hand is itching to play with shader graphics and physics :)

No comments: