mintrt 0.1.1

Security-featured runtime for lua.
//     <<*>> Revenge of snowflake.
//     -^v-  SASWE
//     @ This source code is licensed under a dual license model:
//       1. MPL-2.0 for non-commercial use only
//       2. Commercial License for commercial use
//       See LICENSE and LICENSE_COMMERCIAL files for details.
pub mod runtime;

// 通义添加:日志频率限制模块
pub mod log_rate_limit;
// 通义添加:日志频率限制测试模块
/*
#[cfg(test)]
mod rate_limit_test;
*/

use crate::config::Dandelion;
use mlua::Lua;
use crate::security::env_modify::Modules_no_suffix;

/// 配置模块搜索范围,包括路径和后缀规范: lua: script - .mint ; luac - .wing
/// 1 lext 2 cext 3 exec
/// 建议文件名和模块名一致
pub fn restrict_module_load_path(pkg: &Dandelion, lua: &Lua) -> Result<(), mlua::Error> {
    //应增加全局模块搜索路径
    let dll_lib_path = &pkg.cext;
    let lua_lib_path = &pkg.lext;
    let exec_lib_path = &pkg.exe;
    lua.globals().set("package.cpath", format!("{}/?.dll;{}/?.so;{}/?.wing", dll_lib_path, dll_lib_path, exec_lib_path))?;
    lua.globals().set("package.path", format!("{};{}", format!("{}/?.wing", &lua_lib_path), format!("{}/?.mint", &lua_lib_path)))?;
    Ok(())
}

pub fn generate_lua_script_for_lib_loading(modulelist: Modules_no_suffix) -> String {
    let mut script = String::new();
    for api in modulelist {
        script.push_str(&format!("require(\"{}\")", api));
    }
    script
}