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 :)

Saturday, October 11, 2008

V8 - Lua - V8 - Lua

My mind being trowing between the V8 javascript and lua scripting language, which one should i used for my super duper simple 3d engine?

the advantage of V8 javascript:
1) backed by google,
2) faster than native lua
3) designed to be embeded
4) sandbox design,
5) huge user base from Web and flash

the advantage of lua
1) small and simple
2) api are lower level than V8
3) Lua JIT is faster than javascript

ping-pong my mind between this two scripting, read many v8 scripting vm review, most of the people said it is easier embed v8 to c++ than lua and it is faster.
see this http://blogs.bungeeconnect.com/2008/10/09/connecting-c-to-javascript-via-googles-v8/ for great introduction how to access javascript functions from c++

finally i decided to test run v8 javascript. i immediately run into problem. a suppose to be trivial problem, how to compile an external javascript? most of the example compile string, not by file. shocking google did very bad job in documentation. can't find this simple information in the entire internet.

disappointed, ya lua's api maybe more c-like, bad comment syntax, 1-based index blah blah, but when come to documentation it is much better compare to V8. so my verdict is out, concentrate on Lua first, two to three yeah later check again javascript, by the time tracemonkey and squirrelfish should be out, the documentation of them should be better than current state.