Skip to content

Commit

Permalink
src/lbaselib.js: Complete collectgarbage() implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
daurnimator committed Apr 20, 2019
1 parent f8710d2 commit fbb2036
Showing 1 changed file with 32 additions and 3 deletions.
35 changes: 32 additions & 3 deletions src/lbaselib.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
"use strict";

const {
LUA_GCCOLLECT,
LUA_GCCOUNT,
LUA_GCCOUNTB,
LUA_GCISRUNNING,
LUA_GCRESTART,
LUA_GCSETPAUSE,
LUA_GCSETSTEPMUL,
LUA_GCSTEP,
LUA_GCSTOP,
LUA_MULTRET,
LUA_OK,
LUA_TFUNCTION,
Expand All @@ -15,6 +24,7 @@ const {
lua_callk,
lua_concat,
lua_error,
lua_gc,
lua_getglobal,
lua_geti,
lua_getmetatable,
Expand All @@ -33,6 +43,7 @@ const {
lua_pushinteger,
lua_pushliteral,
lua_pushnil,
lua_pushnumber,
lua_pushstring,
lua_pushvalue,
lua_rawequal,
Expand Down Expand Up @@ -200,10 +211,28 @@ const opts = [
"count", "step", "setpause", "setstepmul",
"isrunning"
].map((e) => to_luastring(e));
const optsnum = [
LUA_GCSTOP, LUA_GCRESTART, LUA_GCCOLLECT,
LUA_GCCOUNT, LUA_GCSTEP, LUA_GCSETPAUSE, LUA_GCSETSTEPMUL,
LUA_GCISRUNNING
];
const luaB_collectgarbage = function(L) {
luaL_checkoption(L, 1, "collect", opts);
luaL_optinteger(L, 2, 0);
luaL_error(L, to_luastring("lua_gc not implemented"));
let o = optsnum[luaL_checkoption(L, 1, "collect", opts)];
let ex = luaL_optinteger(L, 2, 0);
let res = lua_gc(L, o, ex);
switch (o) {
case LUA_GCCOUNT: {
let b = lua_gc(L, LUA_GCCOUNTB, 0);
lua_pushnumber(L, res + b/1024);
return 1;
}
case LUA_GCSTEP: case LUA_GCISRUNNING:
lua_pushboolean(L, res);
return 1;
default:
lua_pushinteger(L, res);
return 1;
}
};

const luaB_type = function(L) {
Expand Down

0 comments on commit fbb2036

Please sign in to comment.