Fix invalidated archetype before invoking onremove hooks

This commit is contained in:
Ukendio 2025-12-21 17:04:15 +01:00
parent 81792fe314
commit f543c06462
2 changed files with 33 additions and 11 deletions

View file

@ -3083,6 +3083,16 @@ local function world_new()
end
local archetype = record.archetype
if archetype then
for _, id in archetype.types do
local cr = component_index[id]
local on_remove = cr.on_remove
if on_remove then
on_remove(entity, id, true)
end
end
archetype_delete(world, record.archetype, record.row)
end
local component_index = world.component_index
local archetypes = world.archetypes
@ -3243,16 +3253,7 @@ local function world_new()
end
end
if archetype then
for _, id in archetype.types do
local cr = component_index[id]
local on_remove = cr.on_remove
if on_remove then
on_remove(entity, id, true)
end
end
archetype_delete(world, record.archetype, record.row)
end
local dense = record.dense
local i_swap = entity_index.alive_count

View file

@ -24,7 +24,28 @@ type Id<T=unknown> = jecs.Id<T>
local entity_visualiser = require("@modules/entity_visualiser")
local dwi = entity_visualiser.stringify
FOCUS()
TEST("deleting t1's archetype before invoking its onremove hooks", function()
local pair = jecs.pair
local world = jecs.world()
local rel = world:component()
local t1 = world:entity()
local t2 = world:entity()
--[[
weirdly enough if i do this (only when adding childof relation after adding (rel, t2) to t1) it does not error. Probably a red herring
world:add(t2, pair(rel, t1))
world:add(t1, pair(rel, t2))
world:add(t2, pair(jecs.ChildOf, t1))
--]]
-- this causes world:delete to error
world:add(t2, pair(jecs.ChildOf, t1))
world:add(t1, pair(rel, t2))
world:delete(t1)
end)
TEST("reproduce idr_t nil archetype bug", function()
local world = jecs.world()