summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordenis <denisv@post.skynet.lt>2017-09-07 03:44:37 +0300
committerdenis <denisv@post.skynet.lt>2017-09-07 15:24:00 +0300
commitb482e19917395c842780bd2b8da7731158947c40 (patch)
treed8951ce0846874d36a22f9e0d7bfbc2b0b5993af
parentfc3d60b9a7cd7b8e46a9b342ec504d84c927aa08 (diff)
downloadfengari-b482e19917395c842780bd2b8da7731158947c40.tar.gz
fengari-b482e19917395c842780bd2b8da7731158947c40.tar.bz2
fengari-b482e19917395c842780bd2b8da7731158947c40.zip
src/lapi.js, src/lauxlib.js: lua_tointeger should return 0 when conversion fails for luaL_ref to work
-rw-r--r--README.md6
-rw-r--r--src/lapi.js11
-rw-r--r--src/lauxlib.js2
-rw-r--r--tests/lauxlib.js30
4 files changed, 43 insertions, 6 deletions
diff --git a/README.md b/README.md
index aa54b96..e2a5e86 100644
--- a/README.md
+++ b/README.md
@@ -40,6 +40,12 @@ In the browser `require` and `package.loadlib` try to find a file by making sync
- `os.execute`
- [Weak tables](http://www.lua.org/manual/5.3/manual.html#2.5.2)
+
+### _Differences_ from C API
+
+- `lua_tointegerx` and `lua_tonumberx` do not have out-parameters indicating conversion success. Instead, ``false`` is returned when conversion fails.
+
+
## Extensions
### `dv = lua_todataview(L, idx)`
diff --git a/src/lapi.js b/src/lapi.js
index b7fed83..1be0837 100644
--- a/src/lapi.js
+++ b/src/lapi.js
@@ -708,11 +708,16 @@ const lua_tocfunction = function(L, idx) {
};
const lua_tointeger = function(L, idx) {
+ let n = lua_tointegerx(L, idx);
+ return n === false ? 0 : n;
+};
+
+const lua_tointegerx = function(L, idx) {
return lvm.tointeger(index2addr(L, idx));
};
const lua_tonumber = function(L, idx) {
- let n = lvm.tonumber(index2addr(L, idx));
+ let n = lua_tonumberx(L, idx);
return n === false ? 0 : n;
};
@@ -814,10 +819,6 @@ const lua_stringtonumber = function(L, s) {
return 0;
};
-const lua_tointegerx = function(L, idx) {
- return lvm.tointeger(index2addr(L, idx));
-};
-
const f_call = function(L, ud) {
ldo.luaD_callnoyield(L, ud.funcOff, ud.nresults);
};
diff --git a/src/lauxlib.js b/src/lauxlib.js
index b551d4f..ab8a8a0 100644
--- a/src/lauxlib.js
+++ b/src/lauxlib.js
@@ -343,7 +343,7 @@ const luaL_optnumber = function(L, arg, def) {
};
const luaL_checkinteger = function(L, arg) {
- let d = lua.lua_tointeger(L, arg);
+ let d = lua.lua_tointegerx(L, arg);
if (d === false)
interror(L, arg);
return d;
diff --git a/tests/lauxlib.js b/tests/lauxlib.js
new file mode 100644
index 0000000..4e37296
--- /dev/null
+++ b/tests/lauxlib.js
@@ -0,0 +1,30 @@
+"use strict";
+
+const test = require('tape');
+
+global.WEB = false;
+
+const lua = require('../src/lua.js');
+const lauxlib = require("../src/lauxlib.js");
+
+test('luaL_ref, lua_rawgeti, luaL_unref, LUA_REGISTRYINDEX', function (t) {
+ let L;
+
+ t.plan(2);
+
+ t.doesNotThrow(function () {
+ L = lauxlib.luaL_newstate();
+ lua.lua_pushstring(L, lua.to_luastring("hello references!"));
+
+ let r = lauxlib.luaL_ref(L, lua.LUA_REGISTRYINDEX); // pops a value, stores it and returns a reference
+ lua.lua_rawgeti(L, lua.LUA_REGISTRYINDEX, r); // pushes a value associated with the reference
+ lauxlib.luaL_unref(L, lua.LUA_REGISTRYINDEX, r); // releases the reference
+
+ }, "JS Lua program ran without error");
+
+ t.strictEqual(
+ lua.lua_tojsstring(L, -1),
+ "hello references!",
+ "top is correct"
+ );
+});