aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--library/lullaby/table.lua6
-rw-r--r--src/lua.h7
-rw-r--r--src/math.c4
-rw-r--r--src/table.c19
-rw-r--r--src/table.h2
5 files changed, 35 insertions, 3 deletions
diff --git a/library/lullaby/table.lua b/library/lullaby/table.lua
index ed6ea6c..b5ee919 100644
--- a/library/lullaby/table.lua
+++ b/library/lullaby/table.lua
@@ -15,6 +15,12 @@ local table = {}
---@return string[]
function table.split(haystack, search, skip) end
+---returns the index that N is located at, or nil
+---@param t any[]
+---@param N any
+---@return any
+function table.contains(t, N) end
+
---compares 2 tables recursivley
---@param A any[]
---@param B any[]
diff --git a/src/lua.h b/src/lua.h
index 834ed85..af6e8ab 100644
--- a/src/lua.h
+++ b/src/lua.h
@@ -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
diff --git a/src/math.c b/src/math.c
index e7cd1bb..47e8391 100644
--- a/src/math.c
+++ b/src/math.c
@@ -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