diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/lua.h | 7 | ||||
| -rw-r--r-- | src/math.c | 4 | ||||
| -rw-r--r-- | src/table.c | 19 | ||||
| -rw-r--r-- | src/table.h | 2 |
4 files changed, 29 insertions, 3 deletions
@@ -115,6 +115,13 @@ extern int _print_errors; sprintf(err, "(%s:%i) %s assertion failed", file, line, #eq);\
luaI_error(L, -1, err);}}
+#define luaI_assert2(L, eq){_helperluaI_assert2(L, eq, __FILE__, __LINE__);}
+#define _helperluaI_assert2(L, eq, file, line){\
+ if(!(eq)){\
+ char err[1024] = {0};\
+ sprintf(err, "(%s:%i) %s assertion failed", file, line, #eq);\
+ luaL_error(L, err);}}
+
int writer(lua_State*, const void*, size_t, void*);
#if LUA_VERSION_NUM != 501
@@ -65,10 +65,10 @@ int l_random(lua_State* L){ size_t min = 1; switch(lua_gettop(L)){ case 2: - min = lua_tointeger(L, 1); + min = luaL_checkinteger(L, 1); lua_remove(L, 1); case 1: - lua_pushinteger(L, (erand48(seedv) * (lua_tointeger(L, 1) - min + 1)) + min); + lua_pushinteger(L, (erand48(seedv) * (luaL_checkinteger(L, 1) - min + 1)) + min); break; case 0: default: diff --git a/src/table.c b/src/table.c index f5bdd5d..e2130dc 100644 --- a/src/table.c +++ b/src/table.c @@ -3,6 +3,23 @@ #include <string.h> #include <stdint.h> +int l_contains(lua_State* L){ + luaI_assert2(L, lua_gettop(L) >= 2); + luaL_checktype(L, 1, LUA_TTABLE); + + lua_pushnil(L); + for(;lua_next(L, 1);){ + if(lua_rawequal(L, -1, 2)){ + lua_pushvalue(L, -2); + return 1; + } + lua_pop(L, 1); + } + + lua_pushnil(L); + return 1; +} + void value_clone_req(lua_State* L){ int idx = lua_gettop(L); @@ -32,7 +49,7 @@ void value_clone_req(lua_State* L){ } int l_dup(lua_State* L){ - if(lua_gettop(L) != 1) return 0; + luaI_assert2(L, lua_gettop(L) == 1); value_clone_req(L); lua_pushvalue(L, 2); diff --git a/src/table.h b/src/table.h index b114cd2..27cbaf7 100644 --- a/src/table.h +++ b/src/table.h @@ -7,6 +7,7 @@ void i_shuffle(double*, size_t); uint64_t i_len(lua_State*,int); +int l_contains(lua_State*); int l_len(lua_State*); //[double+int] -> i int l_reverse(lua_State*); //[double+int] -> arr[N] int l_max(lua_State*); //[double+int] -> i @@ -28,6 +29,7 @@ int l_dup(lua_State*); #warning "docs needed here" static const luaL_Reg table_function_list [] = { + {"contains",l_contains}, {"len",l_len}, {"reverse",l_reverse}, //no docs {"max",l_max}, //no docs |
