forked from M-Labs/nac3
1
0
Fork 0

subkernels: get destination from decorator

This commit is contained in:
mwojcik 2024-10-25 17:46:57 +08:00
parent 9a3463e027
commit c9b234b0a5
1 changed files with 49 additions and 8 deletions

View File

@ -501,9 +501,22 @@ impl Nac3 {
.iter() .iter()
.any(|decorator| decorator_id_string(decorator) == Some("subkernel".to_string())) .any(|decorator| decorator_id_string(decorator) == Some("subkernel".to_string()))
{ {
store_fun.call1(py, (def_id.0.into_py(py), module.getattr(py, name.to_string().as_str()).unwrap())).unwrap(); if let Constant::Int(destination) = decorator_get_destination(decorator) {
let dest = decorator_list.SOMETHING; if destination < 0 || destination > 255 {
subkernel_rpc_ids.push((None, def_id, dest)); return Err(CompileError::new_err(format!(
"compilation failed\n----------\nSubkernel destination must be between 0 and 255 (at {})",
stmt.location
)));
}
subkernel_ids.push((None, def_id, destination));
} else {
return Err(CompileError::new_err(format!(
"compilation failed\n----------\nDestination must be provided for subkernels (at {})",
stmt.location
)));
}
// store_fun.call1(py, (def_id.0.into_py(py), module.getattr(py, name.to_string().as_str()).unwrap())).unwrap();
} }
} }
StmtKind::ClassDef { name, body, .. } => { StmtKind::ClassDef { name, body, .. } => {
@ -527,14 +540,28 @@ impl Nac3 {
} }
rpc_ids.push((Some((class_obj.clone(), *name)), def_id, is_async)); rpc_ids.push((Some((class_obj.clone(), *name)), def_id, is_async));
} else if decorator_list.iter().any(|decorator| matches!(decorator.node, ExprKind::Name { id, .. } if id == "subkernel".into())) { } else if decorator_list.iter().any(|decorator| matches!(decorator.node, ExprKind::Name { id, .. } if id == "subkernel".into())) {
if let Constant::Int(destination) = decorator_get_destination(decorator) {
if name == &"__init__".into() { if name == &"__init__".into() {
return Err(CompileError::new_err(format!( return Err(CompileError::new_err(format!(
"compilation failed\n----------\nThe constructor of class {} should not be decorated with subkernel decorator (at {})", "compilation failed\n----------\nThe constructor of class {} should not be decorated with subkernel decorator (at {})",
class_name, stmt.location class_name, stmt.location
))); )));
} }
let dest = decorator_list.iter(). // figure out what's in the decorator list if destination < 0 || destination > 255 {
subkernel_ids.push((Some((class_obj.clone(), *name)), def_id, dest)); return Err(CompileError::new_err(format!(
"compilation failed\n----------\nSubkernel destination must be between 0 and 255 (at {})",
stmt.location
)));
}
subkernel_ids.push((Some((class_obj.clone(), *name)), def_id, destination));
} else {
return Err(CompileError::new_err(format!(
"compilation failed\n----------\nDestination must be provided for subkernels (at {})",
stmt.location
)));
}
} }
} }
} }
@ -922,6 +949,20 @@ fn decorator_get_flags(decorator: &Located<ExprKind>) -> Vec<Constant> {
flags flags
} }
fn decorator_get_destination(decorator: &Located<ExprKind>) -> Option<Constant> {
if let ExprKind::Call { keywords, .. } = &decorator.node {
for keyword in keywords {
if keyword.node.arg != Some("destination".into()) {
continue;
}
if let ExprKind::Constant { value, .. } = &keyword.node.value.node {
return Some(value.clone());
}
}
}
None
}
fn link_with_lld(elf_filename: String, obj_filename: String) -> PyResult<()> { fn link_with_lld(elf_filename: String, obj_filename: String) -> PyResult<()> {
let linker_args = vec![ let linker_args = vec![
"-shared".to_string(), "-shared".to_string(),