summaryrefslogtreecommitdiff
path: root/src/lcorolib.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/lcorolib.js')
-rw-r--r--src/lcorolib.js134
1 files changed, 83 insertions, 51 deletions
diff --git a/src/lcorolib.js b/src/lcorolib.js
index bcdae8b..eb402b8 100644
--- a/src/lcorolib.js
+++ b/src/lcorolib.js
@@ -1,112 +1,144 @@
"use strict";
-const lua = require('./lua.js');
-const lauxlib = require('./lauxlib.js');
-const {to_luastring} = require("./fengaricore.js");
+const {
+ LUA_OK,
+ LUA_TFUNCTION,
+ LUA_TSTRING,
+ LUA_YIELD,
+ lua_Debug,
+ lua_checkstack,
+ lua_concat,
+ lua_error,
+ lua_getstack,
+ lua_gettop,
+ lua_insert,
+ lua_isyieldable,
+ lua_newthread,
+ lua_pop,
+ lua_pushboolean,
+ lua_pushcclosure,
+ lua_pushliteral,
+ lua_pushthread,
+ lua_pushvalue,
+ lua_resume,
+ lua_status,
+ lua_tothread,
+ lua_type,
+ lua_upvalueindex,
+ lua_xmove,
+ lua_yield
+} = require('./lua.js');
+const {
+ luaL_argcheck,
+ luaL_checktype,
+ luaL_newlib,
+ luaL_where
+} = require('./lauxlib.js');
+const { to_luastring } = require("./fengaricore.js");
const getco = function(L) {
- let co = lua.lua_tothread(L, 1);
- lauxlib.luaL_argcheck(L, co, 1, to_luastring("thread expected", true));
+ let co = lua_tothread(L, 1);
+ luaL_argcheck(L, co, 1, to_luastring("thread expected", true));
return co;
};
const auxresume = function(L, co, narg) {
- if (!lua.lua_checkstack(co, narg)) {
- lua.lua_pushliteral(L, "too many arguments to resume");
+ if (!lua_checkstack(co, narg)) {
+ lua_pushliteral(L, "too many arguments to resume");
return -1; /* error flag */
}
- if (lua.lua_status(co) === lua.LUA_OK && lua.lua_gettop(co) === 0) {
- lua.lua_pushliteral(L, "cannot resume dead coroutine");
+ if (lua_status(co) === LUA_OK && lua_gettop(co) === 0) {
+ lua_pushliteral(L, "cannot resume dead coroutine");
return -1; /* error flag */
}
- lua.lua_xmove(L, co, narg);
- let status = lua.lua_resume(co, L, narg);
- if (status === lua.LUA_OK || status === lua.LUA_YIELD) {
- let nres = lua.lua_gettop(co);
- if (!lua.lua_checkstack(L, nres + 1)) {
- lua.lua_pop(co, nres); /* remove results anyway */
- lua.lua_pushliteral(L, "too many results to resume");
+ lua_xmove(L, co, narg);
+ let status = lua_resume(co, L, narg);
+ if (status === LUA_OK || status === LUA_YIELD) {
+ let nres = lua_gettop(co);
+ if (!lua_checkstack(L, nres + 1)) {
+ lua_pop(co, nres); /* remove results anyway */
+ lua_pushliteral(L, "too many results to resume");
return -1; /* error flag */
}
- lua.lua_xmove(co, L, nres); /* move yielded values */
+ lua_xmove(co, L, nres); /* move yielded values */
return nres;
} else {
- lua.lua_xmove(co, L, 1); /* move error message */
+ lua_xmove(co, L, 1); /* move error message */
return -1; /* error flag */
}
};
const luaB_coresume = function(L) {
let co = getco(L);
- let r = auxresume(L, co, lua.lua_gettop(L) - 1);
+ let r = auxresume(L, co, lua_gettop(L) - 1);
if (r < 0) {
- lua.lua_pushboolean(L, 0);
- lua.lua_insert(L, -2);
+ lua_pushboolean(L, 0);
+ lua_insert(L, -2);
return 2; /* return false + error message */
} else {
- lua.lua_pushboolean(L, 1);
- lua.lua_insert(L, -(r + 1));
+ lua_pushboolean(L, 1);
+ lua_insert(L, -(r + 1));
return r + 1; /* return true + 'resume' returns */
}
};
const luaB_auxwrap = function(L) {
- let co = lua.lua_tothread(L, lua.lua_upvalueindex(1));
- let r = auxresume(L, co, lua.lua_gettop(L));
+ let co = lua_tothread(L, lua_upvalueindex(1));
+ let r = auxresume(L, co, lua_gettop(L));
if (r < 0) {
- if (lua.lua_type(L, -1) === lua.LUA_TSTRING) { /* error object is a string? */
- lauxlib.luaL_where(L, 1); /* add extra info */
- lua.lua_insert(L, -2);
- lua.lua_concat(L, 2);
+ if (lua_type(L, -1) === LUA_TSTRING) { /* error object is a string? */
+ luaL_where(L, 1); /* add extra info */
+ lua_insert(L, -2);
+ lua_concat(L, 2);
}
- return lua.lua_error(L); /* propagate error */
+ return lua_error(L); /* propagate error */
}
return r;
};
const luaB_cocreate = function(L) {
- lauxlib.luaL_checktype(L, 1, lua.LUA_TFUNCTION);
- let NL = lua.lua_newthread(L);
- lua.lua_pushvalue(L, 1); /* move function to top */
- lua.lua_xmove(L, NL, 1); /* move function from L to NL */
+ luaL_checktype(L, 1, LUA_TFUNCTION);
+ let NL = lua_newthread(L);
+ lua_pushvalue(L, 1); /* move function to top */
+ lua_xmove(L, NL, 1); /* move function from L to NL */
return 1;
};
const luaB_cowrap = function(L) {
luaB_cocreate(L);
- lua.lua_pushcclosure(L, luaB_auxwrap, 1);
+ lua_pushcclosure(L, luaB_auxwrap, 1);
return 1;
};
const luaB_yield = function(L) {
- return lua.lua_yield(L, lua.lua_gettop(L));
+ return lua_yield(L, lua_gettop(L));
};
const luaB_costatus = function(L) {
let co = getco(L);
- if (L === co) lua.lua_pushliteral(L, "running");
+ if (L === co) lua_pushliteral(L, "running");
else {
- switch (lua.lua_status(co)) {
- case lua.LUA_YIELD:
- lua.lua_pushliteral(L, "suspended");
+ switch (lua_status(co)) {
+ case LUA_YIELD:
+ lua_pushliteral(L, "suspended");
break;
- case lua.LUA_OK: {
- let ar = new lua.lua_Debug();
- if (lua.lua_getstack(co, 0, ar) > 0) /* does it have frames? */
- lua.lua_pushliteral(L, "normal"); /* it is running */
- else if (lua.lua_gettop(co) === 0)
- lua.lua_pushliteral(L, "dead");
+ case LUA_OK: {
+ let ar = new lua_Debug();
+ if (lua_getstack(co, 0, ar) > 0) /* does it have frames? */
+ lua_pushliteral(L, "normal"); /* it is running */
+ else if (lua_gettop(co) === 0)
+ lua_pushliteral(L, "dead");
else
- lua.lua_pushliteral(L, "suspended"); /* initial state */
+ lua_pushliteral(L, "suspended"); /* initial state */
break;
}
default: /* some error occurred */
- lua.lua_pushliteral(L, "dead");
+ lua_pushliteral(L, "dead");
break;
}
}
@@ -115,12 +147,12 @@ const luaB_costatus = function(L) {
};
const luaB_yieldable = function(L) {
- lua.lua_pushboolean(L, lua.lua_isyieldable(L));
+ lua_pushboolean(L, lua_isyieldable(L));
return 1;
};
const luaB_corunning = function(L) {
- lua.lua_pushboolean(L, lua.lua_pushthread(L));
+ lua_pushboolean(L, lua_pushthread(L));
return 2;
};
@@ -135,7 +167,7 @@ const co_funcs = {
};
const luaopen_coroutine = function(L) {
- lauxlib.luaL_newlib(L, co_funcs);
+ luaL_newlib(L, co_funcs);
return 1;
};