| 1 | /* |
| 2 | * Copyright (c) 2009, Anton Burdinuk <[email protected]> |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions are met: |
| 7 | * * Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 9 | * * Redistributions in binary form must reproduce the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer in the |
| 11 | * documentation and/or other materials provided with the distribution. |
| 12 | * * Neither the name of the organization nor the |
| 13 | * names of its contributors may be used to endorse or promote products |
| 14 | * derived from this software without specific prior written permission. |
| 15 | * |
| 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 19 | * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY |
| 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | */ |
| 27 | |
| 28 | #ifdef WITH_LUA |
| 29 | #include "luawrapper.h" |
| 30 | |
| 31 | namespace lua |
| 32 | { |
| 33 | invoke_type invoke; |
| 34 | m_invoke_type m_invoke; |
| 35 | release_type end; |
| 36 | } |
| 37 | |
| 38 | |
| 39 | void lua::throw_lua_exception(lua_State* st, const std::string& addinfo) |
| 40 | { |
| 41 | size_t len = 0; |
| 42 | |
| 43 | const char* p = lua_tolstring(st, -1, &len); |
| 44 | |
| 45 | std::string s(p, len); |
| 46 | |
| 47 | if (addinfo.length()) |
| 48 | { |
| 49 | s.append(" ", 1); |
| 50 | s.append(addinfo); |
| 51 | } |
| 52 | |
| 53 | lua_pop(st, 1); |
| 54 | |
| 55 | throw exception(s); |
| 56 | } |
| 57 | |
| 58 | void lua::vm::initialize() |
| 59 | { |
| 60 | done(); |
| 61 | |
| 62 | st = lua_open(); |
| 63 | |
| 64 | if (!st) |
| 65 | throw exception("can`t create lua virtual machine instance"); |
| 66 | |
| 67 | luaL_openlibs(st); |
| 68 | } |
| 69 | |
| 70 | void lua::vm::load_file(const char* file) |
| 71 | { |
| 72 | if (!st) |
| 73 | throw exception("lua virtual machine is not ready"); |
| 74 | |
| 75 | if (luaL_loadfile(st, file) || lua_pcall(st, 0, 0, 0)) |
| 76 | throw_lua_exception(st); |
| 77 | } |
| 78 | |
| 79 | |
| 80 | void lua::vm::eval(const std::string& stmt, int offset) |
| 81 | { |
| 82 | enum { max_chunk_name_len = 64 }; |
| 83 | |
| 84 | if (offset > stmt.length()) |
| 85 | offset = stmt.length(); |
| 86 | |
| 87 | const char* p = stmt.c_str() + offset; |
| 88 | |
| 89 | int n = stmt.length() - offset; |
| 90 | |
| 91 | if (luaL_loadbuffer(st, p, n, n > max_chunk_name_len ? (std::string(p, max_chunk_name_len) + "...").c_str() : p) || lua_pcall(st, 0, 0, 0)) |
| 92 | throw_lua_exception(st); |
| 93 | } |
| 94 | |
| 95 | |
| 96 | void lua::bind::lookup(const char* name) |
| 97 | { |
| 98 | if (!refuse) |
| 99 | { |
| 100 | if (lua_type(st, cur_index) != LUA_TNIL) |
| 101 | { |
| 102 | lua_getfield(st, cur_index, name); |
| 103 | cur_index = size(); |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | void lua::bind::end(void) |
| 109 | { |
| 110 | if (!refuse && mutex) |
| 111 | { |
| 112 | int n = size() - st_top; |
| 113 | if (n > 0) |
| 114 | lua_pop(st, n); |
| 115 | |
| 116 | mutex = 0; |
| 117 | |
| 118 | args_number = retvals_number = 0; |
| 119 | cur_index = LUA_GLOBALSINDEX; |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | void lua::bind::invoke(void) |
| 124 | { |
| 125 | int function_index = -(args_number + 1); |
| 126 | |
| 127 | if (!refuse && lua_isfunction(st, function_index)) |
| 128 | { |
| 129 | int n = size(); |
| 130 | |
| 131 | if (lua_pcall(st, args_number, LUA_MULTRET, 0)) |
| 132 | throw_lua_exception(st); |
| 133 | |
| 134 | retvals_number = size() + args_number + 1 - n; |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | void lua::bind::m_invoke(void) |
| 139 | { |
| 140 | if (!refuse) |
| 141 | { |
| 142 | int function_index = -(args_number + 1); |
| 143 | int this_index = function_index - 1; |
| 144 | |
| 145 | if (lua_isfunction(st, function_index) && lua_istable(st, this_index)) |
| 146 | { |
| 147 | lua_pushvalue(st, this_index); |
| 148 | args_number++; |
| 149 | lua_insert(st, -args_number); |
| 150 | |
| 151 | |
| 152 | int n = size(); |
| 153 | |
| 154 | if (lua_pcall(st, args_number, LUA_MULTRET, 0)) |
| 155 | throw_lua_exception(st); |
| 156 | |
| 157 | retvals_number = size() + args_number + 1 - n; |
| 158 | } |
| 159 | } |
| 160 | } |
| 161 | #endif |