diff options
author | daurnimator <quae@daurnimator.com> | 2017-04-26 17:55:37 +1000 |
---|---|---|
committer | daurnimator <quae@daurnimator.com> | 2017-04-26 18:06:57 +1000 |
commit | b688ef577a10e8b6f2cf948faaa8d1af70c7949c (patch) | |
tree | 0ff07d75f65442b92d8a95259b9ab6845e66b27a /src/lmathlib.js | |
parent | 53bd1fcc4b90f24427064419354ad07c552a93d5 (diff) | |
download | fengari-b688ef577a10e8b6f2cf948faaa8d1af70c7949c.tar.gz fengari-b688ef577a10e8b6f2cf948faaa8d1af70c7949c.tar.bz2 fengari-b688ef577a10e8b6f2cf948faaa8d1af70c7949c.zip |
Export lapi.js functions from lua.js
Diffstat (limited to 'src/lmathlib.js')
-rw-r--r-- | src/lmathlib.js | 113 |
1 files changed, 56 insertions, 57 deletions
diff --git a/src/lmathlib.js b/src/lmathlib.js index 9603d2e..4f323c6 100644 --- a/src/lmathlib.js +++ b/src/lmathlib.js @@ -4,7 +4,6 @@ const assert = require('assert'); const seedrandom = require('seedrandom'); const lua = require('./lua.js'); -const lapi = require('./lapi.js'); const lauxlib = require('./lauxlib.js'); const lstate = require('./lstate.js'); const ldo = require('./ldo.js'); @@ -21,9 +20,9 @@ const math_randomseed = function(L) { const math_random = function(L) { let low, up; let r = RNG(); - switch (lapi.lua_gettop(L)) { /* check number of arguments */ + switch (lua.lua_gettop(L)) { /* check number of arguments */ case 0: - lapi.lua_pushnumber(L, r); /* Number between 0 and 1 */ + lua.lua_pushnumber(L, r); /* Number between 0 and 1 */ return 1; case 1: { low = 1; @@ -44,55 +43,55 @@ const math_random = function(L) { lua.to_luastring("interval too large", true)); r *= (up - low) + 1; - lapi.lua_pushinteger(L, r + low); + lua.lua_pushinteger(L, r + low); return 1; }; const math_abs = function(L) { - if (lapi.lua_isinteger(L, 1)) - lapi.lua_pushinteger(L, Math.abs(lapi.lua_tointeger(L, 1))); + if (lua.lua_isinteger(L, 1)) + lua.lua_pushinteger(L, Math.abs(lua.lua_tointeger(L, 1))); else - lapi.lua_pushnumber(L, Math.abs(lauxlib.luaL_checknumber(L, 1))); + lua.lua_pushnumber(L, Math.abs(lauxlib.luaL_checknumber(L, 1))); return 1; }; const math_sin = function(L) { - lapi.lua_pushnumber(L, Math.sin(lauxlib.luaL_checknumber(L, 1))); + lua.lua_pushnumber(L, Math.sin(lauxlib.luaL_checknumber(L, 1))); return 1; }; const math_cos = function(L) { - lapi.lua_pushnumber(L, Math.cos(lauxlib.luaL_checknumber(L, 1))); + lua.lua_pushnumber(L, Math.cos(lauxlib.luaL_checknumber(L, 1))); return 1; }; const math_tan = function(L) { - lapi.lua_pushnumber(L, Math.tan(lauxlib.luaL_checknumber(L, 1))); + lua.lua_pushnumber(L, Math.tan(lauxlib.luaL_checknumber(L, 1))); return 1; }; const math_asin = function(L) { - lapi.lua_pushnumber(L, Math.asin(lauxlib.luaL_checknumber(L, 1))); + lua.lua_pushnumber(L, Math.asin(lauxlib.luaL_checknumber(L, 1))); return 1; }; const math_acos = function(L) { - lapi.lua_pushnumber(L, Math.acos(lauxlib.luaL_checknumber(L, 1))); + lua.lua_pushnumber(L, Math.acos(lauxlib.luaL_checknumber(L, 1))); return 1; }; const math_atan = function(L) { - lapi.lua_pushnumber(L, Math.atan(lauxlib.luaL_checknumber(L, 1))); + lua.lua_pushnumber(L, Math.atan(lauxlib.luaL_checknumber(L, 1))); return 1; }; const math_toint = function(L) { - let n = lapi.lua_tointegerx(L, 1); + let n = lua.lua_tointegerx(L, 1); if (n !== false) - lapi.lua_pushinteger(L, n); + lua.lua_pushinteger(L, n); else { lauxlib.luaL_checkany(L, 1); - lapi.lua_pushnil(L); /* value is not convertible to integer */ + lua.lua_pushnil(L); /* value is not convertible to integer */ } return 1; }; @@ -100,14 +99,14 @@ const math_toint = function(L) { 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 */ + lua.lua_pushinteger(L, n); /* result is integer */ else - lapi.lua_pushnumber(L, d); /* result is float */ + lua.lua_pushnumber(L, d); /* result is float */ }; const math_floor = function(L) { - if (lapi.lua_isinteger(L, 1)) - lapi.lua_settop(L, 1); + if (lua.lua_isinteger(L, 1)) + lua.lua_settop(L, 1); else pushnumint(L, Math.floor(lauxlib.luaL_checknumber(L, 1))); @@ -115,8 +114,8 @@ const math_floor = function(L) { }; const math_ceil = function(L) { - if (lapi.lua_isinteger(L, 1)) - lapi.lua_settop(L, 1); + if (lua.lua_isinteger(L, 1)) + lua.lua_settop(L, 1); else pushnumint(L, Math.ceil(lauxlib.luaL_checknumber(L, 1))); @@ -124,21 +123,21 @@ const math_ceil = function(L) { }; const math_sqrt = function(L) { - lapi.lua_pushnumber(L, Math.sqrt(lauxlib.luaL_checknumber(L, 1))); + lua.lua_pushnumber(L, Math.sqrt(lauxlib.luaL_checknumber(L, 1))); return 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)); + lua.lua_pushboolean(L, Math.abs(a) < Math.abs(b)); return 1; }; const math_log = function(L) { let x = lauxlib.luaL_checknumber(L, 1); let res; - if (lapi.lua_isnoneornil(L, 2)) + if (lua.lua_isnoneornil(L, 2)) res = Math.log(x); else { let base = lauxlib.luaL_checknumber(L, 2); @@ -149,87 +148,87 @@ const math_log = function(L) { else res = Math.log(x)/Math.log(base); } - lapi.lua_pushnumber(L, res); + lua.lua_pushnumber(L, res); return 1; }; const math_exp = function(L) { - lapi.lua_pushnumber(L, Math.exp(lauxlib.luaL_checknumber(L, 1))); + lua.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)); + lua.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)); + lua.lua_pushnumber(L, lauxlib.luaL_checknumber(L, 1) * (Math.PI / 180)); return 1; }; const math_min = function(L) { - let n = lapi.lua_gettop(L); /* number of arguments */ + let n = lua.lua_gettop(L); /* number of arguments */ let imin = 1; /* index of current minimum value */ lauxlib.luaL_argcheck(L, n >= 1, 1, lua.to_luastring("value expected", true)); for (let i = 2; i <= n; i++){ - if (lapi.lua_compare(L, i, imin, lua.LUA_OPLT)) + if (lua.lua_compare(L, i, imin, lua.LUA_OPLT)) imin = i; } - lapi.lua_pushvalue(L, imin); + lua.lua_pushvalue(L, imin); return 1; }; const math_max = function(L) { - let n = lapi.lua_gettop(L); /* number of arguments */ + let n = lua.lua_gettop(L); /* number of arguments */ let imax = 1; /* index of current minimum value */ lauxlib.luaL_argcheck(L, n >= 1, 1, lua.to_luastring("value expected", true)); for (let i = 2; i <= n; i++){ - if (lapi.lua_compare(L, imax, i, lua.LUA_OPLT)) + if (lua.lua_compare(L, imax, i, lua.LUA_OPLT)) imax = i; } - lapi.lua_pushvalue(L, imax); + lua.lua_pushvalue(L, imax); return 1; }; const math_type = function(L) { - if (lapi.lua_type(L, 1) === lua.LUA_TNUMBER) { - if (lapi.lua_isinteger(L, 1)) - lapi.lua_pushliteral(L, "integer"); + if (lua.lua_type(L, 1) === lua.LUA_TNUMBER) { + if (lua.lua_isinteger(L, 1)) + lua.lua_pushliteral(L, "integer"); else - lapi.lua_pushliteral(L, "float"); + lua.lua_pushliteral(L, "float"); } else { lauxlib.luaL_checkany(L, 1); - lapi.lua_pushnil(L); + lua.lua_pushnil(L); } return 1; }; const math_fmod = function(L) { - if (lapi.lua_isinteger(L, 1) && lapi.lua_isinteger(L, 2)) { - let d = lapi.lua_tointeger(L, 2); + if (lua.lua_isinteger(L, 1) && lua.lua_isinteger(L, 2)) { + let d = lua.lua_tointeger(L, 2); if (Math.abs(d) + 1 <= 1) { lauxlib.luaL_argcheck(L, d !== 0, 2, lua.to_luastring("zero", true)); - lapi.lua_pushinteger(L, 0); + lua.lua_pushinteger(L, 0); } else - lapi.lua_pushinteger(L, lapi.lua_tointeger(L, 1) % d); + lua.lua_pushinteger(L, lua.lua_tointeger(L, 1) % d); } else { let a = lauxlib.luaL_checknumber(L, 1); let b = lauxlib.luaL_checknumber(L, 2); - lapi.lua_pushnumber(L, Number((a - (Math.floor(a / b) * b)).toPrecision(8))); + lua.lua_pushnumber(L, Number((a - (Math.floor(a / b) * b)).toPrecision(8))); } return 1; }; const math_modf = function(L) { - if (lapi.lua_isinteger(L, 1)) { - lapi.lua_settop(L, 1); /* number is its own integer part */ - lapi.lua_pushnumber(L, 0); /* no fractional part */ + if (lua.lua_isinteger(L, 1)) { + lua.lua_settop(L, 1); /* number is its own integer part */ + lua.lua_pushnumber(L, 0); /* no fractional part */ } else { let n = lauxlib.luaL_checknumber(L, 1); let ip = n < 0 ? Math.ceil(n) : Math.floor(n); pushnumint(L, ip); - lapi.lua_pushnumber(L, n === ip ? 0 : n - ip); + lua.lua_pushnumber(L, n === ip ? 0 : n - ip); } return 2; }; @@ -262,14 +261,14 @@ const mathlib = { const luaopen_math = function(L) { lauxlib.luaL_newlib(L, mathlib); - lapi.lua_pushnumber(L, Math.PI); - lapi.lua_setfield(L, -2, lua.to_luastring("pi", true)); - lapi.lua_pushnumber(L, Number.MAX_VALUE); - lapi.lua_setfield(L, -2, lua.to_luastring("huge", true)); - lapi.lua_pushinteger(L, llimit.MAX_INT); - lapi.lua_setfield(L, -2, lua.to_luastring("maxinteger", true)); - lapi.lua_pushinteger(L, llimit.MIN_INT); - lapi.lua_setfield(L, -2, lua.to_luastring("mininteger", true)); + lua.lua_pushnumber(L, Math.PI); + lua.lua_setfield(L, -2, lua.to_luastring("pi", true)); + lua.lua_pushnumber(L, Number.MAX_VALUE); + lua.lua_setfield(L, -2, lua.to_luastring("huge", true)); + lua.lua_pushinteger(L, llimit.MAX_INT); + lua.lua_setfield(L, -2, lua.to_luastring("maxinteger", true)); + lua.lua_pushinteger(L, llimit.MIN_INT); + lua.lua_setfield(L, -2, lua.to_luastring("mininteger", true)); return 1; }; |