fix: normalize main entry IDs for consistent internal sharing (#3966)

This commit is contained in:
三咲雅 misaki masa 2026-05-15 07:30:38 +08:00 committed by GitHub
parent fde563380b
commit 3f5cc47a48
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 19 additions and 14 deletions

View file

@ -41,6 +41,11 @@ impl Runtime {
self.frames.back().map(|f| f.id.as_str()).context("No current runtime frame") self.frames.back().map(|f| f.id.as_str()).context("No current runtime frame")
} }
pub fn current_module(&self) -> Result<&str> {
let s = self.current()?;
Ok(s.split('.').next().unwrap_or(s))
}
pub fn current_owned(&self) -> Result<String> { self.current().map(ToOwned::to_owned) } pub fn current_owned(&self) -> Result<String> { self.current().map(ToOwned::to_owned) }
pub fn get_block(&self, id: &str, calls: usize) -> Option<Function> { pub fn get_block(&self, id: &str, calls: usize) -> Option<Function> {

View file

@ -55,9 +55,9 @@ impl PluginOpt {
fn normalize_id(s: SStr) -> SStr { fn normalize_id(s: SStr) -> SStr {
match s { match s {
Cow::Borrowed(s) => s.trim_end_matches(".main").into(), Cow::Borrowed(s) => s.strip_suffix(".main").unwrap_or(s).into(),
Cow::Owned(mut s) => { Cow::Owned(mut s) => {
s.truncate(s.trim_end_matches(".main").len()); s.truncate(s.strip_suffix(".main").unwrap_or(&s).len());
s.into() s.into()
} }
} }

View file

@ -83,7 +83,7 @@ impl Loader {
where where
F: FnOnce(&Chunk) -> T, F: FnOnce(&Chunk) -> T,
{ {
let (id, plugin, entry) = Self::normalize_id(id)?; let (id, plugin, entry) = Self::explode_id_parts(id)?;
if let Some(c) = self.cache.read().get(id) { if let Some(c) = self.cache.read().get(id) {
return Self::compatible_or_error(id, c).map(|_| f(c)); return Self::compatible_or_error(id, c).map(|_| f(c));
} }
@ -103,7 +103,7 @@ impl Loader {
} }
pub async fn load(&self, lua: &Lua, id: &str) -> mlua::Result<Table> { pub async fn load(&self, lua: &Lua, id: &str) -> mlua::Result<Table> {
let (id, ..) = Self::normalize_id(id)?; let (id, ..) = Self::explode_id_parts(id)?;
let loaded: Table = lua.globals().raw_get::<Table>("package")?.raw_get("loaded")?; let loaded: Table = lua.globals().raw_get::<Table>("package")?.raw_get("loaded")?;
if let Ok(t) = loaded.raw_get(id) { if let Ok(t) = loaded.raw_get(id) {
@ -118,7 +118,7 @@ impl Loader {
} }
async fn load_new(&self, lua: &Lua, id: &str) -> mlua::Result<Table> { async fn load_new(&self, lua: &Lua, id: &str) -> mlua::Result<Table> {
let (id, ..) = Self::normalize_id(id)?; let (id, ..) = Self::explode_id_parts(id)?;
let mut mode = ChunkMode::Text; let mut mode = ChunkMode::Text;
let f = match self.cache.read().get(id) { let f = match self.cache.read().get(id) {
@ -141,7 +141,7 @@ impl Loader {
} }
pub fn load_chunk(&self, lua: &Lua, id: &str, chunk: &Chunk) -> mlua::Result<Table> { pub fn load_chunk(&self, lua: &Lua, id: &str, chunk: &Chunk) -> mlua::Result<Table> {
let (id, ..) = Self::normalize_id(id)?; let (id, ..) = Self::explode_id_parts(id)?;
let loaded: Table = lua.globals().raw_get::<Table>("package")?.raw_get("loaded")?; let loaded: Table = lua.globals().raw_get::<Table>("package")?.raw_get("loaded")?;
if let Ok(t) = loaded.raw_get(id) { if let Ok(t) = loaded.raw_get(id) {
@ -156,7 +156,7 @@ impl Loader {
} }
pub fn try_load(&self, lua: &Lua, id: &str) -> mlua::Result<Table> { pub fn try_load(&self, lua: &Lua, id: &str) -> mlua::Result<Table> {
let (id, ..) = Self::normalize_id(id)?; let (id, ..) = Self::explode_id_parts(id)?;
lua.globals().raw_get::<Table>("package")?.raw_get::<Table>("loaded")?.raw_get(id) lua.globals().raw_get::<Table>("package")?.raw_get::<Table>("loaded")?.raw_get(id)
} }
@ -172,8 +172,8 @@ impl Loader {
); );
} }
pub fn normalize_id(id: &str) -> anyhow::Result<(&str, &str, &str)> { fn explode_id_parts(id: &str) -> anyhow::Result<(&str, &str, &str)> {
let id = id.trim_end_matches(".main"); let id = id.strip_suffix(".main").unwrap_or(id);
let (plugin, entry) = if let Some((a, b)) = id.split_once(".") { (a, b) } else { (id, "main") }; let (plugin, entry) = if let Some((a, b)) = id.split_once(".") { (a, b) } else { (id, "main") };
ensure!(plugin.as_bytes().kebab_cased(), "Plugin name `{plugin}` must be in kebab-case"); ensure!(plugin.as_bytes().kebab_cased(), "Plugin name `{plugin}` must be in kebab-case");

View file

@ -89,10 +89,10 @@ impl Require {
} }
fn absolute_id<'a>(lua: &Lua, id: &'a str) -> mlua::Result<Cow<'a, str>> { fn absolute_id<'a>(lua: &Lua, id: &'a str) -> mlua::Result<Cow<'a, str>> {
let Some(stripped) = id.strip_prefix('.') else { return Ok(id.into()) }; Ok(match id.strip_prefix('.') {
None => id.strip_suffix(".main").unwrap_or(id).into(),
let rt = runtime!(lua)?; Some("main") => runtime!(lua)?.current_module()?.to_owned().into(),
let cur = rt.current()?; Some(rel) => format!("{}.{rel}", runtime!(lua)?.current_module()?).into(),
Ok(format!("{}.{stripped}", cur.split('.').next().unwrap_or(cur)).into()) })
} }
} }