summaryrefslogtreecommitdiff
path: root/src/lmathlib.js
diff options
context:
space:
mode:
authorBenoit Giannangeli <benoit.giannangeli@boursorama.fr>2017-02-24 15:05:44 +0100
committerBenoit Giannangeli <benoit.giannangeli@boursorama.fr>2017-02-24 15:53:08 +0100
commit0a006ad403733a85abe5be3f242c0264a4556afb (patch)
tree008b1cf32172a0fc17ddf2ff369a1b77e1618bc4 /src/lmathlib.js
parentfc79b2ae7a85af1b892a103340b9465274153c60 (diff)
downloadfengari-0a006ad403733a85abe5be3f242c0264a4556afb.tar.gz
fengari-0a006ad403733a85abe5be3f242c0264a4556afb.tar.bz2
fengari-0a006ad403733a85abe5be3f242c0264a4556afb.zip
math.abs, math.sin, math.cos, math.tan, math.asin, math.acos, math.atan
Diffstat (limited to 'src/lmathlib.js')
-rw-r--r--src/lmathlib.js70
1 files changed, 70 insertions, 0 deletions
diff --git a/src/lmathlib.js b/src/lmathlib.js
new file mode 100644
index 0000000..d1daca5
--- /dev/null
+++ b/src/lmathlib.js
@@ -0,0 +1,70 @@
+/* jshint esversion: 6 */
+"use strict";
+
+const assert = require('assert');
+
+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');
+const ldebug = require('./ldebug.js');
+const llimit = require('./llimit.js');
+const CT = lua.constant_types;
+const TS = lua.thread_status;
+
+
+const math_abs = function(L) {
+ if (lapi.lua_isinteger(L, 1))
+ lapi.lua_pushinteger(L, Math.abs(lapi.lua_tointeger(L, 1)));
+ else
+ lapi.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)));
+ return 1;
+};
+
+const math_cos = function(L) {
+ lapi.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)));
+ return 1;
+};
+
+const math_asin = function(L) {
+ lapi.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)));
+ return 1;
+};
+
+const math_atan = function(L) {
+ lapi.lua_pushnumber(L, Math.atan(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
+};
+
+const luaopen_math = function(L) {
+ lauxlib.luaL_newlib(L, mathlib);
+ return 1;
+};
+
+module.exports.luaopen_math = luaopen_math; \ No newline at end of file