@barnum/barnum 0.0.0-main-ef6df91f → 0.0.0-main-e8b82cff

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/bind.ts CHANGED
@@ -1,6 +1,12 @@
1
- import { type Action, type ExtractInput, type ExtractOutput, type TypedAction, typedAction } from "./ast.js";
1
+ import {
2
+ type Action,
3
+ type ExtractInput,
4
+ type ExtractOutput,
5
+ type TypedAction,
6
+ typedAction,
7
+ } from "./ast.js";
2
8
  import { identity, drop } from "./builtins.js";
3
- import { allocateEffectId } from "./effect-id.js";
9
+ import { allocateResumeHandlerId, type ResumeHandlerId } from "./effect-id.js";
4
10
  import { pipe } from "./pipe.js";
5
11
 
6
12
  // ---------------------------------------------------------------------------
@@ -16,8 +22,13 @@ import { pipe } from "./pipe.js";
16
22
  */
17
23
  export type VarRef<TValue> = TypedAction<never, TValue>;
18
24
 
19
- function createVarRef<TValue>(effectId: number): VarRef<TValue> {
20
- return typedAction({ kind: "Perform", effect_id: effectId });
25
+ function createVarRef<TValue>(
26
+ resumeHandlerId: ResumeHandlerId,
27
+ ): VarRef<TValue> {
28
+ return typedAction({
29
+ kind: "ResumePerform",
30
+ resume_handler_id: resumeHandlerId,
31
+ });
21
32
  }
22
33
 
23
34
  // ---------------------------------------------------------------------------
@@ -44,23 +55,43 @@ export type InferVarRefs<TBindings extends Action[]> = {
44
55
  // ---------------------------------------------------------------------------
45
56
 
46
57
  /**
47
- * Returns an action that extracts the nth value from the Handle's state
48
- * tuple and resumes with it. When a Perform fires, the engine calls the
49
- * handler with `{ payload, state }`. For bind, `state` is the full All
50
- * output tuple. The handler extracts `state[n]` and wraps it as
51
- * `{ kind: "Resume", value: state[n] }`.
58
+ * Returns an action that extracts the nth value from the ResumeHandle's
59
+ * state tuple and passes state through unchanged. When a ResumePerform
60
+ * fires, the engine calls the handler with `[payload, state]`. For bind,
61
+ * `state` (index 1) is the full All output tuple. The handler produces
62
+ * `[state[n], state]` value is state[n], new_state is state (unchanged).
52
63
  *
53
- * Expanded AST: Chain(ExtractField("state"), Chain(ExtractIndex(n), Tag("Resume")))
64
+ * Expanded AST: All(Chain(ExtractIndex(1), ExtractIndex(n)), ExtractIndex(1))
54
65
  */
55
66
  function readVar(n: number): Action {
56
67
  return {
57
- kind: "Chain",
58
- first: { kind: "Invoke", handler: { kind: "Builtin", builtin: { kind: "ExtractField", value: "state" } } },
59
- rest: {
60
- kind: "Chain",
61
- first: { kind: "Invoke", handler: { kind: "Builtin", builtin: { kind: "ExtractIndex", value: n } } },
62
- rest: { kind: "Invoke", handler: { kind: "Builtin", builtin: { kind: "Tag", value: "Resume" } } },
63
- },
68
+ kind: "All",
69
+ actions: [
70
+ {
71
+ kind: "Chain",
72
+ first: {
73
+ kind: "Invoke",
74
+ handler: {
75
+ kind: "Builtin",
76
+ builtin: { kind: "ExtractIndex", value: 1 },
77
+ },
78
+ },
79
+ rest: {
80
+ kind: "Invoke",
81
+ handler: {
82
+ kind: "Builtin",
83
+ builtin: { kind: "ExtractIndex", value: n },
84
+ },
85
+ },
86
+ },
87
+ {
88
+ kind: "Invoke",
89
+ handler: {
90
+ kind: "Builtin",
91
+ builtin: { kind: "ExtractIndex", value: 1 },
92
+ },
93
+ },
94
+ ],
64
95
  };
65
96
  }
66
97
 
@@ -78,8 +109,8 @@ function readVar(n: number): Action {
78
109
  * Compiles to:
79
110
  * Chain(
80
111
  * All(...bindings, Identity),
81
- * Handle(e0, readVar(0),
82
- * Handle(e1, readVar(1),
112
+ * ResumeHandle(r0, readVar(0),
113
+ * ResumeHandle(r1, readVar(1),
83
114
  * Chain(ExtractIndex(N), body)
84
115
  * )
85
116
  * )
@@ -104,11 +135,11 @@ export function bind<TBindings extends Action[], TOut>(
104
135
  bindings: [...TBindings],
105
136
  body: (vars: InferVarRefs<TBindings>) => BodyResult<TOut>,
106
137
  ): TypedAction<ExtractInput<TBindings[number]>, TOut> {
107
- // 1. Gensym one effectId per binding.
108
- const effectIds = bindings.map(() => allocateEffectId());
138
+ // 1. Gensym one resumeHandlerId per binding.
139
+ const resumeHandlerIds = bindings.map(() => allocateResumeHandlerId());
109
140
 
110
- // 2. Create VarRefs (Perform nodes) for each binding.
111
- const varRefs = effectIds.map((id) => createVarRef(id));
141
+ // 2. Create VarRefs (ResumePerform nodes) for each binding.
142
+ const varRefs = resumeHandlerIds.map((id) => createVarRef(id));
112
143
 
113
144
  // 3. Invoke the body callback with the VarRefs.
114
145
  const bodyAction = body(varRefs as InferVarRefs<TBindings>) as Action;
@@ -118,13 +149,19 @@ export function bind<TBindings extends Action[], TOut>(
118
149
  const pipelineInputIndex = bindings.length;
119
150
  let inner: Action = {
120
151
  kind: "Chain",
121
- first: { kind: "Invoke", handler: { kind: "Builtin", builtin: { kind: "ExtractIndex", value: pipelineInputIndex } } },
152
+ first: {
153
+ kind: "Invoke",
154
+ handler: {
155
+ kind: "Builtin",
156
+ builtin: { kind: "ExtractIndex", value: pipelineInputIndex },
157
+ },
158
+ },
122
159
  rest: bodyAction,
123
160
  };
124
- for (let i = effectIds.length - 1; i >= 0; i--) {
161
+ for (let i = resumeHandlerIds.length - 1; i >= 0; i--) {
125
162
  inner = {
126
- kind: "Handle",
127
- effect_id: effectIds[i],
163
+ kind: "ResumeHandle",
164
+ resume_handler_id: resumeHandlerIds[i],
128
165
  handler: readVar(i),
129
166
  body: inner,
130
167
  };
@@ -156,7 +193,5 @@ export function bind<TBindings extends Action[], TOut>(
156
193
  export function bindInput<TIn, TOut = any>(
157
194
  body: (input: VarRef<TIn>) => BodyResult<TOut>,
158
195
  ): TypedAction<TIn, TOut> {
159
- return bind([identity], ([input]) =>
160
- pipe(drop, body(input)),
161
- );
196
+ return bind([identity], ([input]) => pipe(drop, body(input)));
162
197
  }