浏览代码

Provide nice method for creating native method AmlValues

Isaac Woods 4 年之前
父节点
当前提交
f389da0aff
共有 2 个文件被更改,包括 21 次插入2 次删除
  1. 1 1
      aml/src/term_object.rs
  2. 20 1
      aml/src/value.rs

+ 1 - 1
aml/src/term_object.rs

@@ -369,7 +369,7 @@ where
                             name,
                             &context.current_scope,
                             AmlValue::Method {
-                                flags: MethodFlags::new(flags),
+                                flags: MethodFlags::from(flags),
                                 code: MethodCode::Aml(code.to_vec())
                             },
                         )

+ 20 - 1
aml/src/value.rs

@@ -74,7 +74,18 @@ impl FieldFlags {
 pub struct MethodFlags(u8);
 
 impl MethodFlags {
-    pub fn new(value: u8) -> MethodFlags {
+    pub fn new(arg_count: u8, serialize: bool, sync_level: u8) -> MethodFlags {
+        assert!(arg_count <= 7);
+        assert!(sync_level <= 15);
+
+        let mut value = 0;
+        value.set_bits(0..3, arg_count);
+        value.set_bit(3, serialize);
+        value.set_bits(4..8, sync_level);
+        MethodFlags(value)
+    }
+
+    pub fn from(value: u8) -> MethodFlags {
         MethodFlags(value)
     }
 
@@ -205,6 +216,14 @@ impl AmlValue {
         AmlValue::Integer(u64::max_value())
     }
 
+    pub fn native_method<F>(arg_count: u8, serialize: bool, sync_level: u8, f: F) -> AmlValue
+    where
+        F: Fn(&mut AmlContext) -> Result<AmlValue, AmlError> + 'static,
+    {
+        let flags = MethodFlags::new(arg_count, serialize, sync_level);
+        AmlValue::Method { flags, code: MethodCode::Native(Rc::new(f)) }
+    }
+
     pub fn type_of(&self) -> AmlType {
         match self {
             AmlValue::Boolean(_) => AmlType::Integer,