Ai
2 Star 4 Fork 3

Jally/fusion

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
LuaMgr.cpp 2.22 KB
一键复制 编辑 原始数据 按行查看 历史
Jally 提交于 2019-11-24 20:41 +08:00 . 同步代码
#include "LuaMgr.h"
#include "LuaBus.h"
#include "Logger.h"
#include <sstream>
LuaMgr::LuaMgr()
{
}
LuaMgr::~LuaMgr()
{
}
void LuaMgr::RemoveFile(const std::string &fileName)
{
std::lock_guard<std::shared_mutex> lock(mutex_);
if (fileName.empty()) {
chunks_.clear();
} else {
chunks_.erase(fileName);
}
}
int LuaMgr::LoadFile(lua_State *L, const std::string &fileName)
{
int errcode = CacheFile(L, fileName);
if (errcode != 0) {
ELOG("LoadFile[%s] error: %s.", fileName.c_str(), lua_tostring(L, -1));
lua_pop(L, 1);
lua_pushnil(L);
}
return 1;
}
bool LuaMgr::DoFile(lua_State *L, const std::string &fileName)
{
lua_pushcfunction(L, lua::on_error);
int errfunc = lua_gettop(L);
int errcode = CacheFile(L, fileName);
if (errcode == 0) {
lua_pcall(L, 0, 1, errfunc);
} else {
ELOG("DoFile[%s] error: %s.", fileName.c_str(), lua_tostring(L, -1));
}
lua_pop(L, 2);
return errcode == 0;
}
void LuaMgr::DoFileEnv(lua_State *L, const char *fileName)
{
if (LuaMgr::instance() != nullptr) {
sLuaMgr.DoFile(L, fileName);
} else {
lua::dofile(L, fileName);
}
}
int LuaMgr::CacheFile(lua_State *L, const std::string &fileName)
{
std::string chunk;
do {
std::shared_lock<std::shared_mutex> lock(mutex_);
auto itr = chunks_.find(fileName);
if (itr != chunks_.end()) {
chunk = itr->second;
}
} while (0);
int errcode = !chunk.empty() && false ?
luaL_loadbuffer(L, chunk.data(), chunk.size(), fileName.c_str()) :
luaL_loadfile(L, fileName.c_str());
if (errcode == 0) {
if (chunk.empty()) {
DumpChunk(L, fileName);
}
}
return errcode;
}
void LuaMgr::DumpChunk(lua_State *L, const std::string &fileName)
{
std::ostringstream stream;
lua_dump(L, (lua_Writer)WriteChunk, &stream, 0);
std::pair<std::string, std::string> pair{fileName, stream.str()};
do {
std::lock_guard<std::shared_mutex> lock(mutex_);
chunks_.insert(std::move(pair));
} while (0);
}
int LuaMgr::WriteChunk(lua_State *L, const char *p, size_t sz, std::ostream *stream)
{
(*stream).write(p, sz);
return 0;
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/jallyx/fusion.git
git@gitee.com:jallyx/fusion.git
jallyx
fusion
fusion
master

搜索帮助