Skip to content

Commit

Permalink
Provide fast path to hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
Ukendio committed Oct 23, 2024
1 parent 3e79ff4 commit e4b266d
Showing 1 changed file with 27 additions and 45 deletions.
72 changes: 27 additions & 45 deletions src/init.luau
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,11 @@ local function id_record_ensure(world: World, id: number): IdRecord
size = 0,
cache = {},
flags = flags,
hooks = {
on_add = on_add,
on_set = on_set,
on_remove = on_remove
}
} :: IdRecord
componentIndex[id] = idr
end
Expand Down Expand Up @@ -688,11 +693,8 @@ local function archetype_traverse_remove(world: World, id: i53, from: Archetype)
return to :: Archetype
end

local function invoke_hook(world: World, hook_id: number, id: i53, entity: i53, data: any?)
local hook = world_get_one_inline(world, id, hook_id)
if hook then
hook(entity, data)
end
local function invoke_hook(action, entity, data)
action(entity, data)
end

local function world_add(world: World, entity: i53, id: i53): ()
Expand All @@ -712,10 +714,10 @@ local function world_add(world: World, entity: i53, id: i53): ()
end

local idr = world.componentIndex[id]
local has_on_add = bit32.band(idr.flags, ECS_ID_HAS_ON_ADD) ~= 0
local on_add = idr.hooks.on_add

if has_on_add then
invoke_hook(world, EcsOnAdd, id, entity)
if on_add then
invoke_hook(on_add, entity)
end
end

Expand All @@ -727,7 +729,7 @@ local function world_set(world: World, entity: i53, id: i53, data: unknown): ()
local idr = world.componentIndex[id]
local flags = idr.flags
local is_tag = bit32.band(flags, ECS_ID_IS_TAG) ~= 0
local has_on_set = bit32.band(flags, ECS_ID_HAS_ON_SET) ~= 0
local idr_hooks = idr.hooks

if from == to then
if is_tag then
Expand All @@ -737,8 +739,9 @@ local function world_set(world: World, entity: i53, id: i53, data: unknown): ()
-- and just set the data directly.
local tr = to.records[id]
from.columns[tr.column][record.row] = data
if has_on_set then
invoke_hook(world, EcsOnSet, id, entity, data)
local on_set = idr_hooks.on_set
if on_set then
invoke_hook(on_set, entity, data)
end

return
Expand All @@ -754,10 +757,9 @@ local function world_set(world: World, entity: i53, id: i53, data: unknown): ()
end
end

local has_on_add = bit32.band(flags, ECS_ID_HAS_ON_ADD) ~= 0

if has_on_add then
invoke_hook(world, EcsOnAdd, id, entity)
local on_add = idr_hooks.on_add
if on_add then
invoke_hook(on_add, entity)
end

if is_tag then
Expand All @@ -769,8 +771,9 @@ local function world_set(world: World, entity: i53, id: i53, data: unknown): ()

column[record.row] = data

if has_on_set then
invoke_hook(world, EcsOnSet, id, entity, data)
local on_set = idr_hooks.on_set
if on_set then
invoke_hook(on_set, entity, data)
end
end

Expand Down Expand Up @@ -798,10 +801,9 @@ local function world_remove(world: World, entity: i53, id: i53)

if from and not (from == to) then
local idr = world.componentIndex[id]
local flags = idr.flags
local has_on_remove = bit32.band(flags, ECS_ID_HAS_ON_REMOVE) ~= 0
if has_on_remove then
invoke_hook(world, EcsOnRemove, id, entity)
local on_remove = idr.hooks.on_remove
if on_remove then
invoke_hook(on_remove, entity)
end

entity_move(entity_index, entity, record, to)
Expand Down Expand Up @@ -848,7 +850,10 @@ local function archetype_delete(world: World, archetype: Archetype, row: number,
-- TODO: if last == 0 then deactivate table

for _, id in types do
invoke_hook(world, EcsOnRemove, id, delete)
local on_remove = world_get_one_inline(world, id, EcsOnRemove)
if on_remove then
on_remove(delete)
end
end

if row == last then
Expand Down Expand Up @@ -1534,14 +1539,6 @@ 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 @@ -1566,17 +1563,6 @@ 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}`
local why = `cannot call world:set inside {hook_fn} because it adds the component {get_name(world, id)}`
why ..= `\n[jecs note]: consider handling this logic inside of a system`
throw(why)
return
end
end

world_set(world, entity, id, value)
end

Expand All @@ -1588,10 +1574,6 @@ 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

0 comments on commit e4b266d

Please sign in to comment.