nac3embedded: string interning

This commit is contained in:
Sebastien Bourdeauducq 2021-09-24 09:58:58 +08:00
parent 14662a66dc
commit 5c236271c3
2 changed files with 15 additions and 13 deletions

View File

@ -102,9 +102,10 @@ impl Nac3 {
let top_level = self.top_level.as_ref().unwrap(); let top_level = self.top_level.as_ref().unwrap();
let instance = { let instance = {
let defs = top_level.definitions.read(); let defs = top_level.definitions.read();
let class_def = defs[self.resolver.get_identifier_def(&class_name).unwrap().0].write(); let class_def = defs[self.resolver.get_identifier_def(class_name.into()).unwrap().0].write();
let mut method_def = if let TopLevelDef::Class { methods, .. } = &*class_def { let mut method_def = if let TopLevelDef::Class { methods, .. } = &*class_def {
if let Some((_name, _unification_key, definition_id)) = methods.iter().find(|method| method.0 == method_name) { println!("{:?}", methods);
if let Some((_name, _unification_key, definition_id)) = methods.iter().find(|method| method.0.to_string() == method_name) {
defs[definition_id.0].write() defs[definition_id.0].write()
} else { } else {
return Err(exceptions::PyValueError::new_err("method not found")); return Err(exceptions::PyValueError::new_err("method not found"));

View File

@ -8,20 +8,21 @@ use nac3core::{
}, },
}; };
use parking_lot::Mutex; use parking_lot::Mutex;
use rustpython_parser::ast::StrRef;
use std::{collections::HashMap, sync::Arc}; use std::{collections::HashMap, sync::Arc};
pub struct ResolverInternal { pub struct ResolverInternal {
pub id_to_type: Mutex<HashMap<String, Type>>, pub id_to_type: Mutex<HashMap<StrRef, Type>>,
pub id_to_def: Mutex<HashMap<String, DefinitionId>>, pub id_to_def: Mutex<HashMap<StrRef, DefinitionId>>,
pub class_names: Mutex<HashMap<String, Type>>, pub class_names: Mutex<HashMap<StrRef, Type>>,
} }
impl ResolverInternal { impl ResolverInternal {
pub fn add_id_def(&self, id: String, def: DefinitionId) { pub fn add_id_def(&self, id: StrRef, def: DefinitionId) {
self.id_to_def.lock().insert(id, def); self.id_to_def.lock().insert(id, def);
} }
pub fn add_id_type(&self, id: String, ty: Type) { pub fn add_id_type(&self, id: StrRef, ty: Type) {
self.id_to_type.lock().insert(id, ty); self.id_to_type.lock().insert(id, ty);
} }
} }
@ -29,23 +30,23 @@ impl ResolverInternal {
pub struct Resolver(pub Arc<ResolverInternal>); pub struct Resolver(pub Arc<ResolverInternal>);
impl SymbolResolver for Resolver { impl SymbolResolver for Resolver {
fn get_symbol_type(&self, _: &mut Unifier, _: &PrimitiveStore, str: &str) -> Option<Type> { fn get_symbol_type(&self, _: &mut Unifier, _: &PrimitiveStore, str: StrRef) -> Option<Type> {
let ret = self.0.id_to_type.lock().get(str).cloned(); let ret = self.0.id_to_type.lock().get(&str).cloned();
if ret.is_none() { if ret.is_none() {
// println!("unknown here resolver {}", str); // println!("unknown here resolver {}", str);
} }
ret ret
} }
fn get_symbol_value(&self, _: &str) -> Option<SymbolValue> { fn get_symbol_value(&self, _: StrRef) -> Option<SymbolValue> {
unimplemented!() unimplemented!()
} }
fn get_symbol_location(&self, _: &str) -> Option<Location> { fn get_symbol_location(&self, _: StrRef) -> Option<Location> {
unimplemented!() unimplemented!()
} }
fn get_identifier_def(&self, id: &str) -> Option<DefinitionId> { fn get_identifier_def(&self, id: StrRef) -> Option<DefinitionId> {
self.0.id_to_def.lock().get(id).cloned() self.0.id_to_def.lock().get(&id).cloned()
} }
} }