aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/lapi.js10
-rw-r--r--tests/lbaselib.js15
2 files changed, 19 insertions, 6 deletions
diff --git a/src/lapi.js b/src/lapi.js
index e035aca..307d4e3 100644
--- a/src/lapi.js
+++ b/src/lapi.js
@@ -731,9 +731,13 @@ const lua_compare = function(L, index1, index2, op) {
};
const lua_stringtonumber = function(L, s) {
- L.stack[L.top++] = lobject.luaO_str2num(s);
- assert(L.top <= L.ci.top, "stack overflow");
- return s.length;
+ let tv = lobject.luaO_str2num(s);
+ if (tv) {
+ L.stack[L.top++] = tv;
+ assert(L.top <= L.ci.top, "stack overflow");
+ return s.length;
+ }
+ return 0;
};
const lua_tointegerx = function(L, idx) {
diff --git a/tests/lbaselib.js b/tests/lbaselib.js
index 0a77e5b..ca83467 100644
--- a/tests/lbaselib.js
+++ b/tests/lbaselib.js
@@ -438,10 +438,14 @@ test('select', function (t) {
test('tonumber', function (t) {
let luaCode = `
- return tonumber('123'), tonumber('12.3'), tonumber('az', 36), tonumber('10', 2)
+ return tonumber('foo'),
+ tonumber('123'),
+ tonumber('12.3'),
+ tonumber('az', 36),
+ tonumber('10', 2)
`, L;
-
- t.plan(5);
+
+ t.plan(6);
t.doesNotThrow(function () {
@@ -457,6 +461,11 @@ test('tonumber', function (t) {
}, "JS Lua program ran without error");
+ t.ok(
+ lua.lua_isnil(L, -5),
+ "Correct element(s) on the stack"
+ );
+
t.strictEqual(
lua.lua_tonumber(L, -4),
123,