<commit_before>use runtime::{Module, Runtime, RuntimePtr};

pub mod fs;


/// Fetches the source of a built-in Lua module by a given name.
///
/// Returns an `Option<&str>` containing the Lua source of the given module, or `None` if the
/// module is not defined.
///
/// The list of available built-in modules is determined at compile-time and is embedded into the
/// Rote binary itself.
pub fn fetch(name: &str) -> Option<Module> {
    match name {
        // Statically include and match the built-in modules.
        "core"      => Some(Module::Builtin(include_str!("core.lua"))),
        "cargo"     => Some(Module::Builtin(include_str!("cargo.lua"))),
        "cpp"       => Some(Module::Builtin(include_str!("cpp.lua"))),
        "docker"    => Some(Module::Builtin(include_str!("docker.lua"))),
        "fs"        => Some(Module::Native(fs::MTABLE)),
        "git"       => Some(Module::Builtin(include_str!("git.lua"))),
        "java"      => Some(Module::Builtin(include_str!("java.lua"))),
        "php"       => Some(Module::Builtin(include_str!("php.lua"))),
        "table"     => Some(Module::Builtin(include_str!("table.lua"))),
        // If you want to add a built-in module, add your module name and file here.
        _ => None
    }
}

/// A Lua module loader that loads built-in modules.
///
/// # Lua arguments
/// * `name: string`         - The name of the module to load.
pub fn loader<'r>(runtime: RuntimePtr) -> i32 {
    // Get the module name as the first argument.
    let name = Runtime::borrow(runtime).state.check_string(1);

    if let Some(module) = fetch(name) {
        match module {
            Module::Builtin(source) => {
                Runtime::borrow(runtime).state.load_string(source);
            },
            Module::Native(_) => {
                Runtime::borrow(runtime).push_fn(loader_native);
            },
        };
    } else {
        Runtime::borrow(runtime).state.push_string(&format!("\n\tno builtin module '{}'", name));
    }
    1
}

/// Native module loader callback.
fn loader_native<'r>(runtime: RuntimePtr) -> i32 {
    let name = Runtime::borrow(runtime).state.check_string(1);

    if let Some(Module::Native(mtable)) = fetch(name) {
        Runtime::borrow(runtime).state.new_table();

        for &(name, func) in mtable.0 {
            Runtime::borrow(runtime).push_fn(func);
            Runtime::borrow(runtime).state.set_field(-2, name);
        }
        1
    } else {
        0
    }
}
<commit_msg>Always set global alias for native modules<commit_after>use runtime::{Module, Runtime, RuntimePtr};

pub mod fs;


/// Fetches the source of a built-in Lua module by a given name.
///
/// Returns an `Option<&str>` containing the Lua source of the given module, or `None` if the
/// module is not defined.
///
/// The list of available built-in modules is determined at compile-time and is embedded into the
/// Rote binary itself.
pub fn fetch(name: &str) -> Option<Module> {
    match name {
        // Statically include and match the built-in modules.
        "core"      => Some(Module::Builtin(include_str!("core.lua"))),
        "cargo"     => Some(Module::Builtin(include_str!("cargo.lua"))),
        "cpp"       => Some(Module::Builtin(include_str!("cpp.lua"))),
        "docker"    => Some(Module::Builtin(include_str!("docker.lua"))),
        "fs"        => Some(Module::Native(fs::MTABLE)),
        "git"       => Some(Module::Builtin(include_str!("git.lua"))),
        "java"      => Some(Module::Builtin(include_str!("java.lua"))),
        "php"       => Some(Module::Builtin(include_str!("php.lua"))),
        "table"     => Some(Module::Builtin(include_str!("table.lua"))),
        // If you want to add a built-in module, add your module name and file here.
        _ => None
    }
}

/// A Lua module loader that loads built-in modules.
///
/// # Lua arguments
/// * `name: string`         - The name of the module to load.
pub fn loader<'r>(runtime: RuntimePtr) -> i32 {
    // Get the module name as the first argument.
    let name = Runtime::borrow(runtime).state.check_string(1);

    if let Some(module) = fetch(name) {
        match module {
            Module::Builtin(source) => {
                Runtime::borrow(runtime).state.load_string(source);
            },
            Module::Native(_) => {
                Runtime::borrow(runtime).push_fn(loader_native);
            },
        };
    } else {
        Runtime::borrow(runtime).state.push_string(&format!("\n\tno builtin module '{}'", name));
    }
    1
}

/// Native module loader callback.
fn loader_native<'r>(runtime: RuntimePtr) -> i32 {
    let name = Runtime::borrow(runtime).state.check_string(1);

    if let Some(Module::Native(mtable)) = fetch(name) {
        Runtime::borrow(runtime).state.new_table();

        for &(name, func) in mtable.0 {
            Runtime::borrow(runtime).push_fn(func);
            Runtime::borrow(runtime).state.set_field(-2, name);
        }

        Runtime::borrow(runtime).state.set_global(name);

        1
    } else {
        0
    }
}

{'fraction_non_alphanumeric': 0.16151407055093142, 'fraction_numerical': 0.004359889021006738, 'mean_word_length': 2.524441340782123, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3555713', 'n_tokens_mistral': 1533, 'n_tokens_neox': 1533, 'n_words': 371}