Files
aego/script/compiler/async.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

123 lines
2.7 KiB
Go

package compiler
import (
"aego/script/ast"
"aego/script/bytecode"
"aego/script/checker"
)
func funcSlots(f *checker.Func) []int {
out := make([]int, len(f.Params))
for i, p := range f.Params {
out[i] = p.Type.Slots()
}
return out
}
func signalSlots(sg *checker.ScriptSignal) []int {
out := make([]int, len(sg.Params))
for i, p := range sg.Params {
out[i] = p.Slots()
}
return out
}
func waitKind(k ast.YieldKind) uint8 {
switch k {
case ast.YieldWait:
return bytecode.WaitTime
case ast.YieldFrames:
return bytecode.WaitFrames
case ast.YieldFixed:
return bytecode.WaitFixed
case ast.YieldAwait:
return bytecode.WaitAwait
}
return bytecode.WaitNext
}
func (c *fn) startTo(e *ast.StartExpr, dst int) {
f := c.u.a.Starts[e]
if f == nil {
return
}
idx, ok := c.u.funcs[f]
if !ok {
return
}
c.invoke(bytecode.START, idx, e.Args, funcSlots(f), 1, dst, true)
}
func (c *fn) yield(s *ast.YieldStmt) {
kind := int(waitKind(s.Kind))
if s.Value == nil || s.Kind == ast.YieldNext {
c.b.ABC(bytecode.YIELD, 0, 0, kind)
return
}
mark := c.top
r := c.operand(s.Value)
c.b.ABC(bytecode.YIELD, r, 0, kind)
c.top = mark
}
func (c *fn) selfOr(e ast.Expr, dst int) {
if e == nil {
c.b.ABC(bytecode.SELFREF, dst, 0, 0)
return
}
c.exprTo(e, dst)
}
func (c *fn) asyncCall(e *ast.CallExpr, call *checker.Call, dst int, want bool) {
if !want {
dst = c.alloc(1)
}
switch call.Kind {
case checker.CallEmit:
c.invoke(bytecode.EMIT, call.Signal.Index, e.Args, signalSlots(call.Signal), 0, dst, false)
case checker.CallConnect:
sg, h := call.Signal, call.Handler
idx := c.u.connect(bytecode.ConnectDef{
Source: sg.Script.FullName,
Signal: sg.Name,
Target: h.Script.FullName,
Handler: h.Name,
})
mark := c.top
w := c.alloc(3)
c.selfOr(call.Recv, w+1)
c.selfOr(call.HandlerRecv, w+2)
c.b.ABx(bytecode.CONNECT, w, idx)
c.move(dst, w, 1)
c.top = mark
case checker.CallDisconnect:
mark := c.top
r := c.operand(call.Recv)
c.b.ABC(bytecode.DISCONNECT, r, 0, 0)
c.top = mark
case checker.CallConnected:
mark := c.top
r := c.operand(call.Recv)
c.b.ABC(bytecode.CONNECTED, dst, r, 0)
c.top = mark
case checker.CallCoStop:
mark := c.top
r := c.operand(call.Recv)
c.b.ABC(bytecode.COSTOP, r, 0, 0)
c.top = mark
case checker.CallCoRunning:
mark := c.top
r := c.operand(call.Recv)
c.b.ABC(bytecode.CORUN, dst, r, 0)
c.top = mark
case checker.CallGetScript:
c.exprTo(call.Recv, dst)
c.b.ABx(bytecode.GETSCRIPT, dst, c.u.scriptRef(call.Target))
case checker.CallScriptMethod:
f := call.Func
args := append([]ast.Expr{call.Recv}, e.Args...)
params := append([]int{1}, funcSlots(f)...)
c.invoke(bytecode.CALLR, c.u.xmethod(f), args, params, f.Result.Slots(), dst, want)
}
}