-/*******************************************************************************
-
- Copyright (c) 2009, Charles McGarvey
- All rights reserved.
-
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions are met:
-
- * Redistributions of source code must retain the above copyright notice,
- this list of conditions and the following disclaimer.
- * Redistributions in binary form must reproduce the above copyright notice,
- this list of conditions and the following disclaimer in the documentation
- and/or other materials provided with the distribution.
-
- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
- FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
- SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
- CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-
-*******************************************************************************/
+/*] Copyright (c) 2009-2010, Charles McGarvey [**************************
+**] All rights reserved.
+*
+* vi:ts=4 sw=4 tw=75
+*
+* Distributable under the terms and conditions of the 2-clause BSD license;
+* see the file COPYING for a complete text of the license.
+*
+**************************************************************************/
#ifndef _MOOF_SCRIPT_HH_
#define _MOOF_SCRIPT_HH_
/**
* @file Script.hh
- * A thin wrapper over Lua. This is not meant as a complicated binding package
- * between C++ and Lua. It does not try to make the boundary invisible. It
- * does not hide the concept of the Lua stack, but rather provides that
- * mechanism with a certain level of abstraction while also providing a cleaner,
- * more consistent API.
+ * A thin wrapper over Lua. This is not meant as a complicated binding
+ * package between C++ and Lua. It does not try to make the boundary
+ * invisible. It does not hide the concept of the Lua stack, but rather
+ * provides that mechanism with a certain level of abstraction while also
+ * providing a cleaner, more consistent API.
*/
#include <iostream>
#include <boost/bind.hpp>
#include <boost/function.hpp>
#include <boost/shared_ptr.hpp>
-
#include <lua.hpp>
#include <Moof/Log.hh>
};
/**
- * This is the most prominent abstraction on top of the standard Lua API.
- * A Slot object represents a value on the stack. More specifically, it
- * represents a position on the stack. The distinction is only important
- * when objects are moved around on the stack or if the Slot represents a
- * negative index on the stack (the value of which will change as things are
- * pushed onto and popped from the stack).
+ * This is the most prominent abstraction on top of the standard Lua
+ * API. A Slot object represents a value on the stack. More
+ * specifically, it represents a position on the stack. The
+ * distinction is only important when objects are moved around on the
+ * stack or if the Slot represents a negative index on the stack (the
+ * value of which will change as things are pushed onto and popped from
+ * the stack).
*/
struct Slot
{
/**
- * You have direct access to the index of the value on the stack being
- * represented.
+ * You have direct access to the index of the value on the stack
+ * being represented.
*/
int index;
/**
- * A default-constructed Slot is invalid until a valid Slot is assigned
- * to it. The only method that should be called on such a Slot is
- * isValid(), otherwise chaos may ensue. In this case, the Slot will be
- * invalid even if index is manually changed to a valid index. You have
- * to index the script itself to get a valid Slot.
+ * A default-constructed Slot is invalid until a valid Slot is
+ * assigned to it. The only method that should be called on such a
+ * Slot is isValid(), otherwise chaos may ensue. In this case, the
+ * Slot will be invalid even if index is manually changed to a
+ * valid index. You have to index the script itself to get a valid
+ * Slot.
*/
Slot(lua_State* s = 0, int i = 0) :
index(i),
mState(s) {}
/**
- * A copied value presently points to the same value, except the real
- * index is used. That means that if a value that refers to a frame
- * referenced from the top of the stack will have its normalized index
- * copied into the new value object.
+ * A copied value presently points to the same value, except the
+ * real index is used. That means that if a value that refers to a
+ * frame referenced from the top of the stack will have its
+ * normalized index copied into the new value object.
*/
Slot(const Slot& copy) :
// check the type of the value
- bool isBoolean() const { return (bool)lua_isboolean(mState, index); }
- bool isFunction() const { return (bool)lua_isfunction(mState, index); }
- bool isNil() const { return (bool)lua_isnil(mState, index); }
- bool isNone() const { return (bool)lua_isnone(mState, index); }
- bool isValid() const { return mState != 0 && !isNone(); }
- bool isNoneOrNil() const { return (bool)lua_isnoneornil(mState, index); }
- bool isNumber() const { return (bool)lua_isnumber(mState, index); }
- bool isString() const { return (bool)lua_isstring(mState, index); }
- bool isTable() const { return (bool)lua_istable(mState, index); }
- bool isThread() const { return (bool)lua_isthread(mState, index); }
- bool isData() const { return (bool)lua_isuserdata(mState, index); }
- bool isLightData() const { return (bool)lua_islightuserdata(mState, index); }
+ bool isBoolean() const
+ { return (bool)lua_isboolean(mState, index); }
+ bool isFunction() const
+ { return (bool)lua_isfunction(mState, index); }
+ bool isNil() const
+ { return (bool)lua_isnil(mState, index); }
+ bool isNone() const
+ { return (bool)lua_isnone(mState, index); }
+ bool isValid() const
+ { return mState != 0 && !isNone(); }
+ bool isNoneOrNil() const
+ { return (bool)lua_isnoneornil(mState, index); }
+ bool isNumber() const
+ { return (bool)lua_isnumber(mState, index); }
+ bool isString() const
+ { return (bool)lua_isstring(mState, index); }
+ bool isTable() const
+ { return (bool)lua_istable(mState, index); }
+ bool isThread() const
+ { return (bool)lua_isthread(mState, index); }
+ bool isData() const
+ { return (bool)lua_isuserdata(mState, index); }
+ bool isLightData() const
+ { return (bool)lua_islightuserdata(mState, index); }
/**
- * Check the value and throw an error if its the wrong type. There's a
- * little caveat: This method never returns because it does a long jump.
- * Consequently, constructed C++ objects which exist on the stack
- * between the current frame and some lua function will not be
- * destructed. That's not a problem for objects that only exist on the
- * stack, but any objects that allocate memory on the heap (such as
- * containers or strings) will leak. Therefore, you should only call
- * this method after cleaning up such objects. The best thing to do for
- * defining functions is to simply check all the parameters at the
- * get-go before any C++ objects are even constructed.
+ * Check the value and throw an error if its the wrong type.
+ * There's a little caveat: This method never returns because it
+ * does a long jump. Consequently, constructed C++ objects which
+ * exist on the stack between the current frame and some lua
+ * function will not be destructed. That's not a problem for
+ * objects that only exist on the stack, but any objects that
+ * allocate memory on the heap (such as containers or strings) will
+ * leak. Therefore, you should only call this method after
+ * cleaning up such objects. The best thing to do for defining
+ * functions is to simply check all the parameters at the get-go
+ * before any C++ objects are even constructed.
*/
void requireType(Type type) const
/**
- * Get the length of the value according to the definition given by Lua.
+ * Get the length of the value according to the definition given by
+ * Lua.
*/
size_t getLength() const
/**
- * Get a pointer value (for userdata, tables, threads, and functions).
+ * Get a pointer value (for userdata, tables, threads, and
+ * functions).
*/
const void* getIdentifier() const
}
/**
- * Inserts the top-most value on the stack at position index, shifting other
- * values as needed.
+ * Inserts the top-most value on the stack at position index,
+ * shifting other values as needed.
*/
void insertTopHere()
/**
- * Throw an error with the value at the top of the stack. This method never
- * returns because it does a long jump. Consequently, constructed C++
- * objects which exist on the stack between the current frame and some lua
- * function will not be destructed. That's not a problem for objects that
- * only exist on the stack, but any objects that allocate memory on the heap
- * (such as containers or strings) will leak. Therefore, you should only
- * call this method after cleaning up such objects.
+ * Throw an error with the value at the top of the stack. This method
+ * never returns because it does a long jump. Consequently,
+ * constructed C++ objects which exist on the stack between the
+ * current frame and some lua function will not be destructed. That's
+ * not a problem for objects that only exist on the stack, but any
+ * objects that allocate memory on the heap (such as containers or
+ * strings) will leak. Therefore, you should only call this method
+ * after cleaning up such objects.
*/
void throwError()
}
/**
- * Get the size of the stack; this is also the index of the top-most value.
+ * Get the size of the stack; this is also the index of the top-most
+ * value.
*/
int getSize() const
/**
- * Makes sure there is at least extra more places on the stack. Returns
- * false if space couldn't be created. Just like with the regular Lua API,
- * you are responsible to make sure the stack is big enough to hold whatever
- * you want to push on it. This is usually only an issue if you're pushing
- * stuff in a loop.
+ * Makes sure there is at least extra more places on the stack.
+ * Returns false if space couldn't be created. Just like with the
+ * regular Lua API, you are responsible to make sure the stack is big
+ * enough to hold whatever you want to push on it. This is usually
+ * only an issue if you're pushing stuff in a loop.
*/
bool checkStack(int extra)
return (Result)luaL_loadfile(mState, filename.c_str());
}
- Result pushCode(const std::string& name, const char* buffer, size_t size)
+ Result pushCode(const std::string& name, const char* buffer,
+ size_t size)
{
return (Result)luaL_loadbuffer(mState, buffer, size, name.c_str());
}
/**
* Call a function on the stack. The correct procedure is to push a
- * function onto the stack followed by nargs arguments. This method will
- * pop them off upon return, leaving up to nresults return values (default
- * is any number of return values, depending on the callee).
+ * function onto the stack followed by nargs arguments. This method
+ * will pop them off upon return, leaving up to nresults return values
+ * (default is any number of return values, depending on the callee).
*/
- Result call(int nargs, int nresults = LUA_MULTRET)
+ Result call(int nargs = 0, int nresults = LUA_MULTRET)
{
return (Result)lua_pcall(mState, nargs, nresults, 0);
}
}
else
{
- stream << slot.getTypeName()
- << " (" << slot.getIdentifier() << ")" << std::endl;
+ stream << slot.getTypeName() << " (" << slot.getIdentifier() << ")";
}
return stream;
#endif // _MOOF_SCRIPT_HH_
-/** vim: set ts=4 sw=4 tw=80: *************************************************/
-