@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/artifacts/linux-arm64/barnum +0 -0
- package/artifacts/linux-x64/barnum +0 -0
- package/artifacts/macos-arm64/barnum +0 -0
- package/artifacts/macos-x64/barnum +0 -0
- package/artifacts/win-x64/barnum.exe +0 -0
- package/package.json +2 -1
- package/src/all.ts +116 -26
- package/src/ast.ts +262 -454
- package/src/bind.ts +65 -30
- package/src/builtins.ts +222 -123
- package/src/chain.ts +11 -2
- package/src/effect-id.ts +21 -6
- package/src/handler.ts +75 -32
- package/src/index.ts +11 -3
- package/src/pipe.ts +124 -29
- package/src/race.ts +58 -52
- package/src/run.ts +64 -23
- package/src/schema.ts +118 -0
- package/src/try-catch.ts +29 -30
- package/src/worker.ts +10 -1
package/src/bind.ts
CHANGED
|
@@ -1,6 +1,12 @@
|
|
|
1
|
-
import {
|
|
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 {
|
|
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>(
|
|
20
|
-
|
|
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
|
|
48
|
-
* tuple and
|
|
49
|
-
* handler with `
|
|
50
|
-
* output tuple. The handler
|
|
51
|
-
* `
|
|
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(
|
|
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: "
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
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
|
-
*
|
|
82
|
-
*
|
|
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
|
|
108
|
-
const
|
|
138
|
+
// 1. Gensym one resumeHandlerId per binding.
|
|
139
|
+
const resumeHandlerIds = bindings.map(() => allocateResumeHandlerId());
|
|
109
140
|
|
|
110
|
-
// 2. Create VarRefs (
|
|
111
|
-
const varRefs =
|
|
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: {
|
|
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 =
|
|
161
|
+
for (let i = resumeHandlerIds.length - 1; i >= 0; i--) {
|
|
125
162
|
inner = {
|
|
126
|
-
kind: "
|
|
127
|
-
|
|
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
|
}
|