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.
172 lines
4.8 KiB
Go
172 lines
4.8 KiB
Go
package vm_test
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"aego/script/abi"
|
|
bc "aego/script/bytecode"
|
|
"aego/script/vm"
|
|
)
|
|
|
|
func coFn(name string, regs int, code ...bc.Instr) bc.Function {
|
|
f := fn(name, 0, 0, regs, code...)
|
|
f.Script, f.Coroutine = 0, true
|
|
return f
|
|
}
|
|
|
|
func yieldNext() bc.Instr {
|
|
return bc.ABC(bc.YIELD, 0, 0, 0)
|
|
}
|
|
|
|
func coModule() *bc.Module {
|
|
ret := bc.ABC(bc.RET, 0, 0, 0)
|
|
m := bc.NewModule("co")
|
|
m.Imports = []bc.HostImport{{Module: "t", Name: "hit", Sig: abi.Sig(abi.T(abi.Void), abi.T(abi.Int))}}
|
|
m.Scripts = []bc.ScriptDef{{
|
|
FullName: "co.S", Slots: 2,
|
|
Fields: []bc.FieldDef{{Name: "Id", Type: "int", Slot: 1, Slots: 1}},
|
|
Ready: -1, FixedUpdate: -1, Update: -1, Reloaded: -1,
|
|
}}
|
|
m.Functions = []bc.Function{
|
|
coFn("S.Next", 1, yieldNext(), bc.ABx(bc.GETSF, 0, 1), bc.ABx(bc.CALLH, 0, 0), ret),
|
|
coFn("S.Spin", 1, yieldNext(), bc.AsBx(bc.JMP, 0, -1)),
|
|
coFn("S.Div", 2, yieldNext(), bc.AsBx(bc.LOADI, 0, 1), bc.AsBx(bc.LOADI, 1, 0), bc.ABC(bc.DIVI, 0, 0, 1), ret),
|
|
coFn("S.Nested", 1, yieldNext(), bc.AsBx(bc.LOADI, 0, 20), bc.ABx(bc.CALL, 0, 4), bc.ABx(bc.CALLH, 0, 0), ret),
|
|
fn("double", 1, 1, 1, bc.ABC(bc.ADDI, 0, 0, 0), bc.ABC(bc.RET, 0, 1, 0)),
|
|
}
|
|
return m
|
|
}
|
|
|
|
func coMachine(t *testing.T, budget uint64) (*vm.Machine, *vm.Module, *[]int64) {
|
|
t.Helper()
|
|
hits := &[]int64{}
|
|
m := vm.New(vm.Options{Budget: budget})
|
|
lm, err := m.Load(coModule(), func(bc.HostImport) (vm.HostFunc, bool) {
|
|
return func(_ *vm.Machine, w []vm.Value) error {
|
|
*hits = append(*hits, w[0].Int())
|
|
return nil
|
|
}, true
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return m, lm, hits
|
|
}
|
|
|
|
func self(id int64) []vm.Value {
|
|
return []vm.Value{{}, vm.Int(id)}
|
|
}
|
|
|
|
func TestCoroutineRunsToFirstYield(t *testing.T) {
|
|
m, lm, hits := coMachine(t, 10_000)
|
|
h, fl := m.Start(lm.Func("S.Next"), self(7), nil, 1)
|
|
if fl != nil {
|
|
t.Fatal(fl)
|
|
}
|
|
if len(*hits) != 0 {
|
|
t.Fatal("coroutine ran past its first yield")
|
|
}
|
|
if st, ok := m.CoState(h); !ok || st != vm.CoSuspended {
|
|
t.Fatalf("state %v %v", st, ok)
|
|
}
|
|
done, fl := m.Resume(h, self(7))
|
|
if fl != nil || !done || len(*hits) != 1 || (*hits)[0] != 7 {
|
|
t.Fatalf("resume %v %v %v", done, fl, *hits)
|
|
}
|
|
if _, ok := m.CoState(h); ok {
|
|
t.Fatal("finished coroutine is still live")
|
|
}
|
|
}
|
|
|
|
func TestCoroutineResumeSeesNewSelf(t *testing.T) {
|
|
m, lm, hits := coMachine(t, 10_000)
|
|
h, _ := m.Start(lm.Func("S.Next"), self(1), nil, 1)
|
|
m.Resume(h, self(2))
|
|
if len(*hits) != 1 || (*hits)[0] != 2 {
|
|
t.Fatalf("hits %v", *hits)
|
|
}
|
|
}
|
|
|
|
func TestCoroutineFaultBudgetCancelNested(t *testing.T) {
|
|
m, lm, hits := coMachine(t, 1000)
|
|
h, _ := m.Start(lm.Func("S.Div"), self(1), nil, 1)
|
|
if _, fl := m.Resume(h, self(1)); fl == nil || fl.Code != vm.CodeDivZero {
|
|
t.Fatalf("div %v", fl)
|
|
}
|
|
if _, ok := m.CoState(h); ok {
|
|
t.Fatal("faulted coroutine is still live")
|
|
}
|
|
h, _ = m.Start(lm.Func("S.Spin"), self(1), nil, 1)
|
|
if _, fl := m.Resume(h, self(1)); fl == nil || fl.Code != vm.CodeBudget {
|
|
t.Fatalf("budget %v", fl)
|
|
}
|
|
h, _ = m.Start(lm.Func("S.Next"), self(1), nil, 1)
|
|
m.Cancel(h)
|
|
if done, fl := m.Resume(h, self(1)); !done || fl != nil || len(*hits) != 0 {
|
|
t.Fatal("cancelled coroutine resumed")
|
|
}
|
|
if m.CoRunning(vm.CoValue(h)) {
|
|
t.Fatal("cancelled coroutine reports running")
|
|
}
|
|
h, _ = m.Start(lm.Func("S.Nested"), self(1), nil, 1)
|
|
if _, fl := m.Resume(h, self(1)); fl != nil || len(*hits) != 1 || (*hits)[0] != 40 {
|
|
t.Fatalf("nested %v %v", fl, *hits)
|
|
}
|
|
}
|
|
|
|
type deadScripts struct{}
|
|
|
|
func (deadScripts) Find(vm.Value, string) (vm.Value, error) {
|
|
return vm.Value{}, nil
|
|
}
|
|
|
|
func (deadScripts) Instance(vm.Value, string) ([]vm.Value, vm.InstanceID, bool) {
|
|
return nil, vm.NoInstance, false
|
|
}
|
|
|
|
func refModule() *bc.Module {
|
|
m := bc.NewModule("v")
|
|
m.Scripts = []bc.ScriptDef{{
|
|
FullName: "v.S", Slots: 2,
|
|
Fields: []bc.FieldDef{{Name: "N", Type: "int", Slot: 1, Slots: 1}},
|
|
Ready: 1, FixedUpdate: -1, Update: -1, Reloaded: -1,
|
|
}}
|
|
m.ScriptRefs = []string{"v.S"}
|
|
m.XFields = []bc.XFieldDef{{Ref: 0, Name: "N", Type: "int", Slots: 1}}
|
|
ready := fn("S.Ready", 0, 0, 1, bc.ABC(bc.RET, 0, 0, 0))
|
|
ready.Script = 0
|
|
m.Functions = []bc.Function{
|
|
fn("get", 1, 1, 1, bc.ABC(bc.GETXF, 0, 0, 0), bc.ABC(bc.RET, 0, 1, 0)),
|
|
ready,
|
|
}
|
|
return m
|
|
}
|
|
|
|
func TestScriptRefOnDeadInstance(t *testing.T) {
|
|
m := vm.New(vm.Options{})
|
|
lm, err := m.Load(refModule(), nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := vm.Link([]*vm.Module{lm}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
m.Scripts = deadScripts{}
|
|
if _, fl := m.Call(lm.Func("get"), nil, []vm.Value{{A: 1, B: 1}}); fl == nil || fl.Code != vm.CodeNil {
|
|
t.Fatalf("fault %v", fl)
|
|
}
|
|
}
|
|
|
|
func TestCallRLifecycleIsLinkError(t *testing.T) {
|
|
mod := refModule()
|
|
mod.XMethods = []bc.XMethodDef{{Ref: 0, Name: "Ready"}}
|
|
m := vm.New(vm.Options{})
|
|
lm, err := m.Load(mod, nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := vm.Link([]*vm.Module{lm}); err == nil {
|
|
t.Fatal("CALLR to a lifecycle method linked")
|
|
}
|
|
}
|