summaryrefslogtreecommitdiff
path: root/src/ltablib.js
diff options
context:
space:
mode:
authorBenoit Giannangeli <benoit.giannangeli@boursorama.fr>2017-02-24 14:01:02 +0100
committerBenoit Giannangeli <benoit.giannangeli@boursorama.fr>2017-02-24 14:01:02 +0100
commitfc79b2ae7a85af1b892a103340b9465274153c60 (patch)
tree5e3dc8cab20bbbe994390eda53007c08068feeff /src/ltablib.js
parent2f9fe378bc341921e1ae259a2fec049663100738 (diff)
downloadfengari-fc79b2ae7a85af1b892a103340b9465274153c60.tar.gz
fengari-fc79b2ae7a85af1b892a103340b9465274153c60.tar.bz2
fengari-fc79b2ae7a85af1b892a103340b9465274153c60.zip
table.sort
Using Array.prototype.sort for now
Diffstat (limited to 'src/ltablib.js')
-rw-r--r--src/ltablib.js44
1 files changed, 44 insertions, 0 deletions
diff --git a/src/ltablib.js b/src/ltablib.js
index 97ecd65..fa43523 100644
--- a/src/ltablib.js
+++ b/src/ltablib.js
@@ -180,12 +180,56 @@ const unpack = function(L) {
return n;
};
+
+// TODO: Maybe do the quicksort after all
+const auxsort = function(L) {
+ let t = lapi.index2addr(L, 1);
+
+ if (lapi.lua_type(L, 2) !== CT.LUA_TFUNCTION) { /* no function? */
+ [...t.value.entries()]
+ .sort(function (a, b) {
+ if (typeof a[0] !== 'number') return 1;
+ else if (typeof b[0] !== 'number') return -1;
+ return lapi.lua_compare_(L, a[1], b[1], lua.LUA_OPLT) === 1 ? -1 : 1; /* a < b */
+ })
+ .forEach((e, i) => typeof e[0] === 'number' ? t.value.set(i, e[1]) : true);
+ } else {
+ [...t.value.entries()]
+ .sort(function (a, b) {
+ if (typeof a[0] !== 'number') return 1;
+ else if (typeof b[0] !== 'number') return -1;
+
+ lapi.lua_pushvalue(L, 2); /* push function */
+ lapi.lua_pushtvalue(L, a[1]); /* since we use Map.sort, a and b are not on the stack */
+ lapi.lua_pushtvalue(L, b[1]);
+ lapi.lua_call(L, 2, 1); /* call function */
+ let res = lapi.lua_toboolean(L, -1); /* get result */
+ lapi.lua_pop(L, 1); /* pop result */
+ return res ? -1 : 1;
+ })
+ .forEach((e, i) => typeof e[0] === 'number' ? t.value.set(i, e[1]) : true);
+ }
+};
+
+const sort = function(L) {
+ let n = aux_getn(L, 1, TAB_RW);
+ if (n > 1) { /* non-trivial interval? */
+ lauxlib.luaL_argcheck(L, n < Number.MAX_SAFE_INTEGER, 1, "array too big");
+ if (!lapi.lua_isnoneornil(L, 2)) /* is there a 2nd argument? */
+ lauxlib.luaL_checktype(L, 2, CT.LUA_TFUNCTION); /* must be a function */
+ lapi.lua_settop(L, 2); /* make sure there are two arguments */
+ auxsort(L);
+ }
+ return 0;
+};
+
const tab_funcs = {
"concat": tconcat,
"insert": tinsert,
"move": tmove,
"pack": pack,
"remove": tremove,
+ "sort": sort,
"unpack": unpack
};