Skip to content

Commit c43af39

Browse files
committed
Convert all VM strings to u16 strings
1 parent c05b447 commit c43af39

File tree

12 files changed

+561
-415
lines changed

12 files changed

+561
-415
lines changed

Cargo.lock

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ strum = "0.27.2"
2828
strum_macros = "0.27.2"
2929
thiserror = "2.0.12"
3030
velcro = "0.5.4"
31+
widestring = "1.2.0"
3132

3233
# mlogv32
3334
clap = { version = "4.5.42", features = ["derive"], optional = true }

src/bin/mlogv32.rs

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,12 @@ use mindustry_rs::logic::vm::LVar;
2121
use mindustry_rs::{
2222
logic::vm::{
2323
Building, BuildingData, LValue, LogicVM, LogicVMBuilder, MEMORY_BANK, MESSAGE,
24-
MICRO_PROCESSOR, Processor, SWITCH, WORLD_PROCESSOR, decode_utf16,
24+
MICRO_PROCESSOR, Processor, SWITCH, WORLD_PROCESSOR,
2525
},
2626
types::{Object, Point2, ProcessorConfig, Schematic},
2727
};
2828
use serde::Deserialize;
29+
use widestring::{U16String, u16str};
2930

3031
// tx/rx are from our perspective, not the processor's
3132
const UART_TX_READ: usize = 254;
@@ -121,7 +122,7 @@ fn get_building<'a>(vm: &'a LogicVM, position: MetaPoint2, name: &str) -> &'a Bu
121122
let Some(building) = vm.building(position.into()) else {
122123
panic!("{name} not found at {position}");
123124
};
124-
assert_eq!(building.block.name, name);
125+
assert_eq!(building.block.name.as_str(), name);
125126
building
126127
}
127128

@@ -145,14 +146,14 @@ enum VMCommand {
145146
Continue,
146147
Restart,
147148
SetBreakpoint(Option<u32>),
148-
PrintVar(String, Option<String>),
149+
PrintVar(U16String, Option<String>),
149150
}
150151

151152
struct VMState {
152153
power: bool,
153154
pause: bool,
154155
single_step: bool,
155-
state: Option<String>,
156+
state: Option<U16String>,
156157
pc: u32,
157158
mtime: u32,
158159
mcycle: u32,
@@ -273,7 +274,7 @@ fn tui(stdout: TextContent, debug: TextContent, tx: Sender<VMCommand>, rx: Recei
273274
siv.call_on_name("pause", |v: &mut Checkbox| v.set_checked(pause));
274275
siv.call_on_name("single_step", |v: &mut Checkbox| v.set_checked(single_step));
275276
siv.call_on_name("state", |v: &mut TextView| match state {
276-
Some(state) => v.set_content(state),
277+
Some(state) => v.set_content(state.to_string_lossy()),
277278
None => v.set_content("???"),
278279
});
279280
siv.call_on_name("pc", |v: &mut TextView| {
@@ -309,9 +310,10 @@ fn process_cmd(out: &TextContent, cmd: &str) -> Option<VMCommand> {
309310
}
310311
},
311312
},
312-
"p" | "print" | "v" | "var" if cmd.len() >= 2 => {
313-
VMCommand::PrintVar(cmd[1].to_string(), cmd.get(2).map(|s| s.to_string()))
314-
}
313+
"p" | "print" | "v" | "var" if cmd.len() >= 2 => VMCommand::PrintVar(
314+
U16String::from_str(cmd[1]),
315+
cmd.get(2).map(|s| s.to_string()),
316+
),
315317
/*
316318
"i" | "inspect" if cmd.len() >= 3 => {
317319
let Ok(x) = cmd[1].parse() else {
@@ -472,17 +474,21 @@ fn main() -> Result<(), Box<dyn Error>> {
472474
});
473475
}
474476

475-
let print_var = |processor: &Processor, name: String, radix: Option<String>| {
477+
let print_var = |processor: &Processor, name: U16String, radix: Option<String>| {
476478
match processor
477479
.variable(&name)
478480
.or_else(|| globals.get(&name).map(|v| v.get(&processor.state)))
479481
{
480482
Some(value) => match radix.as_deref() {
481-
Some("x") => tui_println!(debug, "{name} = {:#010x}", value.num() as u32),
482-
Some("b") => tui_println!(debug, "{name} = {:#034b}", value.num() as u32),
483-
_ => tui_println!(debug, "{name} = {value:?}"),
483+
Some("x") => {
484+
tui_println!(debug, "{} = {:#010x}", name.display(), value.num() as u32)
485+
}
486+
Some("b") => {
487+
tui_println!(debug, "{} = {:#034b}", name.display(), value.num() as u32)
488+
}
489+
_ => tui_println!(debug, "{} = {value:?}", name.display()),
484490
},
485-
None => tui_println!(debug, "{name} = <undefined>"),
491+
None => tui_println!(debug, "{} = <undefined>", name.display()),
486492
};
487493
};
488494

@@ -556,19 +562,19 @@ fn main() -> Result<(), Box<dyn Error>> {
556562
*single_step = false;
557563
}
558564
VMCommand::Restart => {
559-
controller.set_variable("pc", 0.into())?;
565+
controller.set_variable(u16str!("pc"), 0.into())?;
560566
*power = true;
561567
*pause = false;
562568
*single_step = false;
563569
start = Instant::now();
564570
next_state_update = start;
565571
}
566572
VMCommand::SetBreakpoint(Some(value)) => {
567-
config.set_variable("BREAKPOINT_ADDRESS", value.into())?;
573+
config.set_variable(u16str!("BREAKPOINT_ADDRESS"), value.into())?;
568574
tui_println!(debug, "Breakpoint set: {value:#010x}");
569575
}
570576
VMCommand::SetBreakpoint(None) => {
571-
config.set_variable("BREAKPOINT_ADDRESS", LValue::Null)?;
577+
config.set_variable(u16str!("BREAKPOINT_ADDRESS"), LValue::Null)?;
572578
tui_println!(debug, "Breakpoint cleared.");
573579
}
574580
VMCommand::PrintVar(name, radix) => print_var(controller, name, radix),
@@ -580,14 +586,14 @@ fn main() -> Result<(), Box<dyn Error>> {
580586
power: *power,
581587
pause: *pause,
582588
single_step: *single_step,
583-
state: match controller.variable("state").unwrap() {
584-
LValue::String(state) => Some(state.to_string()),
589+
state: match controller.variable(u16str!("state")).unwrap() {
590+
LValue::String(state) => Some(state.to_ustring()),
585591
_ => None,
586592
},
587-
pc: controller.variable("pc").unwrap().numu(),
588-
mcycle: controller.variable("csr_mcycle").unwrap().numu(),
589-
mtime: controller.variable("csr_mtime").unwrap().numu(),
590-
minstret: controller.variable("csr_minstret").unwrap().numu(),
593+
pc: controller.variable(u16str!("pc")).unwrap().numu(),
594+
mcycle: controller.variable(u16str!("csr_mcycle")).unwrap().numu(),
595+
mtime: controller.variable(u16str!("csr_mtime")).unwrap().numu(),
596+
minstret: controller.variable(u16str!("csr_minstret")).unwrap().numu(),
591597
})?;
592598

593599
if *power != prev_power {
@@ -597,7 +603,7 @@ fn main() -> Result<(), Box<dyn Error>> {
597603
} else {
598604
tui_println!(debug, "Processor halted.");
599605
if !error_output.is_empty() {
600-
tui_println!(debug, "Error output: {}", decode_utf16(error_output));
606+
tui_println!(debug, "Error output: {}", error_output.display());
601607
}
602608
tui_println!(debug, "Runtime: {time:?}");
603609
tui_println!(debug, "Ticks completed: {ticks}");

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
pub mod logic;
22
pub mod types;
3+
mod utils;

src/logic/vm/buildings.rs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
use std::{cell::RefCell, collections::HashMap, rc::Rc};
1+
use std::{cell::RefCell, rc::Rc};
22

33
use strum_macros::IntoStaticStr;
4+
use widestring::U16String;
45

56
use super::{
6-
LogicVMBuilder, VMLoadError, VMLoadResult,
7-
processor::{Processor, ProcessorBuilder, ProcessorState, encode_utf16},
8-
variables::{LValue, LVar},
7+
LogicVMBuilder, VMLoadError, VMLoadResult, Variables,
8+
processor::{Processor, ProcessorBuilder, ProcessorState},
9+
variables::LValue,
910
};
1011
use crate::types::{
1112
Object, Point2, ProcessorConfig, SchematicTile,
@@ -71,7 +72,7 @@ impl Building {
7172

7273
MESSAGE | WORLD_MESSAGE => BuildingData::Message(match config {
7374
Object::String(Some(value)) if value.len() <= MESSAGE_MAX_LEN => {
74-
let mut result = String::new();
75+
let mut result = U16String::new();
7576
let mut count = 0;
7677
for c in value.trim().chars() {
7778
if c == '\n' {
@@ -80,11 +81,11 @@ impl Building {
8081
}
8182
count += 1;
8283
}
83-
result.push(c);
84+
result.push_char(c);
8485
}
85-
encode_utf16(&result).collect()
86+
result
8687
}
87-
_ => Vec::new(),
88+
_ => U16String::new(),
8889
}),
8990

9091
SWITCH | WORLD_SWITCH => BuildingData::Switch(match config {
@@ -181,12 +182,12 @@ impl Building {
181182
pub fn borrow_data<T, U, R>(
182183
&self,
183184
state: &ProcessorState,
184-
variables: &HashMap<String, LVar>,
185+
variables: &Variables,
185186
f_processor: T,
186187
f_other: U,
187188
) -> R
188189
where
189-
T: FnOnce(&ProcessorState, &HashMap<String, LVar>) -> R,
190+
T: FnOnce(&ProcessorState, &Variables) -> R,
190191
U: FnOnce(&BuildingData) -> R,
191192
{
192193
match self.data.try_borrow() {
@@ -201,12 +202,12 @@ impl Building {
201202
pub fn borrow_data_mut<T, U, R>(
202203
&self,
203204
state: &mut ProcessorState,
204-
variables: &HashMap<String, LVar>,
205+
variables: &Variables,
205206
f_processor: T,
206207
f_other: U,
207208
) -> R
208209
where
209-
T: FnOnce(&mut ProcessorState, &HashMap<String, LVar>) -> R,
210+
T: FnOnce(&mut ProcessorState, &Variables) -> R,
210211
U: FnOnce(&mut BuildingData) -> R,
211212
{
212213
match self.data.try_borrow_mut() {
@@ -223,7 +224,7 @@ impl Building {
223224
pub enum BuildingData {
224225
Processor(Processor),
225226
Memory(Box<[f64]>),
226-
Message(Vec<u16>),
227+
Message(U16String),
227228
Switch(bool),
228229
Unknown {
229230
config: Object,

0 commit comments

Comments
 (0)