diff --git a/.planning/phases/03-data-completion/03-EVENT-CONTRACT.md b/.planning/phases/03-data-completion/03-EVENT-CONTRACT.md index c13dee3..95991e3 100644 --- a/.planning/phases/03-data-completion/03-EVENT-CONTRACT.md +++ b/.planning/phases/03-data-completion/03-EVENT-CONTRACT.md @@ -118,14 +118,13 @@ interface IInsertEventsEventParam { ### `eventInsertEvent` ```ts -interface IInsertEventEventParam { - readonly id: string; -} +type IInsertEventEventParam = Statement[]; ``` -这是 `eventInsertEvents` 的单 id 形式:只临时执行 `id` 对应事件,复用当前 `env`, -不写入事件存储。空 id、缺失事件、缺失事件存储或缺失执行器时安全返回 `void`, -执行 Promise 必须等待。 +接收一段 `Statement[]` 事件语句,直接使用现有 AnonTokyo 解释器执行该语句体, +参数为 `{ custom: {} }`,环境为当前 `env`。不读取事件存储、不解析事件 id, +也不写入事件存储。空语句体、缺失执行器或达到嵌套插入深度上限时安全返回 `void`, +解释器 Promise 必须等待。 ## Registration diff --git a/packages-user/data-state/src/event/event.test.ts b/packages-user/data-state/src/event/event.test.ts index dba3eee..fbc9f64 100644 --- a/packages-user/data-state/src/event/event.test.ts +++ b/packages-user/data-state/src/event/event.test.ts @@ -1,4 +1,5 @@ import { describe, expect, it } from 'vitest'; +import { Statement, StatementType } from 'anon-tokyo'; import { FaceDirection, IGameEvent, @@ -28,11 +29,9 @@ type RegisteredBuiltin = ReturnType< typeof createEventBuiltinRegistrations >[number]; -type BuiltinParameter = Parameters[0]; - -function invokeBuiltin( +function invokeBuiltin( registration: RegisteredBuiltin, - param: BuiltinParameter, + param: TParam, env: IBlockEventEnv ): Promise { return Promise.resolve( @@ -199,8 +198,8 @@ describe('event built-ins', () => { expect(fixture.state.hero.location.x).toBe(0); }); - // 验证真实注册项按顺序等待临时事件序列和单事件 - it('awaits temporary event sequences and single event insertion', async () => { + // 验证真实注册项按顺序等待临时事件序列并直接执行语句体 + it('awaits id sequences and executes a direct statement body', async () => { const fixture = createFixture(); const calls: string[] = []; fixture.state.eventStore.addEvent( @@ -221,12 +220,23 @@ describe('event built-ins', () => { { ids: ['first', 'second', 'missing'] }, fixture.env ); + const body: Statement[] = [ + { + type: StatementType.Call, + functionName: EventBuiltinName.SetBlock, + builtIn: true, + async: true, + parameters: { x: 3, y: 0, tile: 2 } + } + ]; await invokeBuiltin( getRegistration(EventBuiltinName.InsertEvent), - { id: 'first' }, + body, fixture.env ); - expect(calls).toEqual(['first', 'second', 'first']); + expect(calls).toEqual(['first', 'second']); + expect(fixture.layer.getBlock(3, 0)).toBe(2); + expect(fixture.state.eventStore.getEvent('inline-body')).toBeNull(); }); // 验证默认注册项只包含批准的八个稳定名称 @@ -288,7 +298,7 @@ describe('event built-ins', () => { await expect( invokeBuiltin( getRegistration(EventBuiltinName.InsertEvent), - { id: 'missing' }, + [], fixture.env ) ).resolves.toBeUndefined();