Files
aego/script/signal/signal.go
T
sanspie cf68c71f05 Stage 6: coroutines, signals, ScriptRef, workspace CLI, editor tooling
Language: yield/signal/coroutine become keywords, start is contextual.
Coroutine methods (func Name() coroutine) with start and
yield / wait / frames / fixed / await; signals with Emit/Connect and
Connection handles; ScriptRef<T> and node.GetScript<T>() for fields,
plain methods and signals of other scripts. Diagnostics AES2050-AES2059.

Bytecode v3: ScriptRefs, XFields, XMethods and Connects tables resolved by
name in vm.Link, per-script signals, coroutine flag on functions; new
opcodes START/YIELD/EMIT/CONNECT/DISCONNECT/CONNECTED/COSTOP/CORUN/
SELFREF/GETSCRIPT/GETXF/SETXF/CALLR, checked by verify.

Runtime: script/coroutine scheduler (frames/fixed counters, time heap,
await lists; only due coroutines are visited) and script/signal registry
(snapshot per Emit, deferred removal, no allocations in steady state).
Hot reload cancels coroutines of changed scripts, rebinds connections by
name and signature, then runs Reloaded.

Tooling: script/build removed, aego check/test/dump run on the workspace
(test builds include _test.aes); langsvc completion, hover, signatures and
semantic tokens for the new syntax; editor highlighting, snippets with
placeholders, coroutine/signal profiler graphs and stats.

Docs (script.md sections 16-19, editor.md, ARCHITECTURE), test fixtures and
golden files, platformer enemies wired to the player, scriptreload demo,
and run.Config.FrameTime for deterministic headless fixed steps.

Modules (6.5): engine/module registry and ComponentBuilder are the one
registration path for engine subsystems and native code; render2d,
render3d and script/engine are modules over scene/frame, the script
component moved to scene/scriptcomp, and scene/components and
scene/systems are gone. Editor inspector, templates, picking shapes and
extension hooks are driven by descriptors and editor/builtins; sdk gains
Component/EditorHook; depcheck checks the module graph and the
aego_runtime graph.

Also: stop tracking built tool binaries; gl33 activates the scratch unit
before binding for texture updates.

Verification: typed component method adapters (Method0..2, Proc0..2)
with direct field access and no allocations; app.Build as the single
construction path for run and the editor; modules attach per scene
document; depcheck aego_editor pass; diagnostic codes in one place and
every code covered by a test; indexed coroutine heaps with eager
cancel; dead component references and nil intrinsics fault with
AES6004, host faults keep their code; GC roots of struct fields checked
in verify and vm.Link; 255-field limit; AES4010 for script packages
shadowing host modules; architecture registry D111-D147.

Golden: the harness renders on the thread that owns the GL context,
ReadPixels returns rows in memory order (render targets are top-down per
D17), and the first rendering references are committed.
2026-10-12 14:22:08 +03:00

228 lines
3.8 KiB
Go

package signal
import (
"aego/script/abi"
"aego/script/vm"
)
type Conn struct {
Src vm.InstanceID
Sig int
Dst vm.InstanceID
Method int
Signal string
Handler string
Sign abi.Signature
}
type Deliver func(dst vm.InstanceID, method int, args []vm.Value) (bool, error)
type Handle struct {
Index uint32
Gen uint32
}
func Value(h Handle) vm.Value {
if h.Gen == 0 {
return vm.Value{}
}
return vm.Value{A: uint64(h.Gen)<<32 | uint64(h.Index)}
}
func Of(v vm.Value) Handle {
return Handle{Index: uint32(v.A), Gen: uint32(v.A >> 32)}
}
type entry struct {
gen uint32
live bool
dead bool
c Conn
}
type snap struct {
idx uint32
gen uint32
}
type Registry struct {
conns []entry
free []uint32
bySrc map[vm.InstanceID][]uint32
scratch []snap
depth int
pending bool
n int
}
func New() *Registry {
return &Registry{bySrc: map[vm.InstanceID][]uint32{}}
}
func (r *Registry) get(h Handle) *entry {
if h.Gen == 0 || int(h.Index) >= len(r.conns) {
return nil
}
e := &r.conns[h.Index]
if !e.live || e.gen != h.Gen {
return nil
}
return e
}
func (r *Registry) Len() int {
return r.n
}
func (r *Registry) Connect(c Conn) Handle {
var i uint32
if n := len(r.free); n > 0 {
i = r.free[n-1]
r.free = r.free[:n-1]
} else {
r.conns = append(r.conns, entry{})
i = uint32(len(r.conns) - 1)
}
e := &r.conns[i]
e.gen++
if e.gen == 0 {
e.gen = 1
}
e.live, e.dead, e.c = true, false, c
r.bySrc[c.Src] = append(r.bySrc[c.Src], i)
r.n++
return Handle{Index: i, Gen: e.gen}
}
func (r *Registry) Connected(h Handle) bool {
e := r.get(h)
return e != nil && !e.dead
}
func (r *Registry) Disconnect(h Handle) {
if e := r.get(h); e != nil {
r.kill(h.Index)
}
}
func (r *Registry) kill(i uint32) {
e := &r.conns[i]
if !e.dead {
e.dead = true
r.n--
}
if r.depth > 0 {
r.pending = true
return
}
r.remove(i)
}
func (r *Registry) remove(i uint32) {
e := &r.conns[i]
src := e.c.Src
list := r.bySrc[src]
for k, j := range list {
if j == i {
list = append(list[:k], list[k+1:]...)
break
}
}
if len(list) == 0 {
delete(r.bySrc, src)
} else {
r.bySrc[src] = list
}
e.live, e.dead, e.c = false, false, Conn{}
r.free = append(r.free, i)
}
func (r *Registry) compact() {
r.pending = false
for i := range r.conns {
if e := &r.conns[i]; e.live && e.dead {
r.remove(uint32(i))
}
}
}
func (r *Registry) Emit(src vm.InstanceID, sig int, args []vm.Value, deliver Deliver) error {
list := r.bySrc[src]
if len(list) == 0 {
return nil
}
base := len(r.scratch)
for _, i := range list {
if e := &r.conns[i]; !e.dead && e.c.Sig == sig {
r.scratch = append(r.scratch, snap{idx: i, gen: e.gen})
}
}
end := len(r.scratch)
r.depth++
var err error
for k := base; k < end; k++ {
s := r.scratch[k]
e := &r.conns[s.idx]
if !e.live || e.gen != s.gen {
continue
}
dst, method := e.c.Dst, e.c.Method
ok, derr := deliver(dst, method, args)
if !ok {
if e := &r.conns[s.idx]; e.live && e.gen == s.gen {
r.kill(s.idx)
}
}
if derr != nil {
err = derr
break
}
}
r.scratch = r.scratch[:base]
r.depth--
if r.depth == 0 && r.pending {
r.compact()
}
return err
}
func (r *Registry) DropInstance(id vm.InstanceID) {
if id == vm.NoInstance {
return
}
for i := range r.conns {
e := &r.conns[i]
if e.live && !e.dead && (e.c.Src == id || e.c.Dst == id) {
r.kill(uint32(i))
}
}
}
func (r *Registry) Rebind(fn func(*Conn) bool) int {
n := 0
for i := range r.conns {
e := &r.conns[i]
if !e.live || e.dead {
continue
}
if !fn(&e.c) {
r.kill(uint32(i))
n++
}
}
return n
}
func (r *Registry) Reset() {
for i := range r.conns {
e := &r.conns[i]
if e.live {
e.live, e.dead, e.c = false, false, Conn{}
r.free = append(r.free, uint32(i))
}
}
clear(r.bySrc)
r.scratch = r.scratch[:0]
r.depth, r.pending, r.n = 0, false, 0
}