Changeset 901
- Timestamp:
- 03/09/10 20:21:18 (5 months ago)
- Location:
- trunk/engine/Script
- Files:
-
- 2 modified
Legend:
- Unmodified
- Added
- Removed
-
trunk/engine/Script/Core.cpp
r895 r901 168 168 169 169 /** 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. 172 173 * @throw IncompleteExn If the code does not complete a statement; i.e., 173 174 * expecting more tokens. Callers can catch this 174 175 * 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 */ 178 void Core::Compile(const std::string &chunk) 179 { 181 180 int status = luaL_loadbuffer(state, chunk.c_str(), chunk.length(), "=lua"); 182 181 if (status != 0) { … … 192 191 } 193 192 } 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 */ 199 void Core::CallAndPrint() 200 { 201 int initStack = lua_gettop(state); 202 if (initStack == 0) { 203 throw ScriptExn("No function on stack."); 204 } 205 --initStack; 194 206 195 207 // Execute the chunk. 196 status = lua_pcall(state, 0, LUA_MULTRET, 0);208 int status = lua_pcall(state, 0, LUA_MULTRET, 0); 197 209 if (status != 0) { 198 210 throw ScriptExn(PopError()); 199 211 } 200 212 213 int numReturns = lua_gettop(state) - initStack; 214 201 215 // If there were any return values, pass them to print(). 202 if ( lua_gettop(state)> 0) {216 if (numReturns > 0) { 203 217 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); 206 220 if (status != 0) { 207 221 throw ScriptExn(PopError()); 208 222 } 209 223 } 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 */ 235 void Core::Execute(const std::string &chunk) 236 { 237 Compile(chunk); 238 CallAndPrint(); 213 239 } 214 240 -
trunk/engine/Script/Core.h
r895 r901 75 75 76 76 public: 77 void Compile(const std::string &chunk); 78 void CallAndPrint(); 79 77 80 void Execute(const std::string &chunk); 78 81
