Skip to content

Commit

Permalink
Fix unit tests and debug mode to enforce OnRemove rules
Browse files Browse the repository at this point in the history
  • Loading branch information
Ukendio committed Sep 21, 2024
1 parent 1de93f9 commit 5d6c2e8
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
31 changes: 30 additions & 1 deletion src/init.luau
Original file line number Diff line number Diff line change
Expand Up @@ -1613,7 +1613,7 @@ if _G.__JECS_DEBUG then
throw(msg)
end

local function get_name(world, id): string | number
local function get_name(world, id): string
local name: string | nil
if ECS_IS_PAIR(id) then
name = `pair({get_name(world, ECS_ENTITY_T_HI(id))}, {get_name(world, ECS_ENTITY_T_LO(id))})`
Expand All @@ -1634,6 +1634,14 @@ if _G.__JECS_DEBUG then
return not world_has_one_inline(world, ECS_ENTITY_T_HI(id), EcsComponent)
end

local original_invoke_hook = invoke_hook
local invoked_hook = false
invoke_hook = function(...)
invoked_hook = true
original_invoke_hook(...)
invoked_hook = false
end

World.query = function(world: World, ...)
ASSERT((...), "Requires at least a single component")
return world_query(world, ...)
Expand All @@ -1658,6 +1666,19 @@ if _G.__JECS_DEBUG then
return
end



if world_has_one_inline(world, entity, id) then
if invoked_hook then
local file, line = debug.info(2, "sl")
local hook_fn = `{file}::{line}`
throw(
([[cannot call world:set within the
This world:set function invokes a structural change because %s doesn't exist on the entity.\n
call world:add when the hook %s is in process]]):format(get_name(world, id), hook_fn))
end
end

world_set(world, entity, id, value)
end

Expand All @@ -1670,6 +1691,10 @@ if _G.__JECS_DEBUG then
return
end

if invoked_hook then
local hook_fn = debug.info(2, "sl")
throw(`Cannot call world:add when the hook {hook_fn} is in process`)
end
world_add(world, entity, id)
end

Expand Down Expand Up @@ -1704,6 +1729,10 @@ if _G.__JECS_DEBUG then
end
return world_target(world, entity, relation, index)
end

World.remove = function()

end
end

function World.new()
Expand Down
4 changes: 2 additions & 2 deletions test/tests.luau
Original file line number Diff line number Diff line change
Expand Up @@ -1328,14 +1328,14 @@ TEST("Hooks", function()

world:set(A, jecs.OnRemove, function(entity)
world:set(entity, B, true)
CHECK(not world:get(entity, A))
CHECK(world:get(entity, A))
CHECK(world:get(entity, B))
end)

world:set(e, A, true)
world:remove(e, A)
CHECK(not world:get(e, A))
CHECK(world:get(e, B))
CHECK(not world:get(e, B))
end
end

Expand Down

0 comments on commit 5d6c2e8

Please sign in to comment.