| 1 |
/* |
| 2 |
* Copyright (C) 2004 Ivo Danihelka ([email protected]) |
| 3 |
* |
| 4 |
* This program is free software; you can redistribute it and/or modify |
| 5 |
* it under the terms of the GNU General Public License as published by |
| 6 |
* the Free Software Foundation; either version 2 of the License, or |
| 7 |
* (at your option) any later version. |
| 8 |
*/ |
| 9 |
#include "ScriptState.h" |
| 10 |
|
| 11 |
#include "Log.h" |
| 12 |
#include "Path.h" |
| 13 |
#include "ScriptException.h" |
| 14 |
|
| 15 |
extern "C" { |
| 16 |
#include "lualib.h" |
| 17 |
#include "lauxlib.h" |
| 18 |
} |
| 19 |
|
| 20 |
#include "def-script.h" |
| 21 |
|
| 22 |
//----------------------------------------------------------------- |
| 23 |
ScriptState::ScriptState() |
| 24 |
{ |
| 25 |
m_state = lua_open(); |
| 26 |
luaopen_base(m_state); |
| 27 |
luaopen_string(m_state); |
| 28 |
luaopen_math(m_state); |
| 29 |
luaopen_table(m_state); |
| 30 |
|
| 31 |
prepareErrorHandler(); |
| 32 |
} |
| 33 |
//----------------------------------------------------------------- |
| 34 |
ScriptState::~ScriptState() |
| 35 |
{ |
| 36 |
lua_close(m_state); |
| 37 |
} |
| 38 |
//----------------------------------------------------------------- |
| 39 |
/** |
| 40 |
* Prepare error handler at global stack. |
| 41 |
*/ |
| 42 |
void |
| 43 |
ScriptState::prepareErrorHandler() |
| 44 |
{ |
| 45 |
lua_pushliteral(m_state, "_TRACEBACK"); |
| 46 |
lua_pushcfunction(m_state, script_debugStack); |
| 47 |
lua_settable(m_state, LUA_GLOBALSINDEX); |
| 48 |
} |
| 49 |
//----------------------------------------------------------------- |
| 50 |
/** |
| 51 |
* Insert error handler at given local index. |
| 52 |
*/ |
| 53 |
void |
| 54 |
ScriptState::insertErrorHandler(int index) |
| 55 |
{ |
| 56 |
lua_pushliteral(m_state, "_TRACEBACK"); |
| 57 |
lua_rawget(m_state, LUA_GLOBALSINDEX); |
| 58 |
lua_insert(m_state, index); |
| 59 |
} |
| 60 |
//----------------------------------------------------------------- |
| 61 |
/** |
| 62 |
* Process script on stack. |
| 63 |
* @param error script load status |
| 64 |
* @param params number of params |
| 65 |
* @param returns number of values on return |
| 66 |
* |
| 67 |
* @throws ScriptException when script is bad |
| 68 |
*/ |
| 69 |
void |
| 70 |
ScriptState::callStack(int error, int params, int returns) |
| 71 |
{ |
| 72 |
if (0 == error) { |
| 73 |
int base = lua_gettop(m_state) - params; |
| 74 |
insertErrorHandler(base); |
| 75 |
error = lua_pcall(m_state, params, returns, base); |
| 76 |
lua_remove(m_state, base); |
| 77 |
} |
| 78 |
|
| 79 |
if (error) { |
| 80 |
const char *msg = lua_tostring(m_state, -1); |
| 81 |
if (NULL == msg) { |
| 82 |
msg = "(error with no message)"; |
| 83 |
} |
| 84 |
ExInfo info = ExInfo("script failure") |
| 85 |
.addInfo("error", msg); |
| 86 |
lua_pop(m_state, 1); |
| 87 |
throw ScriptException(info); |
| 88 |
} |
| 89 |
} |
| 90 |
//----------------------------------------------------------------- |
| 91 |
/** |
| 92 |
* Process script file. |
| 93 |
* @param file script |
| 94 |
*/ |
| 95 |
void |
| 96 |
ScriptState::doFile(const Path &file) |
| 97 |
{ |
| 98 |
int error = luaL_loadfile(m_state, file.getNative().c_str()); |
| 99 |
callStack(error); |
| 100 |
} |
| 101 |
//----------------------------------------------------------------- |
| 102 |
/** |
| 103 |
* Process string. |
| 104 |
* @param input script |
| 105 |
*/ |
| 106 |
void |
| 107 |
ScriptState::doString(const std::string &input) |
| 108 |
{ |
| 109 |
int error = luaL_loadbuffer(m_state, input.c_str(), input.size(), |
| 110 |
input.c_str()); |
| 111 |
callStack(error); |
| 112 |
} |
| 113 |
//----------------------------------------------------------------- |
| 114 |
void |
| 115 |
ScriptState::registerFunc(const char *name, lua_CFunction func) |
| 116 |
{ |
| 117 |
lua_register(m_state, name, func); |
| 118 |
} |
| 119 |
|
| 120 |
//----------------------------------------------------------------- |
| 121 |
/** |
| 122 |
* Call "bool function(param)" function. |
| 123 |
* @param funcRef function index at registry |
| 124 |
* @param param integer parametr |
| 125 |
* @return boolean result from function |
| 126 |
* @throws ScriptException when function is bad |
| 127 |
*/ |
| 128 |
bool |
| 129 |
ScriptState::callCommand(int funcRef, int param) |
| 130 |
{ |
| 131 |
int numResults = 1; |
| 132 |
lua_rawgeti(m_state, LUA_REGISTRYINDEX, funcRef); |
| 133 |
lua_pushnumber(m_state, param); |
| 134 |
callStack(0, 1, numResults); |
| 135 |
|
| 136 |
if (0 == lua_isboolean(m_state, -1)) { |
| 137 |
const char *type = lua_typename(m_state, lua_type(m_state, -1)); |
| 138 |
lua_pop(m_state, numResults); |
| 139 |
throw ScriptException( |
| 140 |
ExInfo("script command failure - boolean expected") |
| 141 |
.addInfo("got", type)); |
| 142 |
} |
| 143 |
bool result = lua_toboolean(m_state, -1); |
| 144 |
lua_pop(m_state, numResults); |
| 145 |
return result; |
| 146 |
} |
| 147 |
//----------------------------------------------------------------- |
| 148 |
/** |
| 149 |
* Remove function from registry. |
| 150 |
* @param funcRef function index at registry |
| 151 |
*/ |
| 152 |
void |
| 153 |
ScriptState::unref(int funcRef) |
| 154 |
{ |
| 155 |
luaL_unref(m_state, LUA_REGISTRYINDEX, funcRef); |
| 156 |
} |
| 157 |
|
| 158 |
//----------------------------------------------------------------- |
| 159 |
/** |
| 160 |
* Register light userdata for lua script. |
| 161 |
*/ |
| 162 |
void |
| 163 |
ScriptState::registerLeader(Scripter *leader) |
| 164 |
{ |
| 165 |
lua_pushstring(m_state, script_getLeaderName()); |
| 166 |
lua_pushlightuserdata(m_state, leader); |
| 167 |
lua_rawset(m_state, LUA_REGISTRYINDEX); |
| 168 |
} |
| 169 |
|