aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--README.md12
-rw-r--r--src/lapi.js32
-rw-r--r--src/lvm.js11
-rw-r--r--tests/C/lua_pushinteger.c2
-rw-r--r--tests/lapi.js14
5 files changed, 51 insertions, 20 deletions
diff --git a/README.md b/README.md
index ad85079..62c8803 100644
--- a/README.md
+++ b/README.md
@@ -32,6 +32,12 @@
- [x] lua_pushnumber
- [x] lua_pushstring
- [x] lua_pushvalue
+ - [x] lua_tointeger
+ - [x] lua_tointegerx
+ - [x] lua_tolstring
+ - [x] lua_tonumber
+ - [x] lua_tonumberx
+ - [x] lua_toboolean
- [ ] lua_absindex
- [ ] lua_arith
- [ ] lua_call
@@ -120,13 +126,7 @@
- [ ] lua_setuservalue
- [ ] lua_status
- [ ] lua_stringtonumber
- - [ ] lua_toboolean
- [ ] lua_tocfunction
- - [ ] lua_tointeger
- - [ ] lua_tointegerx
- - [ ] lua_tolstring
- - [ ] lua_tonumber
- - [ ] lua_tonumberx
- [ ] lua_topointer
- [ ] lua_tostring
- [ ] lua_tothread
diff --git a/src/lapi.js b/src/lapi.js
index dc60984..6aba915 100644
--- a/src/lapi.js
+++ b/src/lapi.js
@@ -9,11 +9,11 @@ const ltm = require('./ltm.js');
const lfunc = require('./lfunc.js');
const lua = require('./lua.js');
const lstate = require('./lstate.js');
+const lvm = require('./lvm.js');
const nil = ldo.nil;
const MAXUPVAL = lfunc.MAXUPVAL;
const CT = lua.constant_types;
const TS = lua.thread_status;
-const l_isfalse = lobject.l_isfalse;
const TValue = lobject.TValue;
const CClosure = lobject.CClosure;
@@ -169,7 +169,28 @@ const lua_pushlightuserdata = function(L, p) {
const lua_toboolean = function(L, idx) {
let o = index2addr(L, idx);
- return !l_isfalse(o);
+ return !o.l_isfalse();
+};
+
+const lua_tolstring = function(L, idx, len) {
+ let o = index2addr(L, idx);
+
+ if (!o.ttisstring() && !o.ttisnumber())
+ return null;
+
+ return len !== null ? `${o.value}`.substr(0, len) : `${o.value}`;
+};
+
+const lua_tostring = function(L, idx) {
+ return lua_tolstring(L, idx, null);
+};
+
+const lua_tointeger = function(L, idx) {
+ return lvm.tointeger(index2addr(L, idx))
+};
+
+const lua_tonumber = function(L, idx) {
+ return lvm.tonumber(index2addr(L, idx))
};
const f_call = function(L, ud) {
@@ -251,4 +272,9 @@ module.exports.lua_version = lua_version;
module.exports.lua_atpanic = lua_atpanic;
module.exports.lua_gettop = lua_gettop;
module.exports.lua_typename = lua_typename;
-module.exports.lua_type = lua_type; \ No newline at end of file
+module.exports.lua_type = lua_type;
+module.exports.lua_tonumber = lua_tonumber;
+module.exports.lua_tointeger = lua_tointeger;
+module.exports.lua_toboolean = lua_toboolean;
+module.exports.lua_tolstring = lua_tolstring;
+module.exports.lua_tostring = lua_tostring; \ No newline at end of file
diff --git a/src/lvm.js b/src/lvm.js
index e2c78b3..931b7a5 100644
--- a/src/lvm.js
+++ b/src/lvm.js
@@ -785,11 +785,15 @@ const luaV_tointeger = function(obj, mode) {
return false;
};
+const tointeger = function(o) {
+ return o.ttisinteger() ? o.value : luaV_tointeger(o, 0);
+};
+
const tonumber = function(v) {
- if (v.type === CT.LUA_TNUMFLT || v.type === CT.LUA_TNUMINT)
+ if (v.ttnov() === CT.LUA_TNUMBER)
return v.value;
- if (v.type === CT.LUA_TSHRSTR || v.type === CT.LUA_TLNGSTR)
+ if (v.ttnov() === CT.LUA_TSTRING)
return parseFloat(v.value); // TODO: luaO_str2num
return false;
@@ -873,7 +877,7 @@ const tostring = function(L, i) {
let o = L.stack[i];
let str = `${o.value}`;
- if (o.ttisstring() || (o.ttisnumber() && !isNaN(parseFloat(`${str}`)))) {
+ if (o.ttisstring() || (o.ttisnumber() && !isNaN(str))) {
L.stack[i] = new TValue(CT.LUA_TLNGSTR, str);
return true;
}
@@ -1023,6 +1027,7 @@ module.exports.luaV_equalobj = luaV_equalobj;
module.exports.forlimit = forlimit;
module.exports.luaV_tointeger = luaV_tointeger;
module.exports.tonumber = tonumber;
+module.exports.tointeger = tointeger;
module.exports.LTnum = LTnum;
module.exports.LEnum = LEnum;
module.exports.LEintfloat = LEintfloat;
diff --git a/tests/C/lua_pushinteger.c b/tests/C/lua_pushinteger.c
index 0c9a49f..da134b6 100644
--- a/tests/C/lua_pushinteger.c
+++ b/tests/C/lua_pushinteger.c
@@ -12,7 +12,7 @@ int main(void) {
lua_pushinteger(L, 10);
- printf("L->top(%d): %s\n", lua_gettop(L), luaL_typename(L, lua_gettop(L)));
+ printf("L->top(%d): type %s, value %ld\n", lua_gettop(L), luaL_typename(L, lua_gettop(L)), lua_tointeger(L, -1));
lua_close(L);
diff --git a/tests/lapi.js b/tests/lapi.js
index 8dd2680..875a89c 100644
--- a/tests/lapi.js
+++ b/tests/lapi.js
@@ -65,7 +65,7 @@ test('lua_pushnumber', function (t) {
);
t.strictEqual(
- L.stack[lapi.lua_gettop(L)].value,
+ lapi.lua_tonumber(L, -1),
10.5,
"top is correct"
);
@@ -81,7 +81,7 @@ test('lua_pushinteger', function (t) {
L = lauxlib.luaL_newstate();
- lapi.lua_pushnumber(L, 10);
+ lapi.lua_pushinteger(L, 10);
}, "JS Lua program ran without error");
@@ -98,7 +98,7 @@ test('lua_pushinteger', function (t) {
);
t.strictEqual(
- L.stack[lapi.lua_gettop(L)].value,
+ lapi.lua_tointeger(L, -1),
10,
"top is correct"
);
@@ -131,7 +131,7 @@ test('lua_pushstring', function (t) {
);
t.strictEqual(
- L.stack[lapi.lua_gettop(L)].value,
+ lapi.lua_tostring(L, -1),
"hello",
"top is correct"
);
@@ -164,7 +164,7 @@ test('lua_pushboolean', function (t) {
);
t.strictEqual(
- L.stack[lapi.lua_gettop(L)].value,
+ lapi.lua_toboolean(L, -1),
true,
"top is correct"
);
@@ -205,13 +205,13 @@ test('lua_pushvalue', function (t) {
);
t.strictEqual(
- L.stack[lapi.lua_gettop(L)].value,
+ lapi.lua_tostring(L, -1),
"hello",
"top is correct"
);
t.strictEqual(
- L.stack[lapi.lua_gettop(L) - 1].value,
+ lapi.lua_tostring(L, -2),
"hello",
"top is correct"
);