static void file_write_reap(struct lem_async *a) { struct file *f = (struct file *)a; lua_State *T = f->T; int top; if (f->ret) { f->T = NULL; lem_queue(T, io_strerror(T, f->ret)); return; } top = lua_gettop(T); do { if (f->write.idx == top) { f->T = NULL; lua_pushboolean(T, 1); lem_queue(T, 1); return; } f->write.str = lua_tolstring(T, ++f->write.idx, &f->write.len); } while (f->write.len == 0); lem_async_run(&f->a); }
static void db_exec_step_reap(struct lem_async *a) { struct db *db = (struct db *)a; lua_State *T = db->T; int ret; lem_debug("db->ret = %s", db->ret == SQLITE_ROW ? "SQLITE_ROW" : db->ret == SQLITE_DONE ? "SQLITE_DONE" : "error"); switch (db->ret) { case SQLITE_ROW: case SQLITE_DONE: break; default: lua_pushnil(T); lua_pushstring(T, sqlite3_errmsg(db->handle)); (void)sqlite3_finalize(db->req.prep.stmt); ret = 2; goto out; } if (sqlite3_finalize(db->req.prep.stmt) != SQLITE_OK) { lua_pushnil(T); lua_pushstring(T, sqlite3_errmsg(db->handle)); ret = 2; goto out; } if (db->req.prep.tail == NULL) { lua_pushboolean(T, 1); ret = 1; goto out; } db->req.prep.len -= db->req.prep.tail - db->req.prep.sql; db->req.prep.sql = db->req.prep.tail; db->a.work = db_prepare_work; db->a.reap = db_exec_prepare_reap; lem_async_run(&db->a); return; out: lem_queue(T, ret); db->T = NULL; }
static void db_exec_prepare_reap(struct lem_async *a) { struct db *db = (struct db *)a; lua_State *T = db->T; int ret; if (db->ret != SQLITE_OK) { lem_debug("db->ret != SQLITE_OK"); lua_pushnil(T); lua_pushstring(T, sqlite3_errmsg(db->handle)); ret = 2; goto out; } lem_debug("db->ret == SQLITE_OK"); if (db->req.prep.stmt == NULL) { lem_debug("db->req.prep.stmt == NULL"); lua_pushboolean(T, 1); ret = 1; goto out; } if (lua_gettop(T) > 1 && bindtable(T, db->req.prep.stmt)) { (void)sqlite3_finalize(db->req.prep.stmt); lua_pushnil(T); lua_insert(T, -2); ret = 2; goto out; } db->a.work = db_step_work; db->a.reap = db_exec_step_reap; lem_async_run(&db->a); return; out: lem_queue(T, ret); db->T = NULL; }
static void file_readp_reap(struct lem_async *a) { struct file *f = (struct file *)a; lua_State *T = f->T; int ret; if (f->ret) { enum lem_preason res = f->ret < 0 ? LEM_PCLOSED : LEM_PERROR; f->T = NULL; if (f->readp.p->destroy && (ret = f->readp.p->destroy(T, &f->buf, res)) > 0) { lem_queue(T, ret); return; } lua_pushnil(T); if (res == LEM_PCLOSED) lua_pushliteral(T, "eof"); else lua_pushstring(T, strerror(errno)); lem_queue(T, 2); return; } ret = f->readp.p->process(T, &f->buf); if (ret > 0) { f->T = NULL; lem_queue(T, ret); return; } lem_async_run(&f->a); }