summaryrefslogtreecommitdiff
path: root/src/ltablib.js
diff options
context:
space:
mode:
authordaurnimator <quae@daurnimator.com>2017-05-02 16:37:50 +1000
committerdaurnimator <quae@daurnimator.com>2017-05-03 12:20:43 +1000
commitcb0295e52870f22c5dd1a1726342b8d4b147b1c5 (patch)
tree3655d4d6f4873ed246c00c0eb6680a06124f3046 /src/ltablib.js
parent50820e54d065bffbb504e6b20b6c27802e102c25 (diff)
downloadfengari-cb0295e52870f22c5dd1a1726342b8d4b147b1c5.tar.gz
fengari-cb0295e52870f22c5dd1a1726342b8d4b147b1c5.tar.bz2
fengari-cb0295e52870f22c5dd1a1726342b8d4b147b1c5.zip
Refactor table implementation
Diffstat (limited to 'src/ltablib.js')
-rw-r--r--src/ltablib.js56
1 files changed, 34 insertions, 22 deletions
diff --git a/src/ltablib.js b/src/ltablib.js
index 78c71e4..929cf4b 100644
--- a/src/ltablib.js
+++ b/src/ltablib.js
@@ -4,6 +4,7 @@ const lua = require('./lua.js');
const lapi = require('./lapi.js');
const lauxlib = require('./lauxlib.js');
const llimit = require('./llimit.js');
+const ltable = require('./ltable.js');
/*
@@ -175,32 +176,43 @@ const unpack = function(L) {
// TODO: Maybe do the quicksort after all
const auxsort = function(L) {
- let t = lapi.index2addr(L, 1);
+ let a = [];
+ for (let i=1; ;i++) {
+ if(lua.lua_geti(L, 1, i) === lua.LUA_TNIL) {
+ lua.lua_pop(L, 1);
+ break;
+ }
+ let t = lapi.index2addr(L, -1);
+ a.push({
+ oldkey: i,
+ value: t
+ });
+ lua.lua_pop(L, 1);
+ }
+ let sort_function;
if (lua.lua_type(L, 2) !== lua.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 + 1, e[1]) : true);
+ sort_function = function (a, b) {
+ return lapi.lua_compare_(L, a.value, b.value, lua.LUA_OPLT) === 1 ? -1 : 1; /* a < b */
+ };
} else {
- [...t.value.entries()]
- .sort(function (a, b) {
- if (typeof a[0] !== 'number') return 1;
- else if (typeof b[0] !== 'number') return -1;
-
- lua.lua_pushvalue(L, 2); /* push function */
- lua.lua_pushtvalue(L, a[1]); /* since we use Map.sort, a and b are not on the stack */
- lua.lua_pushtvalue(L, b[1]);
- lua.lua_call(L, 2, 1); /* call function */
- let res = lua.lua_toboolean(L, -1); /* get result */
- lua.lua_pop(L, 1); /* pop result */
- return res ? -1 : 1;
- })
- .forEach((e, i) => typeof e[0] === 'number' ? t.value.set(i + 1, e[1]) : true);
+ sort_function = function (a, b) {
+ lua.lua_pushvalue(L, 2); /* push function */
+ lua.lua_pushtvalue(L, a.value); /* since we use Map.sort, a and b are not on the stack */
+ lua.lua_pushtvalue(L, b.value);
+ lua.lua_call(L, 2, 1); /* call function */
+ let res = lua.lua_toboolean(L, -1); /* get result */
+ lua.lua_pop(L, 1); /* pop result */
+ return res ? -1 : 1;
+ };
}
+ a.sort(sort_function)
+ .forEach(function(e, i) {
+ if (e.oldkey != i+1) {
+ lua.lua_pushtvalue(L, e.value);
+ lua.lua_seti(L, 1, i+1);
+ }
+ });
};
const sort = function(L) {