bool LuaCoroutine::CanResume() const { PushToStack(state.get()); lua_State* thread = lua_tothread(state.get(), -1); lua_pop(state.get(), 1); if (lua_status(thread) == LUA_YIELD) { return true; } return false; }
static int luaB_tostring (lua_State *L) { char buff[128]; luaL_checkany(L, 1); if (luaL_callmeta(L, 1, "__tostring")) /* is there a metafield? */ return 1; /* use its value */ switch (lua_type(L, 1)) { case LUA_TNUMBER: lua_pushstring(L, lua_tostring(L, 1)); return 1; case LUA_TSTRING: lua_pushvalue(L, 1); return 1; case LUA_TWSTRING: { luaL_Buffer b; size_t l; size_t i; const lua_WChar *s = lua_towstring(L, 1); l = lua_strlen(L, 1); if (l == 0) { lua_pushstring(L, ""); } else { luaL_buffinit(L, &b); for (i=0; i<l; i++) luaL_putchar(&b, (unsigned char)(s[i])); luaL_pushresult(&b); } return 1; } case LUA_TBOOLEAN: lua_pushstring(L, (lua_toboolean(L, 1) ? "true" : "false")); return 1; case LUA_TTABLE: sprintf(buff, "table: %p", lua_topointer(L, 1)); break; case LUA_TFUNCTION: sprintf(buff, "function: %p", lua_topointer(L, 1)); break; case LUA_TUSERDATA: case LUA_TLIGHTUSERDATA: sprintf(buff, "userdata: %p", lua_touserdata(L, 1)); break; case LUA_TTHREAD: sprintf(buff, "thread: %p", (void *)lua_tothread(L, 1)); break; case LUA_TNIL: lua_pushliteral(L, "nil"); return 1; } lua_pushstring(L, buff); return 1; }
static int co_resume (lua_State *L) { lua_State *co = lua_tothread(L, 1); int r; luaL_argcheck(L, co, 1, "coroutine expected"); r = auxresume(L, co, lua_gettop(L) - 1); if (r < 0) return 2; /* return false + error message */ else return r + 1; /* return true + `resume' returns */ }
int debugger_stop(lua_State* L) { lua_State * t = lua_tothread(L, -1); if (t == NULL) { t = L; } int hookmask = lua_gethookmask(t) & ~LUA_MASKLINE; lua_pushboolean(L, lua_sethook(t, hook, hookmask, 0)); return 1; }
static void mark_thread(lua_State *L, lua_State *dL, const void * parent, const char *desc) { const void * t = readobject(L, dL, parent, desc); if (t == NULL) return; int level = 0; lua_State *cL = lua_tothread(L,-1); if (cL == L) { level = 1; } else { // mark stack int top = lua_gettop(cL); luaL_checkstack(cL, 1, NULL); int i; char tmp[16]; for (i=0;i<top;i++) { lua_pushvalue(cL, i+1); sprintf(tmp, "[%d]", i+1); mark_object(cL, dL, cL, tmp); } } lua_Debug ar; luaL_Buffer b; luaL_buffinit(dL, &b); while (lua_getstack(cL, level, &ar)) { char tmp[128]; lua_getinfo(cL, "Sl", &ar); luaL_addstring(&b, ar.short_src); if (ar.currentline >=0) { char tmp[16]; sprintf(tmp,":%d ",ar.currentline); luaL_addstring(&b, tmp); } int i,j; for (j=1;j>-1;j-=2) { for (i=j;;i+=j) { const char * name = lua_getlocal(cL, &ar, i); if (name == NULL) break; snprintf(tmp, sizeof(tmp), "%s : %s:%d",name,ar.short_src,ar.currentline); mark_object(cL, dL, t, tmp); } } ++level; } luaL_addstring(&b, "thread: "); luaL_pushresult(&b); lua_rawsetp(dL, SOURCE, t); lua_pop(L,1); }
static int luaB_auxwrap (lua_State *L) { lua_State *co = lua_tothread(L, lua_upvalueindex(1)); int r = auxresume(L, co, lua_gettop(L)); if (r < 0) { if (lua_isstring(L, -1)) { /* error object is a string? */ luaL_where(L, 1); /* add extra info */ lua_insert(L, -2); lua_concat(L, 2); } lua_error(L); /* propagate error */ } return r; }
std::string LuaCoroutine::Resume() { PushToStack(state.get()); lua_State* thread = lua_tothread(state.get(), -1); lua_pop(state.get(), 1); int status = lua_resume(thread, NULL, lua_gettop(thread)); if (status != LUA_OK && status != LUA_YIELD) { return LuaGetLastError(thread); } return "No errors"; }
static int luaB_costatus (lua_State *L) { lua_State *co = lua_tothread(L, 1); luaL_argcheck(L, co, 1, "coroutine expected"); if (L == co) lua_pushliteral(L, "running"); else { lua_Debug ar; if (lua_getstack(co, 0, &ar) == 0 && lua_gettop(co) == 0) lua_pushliteral(L, "dead"); else lua_pushliteral(L, "suspended"); } return 1; }
std::string LuaCoroutine::Resume() { PushToStack(state.get()); lua_State* thread = lua_tothread(state.get(), -1); lua_pop(state.get(), 1); int status = lua_resume(thread, NULL, 0); if (status != LUA_OK && status != LUA_YIELD) { const char* r = lua_tostring(thread, -1); return r; } return "No errors"; }
static lua_State *getthread(lua_State *L, int *arg) { if (lua_isthread(L, 1)) { *arg = 1; return lua_tothread(L, 1); } else { *arg = 0; return L; } }
static void tcp_listen_cb(uv_stream_t *handle, int status) { lua_State *l = ls_default_state(); ls_tcp_t *server = containerof(handle, ls_tcp_t, handle); uv_loop_t *loop = uv_default_loop(); lua_State *nl; if (ls_object_is_waited(&server->wait_object)) { int ref = server->wait_object.mthread_ref; server->wait_object.mthread_ref = LUA_NOREF; ls_getref(l, ref); nl = lua_tothread(l, -1); lua_pop(l, 1); if (nl) { ls_clear_waiting(nl); if (status != 0) { ls_last_error_resume(nl, loop); } else { ls_tcp_t *client = new_tcp_handle(l); if (uv_accept(handle, (uv_stream_t*)&client->handle)) { ls_free(nl, client); luaL_error(nl, "accept failed"); } if (uv_read_start((uv_stream_t*)&client->handle, tcp_alloc_cb, tcp_read_cb)) { ls_free(nl, client); luaL_error(nl, "start read failed."); } if (LUA_YIELD == lua_status(nl)) { lua_pushboolean(nl, 1); new_tcp_connection_udata(nl, client); ls_resume(nl, 2); } else { ls_free(nl, client); } } } ls_unref(l, ref); } }
std::string LuaCoroutine::RunScript(std::string script) { PushToStack(state.get()); lua_State* thread = lua_tothread(state.get(), -1); lua_pop(state.get(), 1); int status = luaL_loadstring(thread, script.c_str()); status = lua_resume(thread, NULL, 0); if (status != LUA_OK && status != LUA_YIELD) { return LuaGetLastError(thread); } return "No errors"; }
int AsyncTaskMgr::resume(int task_id) { lua_getglobal(master_state, "TASK_TABLE"); lua_pushinteger(master_state, task_id); lua_gettable(master_state, -2); lua_State *co = lua_tothread(master_state, -1); lua_settop(master_state, 0); if (co == NULL) return -1; return lua_resume(co, master_state, 0); }
static int lmain(lua_State *L) { struct module *s = lua_touserdata(L, lua_upvalueindex(1)); assert(s); luaL_checktype(L, 1, LUA_TFUNCTION); lua_rawsetp(L, LUA_REGISTRYINDEX, _maincb); lua_rawgeti(L, LUA_REGISTRYINDEX, LUA_RIDX_MAINTHREAD); struct lua_State *gL = lua_tothread(L,-1); assert(gL); s->dl.main = _maincb; s->dl.main_ud = gL; return 0; }
static int utils_resume(lua_State *T) { int args; lua_State *S; luaL_checktype(T, 1, LUA_TTHREAD); S = lua_tothread(T, 1); args = lua_gettop(T) - 1; lua_xmove(T, S, args); lem_queue(S, args); return 0; }
void AsyncTaskMgr::set_task_id(int task_id) { lua_getglobal(master_state, "TAST_TABLE"); lua_pushinteger(master_state, task_id); lua_gettable(master_state, -2); lua_State *co = lua_tothread(master_state, -1); lua_settop(master_state, 0); if (co == NULL) return; lua_pushinteger(co, task_id); lua_setglobal(co, "TASK_ID"); }
static int _callback(lua_State *L) { struct skynet_context * context = lua_touserdata(L, lua_upvalueindex(1)); luaL_checktype(L,1,LUA_TFUNCTION); lua_settop(L,1); lua_rawsetp(L, LUA_REGISTRYINDEX, _cb); lua_rawgeti(L, LUA_REGISTRYINDEX, LUA_RIDX_MAINTHREAD); lua_State *gL = lua_tothread(L,-1); skynet_callback(context, gL, _cb); return 0; }
static int luaB_coresume(lua_State *L) { lua_State *co = lua_tothread(L, 1); int r; luaL_argcheck(L, co, 1, "coroutine expected"); r = auxresume(L, co, lua_gettop(L) - 1); if(r < 0) { lua_pushboolean(L, 0); lua_insert(L, -2); return 2; /* return false + error message */ } else { lua_pushboolean(L, 1); lua_insert(L, -(r + 1)); return r + 1; /* return true + `resume' returns */ } }
/** @internal Helper for calling the entrypoint. */ static inline int l_ffi_call(lua_State *L, int argc) { int status = lua_pcall(L, argc, 1, 0); if (status != 0) { fprintf(stderr, "error: %s\n", lua_tostring(L, -1)); lua_pop(L, 1); return kr_error(EIO); } if (lua_isnumber(L, -1)) { /* Return code */ status = lua_tonumber(L, -1); } else if (lua_isthread(L, -1)) { /* Continuations */ status = l_ffi_defer(lua_tothread(L, -1)); } lua_pop(L, 1); return status; }
inline lua_State* toMainThread(lua_State* state) { #if LUA_VERSION_NUM >= 502 if (state) { lua_rawgeti(state, LUA_REGISTRYINDEX, LUA_RIDX_MAINTHREAD); lua_State* mainthread = lua_tothread(state, -1); lua_pop(state, 1); if (mainthread) { return mainthread; } } #endif return state; }
/* {{{ ratchet_error_tostring() */ static int ratchet_error_tostring (lua_State *L) { lua_settop (L, 1); lua_getfield (L, 1, "thread"); lua_State *L1 = lua_tothread (L, -1); lua_getfield (L, 1, "get_string"); lua_pushvalue (L, 1); lua_call (L, 1, 1); const char *msg = lua_tostring (L, -1); if (L1) luaL_traceback (L, L1, msg, 1); return 1; }
int luaopen_silly(lua_State *L) { luaL_Reg tbl[] = { //core {"workid", _lworkid}, {"getenv", _lgetenv}, {"setenv", _lsetenv}, {"exit", _lexit}, //timer {"timerentry", _ltimer_entry}, {"timeradd", _ltimer_add}, {"timernow", _ltimer_now}, //socket {"socketentry", _lsocket_entry}, {"socketconnect", _lsocket_connect}, {"socketclose", _lsocket_close}, {"socketsend", _lsocket_send}, {"dropmessage", _ldrop}, //end {NULL, NULL}, }; luaL_checkversion(L); luaL_newmetatable(L, "silly_socket_data"); luaL_newmetatable(L, "silly_socket_packet"); lua_rawgeti(L, LUA_REGISTRYINDEX, LUA_RIDX_MAINTHREAD); lua_State *m = lua_tothread(L, -1); lua_pop(L, 1); lua_pushlightuserdata(L, (void *)m); lua_gettable(L, LUA_REGISTRYINDEX); struct silly_worker *w = lua_touserdata(L, -1); assert(w); silly_worker_message(w, _process_msg); silly_worker_exit(w, _exit); luaL_newlibtable(L, tbl); lua_pushlightuserdata(L, (void *)w); luaL_setfuncs(L, tbl, 1); return 1; }
/* string tag string userstring thread co (default nil) integer level */ static int ltrace(lua_State *L) { struct skynet_context * context = lua_touserdata(L, lua_upvalueindex(1)); const char * tag = luaL_checkstring(L, 1); const char * user = luaL_checkstring(L, 2); if (lua_isthread(L, 3)) { lua_State * co = lua_tothread (L, 3); struct source_info si[MAX_LEVEL]; lua_Debug d; int level = luaL_checkinteger(L, 4); int index = 0; do { if (!lua_getstack(co, level, &d)) break; lua_getinfo(co, "Sl", &d); level++; si[index].source = d.source; si[index].line = d.currentline; if (d.currentline >= 0) ++index; } while (index < MAX_LEVEL); switch (index) { case 1: skynet_error(context, "<TRACE %s> %" PRId64 " %s : %s:%d", tag, get_time(), user, si[0].source, si[0].line); break; case 2: skynet_error(context, "<TRACE %s> %" PRId64 " %s : %s:%d %s:%d", tag, get_time(), user, si[0].source, si[0].line, si[1].source, si[1].line ); break; case 3: skynet_error(context, "<TRACE %s> %" PRId64 " %s : %s:%d %s:%d %s:%d", tag, get_time(), user, si[0].source, si[0].line, si[1].source, si[1].line, si[2].source, si[2].line ); break; default: skynet_error(context, "<TRACE %s> %" PRId64 " %s", tag, get_time(), user); break; } return 0; } skynet_error(context, "<TRACE %s> %" PRId64 " %s", tag, get_time(), user); return 0; }
void AsyncTaskMgr::push_string(int task_id, const char* lua_var, const char* str) { lua_getglobal(master_state, "TAST_TABLE"); lua_pushinteger(master_state, task_id); lua_gettable(master_state, -2); lua_State *co = lua_tothread(master_state, -1); lua_settop(master_state, 0); if (co == NULL) return; lua_pushstring(co, str); lua_setglobal(co, lua_var); return; }
void AsyncTaskMgr::push_data(int task_id, const char* lua_var, void *data) { lua_getglobal(master_state, "TAST_TABLE"); lua_pushinteger(master_state, task_id); lua_gettable(master_state, -2); lua_State *co = lua_tothread(master_state, -1); lua_settop(master_state, 0); if (co == NULL) return; lua_pushlightuserdata(co, data); lua_setglobal(co, lua_var); return; }
static gboolean poll_events(gpointer data) { int my_id = GPOINTER_TO_INT(data); lua_getfield(darktable.lua_state,LUA_REGISTRYINDEX,"dt_lua_delayed_events"); lua_rawgeti(darktable.lua_state,-1,my_id); if(lua_isnoneornil(darktable.lua_state,-1)) { lua_pop(darktable.lua_state,2); luaL_error(darktable.lua_state,"Unknown thread was called for delay action"); return FALSE; } lua_State * L = lua_tothread(darktable.lua_state,-1); dt_lua_do_chunk(L,lua_gettop(L) -1,0); /* L is finished, remove it from the stack */ luaL_unref(darktable.lua_state,-2,my_id); lua_pop(darktable.lua_state,2); return FALSE; }
lua_tinker::luaValueRef lua_tinker::read(lua_State *L, int index) { #if(LUA_VERSION_NUM == 501) lua_tinker::luaValueRef ref; lua_pushvalue(L, index); ref.rindex = luaL_ref(L, LUA_REGISTRYINDEX); ref.L = nullptr; return ref; #elif(LUA_VERSION_NUM == 503) lua_tinker::luaValueRef ref; lua_pushvalue(L, index); ref.rindex = luaL_ref(L, LUA_REGISTRYINDEX); lua_rawgeti(L, LUA_REGISTRYINDEX, LUA_RIDX_MAINTHREAD); ref.L = lua_tothread(L, -1); lua_pop(L, 1); return ref; #endif }
// Выводит в лог дамп содержимого стека. void LuaScript::StackDumpToLog( lua_State *L ) { int i; int top = lua_gettop(L); std::string s = ""; char buf[20]; snprintf(buf, 20, "%p: ", (void*)L); s += buf; for (i = 1; i <= top; i++){ /* repeat for each level */ int t = lua_type(L, i); switch (t) { case LUA_TSTRING: /* strings */ s += (std::string)"'" + lua_tostring(L, i) + "'"; break; case LUA_TBOOLEAN: /* booleans */ s += lua_toboolean(L, i) ? "true" : "false"; break; case LUA_TNUMBER: /* numbers */ snprintf(buf, 20, "%f", lua_tonumber(L, i)); s += (std::string)buf; break; case LUA_TTHREAD: snprintf(buf, 20, "0x%p", (void*)lua_tothread(L, i)); s += "thread " + (std::string)buf; break; default: /* other values */ s += lua_typename(L, t); break; } s += ", "; /* put a separator */ } s+= "\n"; Debug::debug( Debug::CONFIG, s.c_str() ); }
static int32_t do_chunk_later_callback(dt_job_t *job) { dt_lua_lock(); lua_State* L= darktable.lua_state.state; int reference = GPOINTER_TO_INT(dt_control_job_get_params(job)); lua_getfield(L, LUA_REGISTRYINDEX, "dt_lua_bg_threads"); lua_pushinteger(L,reference); lua_gettable(L,-2); lua_State* thread = lua_tothread(L,-1); lua_pop(L,2); dt_lua_do_chunk_silent(thread,lua_gettop(thread)-1,0); lua_getfield(L, LUA_REGISTRYINDEX, "dt_lua_bg_threads"); lua_pushinteger(L,reference); lua_pushnil(L); lua_settable(L,-3); lua_pop(L,1); dt_lua_unlock(); return 0; }
static PyObject *LuaObject_str(PyObject *obj) { PyObject *ret = NULL; const char *s; lua_rawgeti(LuaState, LUA_REGISTRYINDEX, ((LuaObject*)obj)->ref); if (luaL_callmeta(LuaState, -1, "__tostring")) { s = lua_tostring(LuaState, -1); lua_pop(LuaState, 1); if (s) ret = PyUnicode_FromString(s); } if (!ret) { int type = lua_type(LuaState, -1); switch (type) { case LUA_TTABLE: case LUA_TFUNCTION: ret = PyUnicode_FromFormat("<Lua %s at %p>", lua_typename(LuaState, type), lua_topointer(LuaState, -1)); break; case LUA_TUSERDATA: case LUA_TLIGHTUSERDATA: ret = PyUnicode_FromFormat("<Lua %s at %p>", lua_typename(LuaState, type), lua_touserdata(LuaState, -1)); break; case LUA_TTHREAD: ret = PyUnicode_FromFormat("<Lua %s at %p>", lua_typename(LuaState, type), (void*)lua_tothread(LuaState, -1)); break; default: ret = PyUnicode_FromFormat("<Lua %s>", lua_typename(LuaState, type)); break; } } lua_pop(LuaState, 1); return ret; }