Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 1 | #include <lauxlib.h> |
| 2 | #include <lua.h> |
| 3 | #include <lualib.h> |
| 4 | |
| 5 | /* Lua uses longjmp to perform yield or throwing errors. This |
| 6 | * macro is used only for identifying the function that can |
| 7 | * not return because a longjmp is executed. |
| 8 | * __LJMP marks a prototype of hlua file that can use longjmp. |
| 9 | * WILL_LJMP() marks an lua function that will use longjmp. |
| 10 | * MAY_LJMP() marks an lua function that may use longjmp. |
| 11 | */ |
| 12 | #define __LJMP |
| 13 | #define WILL_LJMP(func) func |
| 14 | #define MAY_LJMP(func) func |
| 15 | |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame^] | 16 | /* Used to check an Lua function type in the stack. It creates and |
| 17 | * returns a reference of the function. This function throws an |
| 18 | * error if the rgument is not a "function". |
| 19 | */ |
| 20 | __LJMP unsigned int hlua_checkfunction(lua_State *L, int argno) |
| 21 | { |
| 22 | if (!lua_isfunction(L, argno)) { |
| 23 | const char *msg = lua_pushfstring(L, "function expected, got %s", luaL_typename(L, -1)); |
| 24 | WILL_LJMP(luaL_argerror(L, argno, msg)); |
| 25 | } |
| 26 | lua_pushvalue(L, argno); |
| 27 | return luaL_ref(L, LUA_REGISTRYINDEX); |
| 28 | } |
| 29 | |
| 30 | /* The three following functions are useful for adding entries |
| 31 | * in a table. These functions takes a string and respectively an |
| 32 | * integer, a string or a function and add it to the table in the |
| 33 | * top of the stack. |
| 34 | * |
| 35 | * These functions throws an error if no more stack size is |
| 36 | * available. |
| 37 | */ |
| 38 | __LJMP static inline void hlua_class_const_int(lua_State *L, const char *name, |
| 39 | unsigned int value) |
| 40 | { |
| 41 | if (!lua_checkstack(L, 2)) |
| 42 | WILL_LJMP(luaL_error(L, "full stack")); |
| 43 | lua_pushstring(L, name); |
| 44 | lua_pushunsigned(L, value); |
| 45 | lua_settable(L, -3); |
| 46 | } |
| 47 | __LJMP static inline void hlua_class_const_str(lua_State *L, const char *name, |
| 48 | const char *value) |
| 49 | { |
| 50 | if (!lua_checkstack(L, 2)) |
| 51 | WILL_LJMP(luaL_error(L, "full stack")); |
| 52 | lua_pushstring(L, name); |
| 53 | lua_pushstring(L, value); |
| 54 | lua_settable(L, -3); |
| 55 | } |
| 56 | __LJMP static inline void hlua_class_function(lua_State *L, const char *name, |
| 57 | int (*function)(lua_State *L)) |
| 58 | { |
| 59 | if (!lua_checkstack(L, 2)) |
| 60 | WILL_LJMP(luaL_error(L, "full stack")); |
| 61 | lua_pushstring(L, name); |
| 62 | lua_pushcclosure(L, function, 0); |
| 63 | lua_settable(L, -3); |
| 64 | } |
| 65 | |
| 66 | /* This function check the number of arguments available in the |
| 67 | * stack. If the number of arguments available is not the same |
| 68 | * then <nb> an error is throwed. |
| 69 | */ |
| 70 | __LJMP static inline void check_args(lua_State *L, int nb, char *fcn) |
| 71 | { |
| 72 | if (lua_gettop(L) == nb) |
| 73 | return; |
| 74 | WILL_LJMP(luaL_error(L, "'%s' needs %d arguments", fcn, nb)); |
| 75 | } |
| 76 | |
| 77 | /* Return true if the data in stack[<ud>] is an object of |
| 78 | * type <class_ref>. |
| 79 | */ |
| 80 | static int hlua_udataistype(lua_State *L, int ud, int class_ref) |
| 81 | { |
| 82 | void *p = lua_touserdata(L, ud); |
| 83 | if (!p) |
| 84 | return 0; |
| 85 | |
| 86 | if (!lua_getmetatable(L, ud)) |
| 87 | return 0; |
| 88 | |
| 89 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_ref); |
| 90 | if (!lua_rawequal(L, -1, -2)) { |
| 91 | lua_pop(L, 2); |
| 92 | return 0; |
| 93 | } |
| 94 | |
| 95 | lua_pop(L, 2); |
| 96 | return 1; |
| 97 | } |
| 98 | |
| 99 | /* Return an object of the expected type, or throws an error. */ |
| 100 | __LJMP static void *hlua_checkudata(lua_State *L, int ud, int class_ref) |
| 101 | { |
| 102 | if (!hlua_udataistype(L, ud, class_ref)) |
| 103 | WILL_LJMP(luaL_argerror(L, 1, NULL)); |
| 104 | return lua_touserdata(L, ud); |
| 105 | } |
| 106 | |
| 107 | /* This fucntion push an error string prefixed by the file name |
| 108 | * and the line number where the error is encountered. |
| 109 | */ |
| 110 | static int hlua_pusherror(lua_State *L, const char *fmt, ...) |
| 111 | { |
| 112 | va_list argp; |
| 113 | va_start(argp, fmt); |
| 114 | luaL_where(L, 1); |
| 115 | lua_pushvfstring(L, fmt, argp); |
| 116 | va_end(argp); |
| 117 | lua_concat(L, 2); |
| 118 | return 1; |
| 119 | } |
| 120 | |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 121 | void hlua_init(void) |
| 122 | { |
| 123 | } |