Skip to content

Commit 55989b3

Browse files
committed
aml: handle strings and buffers in logical ops
1 parent d38ca73 commit 55989b3

File tree

1 file changed

+41
-5
lines changed

1 file changed

+41
-5
lines changed

aml/src/lib.rs

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1130,11 +1130,47 @@ where
11301130
Err(AmlError::InvalidOperationOnObject)?
11311131
};
11321132

1133-
// TODO: for some of the ops, strings and buffers are also allowed :(
1134-
// TODO: apparently when doing this conversion (^), NT's interpreter just takes the first 4
1135-
// bytes of the string/buffer and casts them to an integer lmao
1136-
let left = left.clone().unwrap_transparent_reference().as_integer()?;
1137-
let right = right.clone().unwrap_transparent_reference().as_integer()?;
1133+
/*
1134+
* Some of these operations allow strings and buffers to be used as operands. Apparently
1135+
* NT's interpreter just takes the first 4 bytes of the string/buffer and casts them as an
1136+
* integer...
1137+
*/
1138+
let left = left.clone().unwrap_transparent_reference();
1139+
let right = right.clone().unwrap_transparent_reference();
1140+
let (left, right) = match *left {
1141+
Object::Integer(left) => (left, right.as_integer()?),
1142+
Object::String(ref left) => {
1143+
let left = {
1144+
let mut bytes = [0u8; 4];
1145+
let left_bytes = left.as_bytes();
1146+
(bytes[0..left_bytes.len()]).copy_from_slice(left_bytes);
1147+
u32::from_le_bytes(bytes) as u64
1148+
};
1149+
let right = {
1150+
let mut bytes = [0u8; 4];
1151+
let right = right.as_string()?;
1152+
let right_bytes = right.as_bytes();
1153+
(bytes[0..right_bytes.len()]).copy_from_slice(right_bytes);
1154+
u32::from_le_bytes(bytes) as u64
1155+
};
1156+
(left, right)
1157+
}
1158+
Object::Buffer(ref left) => {
1159+
let Object::Buffer(ref right) = *right else { panic!() };
1160+
let left = {
1161+
let mut bytes = [0u8; 4];
1162+
(bytes[0..left.len()]).copy_from_slice(left);
1163+
u32::from_le_bytes(bytes) as u64
1164+
};
1165+
let right = {
1166+
let mut bytes = [0u8; 4];
1167+
(bytes[0..right.len()]).copy_from_slice(right);
1168+
u32::from_le_bytes(bytes) as u64
1169+
};
1170+
(left, right)
1171+
}
1172+
_ => panic!(),
1173+
};
11381174

11391175
let result = match op.op {
11401176
Opcode::LAnd => (left > 0) && (right > 0),

0 commit comments

Comments
 (0)