world_io.bend

bendvy-ecs-core@0.1.0.1 · 34 lines · 0 definitions

1import Base
2import bendvy-ecs-core@0.1.0.1/world.bend as W
3import bendvy-ecs-core@0.1.0.1/world_namespace.bend as F
4
5# Ordinary creation uses the checked host IO namespace allocator.
6# Canonical linked Native execution and a shared JS realm are qualified separately.
7# with_namespace/convert and raw World construction are trusted integration helpers.
8# No language-wide constructor/authority confinement is claimed.
9# Concrete World constructors remain a trusted schema/admin boundary.
10# World creation outcome: Created owns the new World; Refused returns the
11# caller's resource owner without constructing a usable World.
12type Creation<S: Data,C: Type,R: Type,E: Data> is Type:
13  Created{world: W.World<S,C,R,E>}
14  Refused{resource: R}
15
16def convert(~S: Data,~C: Type,~R: Type,~E: Data,created: W.Creation<S,C,R,E>) -> Creation<S,C,R,E>:
17  match created:
18    case W.Created{_,world}: Created{world}
19    case W.CreateRejected{_,resource}: Refused{resource}
20
21# Trusted integration constructor with a caller-supplied namespace. Rejects
22# reserved namespace values and returns the resource on refusal. Does not
23# allocate a unique namespace: caller must prevent collisions. Prefer create.
24def with_namespace(~S: Data,~C: Type,~R: Type,~E: Data,~empty: Unit -> C,+namespace: U32,resource: R) -> IO(Creation<S,C,R,E>):
25  IO.pure(Creation<S,C,R,E>,convert(~S,~C,~R,~E,W.create_checked(~S,~C,~R,~E,~empty,Bool.and(U32.is_lt(0,namespace),U32.is_lt(namespace,4294967295)),namespace,resource)))
26
27# Create an independent World through the host IO namespace allocator.
28# Consumes resource; Created returns its World, Refused returns resource.
29# empty is trusted schema setup constructing component storage. Namespace
30# uniqueness is scoped to the qualified host allocator realm/process, not
31# arbitrary remote processes or raw constructors.
32# Example: examples/scheduled-health/main.bend; identity: docs/parity/identity.md.
33def create(~S: Data,~C: Type,~R: Type,~E: Data,~empty: Unit -> C,resource: R) -> IO(Creation<S,C,R,E>):
34  IO.bind(U32,Creation<S,C,R,E>,F.fresh(),namespace => with_namespace(~S,~C,~R,~E,~empty,namespace,resource))