00001 #include "lua_cl2.h" 00002 00003 #include "application_config.h" 00004 #include "er_errors.h" 00005 00006 #include "common/logging.h" 00007 00008 #include "lua.hpp" 00009 00010 #ifdef __EPOC32__ 00011 #include <e32std.h> 00012 #endif 00013 00014 #include <stdlib.h> 00015 00016 // -------------------------------------------------- 00017 // app-specific libraries 00018 // -------------------------------------------------- 00019 00020 #include "lua_bindings.h" // CL2 bindings 00021 #include "libluasqlite3.h" 00022 00023 static const luaL_Reg my_lualibs[] = { 00024 {LUA_CL2LIBNAME, luaopen_cl2}, 00025 // {"sqlite3", luaopen_sqlite3}, 00026 {NULL, NULL} 00027 }; 00028 00029 static void my_openlibs (lua_State *L) { 00030 const luaL_Reg *lib = my_lualibs; 00031 for (; lib->func; lib++) { 00032 lua_pushcfunction(L, lib->func); 00033 // Library name for optional use by the luaopen_* functions. 00034 lua_pushstring(L, lib->name); 00035 lua_call(L, 1, 0); 00036 } 00037 } 00038 00039 // -------------------------------------------------- 00040 // Lua state management 00041 // -------------------------------------------------- 00042 00043 // Does not provide access to _any_ libraries, either the standard 00044 // ones or the application specific ones. Add them later as you like. 00045 // See cl_lua_new_libs(). 00046 // 00047 // We are not setting any privileged flag here either, as we do not 00048 // have such a mechanism, at least not yet. 00049 extern "C" lua_State* cl_lua_new() 00050 { 00051 // Second arg is "ud" as passed to l_alloc. 00052 lua_State *L = lua_newstate(l_alloc, NULL); 00053 if (G_UNLIKELY(!L)) { 00054 return NULL; 00055 } 00056 00057 // This setting may not always be ideal, so override as desired. At 00058 // least this is in some sense safe. 00059 lua_atpanic(L, atpanic_log_exit); 00060 00061 return L; 00062 } 00063 00064 extern "C" lua_State* cl_lua_new_libs() 00065 { 00066 lua_State *L = cl_lua_new(); 00067 if (G_LIKELY(L)) { 00068 luaL_openlibs(L); 00069 my_openlibs(L); 00070 } 00071 return L; 00072 } 00073 00074 extern "C" gboolean validate_lua_syntax(const gchar* value, GError** error) 00075 { 00076 lua_State* L = cl_lua_new(); 00077 if (G_UNLIKELY(!L)) { 00078 if (error) 00079 *error = gx_error_no_memory; 00080 return FALSE; 00081 } 00082 00083 int res = luaL_loadstring(L, value); 00084 gboolean success; 00085 switch (res) 00086 { 00087 case 0: 00088 { 00089 success = TRUE; 00090 break; 00091 } 00092 case LUA_ERRMEM: 00093 { 00094 success = FALSE; 00095 if (error) 00096 *error = gx_error_no_memory; 00097 break; 00098 } 00099 default: 00100 { 00101 success = FALSE; 00102 if (error) { 00103 const char* luaErr = lua_tostring(L, -1); 00104 *error = gx_error_new(domain_lua, res, "validation of Lua code failed: %s", luaErr); 00105 } 00106 break; 00107 } 00108 } 00109 00110 lua_close(L); 00111 00112 return success; 00113 } 00114 00115 extern "C" int lua_raise_gerror(lua_State* L, GError* error) 00116 { 00117 if (error) { 00118 gchar* s = gx_error_to_string(error); 00119 g_error_free(error); 00120 if (G_UNLIKELY(!s)) { 00121 lua_pushstring(L, "out of memory"); 00122 } else { 00123 lua_pushstring(L, s); // Lua makes its own copy of "s" 00124 g_free(s); 00125 } 00126 } else { 00127 lua_pushstring(L, "out of memory"); 00128 } 00129 return lua_error(L); 00130 } 00131 00132 extern "C" GError* lua_get_gerror(lua_State* L) 00133 { 00134 const char* luaErr = lua_tostring(L, -1); 00135 // The _literal does not mean that the argument must be a string 00136 // literal, rather it means that it is taken verbatim. 00137 return gx_error_new_literal(domain_lua, 0, luaErr); 00138 } 00139 00140 extern "C" void lua_set_gerror(lua_State* L, GError** error) 00141 { 00142 if (error) { 00143 *error = lua_get_gerror(L); 00144 } 00145 } 00146 00147 #if __DO_LOGGING__ 00148 // Assumes an error message on top of the Lua stack. 00149 static void txtlog_lua_error(lua_State* L) 00150 { 00151 const char* luaErr = lua_tostring(L, -1); 00152 logg("unprotected error in Lua: %s", luaErr); 00153 } 00154 #else 00155 #define txtlog_lua_error(_x) 00156 #endif 00157 00158 // If an error occurs in a Lua VM instance, then either: (1) if the 00159 // Lua operation was done inside a protected environment (e.g., within 00160 // lua_pcall), then the system-wide default non-local return is done 00161 // internally by Lua to return execution back to the pcall; or (2) if 00162 // the operation was done outside a pcall, then the handler one sets 00163 // with 'lua_atpanic' is invoked. Here we have many different possible 00164 // panic handler implementations. 00165 00166 #ifdef __EPOC32__ 00167 extern "C" int atpanic_leave(lua_State* L) 00168 { 00169 User::Leave(KErrLuaErr); 00170 // we did a leave so Lua will not call "exit" 00171 return 0; // not reached 00172 } 00173 00174 extern "C" int atpanic_txtlog_leave(lua_State* L) 00175 { 00176 txtlog_lua_error(L); 00177 return atpanic_leave(L); 00178 } 00179 #endif 00180 00181 extern "C" int atpanic_throw(lua_State *L) 00182 { 00183 (void)L; 00184 throw LuaException(); 00185 return 0; 00186 } 00187 00188 extern "C" int atpanic_txtlog_throw(lua_State *L) 00189 { 00190 txtlog_lua_error(L); 00191 return atpanic_throw(L); 00192 } 00193 00194 extern "C" int atpanic_txtlog_exit(lua_State *L) 00195 { 00196 txtlog_lua_error(L); 00197 er_fatal(); // this may already cause process exit 00198 return 0; // number of Lua results returned, caller will proceed to exit() 00199 } 00200 00201 extern "C" int atpanic_log_exit(lua_State *L) 00202 { 00203 const char* luaErr = lua_tostring(L, -1); 00204 er_log_none(er_FATAL, luaErr); // will not return 00205 return 0; 00206 } 00207 00208 /** 00209 00210 The code above this notice is covered by the following license: 00211 00212 Copyright 2009 Helsinki Institute for Information Technology (HIIT) 00213 and the authors. All rights reserved. 00214 00215 Authors: Tero Hasu <tero.hasu@hut.fi> 00216 00217 Permission is hereby granted, free of charge, to any person 00218 obtaining a copy of this software and associated documentation files 00219 (the "Software"), to deal in the Software without restriction, 00220 including without limitation the rights to use, copy, modify, merge, 00221 publish, distribute, sublicense, and/or sell copies of the Software, 00222 and to permit persons to whom the Software is furnished to do so, 00223 subject to the following conditions: 00224 00225 The above copyright notice and this permission notice shall be 00226 included in all copies or substantial portions of the Software. 00227 00228 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 00229 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 00230 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 00231 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 00232 BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 00233 ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 00234 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 00235 SOFTWARE. 00236 00237 **/ 00238 00239 // -------------------------------------------------- 00240 // code derived from Lua code follows... 00241 // -------------------------------------------------- 00242 00243 /****************************************************************************** 00244 * Copyright (C) 1994-2008 Lua.org, PUC-Rio. All rights reserved. 00245 * 00246 * Permission is hereby granted, free of charge, to any person obtaining 00247 * a copy of this software and associated documentation files (the 00248 * "Software"), to deal in the Software without restriction, including 00249 * without limitation the rights to use, copy, modify, merge, publish, 00250 * distribute, sublicense, and/or sell copies of the Software, and to 00251 * permit persons to whom the Software is furnished to do so, subject to 00252 * the following conditions: 00253 * 00254 * The above copyright notice and this permission notice shall be 00255 * included in all copies or substantial portions of the Software. 00256 * 00257 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 00258 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 00259 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 00260 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 00261 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 00262 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 00263 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 00264 ******************************************************************************/ 00265 00266 extern "C" void *l_alloc (void *ud, void *ptr, size_t osize, size_t nsize) { 00267 (void)ud; 00268 (void)osize; 00269 if (nsize == 0) { 00270 free(ptr); 00271 return NULL; 00272 } 00273 else 00274 return realloc(ptr, nsize); 00275 } 00276
ContextLogger2—ContextLogger2 Logger Daemon Internals—Generated on Mon May 2 13:49:55 2011 by Doxygen 1.6.1