summaryrefslogtreecommitdiff
path: root/src/lmathlib.js
diff options
context:
space:
mode:
authorBenoit Giannangeli <giann008@gmail.com>2017-02-25 14:49:09 +0100
committerBenoit Giannangeli <giann008@gmail.com>2017-02-25 14:49:09 +0100
commit9e2ace863b568ed7c3ef42bf687c7cafd3a71bb7 (patch)
treee750c42fc3b75938c6f223d3d3226d399eb35dfd /src/lmathlib.js
parent0a006ad403733a85abe5be3f242c0264a4556afb (diff)
downloadfengari-9e2ace863b568ed7c3ef42bf687c7cafd3a71bb7.tar.gz
fengari-9e2ace863b568ed7c3ef42bf687c7cafd3a71bb7.tar.bz2
fengari-9e2ace863b568ed7c3ef42bf687c7cafd3a71bb7.zip
math.tointeger, math.floor, math.ceil
Diffstat (limited to 'src/lmathlib.js')
-rw-r--r--src/lmathlib.js54
1 files changed, 47 insertions, 7 deletions
diff --git a/src/lmathlib.js b/src/lmathlib.js
index d1daca5..9921cc9 100644
--- a/src/lmathlib.js
+++ b/src/lmathlib.js
@@ -52,14 +52,54 @@ const math_atan = function(L) {
return 1;
};
+const math_toint = function(L) {
+ let n = lapi.lua_tointegerx(L, 1)
+ if (n !== false)
+ lapi.lua_pushinteger(L, n);
+ else {
+ lauxlib.luaL_checkany(L, 1);
+ lapi.lua_pushnil(L); /* value is not convertible to integer */
+ }
+ return 1;
+};
+
+const pushnumint = function(L, d) {
+ let n = luaconf.lua_numbertointeger(d);
+ if (n !== false) /* does 'd' fit in an integer? */
+ lapi.lua_pushinteger(L, n); /* result is integer */
+ else
+ lapi.lua_pushnumber(L, d); /* result is float */
+};
+
+const math_floor = function(L) {
+ if (lapi.lua_isinteger(L, 1))
+ lapi.lua_settop(L, 1);
+ else
+ pushnumint(L, lauxlib.luaL_checknumber(L, 1));
+
+ return 1;
+};
+
+const math_ceil = function(L) {
+ if (lapi.lua_isinteger(L, 1))
+ lapi.lua_settop(L, 1);
+ else
+ pushnumint(L, Math.ceil(lauxlib.luaL_checknumber(L, 1)));
+
+ return 1;
+};
+
const mathlib = {
- "abs": math_abs,
- "sin": math_sin,
- "cos": math_cos,
- "tan": math_tan,
- "asin": math_asin,
- "acos": math_acos,
- "atan": math_atan
+ "abs": math_abs,
+ "acos": math_acos,
+ "asin": math_asin,
+ "atan": math_atan,
+ "ceil": math_ceil,
+ "cos": math_cos,
+ "floor": math_floor,
+ "sin": math_sin,
+ "tan": math_tan,
+ "tointeger": math_toint
};
const luaopen_math = function(L) {