Răsfoiți Sursa

Move methods to add values to the namespace over to the new system

Isaac Woods 4 ani în urmă
părinte
comite
0ecacfe495
2 a modificat fișierele cu 23 adăugiri și 15 ștergeri
  1. 17 9
      aml/src/namespace.rs
  2. 6 6
      aml/src/term_object.rs

+ 17 - 9
aml/src/namespace.rs

@@ -108,33 +108,41 @@ impl Namespace {
     /// Add a value to the namespace at the given path, which must be a normalized, absolute AML
     /// name. If you want to add at a path relative to a given scope, use `add_at_resolved_path`
     /// instead.
-    pub fn add(&mut self, path: AmlName, value: AmlValue) -> Result<AmlHandle, AmlError> {
+    pub fn add_value(&mut self, path: AmlName, value: AmlValue) -> Result<AmlHandle, AmlError> {
         assert!(path.is_absolute());
-        assert!(path.is_normal());
+        let path = path.normalize()?;
+
+        let (last_seg, levels) = path.0[1..].split_last().unwrap();
+        let last_seg = last_seg.as_segment().unwrap();
 
-        if self.name_map.contains_key(&path) {
-            return Err(AmlError::NameCollision(path.clone()));
+        let mut current_level = &mut self.root;
+        for level in levels {
+            current_level = current_level
+                .children
+                .get_mut(&level.as_segment().unwrap())
+                .ok_or(AmlError::LevelDoesNotExist(path.clone()))?;
         }
 
         let handle = self.next_handle;
         self.next_handle.increment();
-
         self.object_map.insert(handle, value);
-        self.name_map.insert(path, handle);
 
-        Ok(handle)
+        match current_level.values.insert(last_seg, handle) {
+            None => Ok(handle),
+            Some(_) => Err(AmlError::NameCollision(path)),
+        }
     }
 
     /// Helper method for adding a value to the namespace at a path that is relative to the given
     /// scope. This operation involves a lot of error handling in parts of the parser, so is
     /// encapsulated here.
-    pub fn add_at_resolved_path(
+    pub fn add_value_at_resolved_path(
         &mut self,
         path: AmlName,
         scope: &AmlName,
         value: AmlValue,
     ) -> Result<AmlHandle, AmlError> {
-        self.add(path.resolve(scope)?, value)
+        self.add_value(path.resolve(scope)?, value)
     }
 
     pub fn get(&self, handle: AmlHandle) -> Result<&AmlValue, AmlError> {

+ 6 - 6
aml/src/term_object.rs

@@ -109,7 +109,7 @@ where
             name_string().then(data_ref_object()).map_with_context(|(name, data_ref_object), context| {
                 try_with_context!(
                     context,
-                    context.namespace.add_at_resolved_path(name, &context.current_scope, data_ref_object,)
+                    context.namespace.add_value_at_resolved_path(name, &context.current_scope, data_ref_object,)
                 );
                 (Ok(()), context)
             }),
@@ -202,7 +202,7 @@ where
 
                     try_with_context!(
                         context,
-                        context.namespace.add_at_resolved_path(
+                        context.namespace.add_value_at_resolved_path(
                             name,
                             &context.current_scope,
                             AmlValue::OpRegion { region, offset, length }
@@ -293,7 +293,7 @@ where
     let named_field = name_seg().then(pkg_length()).map_with_context(move |(name_seg, length), context| {
         try_with_context!(
             context,
-            context.namespace.add_at_resolved_path(
+            context.namespace.add_value_at_resolved_path(
                 AmlName::from_name_seg(name_seg),
                 &context.current_scope,
                 AmlValue::Field {
@@ -333,7 +333,7 @@ where
                 .map_with_context(|(name, flags, code), context| {
                     try_with_context!(
                         context,
-                        context.namespace.add_at_resolved_path(
+                        context.namespace.add_value_at_resolved_path(
                             name,
                             &context.current_scope,
                             AmlValue::Method { flags: MethodFlags::new(flags), code: code.to_vec() },
@@ -396,7 +396,7 @@ where
                 .map_with_context(|((((pkg_length, name), proc_id), pblk_address), pblk_len), context| {
                     try_with_context!(
                         context,
-                        context.namespace.add_at_resolved_path(
+                        context.namespace.add_value_at_resolved_path(
                             name.clone(),
                             &context.current_scope,
                             AmlValue::Processor { id: proc_id, pblk_address, pblk_len }
@@ -433,7 +433,7 @@ where
             name_string().then(take()).map_with_context(|(name, sync_level), context| {
                 try_with_context!(
                     context,
-                    context.namespace.add_at_resolved_path(
+                    context.namespace.add_value_at_resolved_path(
                         name,
                         &context.current_scope,
                         AmlValue::Mutex { sync_level }