do not return value from run(), add run() template

This commit is contained in:
Sebastien Bourdeauducq 2024-10-03 21:14:49 +08:00
parent bdf53a6185
commit c06eeb9684
1 changed files with 24 additions and 20 deletions

View File

@ -276,7 +276,7 @@ fn compile(code: &String, run_symbol: &String, output_filename: &Path) -> Result
let signature = typedef::FunSignature {
args: vec![],
ret: primitive.int32,
ret: primitive.none,
vars: typedef::VarMap::new(),
};
let mut store = codegen::concrete_type::ConcreteTypeStore::new();
@ -420,7 +420,7 @@ fn link_with_lld(elf_filename: &Path, obj_filename: &Path) -> Result<(), String>
Ok(())
}
type RunFn = unsafe extern "C" fn() -> i32;
type RunFn = unsafe extern "C" fn();
struct CellBin {
cell_id: usize,
@ -470,13 +470,19 @@ struct Cell {
}
impl Cell {
const DEFAULT_CODE: &'static str = "def run():\n pass";
fn new() -> Self {
Self {
code: "".to_string(),
code: Self::DEFAULT_CODE.to_string(),
result: Err("".to_string()),
}
}
fn is_default(&self) -> bool {
self.code == Self::DEFAULT_CODE
}
fn update(&mut self) {
let mut new_bin = CellBin::new(0usize);
self.result = new_bin
@ -508,9 +514,13 @@ pub struct List {
pub extern "C" fn __nac3_cells_plot(data: *const List) {
let ui = unsafe { CELL_UI.as_mut().unwrap() };
let data_slice = unsafe { std::slice::from_raw_parts((*data).data, (*data).length) };
let points: Vec<[f64; 2]> = (0..data_slice.len()).map(|i| [i as f64, data_slice[i]]).collect();
let points: Vec<[f64; 2]> = (0..data_slice.len())
.map(|i| [i as f64, data_slice[i]])
.collect();
let line = egui_plot::Line::new(points);
egui_plot::Plot::new("my_plot").view_aspect(2.0).show(ui, |plot_ui| plot_ui.line(line));
egui_plot::Plot::new("my_plot")
.view_aspect(2.0)
.show(ui, |plot_ui| plot_ui.line(line));
}
impl Cells {
@ -527,7 +537,8 @@ impl Cells {
}
fn ensure_last(&mut self) {
if self.cells.last().unwrap().result.is_ok() {
let last_cell = self.cells.last().unwrap();
if last_cell.result.is_ok() && !last_cell.is_default() {
self.cells.push(Cell::new());
}
}
@ -564,20 +575,13 @@ impl Cells {
);
});
match &cell.result {
Ok(bin) => {
let result = {
unsafe {
CELL_UI.replace(std::mem::transmute::<
&mut egui::Ui,
&'static mut egui::Ui,
>(ui))
};
let result = unsafe { bin.run_fn.unwrap()() };
unsafe { CELL_UI.take() };
result
};
ui.label(format!("{}", result));
}
Ok(bin) => unsafe {
CELL_UI.replace(
std::mem::transmute::<&mut egui::Ui, &'static mut egui::Ui>(ui),
);
bin.run_fn.unwrap()();
CELL_UI.take();
},
Err(msg) => {
if !msg.is_empty() {
ui.colored_label(egui::Color32::from_rgb(255, 0, 0), msg);