Changeset 901 – HoverRace

Changeset 901

Show
Ignore:
Timestamp:
03/09/10 20:21:18 (5 months ago)
Author:
zoogie
Message:

Split Execute() into separate Compile() and CallAndPrint() steps for finer-grained control.

Location:
trunk/engine/Script
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • trunk/engine/Script/Core.cpp

    r895 r901  
    168168 
    169169/** 
    170  * Execute a chunk of code. 
    171  * @param chunk The code to execute. 
     170 * Compile a chunk of code. 
     171 * Upon successful execution, the compiled chunk will be pushed to the stack. 
     172 * @param chunk The code to compile. 
    172173 * @throw IncompleteExn If the code does not complete a statement; i.e., 
    173174 *                      expecting more tokens.  Callers can catch this 
    174175 *                      to keep reading more data to finish the statement. 
    175  * @throw ScriptExn The code either failed to compile or signaled an error 
    176  *                  while executing. 
    177  */ 
    178 void Core::Execute(const std::string &chunk) 
    179 { 
    180     // Compile the chunk. 
     176 * @throw ScriptExn The code failed to compile. 
     177 */ 
     178void Core::Compile(const std::string &chunk) 
     179{ 
    181180    int status = luaL_loadbuffer(state, chunk.c_str(), chunk.length(), "=lua"); 
    182181    if (status != 0) { 
     
    192191        } 
    193192    } 
     193} 
     194 
     195/** 
     196 * Pop a function off the stack and execute it, printing any return values. 
     197 * @throw ScriptExn The code signaled an error while executing. 
     198 */ 
     199void Core::CallAndPrint() 
     200{ 
     201    int initStack = lua_gettop(state); 
     202    if (initStack == 0) { 
     203        throw ScriptExn("No function on stack."); 
     204    } 
     205    --initStack; 
    194206 
    195207    // Execute the chunk. 
    196     status = lua_pcall(state, 0, LUA_MULTRET, 0); 
     208    int status = lua_pcall(state, 0, LUA_MULTRET, 0); 
    197209    if (status != 0) { 
    198210        throw ScriptExn(PopError()); 
    199211    } 
    200212 
     213    int numReturns = lua_gettop(state) - initStack; 
     214 
    201215    // If there were any return values, pass them to print(). 
    202     if (lua_gettop(state) > 0) { 
     216    if (numReturns > 0) { 
    203217        lua_getglobal(state, "print"); 
    204         lua_insert(state, 1); 
    205         status = lua_pcall(state, lua_gettop(state) - 1, 0, 0); 
     218        lua_insert(state, initStack + 1); 
     219        status = lua_pcall(state, numReturns, 0, 0); 
    206220        if (status != 0) { 
    207221            throw ScriptExn(PopError()); 
    208222        } 
    209223    } 
    210  
    211     // Ensure that the stack is now empty for the next run. 
    212     lua_settop(state, 0); 
     224} 
     225 
     226/** 
     227 * Compile and execute a chunk of code. 
     228 * @param chunk The code to execute. 
     229 * @throw IncompleteExn If the code does not complete a statement; i.e., 
     230 *                      expecting more tokens.  Callers can catch this 
     231 *                      to keep reading more data to finish the statement. 
     232 * @throw ScriptExn The code either failed to compile or signaled an error 
     233 *                  while executing. 
     234 */ 
     235void Core::Execute(const std::string &chunk) 
     236{ 
     237    Compile(chunk); 
     238    CallAndPrint(); 
    213239} 
    214240 
  • trunk/engine/Script/Core.h

    r895 r901  
    7575 
    7676    public: 
     77        void Compile(const std::string &chunk); 
     78        void CallAndPrint(); 
     79 
    7780        void Execute(const std::string &chunk); 
    7881