Add `string.buffer` to Lua environment (if it exists)

master
luk3yx 2022-08-07 09:00:45 +12:00 committed by GitHub
parent 17f710bfd5
commit c658140b4b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 28 additions and 0 deletions

View File

@ -97,6 +97,33 @@ ScriptApiBase::ScriptApiBase(ScriptingType type):
lua_call(m_luastack, 0, 1);
lua_setglobal(m_luastack, "chacha");
// Load string.buffer if it is in package.preload
lua_getglobal(m_luastack, "package");
lua_getfield(m_luastack, -1, "preload");
lua_getfield(m_luastack, -1, "string.buffer");
if (!lua_isnil(m_luastack, -1)) {
// string.buffer = require("string.buffer")
lua_getglobal(m_luastack, "string");
lua_getglobal(m_luastack, "require");
lua_pushstring(m_luastack, "string.buffer");
lua_call(m_luastack, 1, 1);
lua_setfield(m_luastack, -2, "buffer");
lua_pop(m_luastack, 1); // Pop string
}
// Pop package, package.preload, and package.preload["string.buffer"]
lua_pop(m_luastack, 3);
// Delete references to package on the client (as they're no longer needed)
if (m_type == ScriptingType::Client) {
lua_pushnil(m_luastack);
lua_setglobal(m_luastack, "module");
lua_pushnil(m_luastack);
lua_setglobal(m_luastack, "require");
lua_pushnil(m_luastack);
lua_setglobal(m_luastack, "package");
}
// Make the ScriptApiBase* accessible to ModApiBase
#if INDIRECT_SCRIPTAPI_RIDX
*(void **)(lua_newuserdata(m_luastack, sizeof(void *))) = this;
@ -160,6 +187,7 @@ void ScriptApiBase::clientOpenLibs(lua_State *L)
{ LUA_STRLIBNAME, luaopen_string },
{ LUA_MATHLIBNAME, luaopen_math },
{ LUA_DBLIBNAME, luaopen_debug },
{ LUA_LOADLIBNAME, luaopen_package },
#if USE_LUAJIT
{ LUA_JITLIBNAME, luaopen_jit },
#endif