Compare commits

..

1 commit

Author SHA1 Message Date
marked
733d43d360 Sync to upstream Jecs 0.5.5-nightly.20250412T181729Z 2025-04-12 18:17:45 +00:00
10 changed files with 181 additions and 123 deletions

View file

@ -4,7 +4,7 @@
"testkit": "tools/testkit", "testkit": "tools/testkit",
"mirror": "mirror", "mirror": "mirror",
"tools": "tools", "tools": "tools",
"addons": "addons" "addons": "addons",
}, },
"languageMode": "strict" "languageMode": "strict"
} }

View file

@ -23,8 +23,6 @@ The format is based on [Keep a Changelog][kac], and this project adheres to
- This should allow a more lenient window for modifying data - This should allow a more lenient window for modifying data
- Changed `OnRemove` to lazily lookup which archetype the entity will move to - Changed `OnRemove` to lazily lookup which archetype the entity will move to
- Can now have interior structural changes within `OnRemove` hooks - Can now have interior structural changes within `OnRemove` hooks
- Optimized `world:has` for both single component and multiple component presence.
- This comes at the cost that it cannot check the component presence for more than 4 components at a time. If this is important, consider calling to this function multiple times.
## [0.5.0] - 2024-12-26 ## [0.5.0] - 2024-12-26

View file

@ -1,32 +1,20 @@
local jecs = require("@jecs") local jecs = require("@jecs")
local testkit = require("@testkit")
type Observer<T...> = {
callback: (jecs.Entity) -> (),
query: jecs.Query<T...>,
}
export type PatchedWorld = jecs.World & {
added: (PatchedWorld, jecs.Id, (e: jecs.Entity, id: jecs.Id, value: any) -> ()) -> (),
removed: (PatchedWorld, jecs.Id, (e: jecs.Entity, id: jecs.Id) -> ()) -> (),
changed: (PatchedWorld, jecs.Id, (e: jecs.Entity, id: jecs.Id) -> ()) -> (),
observer: (PatchedWorld, Observer<any>) -> (),
monitor: (PatchedWorld, Observer<any>) -> (),
}
local function observers_new(world, description) local function observers_new(world, description)
local query = description.query local query = description.query
local callback = description.callback local callback = description.callback
local terms = query.filter_with :: { jecs.Id } local terms = query.filter_with
if not terms then if not terms then
local ids = query.ids local ids = query.ids
query.filter_with = ids query.filter_with = ids
terms = ids terms = ids
end end
local entity_index = world.entity_index :: any local entity_index = world.entity_index
local function emplaced(entity: jecs.Entity) local function emplaced(entity)
local r = jecs.entity_index_try_get_fast( local r = jecs.entity_index_try_get_fast(
entity_index, entity :: any) entity_index, entity)
if not r then if not r then
return return
@ -45,20 +33,18 @@ local function observers_new(world, description)
end end
end end
local function monitors_new(world, description) local function world_track(world, ...)
local query = description.query local entity_index = world.entity_index
local callback = description.callback local terms = { ... }
local terms = query.filter_with :: { jecs.Id } local q_shim = { filter_with = terms }
if not terms then
local ids = query.ids
query.filter_with = ids
terms = ids
end
local entity_index = world.entity_index :: any local n = 0
local function emplaced(entity: jecs.Entity) local dense_array = {}
local sparse_array = {}
local function emplaced(entity)
local r = jecs.entity_index_try_get_fast( local r = jecs.entity_index_try_get_fast(
entity_index, entity :: any) entity_index, entity)
if not r then if not r then
return return
@ -66,39 +52,55 @@ local function monitors_new(world, description)
local archetype = r.archetype local archetype = r.archetype
if jecs.query_match(query, archetype) then if jecs.query_match(q_shim :: any, archetype) then
callback(entity, jecs.OnAdd) n += 1
dense_array[n] = entity
sparse_array[entity] = n
end end
end end
local function removed(entity: jecs.Entity, component: jecs.Id) local function removed(entity)
local r = jecs.entity_index_try_get_fast( local i = sparse_array[entity]
entity_index, entity :: any) if i ~= n then
dense_array[i] = dense_array[n]
if not r then
return
end end
local archetype = r.archetype dense_array[n] = nil
if jecs.query_match(query, archetype) then
callback(entity, jecs.OnRemove)
end
end end
for _, term in terms do for _, term in terms do
world:added(term, emplaced) world:added(term, emplaced)
world:removed(term, removed) world:changed(term, emplaced)
end end
local function iter()
local i = n
return function()
local row = i
if row == 0 then
return nil
end
i -= 1
return dense_array[row] :: any
end
end
local it = {
__iter = iter,
without = function(self, ...)
q_shim.filter_without = { ... }
return self
end
}
return setmetatable(it, it)
end end
local function observers_add(world: jecs.World & { [string]: any }): PatchedWorld local function observers_add(world)
local signals = { local signals = {
added = {}, added = {},
emplaced = {}, emplaced = {},
removed = {} removed = {}
} }
world.added = function(_, component, fn) world.added = function(_, component, fn)
local listeners = signals.added[component] local listeners = signals.added[component]
if not listeners then if not listeners then
@ -107,7 +109,7 @@ local function observers_add(world: jecs.World & { [string]: any }): PatchedWorl
local idr = jecs.id_record_ensure(world, component) local idr = jecs.id_record_ensure(world, component)
idr.hooks.on_add = function(entity) idr.hooks.on_add = function(entity)
for _, listener in listeners do for _, listener in listeners do
listener(entity, component) listener(entity)
end end
end end
end end
@ -122,7 +124,7 @@ local function observers_add(world: jecs.World & { [string]: any }): PatchedWorl
local idr = jecs.id_record_ensure(world, component) local idr = jecs.id_record_ensure(world, component)
idr.hooks.on_change = function(entity, value) idr.hooks.on_change = function(entity, value)
for _, listener in listeners do for _, listener in listeners do
listener(entity, component, value) listener(entity, value)
end end
end end
end end
@ -137,7 +139,7 @@ local function observers_add(world: jecs.World & { [string]: any }): PatchedWorl
local idr = jecs.id_record_ensure(world, component) local idr = jecs.id_record_ensure(world, component)
idr.hooks.on_remove = function(entity) idr.hooks.on_remove = function(entity)
for _, listener in listeners do for _, listener in listeners do
listener(entity, component) listener(entity)
end end
end end
end end
@ -146,10 +148,9 @@ local function observers_add(world: jecs.World & { [string]: any }): PatchedWorl
world.signals = signals world.signals = signals
world.track = world_track
world.observer = observers_new world.observer = observers_new
world.monitor = monitors_new
return world return world
end end

View file

@ -1,2 +1,2 @@
modified = ["addons/observers.luau", ".luaurc", "CHANGELOG.md", "jecs.luau"] modified = ["addons/observers.luau", ".luaurc", "CHANGELOG.md", "jecs.luau"]
version = "0.5.5-nightly.20250413T001101Z" version = "0.5.5-nightly.20250412T181729Z"

View file

@ -466,9 +466,7 @@ local function world_has_one_inline(world: ecs_world_t, entity: i53, id: i53): b
return records[id] ~= nil return records[id] ~= nil
end end
local function world_has(world: ecs_world_t, entity: i53, local function world_has(world: ecs_world_t, entity: i53, ...: i53): boolean
a: i53, b: i53?, c: i53?, d: i53?, e: i53?): boolean
local record = entity_index_try_get_fast(world.entity_index, entity) local record = entity_index_try_get_fast(world.entity_index, entity)
if not record then if not record then
return false return false
@ -481,11 +479,13 @@ local function world_has(world: ecs_world_t, entity: i53,
local records = archetype.records local records = archetype.records
return records[a] ~= nil and for i = 1, select("#", ...) do
(b == nil or records[b] ~= nil) and if not records[select(i, ...)] then
(c == nil or records[c] ~= nil) and return false
(d == nil or records[d] ~= nil) and end
(e == nil or error("args exceeded")) end
return true
end end
local function world_target(world: ecs_world_t, entity: i53, relation: i24, index: number?): i24? local function world_target(world: ecs_world_t, entity: i53, relation: i24, index: number?): i24?
@ -2420,10 +2420,7 @@ export type World = {
& <A, B, C, D>(self: World, id: Entity, Id<A>, Id<B>, Id<C>, Id<D>) -> (A?, B?, C?, D?), & <A, B, C, D>(self: World, id: Entity, Id<A>, Id<B>, Id<C>, Id<D>) -> (A?, B?, C?, D?),
--- Returns whether the entity has the ID. --- Returns whether the entity has the ID.
has: (<A>(World, Entity, A) -> boolean) has: (self: World, entity: Entity, ...Id) -> boolean,
& (<A, B>(World, Entity, A, B) -> boolean)
& (<A, B, C>(World, Entity, A, B, C) -> boolean)
& <A, B, C, D>(World, Entity, A, B, C, D) -> boolean,
--- Get parent (target of ChildOf relationship) for entity. If there is no ChildOf relationship pair, it will return nil. --- Get parent (target of ChildOf relationship) for entity. If there is no ChildOf relationship pair, it will return nil.
parent:(self: World, entity: Entity) -> Entity, parent:(self: World, entity: Entity) -> Entity,
@ -2490,9 +2487,9 @@ return {
ECS_ID_DELETE = ECS_ID_DELETE, ECS_ID_DELETE = ECS_ID_DELETE,
IS_PAIR = (ECS_IS_PAIR :: any) :: <P, O>(pair: Pair<P, O>) -> boolean, IS_PAIR = ECS_IS_PAIR,
pair_first = (ecs_pair_first :: any) :: <P, O>(world: World, pair: Pair<P, O>) -> Id<P>, pair_first = ecs_pair_first,
pair_second = (ecs_pair_second :: any) :: <P, O>(world: World, pair: Pair<P, O>) -> Id<O>, pair_second = ecs_pair_second,
entity_index_get_alive = entity_index_get_alive, entity_index_get_alive = entity_index_get_alive,
archetype_append_to_records = archetype_append_to_records, archetype_append_to_records = archetype_append_to_records,

View file

@ -3,7 +3,7 @@ includes = ["init.luau", "pesde.toml", "README.md", "CHANGELOG.md", "LICENSE", "
license = "MIT" license = "MIT"
name = "marked/jecs_nightly" name = "marked/jecs_nightly"
repository = "https://git.devmarked.win/marked/jecs-nightly" repository = "https://git.devmarked.win/marked/jecs-nightly"
version = "0.5.5-nightly.20250413T001101Z" version = "0.5.5-nightly.20250412T181729Z"
[indices] [indices]
default = "https://github.com/pesde-pkg/index" default = "https://github.com/pesde-pkg/index"

View file

@ -3,7 +3,7 @@ includes = ["init.luau", "pesde.toml", "README.md", "CHANGELOG.md", "LICENSE", "
license = "MIT" license = "MIT"
name = "marked/jecs_nightly" name = "marked/jecs_nightly"
repository = "https://git.devmarked.win/marked/jecs-nightly" repository = "https://git.devmarked.win/marked/jecs-nightly"
version = "0.5.5-nightly.20250413T001101Z" version = "0.5.5-nightly.20250412T181729Z"
[indices] [indices]
default = "https://github.com/pesde-pkg/index" default = "https://github.com/pesde-pkg/index"

View file

@ -1,2 +1,2 @@
passed = true passed = true
timestamp = "20250413T001103Z" timestamp = "20250412T181730Z"

View file

@ -1,39 +1,77 @@
*created e271v0
*created e272v0
*created e273v0
*created e274v0
*created e275v0
*created e276v0
*created e277v0
*created e278v0
*created e279v0
*created e280v0
|-alive--|
| e271v0 |
|--------|
| e272v0 |
|--------|
| e273v0 |
|--------|
| e274v0 |
|--------|
| e275v0 |
|--------|
| e276v0 |
|--------|
| e277v0 |
|--------|
| e278v0 |
|--------|
| e279v0 |
|--------|
| e280v0 |
|--------|
*deleted e270v0
*deleted e271v0
*deleted e272v0
*deleted e273v0
*deleted e274v0
*deleted e275v0
*deleted e274v1
*deleted e273v1
*deleted e272v1
*deleted e271v1
*deleted e270v1
----idempotent ----idempotent
1_2 1_2
7.4 us  2 kB│ delete children of entity 1_2
9.5 us  1 kB│ remove friends of entity 7.1 us  1 kB│ delete children of entity
352 ns  0  B│ simple deletion of entity 9.2 us  1 kB│ remove friends of entity
world:add() 346 ns  0  B│ simple deletion of entity
PASS│ idempotent the great reset
PASS│ archetype move
world:children()
PASS│  PASS│ 
world:clear() #repro3
PASS│ should remove its components PASS│ should add the correct ModelBase for parts
PASS│ remove cleared ID from entities PASS│ should add the correct ModelBase for parts
world:component()
PASS│ only components should have EcsComponent trait
PASS│ tag
world:contains()
PASS│  PASS│ 
PASS│ should not exist after delete
world:delete() #adding a recycled target
PASS│ invoke OnRemove hooks PASS│ 
PASS│ delete recycled entity id used as component
PASS│ bug: Empty entity does not respect cleanup policy
PASS│ should allow deleting components
PASS│ delete entities using another Entity as component with Delete cleanup action
PASS│ delete children
PASS│ remove deleted ID from entities
PASS│ fast delete
PASS│ cycle
world:each() #repro2
PASS│ 
another
PASS│ 
#repro
PASS│ 
archetype
PASS│ 
world:cleanup()
PASS│  PASS│ 
world:entity() world:entity()
@ -43,9 +81,16 @@
PASS│ Recycling PASS│ Recycling
PASS│ Recycling max generation PASS│ Recycling max generation
world:has() world:set()
PASS│ should find Tag on entity PASS│ archetype move
PASS│ should return false when missing one tag PASS│ pairs
world:remove()
PASS│ should allow remove a component that doesn't exist on entity
world:add()
PASS│ idempotent
PASS│ archetype move
world:query() world:query()
PASS│ cached PASS│ cached
@ -65,32 +110,49 @@
PASS│ should error when setting invalid pair PASS│ should error when setting invalid pair
PASS│ should find target for ChildOf PASS│ should find target for ChildOf
PASS│ despawning while iterating PASS│ despawning while iterating
NONE│ iterator invalidation
SKIP│ adding
PASS│ spawning
PASS│ should not find any entities PASS│ should not find any entities
PASS│ world:query():without() PASS│ without
world:remove() world:each
PASS│ should allow remove a component that doesn't exist on entity PASS│ 
world:set() world:children
PASS│ archetype move PASS│ 
PASS│ pairs
world:clear()
PASS│ should remove its components
PASS│ remove cleared ID from entities
world:has()
PASS│ should find Tag on entity
PASS│ should return false when missing one tag
world:component()
PASS│ only components should have EcsComponent trait
PASS│ tag
world:delete
PASS│ invoke OnRemove hooks
PASS│ delete recycled entity id used as component
PASS│ bug: Empty entity does not respect cleanup policy
PASS│ should allow deleting components
PASS│ delete entities using another Entity as component with Delete cleanup action
PASS│ delete children
PASS│ remove deleted ID from entities
PASS│ fast delete
PASS│ cycle
world:target world:target
PASS│ nth index PASS│ nth index
PASS│ infer index when unspecified PASS│ infer index when unspecified
PASS│ loop until no target PASS│ loop until no target
#adding a recycled target world:contains
PASS│ 
#repro2
PASS│ 
another
PASS│ 
#repro
PASS│  PASS│ 
PASS│ should not exist after delete
Hooks Hooks
PASS│ OnAdd PASS│ OnAdd
@ -115,5 +177,5 @@
PASS│ #2 PASS│ #2
PASS│ #3 PASS│ #3
68/68 test cases passed in 29.621 ms. 77/77 test cases passed in 29.411 ms.
0 fails 0 fails

View file

@ -5,4 +5,4 @@ license = "MIT"
name = "mark-marks/jecs-nightly" name = "mark-marks/jecs-nightly"
realm = "shared" realm = "shared"
registry = "https://github.com/UpliftGames/wally-index" registry = "https://github.com/UpliftGames/wally-index"
version = "0.5.5-nightly.20250413T001101Z" version = "0.5.5-nightly.20250412T181729Z"