Skip to main content

luaur_analysis/methods/
scope_linear_search_for_binding.rs

1use crate::records::binding::Binding;
2use crate::records::scope::Scope;
3use crate::records::symbol::Symbol;
4use alloc::string::String;
5
6impl Scope {
7    pub fn linear_search_for_binding(
8        &self,
9        name: &String,
10        traverse_scope_chain: bool,
11    ) -> Option<Binding> {
12        let mut scope: Option<&Scope> = Some(self);
13
14        while let Some(current_scope) = scope {
15            for (symbol, binding) in &current_scope.bindings {
16                let sym_name =
17                    unsafe { core::ffi::CStr::from_ptr(symbol.c_str()).to_string_lossy() };
18                if sym_name == *name {
19                    return Some(binding.clone());
20                }
21            }
22
23            scope = current_scope.parent.as_ref().map(|p| p.as_ref());
24
25            if !traverse_scope_chain {
26                break;
27            }
28        }
29
30        None
31    }
32}