Share native checkpoint command bindings and output probes

This commit is contained in:
gloamxun committed 2026-11-29 01:00:10 +08:00
1 parent 8aa079ec25
commit 373204a8d8
4 files changed
+167 -50

No files matched your search

+57 -43
View File
@@ -1,5 +1,6 @@
use std::collections::BTreeMap;
use std::hash::{DefaultHasher, Hash, Hasher};
use std::sync::Arc;
use super::{
CatCode, FontId, Glue, JobParameter, JobTime, NativeBox, Parameter, ParameterValue, Register,
@@ -789,10 +790,15 @@ enum PdftexRevision {
R24,
}
#[derive(Clone, Debug, Default, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
#[serde(transparent)]
struct CommandBindings(#[serde(with = "ordered_map")] BTreeMap<Arc<CommandName>, Meaning>);
#[derive(Clone, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
pub struct State {
#[serde(with = "ordered_map")]
commands: BTreeMap<CommandName, Meaning>,
// Checkpoints share the table. A write copies the ordered bindings, but
// their immutable names and macro bodies stay shared across revisions.
commands: Arc<CommandBindings>,
catcodes: BTreeMap<char, CatCode>,
counts: BTreeMap<u16, i32>,
toks: BTreeMap<u16, Vec<Token>>,
@@ -1035,7 +1041,7 @@ impl Default for State {
fn default() -> Self {
use Primitive::*;
let mut state = Self {
commands: BTreeMap::new(),
commands: Arc::default(),
catcodes: BTreeMap::new(),
counts: BTreeMap::new(),
toks: BTreeMap::new(),
@@ -1330,13 +1336,13 @@ impl Default for State {
("font", Font),
("spacefactor", Spacefactor),
] {
state.commands.insert(
CommandName::Control(name.into()),
state.command_bindings_mut().insert(
CommandName::Control(name.into()).into(),
Meaning::Primitive(primitive),
);
}
state.commands.insert(
CommandName::Control(" ".into()),
state.command_bindings_mut().insert(
CommandName::Control(" ".into()).into(),
Meaning::Primitive(Explicitspace),
);
for p in TokenParameter::ALL {
@@ -1344,8 +1350,8 @@ impl Default for State {
continue;
}
state
.commands
.insert(CommandName::Control(p.name()), Meaning::TokenParameter(*p));
.command_bindings_mut()
.insert(CommandName::Control(p.name()).into(), Meaning::TokenParameter(*p));
}
for p in Parameter::ALL {
if matches!(
@@ -1364,11 +1370,11 @@ impl Default for State {
continue;
}
state
.commands
.insert(CommandName::Control(p.name()), Meaning::Parameter(*p));
.command_bindings_mut()
.insert(CommandName::Control(p.name()).into(), Meaning::Parameter(*p));
}
state.commands.insert(
CommandName::Control("nullfont".into()),
state.command_bindings_mut().insert(
CommandName::Control("nullfont".into()).into(),
Meaning::Font(FontId::NULL),
);
// Explicit text-bootstrap environment, not INITEX defaults or a format.
@@ -1400,9 +1406,9 @@ impl State {
}
return;
}
c.usage.state.command_entries = self.commands.len();
for (name, meaning) in &self.commands {
if let CommandName::Control(name) = name {
c.usage.state.command_entries = self.commands.0.len();
for (name, meaning) in &self.commands.0 {
if let CommandName::Control(name) = name.as_ref() {
c.usage.state.control_name_bytes += name.len();
}
if let Meaning::Macro(macro_definition) = meaning {
@@ -1620,8 +1626,8 @@ impl State {
self.install_line_penalty_primitives();
self.install_primitive_commands();
let hook = TokenParameter::Everyeof;
self.commands.insert(
CommandName::Control(hook.name()),
self.command_bindings_mut().insert(
CommandName::Control(hook.name()).into(),
Meaning::TokenParameter(hook),
);
for parameter in [
@@ -1635,8 +1641,8 @@ impl State {
Parameter::Lastlinefit,
Parameter::Savinghyphcodes,
] {
self.commands.insert(
CommandName::Control(parameter.name()),
self.command_bindings_mut().insert(
CommandName::Control(parameter.name()).into(),
Meaning::Parameter(parameter),
);
}
@@ -1681,8 +1687,8 @@ impl State {
("fontchardp", Fontchardp),
("fontcharic", Fontcharic),
] {
self.commands.insert(
CommandName::Control(name.into()),
self.command_bindings_mut().insert(
CommandName::Control(name.into()).into(),
Meaning::Primitive(primitive),
);
}
@@ -1870,8 +1876,8 @@ impl State {
Meaning::Parameter(Parameter::Pdfsuppresswarningdupmap),
),
] {
self.commands
.entry(CommandName::Control(name.into()))
self.command_bindings_mut()
.entry(CommandName::Control(name.into()).into())
.or_insert(meaning);
}
}
@@ -1887,8 +1893,8 @@ impl State {
pub(super) fn install_line_penalty_primitives(&mut self) {
if self.profile == Profile::EtexBootstrap {
for kind in LinePenaltyKind::ALL {
self.commands
.entry(CommandName::Control(kind.name().into()))
self.command_bindings_mut()
.entry(CommandName::Control(kind.name().into()).into())
.or_insert(Meaning::Primitive(Primitive::LinePenalties(kind)));
}
}
@@ -1896,8 +1902,8 @@ impl State {
pub(super) fn restore_legacy_etex_parameter(&mut self, parameter: Parameter) {
if self.profile == Profile::EtexBootstrap {
self.commands
.entry(CommandName::Control(parameter.name()))
self.command_bindings_mut()
.entry(CommandName::Control(parameter.name()).into())
.or_insert(Meaning::Parameter(parameter));
}
}
@@ -1985,9 +1991,9 @@ impl State {
fn update_profile_command(&mut self, name: &str, meaning: Meaning, enabled: bool) {
let name = CommandName::Control(name.into());
if enabled {
self.commands.entry(name).or_insert(meaning);
} else if self.commands.get(&name) == Some(&meaning) {
self.commands.remove(&name);
self.command_bindings_mut().entry(name.into()).or_insert(meaning);
} else if self.commands.0.get(&name) == Some(&meaning) {
self.command_bindings_mut().remove(&name);
}
}
@@ -2143,9 +2149,9 @@ impl State {
let name = CommandName::Control(name.into());
let meaning = Meaning::Primitive(primitive);
if self.input_profile != super::InputProfile::TeX8Bit {
self.commands.insert(name, meaning);
} else if self.commands.get(&name) == Some(&meaning) {
self.commands.remove(&name);
self.command_bindings_mut().insert(name.into(), meaning);
} else if self.commands.0.get(&name) == Some(&meaning) {
self.command_bindings_mut().remove(&name);
}
}
}
@@ -2213,15 +2219,23 @@ impl State {
.get_or_init(|| {
let state = Self::initex().with_input_profile(self.input_profile);
let state = if extended { state.with_etex() } else { state };
state.commands.into_iter().filter_map(|(name, meaning)| match name {
CommandName::Control(name) => Some((name, meaning)),
_ => None,
}).collect()
Arc::unwrap_or_clone(state.commands)
.0
.into_iter()
.filter_map(|(name, meaning)| match Arc::unwrap_or_clone(name) {
CommandName::Control(name) => Some((name, meaning)),
_ => None,
})
.collect()
})
.get(name)
.map(|meaning| (name, meaning))
}
fn command_bindings_mut(&mut self) -> &mut BTreeMap<Arc<CommandName>, Meaning> {
&mut Arc::make_mut(&mut self.commands).0
}
pub fn meaning(&self, name: &CommandName) -> Meaning {
match name {
CommandName::FrozenRelax => Meaning::Primitive(Primitive::Relax),
@@ -2229,7 +2243,7 @@ impl State {
CommandName::FrozenPrimitive(_) => self.original_primitive(name)
.map(|(_, meaning)| meaning.clone()).unwrap_or(Meaning::Undefined),
CommandName::Font(font) => Meaning::Font(*font),
_ => self.commands.get(name).cloned().unwrap_or(Meaning::Undefined),
_ => self.commands.0.get(name).cloned().unwrap_or(Meaning::Undefined),
}
}
/// Classic projection for byte-table consumers. A code outside the
@@ -2486,10 +2500,10 @@ impl State {
match (key, value) {
(Key::Command(name), Saved::Command(value)) => match value {
Some(value) => {
self.commands.insert(name, value);
self.command_bindings_mut().insert(name.into(), value);
}
None => {
self.commands.remove(&name);
self.command_bindings_mut().remove(&name);
}
},
(Key::Catcode(c), Saved::Catcode(value)) => match value {
@@ -2628,7 +2642,7 @@ impl State {
return;
}
let value = match &key {
Key::Command(name) => Saved::Command(self.commands.get(name).cloned()),
Key::Command(name) => Saved::Command(self.commands.0.get(name).cloned()),
Key::Catcode(c) => Saved::Catcode(self.catcodes.get(c).copied()),
Key::Count(index) => Saved::Integer(self.count(*index)),
Key::Endlinechar => Saved::Integer(self.endlinechar),
@@ -2666,7 +2680,7 @@ impl State {
}
pub(super) fn define(&mut self, name: CommandName, meaning: Meaning, global: bool) {
self.save(Key::Command(name.clone()), global);
self.commands.insert(name, meaning);
self.command_bindings_mut().insert(name.into(), meaning);
}
pub(super) fn assign_catcode(&mut self, c: char, value: CatCode, global: bool) {
self.save(Key::Catcode(c), global);