aboutsummaryrefslogtreecommitdiff
path: root/src
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
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')
-rw-r--r--src/lapi.js10
-rw-r--r--src/lauxlib.js10
-rw-r--r--src/linit.js8
-rw-r--r--src/lmathlib.js70
-rw-r--r--src/lvm.js2
5 files changed, 95 insertions, 5 deletions
diff --git a/src/lapi.js b/src/lapi.js
index b39046b..4eb55cd 100644
--- a/src/lapi.js
+++ b/src/lapi.js
@@ -603,6 +603,14 @@ const lua_istable = function(L, idx) {
return index2addr(L, idx).ttistable();
};
+const lua_isinteger = function(L, idx) {
+ return index2addr(L, idx).ttisinteger();
+};
+
+const lua_isnumber = function(L, idx) {
+ return lvm.tonumber(L, idx);
+};
+
const lua_isstring = function(L, idx) {
let o = index2addr(L, idx);
return o.ttisstring() || o.ttisnumber();
@@ -789,7 +797,9 @@ module.exports.lua_getmetatable = lua_getmetatable;
module.exports.lua_gettable = lua_gettable;
module.exports.lua_gettop = lua_gettop;
module.exports.lua_insert = lua_insert;
+module.exports.lua_isinteger = lua_isinteger;
module.exports.lua_isnoneornil = lua_isnoneornil;
+module.exports.lua_isnumber = lua_isnumber;
module.exports.lua_isstring = lua_isstring;
module.exports.lua_istable = lua_istable;
module.exports.lua_len = lua_len;
diff --git a/src/lauxlib.js b/src/lauxlib.js
index 3774309..728469a 100644
--- a/src/lauxlib.js
+++ b/src/lauxlib.js
@@ -179,6 +179,13 @@ const interror = function(L, arg) {
tag_error(L, arg, CT.LUA_TNUMBER);
};
+const luaL_checknumber = function(L, arg) {
+ let d = lapi.lua_tonumber(L, arg);
+ if (d === false)
+ tag_error(L, arg, CT.LUA_TNUMBER);
+ return d;
+};
+
const luaL_checkinteger = function(L, arg) {
let d = lapi.lua_tointeger(L, arg);
if (d === false)
@@ -356,17 +363,18 @@ const luaL_newlib = function(L, l) {
};
module.exports.LUA_LOADED_TABLE = LUA_LOADED_TABLE;
+module.exports.luaL_Buffer = luaL_Buffer;
module.exports.luaL_addlstring = luaL_addlstring;
module.exports.luaL_addstring = luaL_addstring;
module.exports.luaL_addvalue = luaL_addvalue;
module.exports.luaL_argcheck = luaL_argcheck;
module.exports.luaL_argerror = luaL_argerror;
-module.exports.luaL_Buffer = luaL_Buffer;
module.exports.luaL_buffinit = luaL_buffinit;
module.exports.luaL_callmeta = luaL_callmeta;
module.exports.luaL_checkany = luaL_checkany;
module.exports.luaL_checkinteger = luaL_checkinteger;
module.exports.luaL_checklstring = luaL_checklstring;
+module.exports.luaL_checknumber = luaL_checknumber;
module.exports.luaL_checkstack = luaL_checkstack;
module.exports.luaL_checktype = luaL_checktype;
module.exports.luaL_error = luaL_error;
diff --git a/src/linit.js b/src/linit.js
index eb8caf2..36c0121 100644
--- a/src/linit.js
+++ b/src/linit.js
@@ -9,11 +9,13 @@ const lualib = require('./lualib.js');
const lbaselib = require('./lbaselib.js');
const lcorolib = require('./lcorolib.js');
const ltablib = require('./ltablib.js');
+const lmathlib = require('./lmathlib.js');
const loadedlibs = {
- [lualib.LUA_TABLIBNAME]: ltablib.luaopen_table,
- [lualib.LUA_COLIBNAME]: lcorolib.luaopen_coroutine,
- "_G": lbaselib.luaopen_base
+ [lualib.LUA_MATHLIBNAME]: lmathlib.luaopen_math,
+ [lualib.LUA_TABLIBNAME]: ltablib.luaopen_table,
+ [lualib.LUA_COLIBNAME]: lcorolib.luaopen_coroutine,
+ "_G": lbaselib.luaopen_base
};
const luaL_openlibs = function(L) {
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
diff --git a/src/lvm.js b/src/lvm.js
index a0822cc..d623a72 100644
--- a/src/lvm.js
+++ b/src/lvm.js
@@ -845,7 +845,7 @@ const luaV_tointeger = function(obj, mode) {
};
const tointeger = function(o) {
- return o.ttisinteger() ? o.value : luaV_tointeger(o, 0);
+ return o.ttisinteger() ? o.value|0 : luaV_tointeger(o, 0);
};
const tonumber = function(v) {