aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
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) {