Add div to allow safe integer division from lua
authornemo
Sat, 10 Mar 2012 23:00:39 -0500
changeset 6764 e56ff0231540
parent 6763 8a06161ede49
child 6765 8fcd2508c49f
Add div to allow safe integer division from lua
hedgewars/uScript.pas
--- a/hedgewars/uScript.pas	Sat Mar 10 16:32:46 2012 -0500
+++ b/hedgewars/uScript.pas	Sat Mar 10 23:00:39 2012 -0500
@@ -142,6 +142,18 @@
     lc_bnot := 1;
 end;
 
+function lc_div(L: PLua_State): LongInt; Cdecl;
+begin
+    if lua_gettop(L) <> 2 then 
+        begin
+        LuaError('Lua: Wrong number of parameters passed to div!');
+        lua_pushnil(L);
+        end
+    else
+        lua_pushinteger(L, lua_tointeger(L, 2) div lua_tointeger(L, 1));
+    lc_div := 1;
+end;
+
 function lc_getinputmask(L : Plua_State) : LongInt; Cdecl;
 begin
     if lua_gettop(L) <> 0 then
@@ -2133,6 +2145,7 @@
 lua_register(luaState, 'band', @lc_band);
 lua_register(luaState, 'bor', @lc_bor);
 lua_register(luaState, 'bnot', @lc_bnot);
+lua_register(luaState, 'div', @lc_div);
 lua_register(luaState, 'GetInputMask', @lc_getinputmask);
 lua_register(luaState, 'SetInputMask', @lc_setinputmask);
 lua_register(luaState, 'AddGear', @lc_addgear);