diff options
author | Benoit Giannangeli <giann008@gmail.com> | 2017-02-26 08:42:11 +0100 |
---|---|---|
committer | Benoit Giannangeli <giann008@gmail.com> | 2017-02-26 08:42:11 +0100 |
commit | 616d80301734074e55cedec8ba187b5cab89ea95 (patch) | |
tree | 127265d85dc6bd94556899c0cd40102db114f219 /src/lmathlib.js | |
parent | 9e2ace863b568ed7c3ef42bf687c7cafd3a71bb7 (diff) | |
download | fengari-616d80301734074e55cedec8ba187b5cab89ea95.tar.gz fengari-616d80301734074e55cedec8ba187b5cab89ea95.tar.bz2 fengari-616d80301734074e55cedec8ba187b5cab89ea95.zip |
math.sqrt, math.ult, math.log, math.exp, math.deg, math.rad, ...
math.min/max, math.type
Diffstat (limited to 'src/lmathlib.js')
-rw-r--r-- | src/lmathlib.js | 99 |
1 files changed, 98 insertions, 1 deletions
diff --git a/src/lmathlib.js b/src/lmathlib.js index 9921cc9..f4cc978 100644 --- a/src/lmathlib.js +++ b/src/lmathlib.js @@ -89,6 +89,85 @@ const math_ceil = function(L) { return 1; }; +const math_sqrt = function(L) { + lapi.lua_pushnumber(L, Math.sqrt(lauxlib.luaL_checknumber(L, 1))); +}; + +const math_ult = function(L) { + let a = lauxlib.luaL_checkinteger(L, 1); + let b = lauxlib.luaL_checkinteger(L, 2); + lapi.lua_pushboolean(L, Math.abs(a) < Math.abs(b)); + return 1; +}; + +const math_log = function(L) { + let x = lauxlib.luaL_checknumber(L, 1); + if (lapi.lua_isnoneornil(L, 2)) + res = Math.log(x); + else { + let base = lauxlib.luaL_checknumber(L, 2); + if (base === 2) + res = Math.log2(x); + else if (base === 10) + res = Math.log10(x); + else + res = Math.log(x)/Math.log(base); + } + lapi.lua_pushnumber(L, res); + return 1; +}; + +const math_exp = function(L) { + lapi.lua_pushnumber(L, Math.exp(lauxlib.luaL_checknumber(L, 1))); + return 1; +}; + +const math_deg = function(L) { + lapi.lua_pushnumber(L, lauxlib.luaL_checknumber(L, 1) * (180 / Math.PI)); + return 1; +}; + +const math_rad = function(L) { + lapi.lua_pushnumber(L, lauxlib.luaL_checknumber(L, 1) * (Math.PI / 180)); +}; + +const math_min = function(L) { + let n = lapi.lua_gettop(L); /* number of arguments */ + let imin = 1; /* index of current minimum value */ + lauxlib.luaL_argcheck(L, n >= 1, 1, "value expected"); + for (let i = 2; i <= n; i++){ + if (lapi.lua_compare(L, i, imin, lua.LUA_OPLT)) + imin = i; + } + lapi.lua_pushvalue(L, imin); + return 1; +}; + +const math_max = function(L) { + let n = lapi.lua_gettop(L); /* number of arguments */ + let imax = 1; /* index of current minimum value */ + lauxlib.luaL_argcheck(L, n >= 1, 1, "value expected"); + for (let i = 2; i <= n; i++){ + if (lapi.lua_compare(L, imax, i, lua.LUA_OPLT)) + imax = i; + } + lapi.lua_pushvalue(L, imax); + return 1; +}; + +const math_type = function(L) { + if (lapi.lua_type(L, 1) === CT.LUA_TNUMBER) { + if (lapi.lua_isinteger(L, 1)) + lapi.lua_pushliteral(L, "integer"); + else + lapi.lua_pushliteral(L, "float"); + } else { + lauxlib.luaL_checkany(L, 1); + lapi.lua_pushnil(L); + } + return 1; +}; + const mathlib = { "abs": math_abs, "acos": math_acos, @@ -96,14 +175,32 @@ const mathlib = { "atan": math_atan, "ceil": math_ceil, "cos": math_cos, + "deg": math_deg, + "exp": math_exp, "floor": math_floor, + "log": math_log, + "max": math_max, + "min": math_min, + "rad": math_rad, "sin": math_sin, + "sqrt": math_sqrt, + "sqrt": math_sqrt, "tan": math_tan, - "tointeger": math_toint + "tointeger": math_toint, + "type": math_type, + "ult": math_ult }; const luaopen_math = function(L) { lauxlib.luaL_newlib(L, mathlib); + lapi.lua_pushnumber(L, Math.PI); + lapi.lua_setfield(L, -2, "pi"); + lapi.lua_pushnumber(L, Number.MAX_VALUE); + lapi.lua_setfield(L, -2, "huge"); + lapi.lua_pushnumber(L, Number.MAX_SAFE_INTEGER); + lapi.lua_setfield(L, -2, "maxinteger"); + lapi.lua_pushnumber(L, Number.MIN_SAFE_INTEGER); + lapi.lua_setfield(L, -2, "mininteger"); return 1; }; |