Skip to content

Commit b48a085

Browse files
authored
Add def_alias, alias shares handle with target (#153)
1 parent 1650c57 commit b48a085

File tree

3 files changed

+72
-1
lines changed

3 files changed

+72
-1
lines changed

aml/src/namespace.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,26 @@ impl Namespace {
157157
self.add_value(path.resolve(scope)?, value)
158158
}
159159

160+
/// Add an alias for an existing name. The alias will refer to the same value as the original,
161+
/// and the fact that the alias exists is forgotten.
162+
pub fn add_alias_at_resolved_path(
163+
&mut self,
164+
path: AmlName,
165+
scope: &AmlName,
166+
target: AmlName
167+
) -> Result<AmlHandle, AmlError> {
168+
let path = path.resolve(scope)?;
169+
let target = target.resolve(scope)?;
170+
171+
let handle = self.get_handle(&target)?;
172+
173+
let (level, last_seg) = self.get_level_for_path_mut(&path)?;
174+
match level.values.insert(last_seg, handle) {
175+
None => Ok(handle),
176+
Some(_) => Err(AmlError::NameCollision(path)),
177+
}
178+
}
179+
160180
pub fn get(&self, handle: AmlHandle) -> Result<&AmlValue, AmlError> {
161181
Ok(self.object_map.get(&handle).unwrap())
162182
}
@@ -722,6 +742,33 @@ mod tests {
722742
}
723743
}
724744

745+
#[test]
746+
fn test_alias() {
747+
let mut namespace = Namespace::new();
748+
749+
assert_eq!(namespace.add_level((AmlName::from_str("\\FOO")).unwrap(), LevelType::Scope), Ok(()));
750+
751+
assert!(
752+
namespace.add_value_at_resolved_path(
753+
AmlName::from_str("BAR").unwrap(),
754+
&AmlName::from_str("\\FOO").unwrap(),
755+
AmlValue::Integer(100))
756+
.is_ok()
757+
);
758+
assert!(
759+
namespace.add_alias_at_resolved_path(
760+
AmlName::from_str("BARA").unwrap(),
761+
&AmlName::from_str("\\FOO").unwrap(),
762+
AmlName::from_str("BAR").unwrap())
763+
.is_ok()
764+
);
765+
assert!(namespace.get_by_path(&AmlName::from_str("\\FOO.BARA").unwrap()).is_ok());
766+
assert_eq!(
767+
namespace.get_handle(&AmlName::from_str("\\FOO.BARA").unwrap()),
768+
namespace.get_handle(&AmlName::from_str("\\FOO.BAR").unwrap())
769+
);
770+
}
771+
725772
#[test]
726773
fn test_get_level_for_path() {
727774
let mut namespace = Namespace::new();

aml/src/opcode.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ pub const DWORD_CONST: u8 = 0x0c;
2020
pub const STRING_PREFIX: u8 = 0x0d;
2121
pub const QWORD_CONST: u8 = 0x0e;
2222

23+
pub const DEF_ALIAS_OP: u8 = 0x06;
2324
pub const DEF_NAME_OP: u8 = 0x08;
2425
pub const DEF_SCOPE_OP: u8 = 0x10;
2526
pub const DEF_BUFFER_OP: u8 = 0x11;

aml/src/term_object.rs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ where
7777
/*
7878
* NamespaceModifierObj := DefAlias | DefName | DefScope
7979
*/
80-
choice!(def_name(), def_scope())
80+
choice!(def_alias(), def_name(), def_scope())
8181
}
8282

8383
pub fn named_obj<'a, 'c>() -> impl Parser<'a, 'c, ()>
@@ -137,6 +137,29 @@ where
137137
.discard_result()
138138
}
139139

140+
pub fn def_alias<'a, 'c>() -> impl Parser<'a, 'c, ()>
141+
where
142+
'c: 'a,
143+
{
144+
/*
145+
* DefAlias := 0x06 NameString NameString
146+
* The second name refers to the same object as the first
147+
*/
148+
opcode(opcode::DEF_ALIAS_OP)
149+
.then(comment_scope(
150+
DebugVerbosity::Scopes,
151+
"DefAlias",
152+
name_string().then(name_string()).map_with_context(|(target, alias), context| {
153+
try_with_context!(
154+
context,
155+
context.namespace.add_alias_at_resolved_path(alias, &context.current_scope, target)
156+
);
157+
(Ok(()), context)
158+
}),
159+
))
160+
.discard_result()
161+
}
162+
140163
pub fn def_scope<'a, 'c>() -> impl Parser<'a, 'c, ()>
141164
where
142165
'c: 'a,

0 commit comments

Comments
 (0)