behavior-contracts 0.12.1 → 0.13.0
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/README.md +20 -0
- package/go/api_test.go +675 -0
- package/go/behavior.go +1202 -0
- package/go/cmd/conformance/main.go +40 -0
- package/go/codec.go +170 -0
- package/go/coderuntime/behaviorcore.go +93 -0
- package/go/coderuntime/canonical.go +287 -0
- package/go/coderuntime/exprcore.go +334 -0
- package/go/coderuntime/plan.go +422 -0
- package/go/coderuntime/primitives.go +383 -0
- package/go/coderuntime/rawabi.go +281 -0
- package/go/coderuntime/spec.go +17 -0
- package/go/coderuntime/template.go +139 -0
- package/go/coderuntime/value.go +138 -0
- package/go/coderuntime/version.go +9 -0
- package/go/compat.go +289 -0
- package/go/conformance.go +1046 -0
- package/go/envelope.go +77 -0
- package/go/expr.go +347 -0
- package/go/fingerprint.go +147 -0
- package/go/fingerprint_test.go +94 -0
- package/go/go.mod +3 -0
- package/go/guard.go +538 -0
- package/go/jsonord.go +151 -0
- package/go/plan_parallel_test.go +279 -0
- package/go/primitives_test.go +227 -0
- package/package.json +3 -1
|
@@ -0,0 +1,383 @@
|
|
|
1
|
+
package coderuntime
|
|
2
|
+
|
|
3
|
+
// primitives.go — codegen が直呼びできる安定 primitive 公開面(bc#36 / typed-codegen.md §3.4)。
|
|
4
|
+
//
|
|
5
|
+
// A0 = 公開面の確定(emitter 生成ロジックは A1/#37)。生成コードが「細粒度 primitive を
|
|
6
|
+
// 呼ぶ」ための callable surface を additive に切り出す。
|
|
7
|
+
//
|
|
8
|
+
// 最重要規律(AC 7): 演算子意味論は再実装しない。各 primitive は Expression IR の単一キー
|
|
9
|
+
// ノードを組んで既存 SSoT evaluate(親パッケージ expr.go)へ委譲するだけの薄いラッパ。よって
|
|
10
|
+
// 意味論(INT_OVERFLOW / PRECISION_LOSS / MOD_ZERO / NULL_REF / TYPE_MISMATCH / 短絡未評価 /
|
|
11
|
+
// code-point 順 等)は evaluate と完全一致する。
|
|
12
|
+
//
|
|
13
|
+
// 短絡の保存: And/Or/Cond/Coalesce は未評価の IR ノード(JNode)を引数に取り、内部で evaluate
|
|
14
|
+
// が短絡する(値を先に渡す API は採らない)。
|
|
15
|
+
//
|
|
16
|
+
// 加えて RunPlan / bounded 並行 / skip・unproduced / hydration helper を再エクスポートまたは
|
|
17
|
+
// 薄く公開する(いずれも既存実装を SSoT として共有)。
|
|
18
|
+
|
|
19
|
+
import (
|
|
20
|
+
"fmt"
|
|
21
|
+
"sync"
|
|
22
|
+
)
|
|
23
|
+
|
|
24
|
+
// ── (a) 演算子/構築/リテラルの native callable surface(bc no-ir invariant)──────
|
|
25
|
+
//
|
|
26
|
+
// codegen は IR を出力しない。かつてここには「単一キー IR ノードを組んで EvaluateExpression に
|
|
27
|
+
// 委譲する」薄いラッパ(PrimRef/PrimObj/PrimAdd/…/EvalExpr)が並んでいたが、それは生成コードに
|
|
28
|
+
// インタプリタを再導入する松葉杖だった。撤去済み。native primitive は被演算子を **ExprFn(scope を
|
|
29
|
+
// 受けて評価済み Value を返す thunk)** で受け取り、意味論 SSoT の value-core(exprcore.go の
|
|
30
|
+
// ArithValues/CompareValues/… — evaluate の各アームと同一実装)へ委譲する。短絡(and/or/cond/coalesce)
|
|
31
|
+
// は thunk を条件付きで呼ぶ(採用側のみ評価)。生 IR の実行が要る経路(ir exec surface)は
|
|
32
|
+
// EvaluateExpression を直接使う。ref/refOpt/concat は生成 module 内の native helper(slRef/slConcat)。
|
|
33
|
+
|
|
34
|
+
// ExprFn は codegen が native primitive へ渡す被演算子 thunk(scope→評価済み Value)。
|
|
35
|
+
type ExprFn = func(*Obj) (Value, error)
|
|
36
|
+
|
|
37
|
+
func eval2(a, b ExprFn, scope *Obj) (Value, Value, error) {
|
|
38
|
+
av, err := a(scope)
|
|
39
|
+
if err != nil {
|
|
40
|
+
return nil, nil, err
|
|
41
|
+
}
|
|
42
|
+
bv, err := b(scope)
|
|
43
|
+
if err != nil {
|
|
44
|
+
return nil, nil, err
|
|
45
|
+
}
|
|
46
|
+
return av, bv, nil
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// AddNative/SubNative/MulNative は算術(int×int checked / float×float finite / 混在 TYPE_MISMATCH)。
|
|
50
|
+
func AddNative(a, b ExprFn, scope *Obj) (Value, error) {
|
|
51
|
+
av, bv, err := eval2(a, b, scope)
|
|
52
|
+
if err != nil {
|
|
53
|
+
return nil, err
|
|
54
|
+
}
|
|
55
|
+
return ArithValues("add", av, bv)
|
|
56
|
+
}
|
|
57
|
+
func SubNative(a, b ExprFn, scope *Obj) (Value, error) {
|
|
58
|
+
av, bv, err := eval2(a, b, scope)
|
|
59
|
+
if err != nil {
|
|
60
|
+
return nil, err
|
|
61
|
+
}
|
|
62
|
+
return ArithValues("sub", av, bv)
|
|
63
|
+
}
|
|
64
|
+
func MulNative(a, b ExprFn, scope *Obj) (Value, error) {
|
|
65
|
+
av, bv, err := eval2(a, b, scope)
|
|
66
|
+
if err != nil {
|
|
67
|
+
return nil, err
|
|
68
|
+
}
|
|
69
|
+
return ArithValues("mul", av, bv)
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// NegNative は単項マイナス。
|
|
73
|
+
func NegNative(a ExprFn, scope *Obj) (Value, error) {
|
|
74
|
+
av, err := a(scope)
|
|
75
|
+
if err != nil {
|
|
76
|
+
return nil, err
|
|
77
|
+
}
|
|
78
|
+
return NegValue(av)
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// DivNative は float 除算。
|
|
82
|
+
func DivNative(a, b ExprFn, scope *Obj) (Value, error) {
|
|
83
|
+
av, bv, err := eval2(a, b, scope)
|
|
84
|
+
if err != nil {
|
|
85
|
+
return nil, err
|
|
86
|
+
}
|
|
87
|
+
return DivValues(av, bv)
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// ModNative は truncated 剰余。
|
|
91
|
+
func ModNative(a, b ExprFn, scope *Obj) (Value, error) {
|
|
92
|
+
av, bv, err := eval2(a, b, scope)
|
|
93
|
+
if err != nil {
|
|
94
|
+
return nil, err
|
|
95
|
+
}
|
|
96
|
+
return ModValues(av, bv)
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// EqNative/NeNative は等価/非等価。
|
|
100
|
+
func EqNative(a, b ExprFn, scope *Obj) (Value, error) {
|
|
101
|
+
av, bv, err := eval2(a, b, scope)
|
|
102
|
+
if err != nil {
|
|
103
|
+
return nil, err
|
|
104
|
+
}
|
|
105
|
+
return EqNeValues("eq", av, bv)
|
|
106
|
+
}
|
|
107
|
+
func NeNative(a, b ExprFn, scope *Obj) (Value, error) {
|
|
108
|
+
av, bv, err := eval2(a, b, scope)
|
|
109
|
+
if err != nil {
|
|
110
|
+
return nil, err
|
|
111
|
+
}
|
|
112
|
+
return EqNeValues("ne", av, bv)
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// LtNative/LeNative/GtNative/GeNative は比較(同一型 int/float/string、string は code-point 順)。
|
|
116
|
+
func LtNative(a, b ExprFn, scope *Obj) (Value, error) {
|
|
117
|
+
av, bv, err := eval2(a, b, scope)
|
|
118
|
+
if err != nil {
|
|
119
|
+
return nil, err
|
|
120
|
+
}
|
|
121
|
+
return CompareValues("lt", av, bv)
|
|
122
|
+
}
|
|
123
|
+
func LeNative(a, b ExprFn, scope *Obj) (Value, error) {
|
|
124
|
+
av, bv, err := eval2(a, b, scope)
|
|
125
|
+
if err != nil {
|
|
126
|
+
return nil, err
|
|
127
|
+
}
|
|
128
|
+
return CompareValues("le", av, bv)
|
|
129
|
+
}
|
|
130
|
+
func GtNative(a, b ExprFn, scope *Obj) (Value, error) {
|
|
131
|
+
av, bv, err := eval2(a, b, scope)
|
|
132
|
+
if err != nil {
|
|
133
|
+
return nil, err
|
|
134
|
+
}
|
|
135
|
+
return CompareValues("gt", av, bv)
|
|
136
|
+
}
|
|
137
|
+
func GeNative(a, b ExprFn, scope *Obj) (Value, error) {
|
|
138
|
+
av, bv, err := eval2(a, b, scope)
|
|
139
|
+
if err != nil {
|
|
140
|
+
return nil, err
|
|
141
|
+
}
|
|
142
|
+
return CompareValues("ge", av, bv)
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
// NotNative は論理否定(strict bool)。
|
|
146
|
+
func NotNative(a ExprFn, scope *Obj) (Value, error) {
|
|
147
|
+
av, err := a(scope)
|
|
148
|
+
if err != nil {
|
|
149
|
+
return nil, err
|
|
150
|
+
}
|
|
151
|
+
b, err := RequireBool(av, "not")
|
|
152
|
+
if err != nil {
|
|
153
|
+
return nil, err
|
|
154
|
+
}
|
|
155
|
+
return !b, nil
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
// AndNative は論理積(短絡: 左辺 false なら右辺は評価しない)。
|
|
159
|
+
func AndNative(a, b ExprFn, scope *Obj) (Value, error) {
|
|
160
|
+
av, err := a(scope)
|
|
161
|
+
if err != nil {
|
|
162
|
+
return nil, err
|
|
163
|
+
}
|
|
164
|
+
ab, err := RequireBool(av, "and")
|
|
165
|
+
if err != nil {
|
|
166
|
+
return nil, err
|
|
167
|
+
}
|
|
168
|
+
if !ab {
|
|
169
|
+
return false, nil
|
|
170
|
+
}
|
|
171
|
+
bv, err := b(scope)
|
|
172
|
+
if err != nil {
|
|
173
|
+
return nil, err
|
|
174
|
+
}
|
|
175
|
+
rb, err := RequireBool(bv, "and")
|
|
176
|
+
if err != nil {
|
|
177
|
+
return nil, err
|
|
178
|
+
}
|
|
179
|
+
return rb, nil
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
// OrNative は論理和(短絡: 左辺 true なら右辺は評価しない)。
|
|
183
|
+
func OrNative(a, b ExprFn, scope *Obj) (Value, error) {
|
|
184
|
+
av, err := a(scope)
|
|
185
|
+
if err != nil {
|
|
186
|
+
return nil, err
|
|
187
|
+
}
|
|
188
|
+
ab, err := RequireBool(av, "or")
|
|
189
|
+
if err != nil {
|
|
190
|
+
return nil, err
|
|
191
|
+
}
|
|
192
|
+
if ab {
|
|
193
|
+
return true, nil
|
|
194
|
+
}
|
|
195
|
+
bv, err := b(scope)
|
|
196
|
+
if err != nil {
|
|
197
|
+
return nil, err
|
|
198
|
+
}
|
|
199
|
+
rb, err := RequireBool(bv, "or")
|
|
200
|
+
if err != nil {
|
|
201
|
+
return nil, err
|
|
202
|
+
}
|
|
203
|
+
return rb, nil
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
// CoalesceNative は null 合体(左が null のときだけ右を評価)。
|
|
207
|
+
func CoalesceNative(a, b ExprFn, scope *Obj) (Value, error) {
|
|
208
|
+
av, err := a(scope)
|
|
209
|
+
if err != nil {
|
|
210
|
+
return nil, err
|
|
211
|
+
}
|
|
212
|
+
if av == nil {
|
|
213
|
+
return b(scope)
|
|
214
|
+
}
|
|
215
|
+
return av, nil
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
// CondNative は条件(strict bool。採用側のみ評価)。
|
|
219
|
+
func CondNative(c, then, els ExprFn, scope *Obj) (Value, error) {
|
|
220
|
+
cv, err := c(scope)
|
|
221
|
+
if err != nil {
|
|
222
|
+
return nil, err
|
|
223
|
+
}
|
|
224
|
+
cb, err := RequireBool(cv, "cond")
|
|
225
|
+
if err != nil {
|
|
226
|
+
return nil, err
|
|
227
|
+
}
|
|
228
|
+
if cb {
|
|
229
|
+
return then(scope)
|
|
230
|
+
}
|
|
231
|
+
return els(scope)
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
// LenNative は配列長。
|
|
235
|
+
func LenNative(a ExprFn, scope *Obj) (Value, error) {
|
|
236
|
+
av, err := a(scope)
|
|
237
|
+
if err != nil {
|
|
238
|
+
return nil, err
|
|
239
|
+
}
|
|
240
|
+
return LenValue(av)
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
// ArrNative は array 構築(要素 thunk を順に評価)。
|
|
244
|
+
func ArrNative(elems []ExprFn, scope *Obj) (Value, error) {
|
|
245
|
+
out := make([]Value, 0, len(elems))
|
|
246
|
+
for _, e := range elems {
|
|
247
|
+
v, err := e(scope)
|
|
248
|
+
if err != nil {
|
|
249
|
+
return nil, err
|
|
250
|
+
}
|
|
251
|
+
out = append(out, v)
|
|
252
|
+
}
|
|
253
|
+
return out, nil
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
// ObjNative は object 構築。キーは静的・順序保存。__proto__ は値評価前に FORBIDDEN_KEY(evaluate と同順)。
|
|
257
|
+
func ObjNative(keys []string, vals []ExprFn, scope *Obj) (Value, error) {
|
|
258
|
+
out := NewObj()
|
|
259
|
+
for i, k := range keys {
|
|
260
|
+
if k == ForbiddenObjectKey {
|
|
261
|
+
return nil, exprFail("FORBIDDEN_KEY", fmt.Sprintf("obj key %q is forbidden (fail-closed)", ForbiddenObjectKey))
|
|
262
|
+
}
|
|
263
|
+
v, err := vals[i](scope)
|
|
264
|
+
if err != nil {
|
|
265
|
+
return nil, err
|
|
266
|
+
}
|
|
267
|
+
out.Set(k, v)
|
|
268
|
+
}
|
|
269
|
+
return out, nil
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
// IntLitNative は {int:"…"} リテラル。
|
|
273
|
+
func IntLitNative(literal string) (Value, error) {
|
|
274
|
+
return IntLitValue(literal)
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
// FloatLitNative は {float:n} リテラル(finite 検査)。
|
|
278
|
+
func FloatLitNative(n float64) (Value, error) {
|
|
279
|
+
return CheckFinite(n)
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
// NumberLitNative は bare number リテラル(§2.3 分類)。
|
|
283
|
+
func NumberLitNative(n float64) (Value, error) {
|
|
284
|
+
return numberLitValue(n)
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
// ── (b) 直線 emit(A2/#38)が必要とする plan-driving / fail-closed 公開面 ──────────
|
|
288
|
+
// A1/A2 の直線生成コードは RunBehavior の tree-walk を経由せず自前の exec クロージャで
|
|
289
|
+
// plan を駆動するため、RunBehavior が内部で使う「handler ディスパッチ・coded failure 生成・
|
|
290
|
+
// exec-internal failure の逐次優先復元・type ラベル」を callable surface として additive に
|
|
291
|
+
// 公開する。いずれも既存の SSoT(behaviorcore.go / exprcore.go / value.go)へ委譲する薄いラッパで、
|
|
292
|
+
// 意味論の再実装はしない(TS runtime が BehaviorFailure/runPlan/unproducedValue を公開して
|
|
293
|
+
// straight-line 生成コードに使わせるのと鏡映)。
|
|
294
|
+
|
|
295
|
+
// NewBehaviorFailure は RunBehavior と同一の coded failure(UNKNOWN_COMPONENT /
|
|
296
|
+
// UNKNOWN_NODE_KIND / UNKNOWN_ENTRY 等)を直線生成コードから raise するための公開コンストラクタ。
|
|
297
|
+
func NewBehaviorFailure(code, msg string) error { return behaviorFail(code, msg) }
|
|
298
|
+
|
|
299
|
+
// NewExprFailure は expr.go と同一の coded failure(UNKNOWN_BINDING / NULL_REF / MISSING_PROP /
|
|
300
|
+
// TYPE_MISMATCH / INVALID_NODE 等)を、native 化した ref/refOpt/concat helper から raise する
|
|
301
|
+
// ための公開コンストラクタ(意味論の code は evaluate SSoT と一致)。
|
|
302
|
+
func NewExprFailure(code, msg string) error { return exprFail(code, msg) }
|
|
303
|
+
|
|
304
|
+
// NewPlanFailure は plan.go と同一の coded failure(OP_FAILED / UNKNOWN_POLICY 等)を、逐次直線
|
|
305
|
+
// 生成コード(bc#75: RunPlan を経由しない strictly-sequential plan)が policy 解釈から直接 raise
|
|
306
|
+
// するための公開コンストラクタ。code/message は interpretOutcome(plan.go)と byte 一致。
|
|
307
|
+
func NewPlanFailure(code, msg string) error { return planFail(code, msg) }
|
|
308
|
+
|
|
309
|
+
// TypeName は runtime Value の規範型ラベル(null/bool/int/float/string/arr/obj)を返す。
|
|
310
|
+
// native 化した helper が expr.go と同一の TYPE_MISMATCH メッセージを綴じるのに使う。
|
|
311
|
+
func TypeName(v Value) string { return typeName(v) }
|
|
312
|
+
|
|
313
|
+
// ExecHandler は境界注入 handler へのディスパッチ(ComponentCtxExec があれば ExecCtx、
|
|
314
|
+
// なければ Exec)。直線生成コードの componentRef arm が RunBehavior と同一規律で handler を
|
|
315
|
+
// 呼ぶための公開面。
|
|
316
|
+
func ExecHandler(handlers ComponentExec, nodeID, component string, ports *Obj, bound Value) (ExecOutcome, bool) {
|
|
317
|
+
return execHandler(handlers, nodeID, component, ports, bound)
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
// FirstExecErrInVisitOrder は exec クロージャ内で起きた失敗(BehaviorFailure/ExprFailure)の
|
|
321
|
+
// 逐次優先を復元する(bc#23)。RunPlan の合成 OP_FAILED unwind より、plan visit 順で最初に
|
|
322
|
+
// 記録された exec-internal error を優先する — 直線生成コードが RunBehavior と同一の失敗同一性を
|
|
323
|
+
// 持つために必須(RunBehavior が内部で使うのと同一ロジックへ委譲)。
|
|
324
|
+
func FirstExecErrInVisitOrder(execErrs []error, plan *ExecutionPlanSpec, n int, planErr error) error {
|
|
325
|
+
return firstExecErrInVisitOrder(execErrs, plan, n, planErr)
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
// ── (c) bounded 並行プリミティブ(順序保存)─────────────────────────────────────
|
|
329
|
+
|
|
330
|
+
// MapWithConcurrency は入力順を保存し in-flight を concurrency で上限する bounded 並行 map。
|
|
331
|
+
//
|
|
332
|
+
// execution-plan.md §4 の determinism 規律(output[i]=worker(items[i]) / 完了順非依存 /
|
|
333
|
+
// bounded dispatch)を単体 primitive として公開する。RunPlan の並列経路と同一機構
|
|
334
|
+
// (goroutine worker pool + 固定 index スロット書き込み)。
|
|
335
|
+
//
|
|
336
|
+
// worker は並行に呼ばれ得る(concurrency>1 のとき)。結果は index 固定スロットへ書くため
|
|
337
|
+
// 完了順に依存しない(逐次 map と同一の結果列)。
|
|
338
|
+
func MapWithConcurrency[T any, U any](items []T, concurrency int64, worker func(item T, index int) U) []U {
|
|
339
|
+
n := len(items)
|
|
340
|
+
out := make([]U, n)
|
|
341
|
+
limit := int(concurrency)
|
|
342
|
+
if limit < 1 {
|
|
343
|
+
limit = 1
|
|
344
|
+
}
|
|
345
|
+
if limit <= 1 || n <= 1 {
|
|
346
|
+
for i, it := range items {
|
|
347
|
+
out[i] = worker(it, i)
|
|
348
|
+
}
|
|
349
|
+
return out
|
|
350
|
+
}
|
|
351
|
+
workers := limit
|
|
352
|
+
if workers > n {
|
|
353
|
+
workers = n
|
|
354
|
+
}
|
|
355
|
+
jobs := make(chan int)
|
|
356
|
+
var wg sync.WaitGroup
|
|
357
|
+
for w := 0; w < workers; w++ {
|
|
358
|
+
wg.Add(1)
|
|
359
|
+
go func() {
|
|
360
|
+
defer wg.Done()
|
|
361
|
+
for i := range jobs {
|
|
362
|
+
out[i] = worker(items[i], i) // 完了順非依存: disjoint index スロット
|
|
363
|
+
}
|
|
364
|
+
}()
|
|
365
|
+
}
|
|
366
|
+
for i := 0; i < n; i++ {
|
|
367
|
+
jobs <- i
|
|
368
|
+
}
|
|
369
|
+
close(jobs)
|
|
370
|
+
wg.Wait()
|
|
371
|
+
return out
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
// ── (d) skip / unproduced-value helper ────────────────────────────────────────
|
|
375
|
+
|
|
376
|
+
// UnproducedValue は未生成 Port(Skip)の表現値。single→nil / connection→空 connection。
|
|
377
|
+
// 判定 SSoT は plan.go の unproducedValue(唯一の producing site)。ここは公開面として委譲する。
|
|
378
|
+
func UnproducedValue(kind string) Value { return unproducedValue(kind) }
|
|
379
|
+
|
|
380
|
+
// SkipConnection は connection relation の未生成 = {items:[],cursor:nil}(unproducedValue("connection") の明示版)。
|
|
381
|
+
func SkipConnection() Value { return unproducedValue("connection") }
|
|
382
|
+
|
|
383
|
+
// ── (e) hydration / relation helper ───────────────────────────────────────────
|
|
@@ -0,0 +1,281 @@
|
|
|
1
|
+
// rawabi.go — the RAW handler ABI (bc#76): a de-boxed handler boundary that lets a
|
|
2
|
+
// generated typed module materialize a struct DIRECTLY from the wire payload, with no
|
|
3
|
+
// intermediate dynamic Value/*Obj tree on the row data plane.
|
|
4
|
+
//
|
|
5
|
+
// # Why this exists (the boxing #75 left)
|
|
6
|
+
//
|
|
7
|
+
// The typed codegen (#47/#48) materialized structs from an ALREADY-BOXED Value:
|
|
8
|
+
//
|
|
9
|
+
// func marshal_T0(raw Value) (T0, error) // raw is a *Obj — a dynamic Value tree
|
|
10
|
+
//
|
|
11
|
+
// The consumer's read handler first built the full dynamic Value tree (in graphddb:
|
|
12
|
+
// AttributeValue -> OMap -> hydrate -> behaviorFromNative -> Value), and the generated
|
|
13
|
+
// module then WALKED that Value into a struct. That ADDS a Value->struct conversion on
|
|
14
|
+
// top of the boxing instead of REPLACING it. The residual ~22% Rust gap #75 measured is
|
|
15
|
+
// exactly that Value/*Obj deep-clone at the handler ABI boundary.
|
|
16
|
+
//
|
|
17
|
+
// bc's conformance handlers return a Value mock, so the suite structurally could not see
|
|
18
|
+
// what the consumer's real handler did BEFORE the Value reached the module — the same
|
|
19
|
+
// blind spot that let bc#47's B2-go layering recur one layer out as bc#76.
|
|
20
|
+
//
|
|
21
|
+
// # The seam
|
|
22
|
+
//
|
|
23
|
+
// A RAW handler yields a RawValue — an opaque, native-backed accessor tree that is NOT a
|
|
24
|
+
// Value/*Obj. The generated raw marshaller reads it field-by-field (RawRow.Field, native
|
|
25
|
+
// scalar type-switch, []RawValue) straight into the concrete struct. The dynamic Value
|
|
26
|
+
// tree never exists on the row data plane; boxing is REPLACED, not layered.
|
|
27
|
+
//
|
|
28
|
+
// v1-native (hand-written struct-from-row) reads the wire payload directly into a struct.
|
|
29
|
+
// The RAW ABI reproduces that shape mechanically: RawRow.Field is the monomorphized wire
|
|
30
|
+
// read, and the generated marshaller is the fixed wiring into the struct.
|
|
31
|
+
package coderuntime
|
|
32
|
+
|
|
33
|
+
// RawValue is a handler result on the RAW data plane. It is deliberately NOT a Value:
|
|
34
|
+
// the whole point of #76 is that the dynamic Value/*Obj tree is never built here. The
|
|
35
|
+
// concrete dynamic types a raw marshaller accepts are:
|
|
36
|
+
//
|
|
37
|
+
// nil / bool / int64 / float64 / string -> native scalars (read directly)
|
|
38
|
+
// RawRow -> a native-backed object accessor (NOT *Obj)
|
|
39
|
+
// []RawValue -> a native-backed array (NOT []Value)
|
|
40
|
+
//
|
|
41
|
+
// A consumer's real handler returns its native wire shape wrapped as a RawRow whose Field
|
|
42
|
+
// reads the native storage (e.g. a DynamoDB AttributeValue map) with no Value allocation.
|
|
43
|
+
type RawValue interface{}
|
|
44
|
+
|
|
45
|
+
// RawRow is the object accessor on the raw data plane: a direct native field read that
|
|
46
|
+
// materializes NO *Obj. Consumers implement it over their wire payload (e.g. an
|
|
47
|
+
// AttributeValue map); bc conformance uses RawObj (a native map-backed RawRow) so the
|
|
48
|
+
// generated raw marshaller exercises the de-boxed path without a real wire format.
|
|
49
|
+
type RawRow interface {
|
|
50
|
+
// Field returns the RawValue for key and whether it is present. The returned RawValue
|
|
51
|
+
// is a native scalar, a nested RawRow, or a []RawValue — never a *Obj/Value tree.
|
|
52
|
+
Field(key string) (RawValue, bool)
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// RawOutcome is a RAW handler execution result. Ok carries a RawValue (native-backed),
|
|
56
|
+
// NOT a boxed Value — that is the whole de-box.
|
|
57
|
+
type RawOutcome struct {
|
|
58
|
+
IsError bool
|
|
59
|
+
Ok RawValue
|
|
60
|
+
Err string
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// OkRaw / ErrRaw are convenience constructors mirroring OkOutcome / ErrOutcome.
|
|
64
|
+
func OkRaw(v RawValue) RawOutcome { return RawOutcome{Ok: v} }
|
|
65
|
+
func ErrRaw(e string) RawOutcome { return RawOutcome{IsError: true, Err: e} }
|
|
66
|
+
|
|
67
|
+
// RawComponentExec is the RAW handler registry seam: like ComponentExec, but the handler
|
|
68
|
+
// returns a RawValue (native-backed) instead of a boxed Value. A generated raw-ABI module
|
|
69
|
+
// dispatches through this and materializes structs directly from the RawValue — no dynamic
|
|
70
|
+
// Value tree on the row data plane.
|
|
71
|
+
type RawComponentExec interface {
|
|
72
|
+
ExecRaw(component string, ports *Obj, bound Value) (RawOutcome, bool)
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// ── Native PORTS ABI (bc 0.5.0) ────────────────────────────────────────────────────────
|
|
76
|
+
//
|
|
77
|
+
// # Why this exists (the generic key-value plumbing the RESULT-side de-box left)
|
|
78
|
+
//
|
|
79
|
+
// The RESULT side (RawComponentExec above) de-boxed the handler RESULT (row data plane).
|
|
80
|
+
// But the PORTS side of EVERY generated componentRef still built a generic key-value
|
|
81
|
+
// container: `NewObj()` (a map[string]Value alloc) + `.Set("PK", val)` per port (a boxed
|
|
82
|
+
// Value insert), then handed that *Obj to a generic dispatch (`ExecRaw`/`ExecHandler`).
|
|
83
|
+
// Measured (go1.26): NewObj()+3×Set == ~326 ns/op + 6 allocs/op — which is essentially the
|
|
84
|
+
// ENTIRE cost of a single-op straight-line component (~335 ns/op). Building the equivalent
|
|
85
|
+
// native struct by field assignment is 0.7 ns/op + 0 allocs. That generic key-value port
|
|
86
|
+
// container is the residual "遅い処理" that made single-op codegen tie/lose the interpreter.
|
|
87
|
+
//
|
|
88
|
+
// # The seam
|
|
89
|
+
//
|
|
90
|
+
// A generated module now builds, per componentRef node, a NATIVE ports struct (typed fields,
|
|
91
|
+
// direct assignment, zero map/boxing) and dispatches it through this native seam. The struct
|
|
92
|
+
// value is passed as a NativePorts (an `any` carrying the concrete struct); a consumer's real
|
|
93
|
+
// handler type-switches to its own generated struct type (or reads via the PortReader accessor
|
|
94
|
+
// below when it wants a uniform read). NO map[string]Value is allocated on the port plane.
|
|
95
|
+
//
|
|
96
|
+
// The generic ComponentExec(ports *Obj) / RawComponentExec(ports *Obj) paths remain as
|
|
97
|
+
// fallbacks for consumers that have not adopted the native ABI — this is purely ADDITIVE.
|
|
98
|
+
//
|
|
99
|
+
// # Fail-closed semantics unchanged
|
|
100
|
+
//
|
|
101
|
+
// Only the port CONTAINER + dispatch go native. Operator overflow / short-circuit / type
|
|
102
|
+
// errors still route through the A0 primitives; a port that genuinely can fail keeps its
|
|
103
|
+
// error path (returned directly, before the struct is built — see the generated code).
|
|
104
|
+
|
|
105
|
+
// NativePorts carries a generated per-node native ports struct across the handler ABI with
|
|
106
|
+
// NO map[string]Value container. It is deliberately `any`: the concrete type is the module-
|
|
107
|
+
// private ports struct (e.g. `portsGetItem{Table, PK, SK string}`), built by direct field
|
|
108
|
+
// assignment. A consumer's handler type-switches to its own type, or reads via PortReader.
|
|
109
|
+
type NativePorts = any
|
|
110
|
+
|
|
111
|
+
// PortReader is the OPTIONAL uniform read accessor a generated native ports struct implements
|
|
112
|
+
// so a consumer that does not want to type-switch can still read ports by name. Reading a
|
|
113
|
+
// statically-typed field this way boxes ONLY that one field into a Value on demand (no map,
|
|
114
|
+
// no eager boxing of every port) — a consumer on the pure hot path type-switches instead and
|
|
115
|
+
// reads the typed fields directly with zero boxing (that is what bc's micro-bench stub does).
|
|
116
|
+
type PortReader interface {
|
|
117
|
+
// Port returns the Value for a port name and whether it is a declared port of this node.
|
|
118
|
+
Port(name string) (Value, bool)
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
// NativeComponentExec is the NATIVE-PORTS handler registry seam: the handler receives the
|
|
122
|
+
// generated native ports struct (via NativePorts) and returns an ExecOutcome (a boxed Value
|
|
123
|
+
// RESULT — the UNTYPED straight-line path has no target struct to de-box the result into, and
|
|
124
|
+
// returns the boxed Value as the component output, exactly as the generic path does). NO
|
|
125
|
+
// generic *Obj port CONTAINER is built on the port plane — that is the de-plumb. The RESULT-
|
|
126
|
+
// side de-box (RawComponentExec, a native RawValue materialized via a marshaller) is a SEPARATE
|
|
127
|
+
// concern that only applies to the TYPED path; the two compose (a typed native-ports emitter
|
|
128
|
+
// would return a RawOutcome). Here (untyped) only the port container + dispatch go native.
|
|
129
|
+
type NativeComponentExec interface {
|
|
130
|
+
ExecNative(component string, ports NativePorts, bound Value) (ExecOutcome, bool)
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// NativeComponentCtxExec is the ctx-carrying extension (mirrors ComponentCtxExec).
|
|
134
|
+
type NativeComponentCtxExec interface {
|
|
135
|
+
ExecNativeCtx(nodeID, component string, ports NativePorts, bound Value) (ExecOutcome, bool)
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
// ExecNativeHandler dispatches to ExecNativeCtx when available, else ExecNative. Public so the
|
|
139
|
+
// generated native-ports module can call the handler with the same resolution discipline as
|
|
140
|
+
// ExecHandler/ExecRawHandler.
|
|
141
|
+
func ExecNativeHandler(handlers NativeComponentExec, nodeID, component string, ports NativePorts, bound Value) (ExecOutcome, bool) {
|
|
142
|
+
if hc, ok := handlers.(NativeComponentCtxExec); ok {
|
|
143
|
+
return hc.ExecNativeCtx(nodeID, component, ports, bound)
|
|
144
|
+
}
|
|
145
|
+
return handlers.ExecNative(component, ports, bound)
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
// ── Combined NATIVE-ports + RAW-result ABI (bc#77, the 1.0 read de-box) ─────────────────────
|
|
149
|
+
//
|
|
150
|
+
// # Why this exists (each existing seam does HALF)
|
|
151
|
+
//
|
|
152
|
+
// NativeComponentExec (above) de-plumbed the PORT container (native ports struct in) but still
|
|
153
|
+
// returns a boxed Value RESULT (ExecOutcome). RawComponentExec de-boxed the RESULT (RawValue
|
|
154
|
+
// out) but keeps the generic *Obj PORT container in AND is driven by RunPlan — whose relation-
|
|
155
|
+
// child skip-gate (plan.go preflightOp) reads the PARENT's result *Obj; a struct-native (nil
|
|
156
|
+
// Value) parent makes it skip relation children, so typed relation-child / nestedBatchGet
|
|
157
|
+
// DIVERGE from run_behavior (graphddb#323 / bc#74).
|
|
158
|
+
//
|
|
159
|
+
// This seam COMBINES both halves: the handler receives the generated per-node NATIVE ports
|
|
160
|
+
// struct (via NativePorts — no *Obj container) AND returns a native RawValue (RawOutcome — no
|
|
161
|
+
// boxed Value result tree). The generated combined read runner drives the nodes INLINE
|
|
162
|
+
// (sequential, no RunPlan): each node's result is materialized DIRECTLY into its outType struct
|
|
163
|
+
// via marshal_raw_T*, node results live as struct locals, and a relation child reads the
|
|
164
|
+
// parent's REAL struct result inline — so the child-present decision and the parent-field port
|
|
165
|
+
// ref are made from the real parent value (never a nil Value), and relation-child / connection
|
|
166
|
+
// reads converge with run_behavior. Zero boxed Value end-to-end on both the port AND row plane.
|
|
167
|
+
//
|
|
168
|
+
// NativeRawComponentExec is the combined handler registry seam (bc#94, concretized on the covered
|
|
169
|
+
// path): native ports in, RawValue out. The single method ALWAYS carries nodeID (the separate ctx
|
|
170
|
+
// interface + its runtime resolution type-assert are gone — the covered read always knows the node).
|
|
171
|
+
//
|
|
172
|
+
// # bc#94 covered-path concreteness (and the honest Go residual)
|
|
173
|
+
//
|
|
174
|
+
// The generated covered runner is GENERIC over the concrete handler type: `func
|
|
175
|
+
// RunNativeRawStruct_X[H NativeRawComponentExec](handlers H, …)`. Go monomorphizes the
|
|
176
|
+
// `handlers.ExecNativeRaw(…)` call on the type parameter `H` to a DIRECT (devirtualized) call — the
|
|
177
|
+
// handler is NOT boxed into an `interface{}` and dispatched dynamically (there is no
|
|
178
|
+
// `ExecNativeRawHandler(iface, …)` interface-dispatch shim on the covered path any more).
|
|
179
|
+
//
|
|
180
|
+
// What Go CANNOT concretize (reported honestly, not faked): a single Go interface method cannot be
|
|
181
|
+
// generic over the per-node ports struct type, so `ports` still crosses as `NativePorts` (`any`);
|
|
182
|
+
// and `RawValue` is `interface{}` (the runtime's dynamic wire value), so the handler's result is
|
|
183
|
+
// read back with a `.(T)` at the marshaller boundary. Rust removes both via monomorphization +
|
|
184
|
+
// `match`; Go's type system does not admit the equivalent without abandoning the single-registry
|
|
185
|
+
// consumer contract (a distinct concrete handler type per component). This is the documented Go
|
|
186
|
+
// boundary of the seam — the DISPATCH is devirtualized; the ports/result CROSSING stays `any`.
|
|
187
|
+
type NativeRawComponentExec interface {
|
|
188
|
+
ExecNativeRaw(nodeID, component string, ports NativePorts, bound Value) (RawOutcome, bool)
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
// RawComponentCtxExec is the ctx-carrying extension (mirrors ComponentCtxExec).
|
|
192
|
+
type RawComponentCtxExec interface {
|
|
193
|
+
ExecRawCtx(nodeID, component string, ports *Obj, bound Value) (RawOutcome, bool)
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
// ExecRawHandler dispatches to ExecRawCtx when available, else ExecRaw. Public so the
|
|
197
|
+
// generated raw-ABI typed module can call the handler with the same resolution discipline
|
|
198
|
+
// as the boxed ExecHandler.
|
|
199
|
+
func ExecRawHandler(handlers RawComponentExec, nodeID, component string, ports *Obj, bound Value) (RawOutcome, bool) {
|
|
200
|
+
if hc, ok := handlers.(RawComponentCtxExec); ok {
|
|
201
|
+
return hc.ExecRawCtx(nodeID, component, ports, bound)
|
|
202
|
+
}
|
|
203
|
+
return handlers.ExecRaw(component, ports, bound)
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
// RawFieldType returns the normative type label of a RawValue (for TYPE_MISMATCH messages
|
|
207
|
+
// symmetric with TypeName on Value). RawRow -> "obj", []RawValue -> "arr".
|
|
208
|
+
func RawFieldType(v RawValue) string {
|
|
209
|
+
switch v.(type) {
|
|
210
|
+
case nil:
|
|
211
|
+
return "null"
|
|
212
|
+
case bool:
|
|
213
|
+
return "bool"
|
|
214
|
+
case int64:
|
|
215
|
+
return "int"
|
|
216
|
+
case float64:
|
|
217
|
+
return "float"
|
|
218
|
+
case string:
|
|
219
|
+
return "string"
|
|
220
|
+
case []RawValue:
|
|
221
|
+
return "arr"
|
|
222
|
+
case RawRow:
|
|
223
|
+
return "obj"
|
|
224
|
+
default:
|
|
225
|
+
return "unknown"
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
// RawObj is bc's native map-backed RawRow used by the conformance harness (no real wire
|
|
230
|
+
// format). Storage is native scalars / nested RawObj / []RawValue — it is NOT a Value/*Obj,
|
|
231
|
+
// so a raw marshaller that reads it exercises the de-boxed data plane. Key order is
|
|
232
|
+
// preserved so nested serialization (equivalence pin) can reconstruct canonical Values.
|
|
233
|
+
type RawObj struct {
|
|
234
|
+
Keys []string
|
|
235
|
+
Vals map[string]RawValue
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
// NewRawObj returns an empty native-backed RawRow.
|
|
239
|
+
func NewRawObj() *RawObj {
|
|
240
|
+
return &RawObj{Keys: nil, Vals: map[string]RawValue{}}
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
// Set inserts or updates a key, preserving first-insertion order.
|
|
244
|
+
func (o *RawObj) Set(k string, v RawValue) {
|
|
245
|
+
if _, ok := o.Vals[k]; !ok {
|
|
246
|
+
o.Keys = append(o.Keys, k)
|
|
247
|
+
}
|
|
248
|
+
o.Vals[k] = v
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
// Field implements RawRow (direct native read; no *Obj materialized).
|
|
252
|
+
func (o *RawObj) Field(key string) (RawValue, bool) {
|
|
253
|
+
v, ok := o.Vals[key]
|
|
254
|
+
return v, ok
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
// RawFromValue lowers a dynamic Value into the equivalent native-backed RawValue. This is
|
|
258
|
+
// the ADAPTER bc's conformance uses to feed the RAW path from the SAME vectors the boxed
|
|
259
|
+
// path uses (so behavior-equality is checked against run_behavior on identical data). It is
|
|
260
|
+
// a TEST/BENCH-ADAPTER that intentionally lives OFF the hot path: a real consumer never
|
|
261
|
+
// builds a Value first — it produces RawValue from its wire payload directly. The de-box
|
|
262
|
+
// win is measured against v1-native precisely because production skips this step.
|
|
263
|
+
func RawFromValue(v Value) RawValue {
|
|
264
|
+
switch t := v.(type) {
|
|
265
|
+
case *Obj:
|
|
266
|
+
ro := NewRawObj()
|
|
267
|
+
for _, k := range t.Keys {
|
|
268
|
+
ro.Set(k, RawFromValue(t.Vals[k]))
|
|
269
|
+
}
|
|
270
|
+
return ro
|
|
271
|
+
case []Value:
|
|
272
|
+
out := make([]RawValue, 0, len(t))
|
|
273
|
+
for _, e := range t {
|
|
274
|
+
out = append(out, RawFromValue(e))
|
|
275
|
+
}
|
|
276
|
+
return out
|
|
277
|
+
default:
|
|
278
|
+
// native scalar (nil/bool/int64/float64/string) passes through unchanged.
|
|
279
|
+
return v
|
|
280
|
+
}
|
|
281
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
package coderuntime
|
|
2
|
+
|
|
3
|
+
// SpecVersions are the supported IR/vector spec versions (distinct from the
|
|
4
|
+
// module's release version). Out-of-range versions are fail-closed.
|
|
5
|
+
var SpecVersions = map[string]int64{
|
|
6
|
+
"expression": 2, // v2: obj __proto__ own key -> FORBIDDEN_KEY (fail-closed)
|
|
7
|
+
"template": 1,
|
|
8
|
+
"plan": 1,
|
|
9
|
+
"canonical": 2, // v2: obj value __proto__ own key -> FORBIDDEN_KEY (fail-closed)
|
|
10
|
+
"behavior": 6, // v6: de-box is TOTAL against the declared type — a key the wire carries that the declared outType does not name is DROPPED from the node result (previously the interpreters kept it while typed-native could not, so the same IR and the same data returned different key sets per language family). v5 carried two changes: (a) a map node's Element Error Policy Kind (error|skip) + the structured detail a Failure carries (scp-error.md); (b) an OMITTED input key for a port DECLARED optional binds to null.
|
|
11
|
+
"guard": 4, // The opaque-wire contract now decides identically in all five languages (#205): the type notation accepts `value` (bc#156) and the map compound, a node's wirePassthrough and outType are equivalent, and a component outputType may not carry `value` (§D4b). (v3 was the map elementPolicy closed set + skip-on-batched reject; v2 was AssertCompiled + UNTYPED_NODE + operator-type-signature SSoT.)
|
|
12
|
+
"c2": 1, // v1 (bc#28: c2-catalog-swap — catalog-swap execution + IR structural identity)
|
|
13
|
+
"provenance": 1, // v1 (#128/A6): serialization-boundary canonical fingerprint match (5-language loud reject of tampered/stale) + non-vacuity (CONFORMANCE_MUTATE)
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
// EnvelopeSpecVersion is the default graphddb-shaped supported envelope version.
|
|
17
|
+
const EnvelopeSpecVersion = "1.1"
|