Skip to content

feat(craft): event arguments in handlers; state belongs to its template - #56

Open
innkuika wants to merge 1 commit into
craft/fix-rendererfrom
craft/event-args
Open

innkuika wants to merge 1 commit into
craft/fix-rendererfrom
craft/event-args

Conversation

@innkuika

@innkuika innkuika commented Dec 2, 2026

Copy link
Copy Markdown
Contributor

Description

Lets a handler use the arguments of the event that fired it, and makes local state belong to the template that declares it. Stacked on #39 (stack #51).

  • event.<parts> (text format requirement 7): set state.query = event.value, or call args.onPick { id: event.id }. It may appear only inside a handler's payload (a setter's value, the arguments of call or event "…" {…}). Canonical form {"$": "eventRef", …}.
  • Binary: core tag 0x17 with format version 2, written only when a library uses it; other blobs stay version 1. Older readers reject version-2 blobs cleanly (unsupportedVersion).
  • Fire-time payloads (runtime requirement 14): a payload that reads event.* is evaluated when the handler fires. state.* is read as at the start of the firing; arguments, data and the media context are read as at expansion. A failing payload fails only that firing, with a typed firing error. All payloads of one firing share one step budget (limits requirement 9).
  • state belongs to its declaring template (fixes our B17): a template without its own state reads absent and its setters fail with noState; state crosses templates only through arguments. This is a deliberate difference from RFW, which binds to the nearest stateful ancestor.
  • Fixes: typed text can now go into local state (set state.q = event.value), and rapid repeated events on handlers that read event no longer clobber each other. Setters that read only state are still computed at expansion, which is specified and covered by a control case.
  • Tests: new conformance cases in text_format, binary_format, runtime, limits and media_context, with controls; widget tests; unit tests. Passes with dart test -p node with no new skips.

An independent review checked the blueprints against the tests and the code. It found no previously valid source that changes meaning (150k corpus and fuzzed sources), no v2 blob that an old decoder accepts, and no firing that escapes the limits.

Pre-launch Checklist

  • I signed the CLA.
  • I read the Contributors Guide.
  • I updated/added relevant documentation.
  • My code changes (if any) have tests.
  • License checks pass (python3 ./scripts/fix_licenses.py --check).
  • Format checks pass (./scripts/fix_format.sh --check).

@innkuika
innkuika requested a review from ditman December 2, 2026 21:02
@innkuika
innkuika added this pull request to stack #51 December 2, 2026 21:02

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces support for event argument references (event.<parts>) across the Craft engine, allowing event handlers to access event payload data at firing time. The changes span multiple blueprints, adding format version 2 to the binary format (introducing tag 17 for event references), updating the text format parser to recognize event references within handler payloads, and modifying the runtime to defer payload evaluation until firing. Additionally, conformance tests, unit tests, and Flutter integration tests have been added to validate the new functionality. There are no review comments, and I have no additional feedback to provide.

…emplate

Event arguments (`event.<parts>`):
- Text format: `event` followed by `.part` is an event argument reference,
  accepted only in a handler's payload (a setter's value, the arguments of
  `call` and of an event handler); `event "name" {...}` is unchanged.
  Canonical form `{"$": "eventRef", "parts": [...]}`.
- Binary format: core tag 0x17, format version 2. Encoders write version 2
  with minimum reader version 2 only for a library that uses the tag, so
  version 1 decoders reject it with unsupportedVersion; every other blob
  stays version 1. Tag 0x17 is no core tag in version 0/1 blobs.
- Runtime: a payload that reads `event` is left for firing (`{"$": "fireTime"}`
  in the expansion) and evaluated at fire() under the limits, with the
  event's arguments, the state as the firing began, and the arguments, data
  and media of its expansion. Setters can now store event values (B14), and
  handlers of an earlier expansion fired again before the next one add up
  instead of clobbering (B03, for these handlers). Payload failures are
  firing errors of the expansion's kinds; the payloads of one firing share
  one maxSteps budget.

State (B17): `state` is only that of the template whose body reads it. A
template without initial state reads absent and its setters fail with
noState, instead of binding to the nearest stateful caller; state crosses
templates through arguments.

Changed conformance cases:
- craft_runtime a_stateless_template_uses_the_nearest_stateful_one is
  replaced by state_is_only_that_of_the_declaring_template (B17).
- craft_binary_format library_minimum_reader_version_too_high uses minimum
  reader version 3 (2 is now readable); first_future_core_tag_is_rejected
  states that 17 is a future tag only in a version 1 blob.

Proposed in a2ui-project/a2ui#4484 ("Fire-time EventArgReference").
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant