[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Lua <-> C++ integration with "unused" stack slots
- From: pan shizhu <pan.shizhu@...>
- Date: Fri, 5 Feb 2010 17:12:48 +0800
> My intention with using the "unused stack slots" was to make it easy
> to write C++ code that uses some Lua objects without the need
> to manage stack indices manually.
But you generally should not use a C++ object to hold a Lua stack
value. If you write something to wrap the usage, you futher promoted
the abuse.
> LuaVar f(LuaVar x, const char* str)
> {
> LuaVar rslt;
> if (x.isTable()) {
> LuaVar v = x[str];
> rslt = ...
> ...
> } else {
> ...
> }
> return rslt;
> }
>
> int main(int argc, char** argv)
> {
> ...
> LuaVar x = ...
> ...
> while (...) {
> LuaVar y = f(x, argv[i])
> if (y.isString()) {
> ...
> }
> }
> ...
> }
This should instead be:
> string f(map x, const char* str)
> {
> string rslt;
> if (x.isTable()) {
> string v = x[str];
> rslt = ...
> ...
> } else {
> ...
> }
> return rslt;
> }
> int main(int argc, char** argv)
> {
> ...
> cppstring x = ...
> ...
> while (...) {
> string y = f(x, argv[i])
> if (y) {
> ...
> }
> }
> ...
> }
It is better to only use lua stack variable within one function, pass
lua stack value between c functions are dangerous and result in
difficult-to-maintain code.
A program which contains only standard C++ class, and a program which
contains lot's of lua private classes. What is more readable?
You're introducing the Python way of embed, and that reduces the
readibility of source code more than improvement.