behavior-contracts 0.11.23 → 0.12.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/cli-contract.yaml +27 -20
- package/dist/cli.js +3 -3
- package/dist/cli.js.map +1 -1
- package/dist/generated/contract.d.ts.map +1 -1
- package/dist/generated/contract.js +2 -2
- package/dist/generated/contract.js.map +1 -1
- package/dist/generated/program.js +4 -4
- package/dist/generated/program.js.map +1 -1
- package/dist/generator/core.d.ts +13 -10
- package/dist/generator/core.d.ts.map +1 -1
- package/dist/generator/core.js.map +1 -1
- package/dist/generator/emit-native-dynamic.d.ts +203 -0
- package/dist/generator/emit-native-dynamic.d.ts.map +1 -0
- package/dist/generator/emit-native-dynamic.js +733 -0
- package/dist/generator/emit-native-dynamic.js.map +1 -0
- package/dist/generator/emit-native-php.d.ts +4 -0
- package/dist/generator/emit-native-php.d.ts.map +1 -0
- package/dist/generator/emit-native-php.js +668 -0
- package/dist/generator/emit-native-php.js.map +1 -0
- package/dist/generator/emit-native-python.d.ts +4 -0
- package/dist/generator/emit-native-python.d.ts.map +1 -0
- package/dist/generator/emit-native-python.js +592 -0
- package/dist/generator/emit-native-python.js.map +1 -0
- package/dist/generator/emit-php.d.ts +4 -0
- package/dist/generator/emit-php.d.ts.map +1 -1
- package/dist/generator/emit-php.js +6 -2
- package/dist/generator/emit-php.js.map +1 -1
- package/dist/generator/emit-python.d.ts +4 -0
- package/dist/generator/emit-python.d.ts.map +1 -1
- package/dist/generator/emit-python.js +2 -2
- package/dist/generator/emit-python.js.map +1 -1
- package/dist/generator/emit-typed-native-go.d.ts.map +1 -1
- package/dist/generator/emit-typed-native-go.js +31 -542
- package/dist/generator/emit-typed-native-go.js.map +1 -1
- package/dist/generator/emit-typed-native-rust.d.ts.map +1 -1
- package/dist/generator/emit-typed-native-rust.js +33 -461
- package/dist/generator/emit-typed-native-rust.js.map +1 -1
- package/dist/generator/index.d.ts +2 -0
- package/dist/generator/index.d.ts.map +1 -1
- package/dist/generator/index.js +9 -1
- package/dist/generator/index.js.map +1 -1
- package/dist/generator/native-coverage.d.ts +162 -0
- package/dist/generator/native-coverage.d.ts.map +1 -0
- package/dist/generator/native-coverage.js +412 -0
- package/dist/generator/native-coverage.js.map +1 -0
- package/dist/generator/native-expr.d.ts +1 -1
- package/dist/generator/native-expr.d.ts.map +1 -1
- package/dist/generator/typed.d.ts +6 -0
- package/dist/generator/typed.d.ts.map +1 -1
- package/dist/generator/typed.js +17 -0
- package/dist/generator/typed.js.map +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,668 @@
|
|
|
1
|
+
import { GeneratorFailure } from "./core.js";
|
|
2
|
+
import { emitDynamicNative, sanitize } from "./emit-native-dynamic.js";
|
|
3
|
+
import { phpMethodName, phpVarName } from "./emit-php.js";
|
|
4
|
+
import { findDecl } from "./typed.js";
|
|
5
|
+
const ERR = "\\BehaviorContractsNative\\BehaviorError";
|
|
6
|
+
/** a php string literal (double-quoted, every special byte escaped — never a raw newline in the source). */
|
|
7
|
+
function phpStr(s) {
|
|
8
|
+
let out = '"';
|
|
9
|
+
for (const ch of s) {
|
|
10
|
+
const c = ch.codePointAt(0);
|
|
11
|
+
if (ch === "\\")
|
|
12
|
+
out += "\\\\";
|
|
13
|
+
else if (ch === '"')
|
|
14
|
+
out += '\\"';
|
|
15
|
+
else if (ch === "$")
|
|
16
|
+
out += "\\$";
|
|
17
|
+
else if (ch === "\n")
|
|
18
|
+
out += "\\n";
|
|
19
|
+
else if (ch === "\r")
|
|
20
|
+
out += "\\r";
|
|
21
|
+
else if (ch === "\t")
|
|
22
|
+
out += "\\t";
|
|
23
|
+
else if (c < 0x20 || c === 0x7f)
|
|
24
|
+
out += `\\x${c.toString(16).padStart(2, "0")}`;
|
|
25
|
+
else
|
|
26
|
+
out += ch;
|
|
27
|
+
}
|
|
28
|
+
return out + '"';
|
|
29
|
+
}
|
|
30
|
+
/** a php float literal: always a float (`2` → `2.0`), negative zero kept. */
|
|
31
|
+
function phpFloat(n) {
|
|
32
|
+
if (Object.is(n, -0))
|
|
33
|
+
return "-0.0";
|
|
34
|
+
const t = String(n);
|
|
35
|
+
return /[.eE]/.test(t) ? t : `${t}.0`;
|
|
36
|
+
}
|
|
37
|
+
/** a php int literal (i64 min has no literal form: `-9223372036854775808` parses as a float). */
|
|
38
|
+
function phpInt(dec) {
|
|
39
|
+
return dec === "-9223372036854775808" ? "\\PHP_INT_MIN" : dec;
|
|
40
|
+
}
|
|
41
|
+
/** a property access on a `\stdClass` (`->k`, or `->{"k"}` for a key that is not an identifier). */
|
|
42
|
+
function prop(base, key) {
|
|
43
|
+
return /^[A-Za-z_][A-Za-z0-9_]*$/.test(key) ? `${base}->${key}` : `${base}->{${phpStr(key)}}`;
|
|
44
|
+
}
|
|
45
|
+
function objLit(entries) {
|
|
46
|
+
return entries.length === 0 ? "new \\stdClass()" : `(object) [${entries.map((e) => `${phpStr(e.key)} => ${e.expr}`).join(", ")}]`;
|
|
47
|
+
}
|
|
48
|
+
function assocLit(entries) {
|
|
49
|
+
return `[${entries.map((e) => `${phpStr(e.key)} => ${e.expr}`).join(", ")}]`;
|
|
50
|
+
}
|
|
51
|
+
function phpParam(port) {
|
|
52
|
+
const n = phpVarName(port);
|
|
53
|
+
return n === "this" ? "this_" : n;
|
|
54
|
+
}
|
|
55
|
+
/** indent every line of every statement string one level (php guard blocks). */
|
|
56
|
+
function indentLines(stmts) {
|
|
57
|
+
return stmts.flatMap((s) => s.split("\n").map((l) => ` ${l}`));
|
|
58
|
+
}
|
|
59
|
+
// ── the helper library (private static methods; each emitted only when used) ─────────────
|
|
60
|
+
const HELPERS = {
|
|
61
|
+
opFailed: {
|
|
62
|
+
deps: [],
|
|
63
|
+
code: `/** OP_FAILED: the leaf's failure under the node's 'fail' / 'retry' Kind (its message and structured detail ride through). */
|
|
64
|
+
private static function opFailed(string $node, string $policy, \\Throwable $e): ${ERR}
|
|
65
|
+
{
|
|
66
|
+
$where = "operation '" . $node . "' failed under '" . $policy . "' policy" . ($policy === 'retry' ? ' (exhausted)' : '');
|
|
67
|
+
$detail = \\property_exists($e, 'detail') && \\is_array($e->detail) ? $e->detail : null;
|
|
68
|
+
return new ${ERR}('OP_FAILED', $where . ': ' . $e->getMessage(), $detail, $e);
|
|
69
|
+
}`,
|
|
70
|
+
},
|
|
71
|
+
wireType: {
|
|
72
|
+
deps: [],
|
|
73
|
+
code: `private static function wireType(mixed $v): string
|
|
74
|
+
{
|
|
75
|
+
if ($v === null) {
|
|
76
|
+
return 'null';
|
|
77
|
+
}
|
|
78
|
+
if (\\is_bool($v)) {
|
|
79
|
+
return 'bool';
|
|
80
|
+
}
|
|
81
|
+
if (\\is_int($v)) {
|
|
82
|
+
return 'int';
|
|
83
|
+
}
|
|
84
|
+
if (\\is_float($v)) {
|
|
85
|
+
return 'float';
|
|
86
|
+
}
|
|
87
|
+
if (\\is_string($v)) {
|
|
88
|
+
return 'string';
|
|
89
|
+
}
|
|
90
|
+
if (\\is_array($v)) {
|
|
91
|
+
return 'arr';
|
|
92
|
+
}
|
|
93
|
+
return 'obj';
|
|
94
|
+
}`,
|
|
95
|
+
},
|
|
96
|
+
rawValue: {
|
|
97
|
+
deps: [],
|
|
98
|
+
code: `private static function rawValue(mixed $v): string
|
|
99
|
+
{
|
|
100
|
+
if (\\is_string($v)) {
|
|
101
|
+
return $v;
|
|
102
|
+
}
|
|
103
|
+
if (\\is_bool($v)) {
|
|
104
|
+
return $v ? 'true' : 'false';
|
|
105
|
+
}
|
|
106
|
+
if ($v === null) {
|
|
107
|
+
return 'null';
|
|
108
|
+
}
|
|
109
|
+
if (\\is_int($v)) {
|
|
110
|
+
return (string) $v;
|
|
111
|
+
}
|
|
112
|
+
return (string) \\json_encode($v, \\JSON_UNESCAPED_UNICODE | \\JSON_UNESCAPED_SLASHES | \\JSON_PRESERVE_ZERO_FRACTION | \\JSON_PARTIAL_OUTPUT_ON_ERROR);
|
|
113
|
+
}`,
|
|
114
|
+
},
|
|
115
|
+
mismatch: {
|
|
116
|
+
deps: ["wireType", "rawValue"],
|
|
117
|
+
code: `private static function mismatch(string $model, string $field, string $expected, mixed $v): ${ERR}
|
|
118
|
+
{
|
|
119
|
+
$wire = self::wireType($v);
|
|
120
|
+
return new ${ERR}(
|
|
121
|
+
'TYPE_MISMATCH',
|
|
122
|
+
"node '" . $model . "': " . $field . ': expected ' . $expected . ', got ' . $wire,
|
|
123
|
+
['kind' => 'typeMismatch', 'model' => $model, 'field' => $field, 'expectedType' => $expected, 'actualWireType' => $wire, 'rawValue' => self::rawValue($v)],
|
|
124
|
+
);
|
|
125
|
+
}`,
|
|
126
|
+
},
|
|
127
|
+
missing: {
|
|
128
|
+
deps: [],
|
|
129
|
+
code: `private static function missing(string $model, string $field, string $expected): ${ERR}
|
|
130
|
+
{
|
|
131
|
+
return new ${ERR}(
|
|
132
|
+
'MISSING_PROP',
|
|
133
|
+
"node '" . $model . "': " . $field . ': required field is absent',
|
|
134
|
+
['kind' => 'missingField', 'model' => $model, 'field' => $field, 'expectedType' => $expected],
|
|
135
|
+
);
|
|
136
|
+
}`,
|
|
137
|
+
},
|
|
138
|
+
deString: {
|
|
139
|
+
deps: ["mismatch"],
|
|
140
|
+
code: `private static function deString(mixed $v, string $model, string $field, string $expected): string
|
|
141
|
+
{
|
|
142
|
+
if (\\is_string($v)) {
|
|
143
|
+
return $v;
|
|
144
|
+
}
|
|
145
|
+
throw self::mismatch($model, $field, $expected, $v);
|
|
146
|
+
}`,
|
|
147
|
+
},
|
|
148
|
+
deInt: {
|
|
149
|
+
deps: ["mismatch"],
|
|
150
|
+
code: `private static function deInt(mixed $v, string $model, string $field, string $expected): int
|
|
151
|
+
{
|
|
152
|
+
// an integral float within i64 is the same value spelled differently (#261).
|
|
153
|
+
if (\\is_int($v)) {
|
|
154
|
+
return $v;
|
|
155
|
+
}
|
|
156
|
+
if (\\is_float($v) && $v === \\floor($v) && $v >= -9223372036854775808.0 && $v < 9223372036854775808.0) {
|
|
157
|
+
return (int) $v;
|
|
158
|
+
}
|
|
159
|
+
throw self::mismatch($model, $field, $expected, $v);
|
|
160
|
+
}`,
|
|
161
|
+
},
|
|
162
|
+
deFloat: {
|
|
163
|
+
deps: ["mismatch"],
|
|
164
|
+
code: `private static function deFloat(mixed $v, string $model, string $field, string $expected): float
|
|
165
|
+
{
|
|
166
|
+
if (\\is_float($v)) {
|
|
167
|
+
return $v;
|
|
168
|
+
}
|
|
169
|
+
if (\\is_int($v)) {
|
|
170
|
+
return (float) $v;
|
|
171
|
+
}
|
|
172
|
+
throw self::mismatch($model, $field, $expected, $v);
|
|
173
|
+
}`,
|
|
174
|
+
},
|
|
175
|
+
deBool: {
|
|
176
|
+
deps: ["mismatch"],
|
|
177
|
+
code: `private static function deBool(mixed $v, string $model, string $field, string $expected): bool
|
|
178
|
+
{
|
|
179
|
+
if (\\is_bool($v)) {
|
|
180
|
+
return $v;
|
|
181
|
+
}
|
|
182
|
+
throw self::mismatch($model, $field, $expected, $v);
|
|
183
|
+
}`,
|
|
184
|
+
},
|
|
185
|
+
i64: {
|
|
186
|
+
deps: [],
|
|
187
|
+
code: `/** php widens an int that leaves i64 to a float — that widening IS the overflow. */
|
|
188
|
+
private static function i64(mixed $r): int
|
|
189
|
+
{
|
|
190
|
+
if (!\\is_int($r)) {
|
|
191
|
+
throw new ${ERR}('INT_OVERFLOW', 'i64 overflow');
|
|
192
|
+
}
|
|
193
|
+
return $r;
|
|
194
|
+
}`,
|
|
195
|
+
},
|
|
196
|
+
finite: {
|
|
197
|
+
deps: [],
|
|
198
|
+
code: `private static function finite(float $v): float
|
|
199
|
+
{
|
|
200
|
+
if (\\is_nan($v) || \\is_infinite($v)) {
|
|
201
|
+
throw new ${ERR}('NAN_OR_INF', 'non-finite float');
|
|
202
|
+
}
|
|
203
|
+
return $v;
|
|
204
|
+
}`,
|
|
205
|
+
},
|
|
206
|
+
widen: {
|
|
207
|
+
deps: [],
|
|
208
|
+
code: `private static function widen(int $v): float
|
|
209
|
+
{
|
|
210
|
+
if ($v > 9007199254740992 || $v < -9007199254740992) {
|
|
211
|
+
throw new ${ERR}('PRECISION_LOSS', 'int ' . $v . ' exceeds exact float range (±2^53)');
|
|
212
|
+
}
|
|
213
|
+
return (float) $v;
|
|
214
|
+
}`,
|
|
215
|
+
},
|
|
216
|
+
divF: {
|
|
217
|
+
deps: ["finite"],
|
|
218
|
+
code: `private static function divF(float $a, float $b): float
|
|
219
|
+
{
|
|
220
|
+
if ($b === 0.0) {
|
|
221
|
+
throw new ${ERR}('NAN_OR_INF', 'division produced a non-finite float (div by zero)');
|
|
222
|
+
}
|
|
223
|
+
return self::finite($a / $b);
|
|
224
|
+
}`,
|
|
225
|
+
},
|
|
226
|
+
modI64: {
|
|
227
|
+
deps: [],
|
|
228
|
+
code: `/** the truncated-division remainder (the sign follows the dividend) — php's own \`%\`. */
|
|
229
|
+
private static function modI64(int $a, int $b): int
|
|
230
|
+
{
|
|
231
|
+
if ($b === 0) {
|
|
232
|
+
throw new ${ERR}('MOD_ZERO', 'int mod by zero');
|
|
233
|
+
}
|
|
234
|
+
return $a % $b;
|
|
235
|
+
}`,
|
|
236
|
+
},
|
|
237
|
+
modF: {
|
|
238
|
+
deps: ["finite"],
|
|
239
|
+
code: `private static function modF(float $a, float $b): float
|
|
240
|
+
{
|
|
241
|
+
return self::finite(\\fmod($a, $b));
|
|
242
|
+
}`,
|
|
243
|
+
},
|
|
244
|
+
};
|
|
245
|
+
/** the logical helper names of the expression compiler → the emitted php call. */
|
|
246
|
+
function helperCall(used, helper, args) {
|
|
247
|
+
const [a, b] = args;
|
|
248
|
+
const use = (name, call) => {
|
|
249
|
+
used.add(name);
|
|
250
|
+
return call;
|
|
251
|
+
};
|
|
252
|
+
switch (helper) {
|
|
253
|
+
case "addI64":
|
|
254
|
+
return use("i64", `self::i64(${a} + ${b})`);
|
|
255
|
+
case "subI64":
|
|
256
|
+
return use("i64", `self::i64(${a} - ${b})`);
|
|
257
|
+
case "mulI64":
|
|
258
|
+
return use("i64", `self::i64(${a} * ${b})`);
|
|
259
|
+
case "negI64":
|
|
260
|
+
return use("i64", `self::i64(-(${a}))`);
|
|
261
|
+
case "addF":
|
|
262
|
+
return use("finite", `self::finite(${a} + ${b})`);
|
|
263
|
+
case "subF":
|
|
264
|
+
return use("finite", `self::finite(${a} - ${b})`);
|
|
265
|
+
case "mulF":
|
|
266
|
+
return use("finite", `self::finite(${a} * ${b})`);
|
|
267
|
+
case "negF":
|
|
268
|
+
return use("finite", `self::finite(-(${a}))`);
|
|
269
|
+
case "widenI64F":
|
|
270
|
+
return use("widen", `self::widen(${a})`);
|
|
271
|
+
case "divF":
|
|
272
|
+
return use("divF", `self::divF(${a}, ${b})`);
|
|
273
|
+
case "modI64":
|
|
274
|
+
return use("modI64", `self::modI64(${a}, ${b})`);
|
|
275
|
+
case "modF":
|
|
276
|
+
return use("modF", `self::modF(${a}, ${b})`);
|
|
277
|
+
case "lenArr":
|
|
278
|
+
return `\\count(${a})`;
|
|
279
|
+
// the code-point order of UTF-8 strings is their byte order (strcmp — no numeric-string juggling).
|
|
280
|
+
case "strLt":
|
|
281
|
+
return `(\\strcmp(${a}, ${b}) < 0)`;
|
|
282
|
+
case "strLe":
|
|
283
|
+
return `(\\strcmp(${a}, ${b}) <= 0)`;
|
|
284
|
+
case "strGt":
|
|
285
|
+
return `(\\strcmp(${a}, ${b}) > 0)`;
|
|
286
|
+
case "strGe":
|
|
287
|
+
return `(\\strcmp(${a}, ${b}) >= 0)`;
|
|
288
|
+
}
|
|
289
|
+
throw new GeneratorFailure("UNSUPPORTED_NODE_STRAIGHTLINE", `php native expr: no helper '${helper}'`);
|
|
290
|
+
}
|
|
291
|
+
class PhpBackend {
|
|
292
|
+
lang = "php";
|
|
293
|
+
surface = "php native";
|
|
294
|
+
threads = false;
|
|
295
|
+
none = "null";
|
|
296
|
+
emptySet = "[]";
|
|
297
|
+
cont = "continue;";
|
|
298
|
+
brk = "break;";
|
|
299
|
+
used = new Set();
|
|
300
|
+
leafClass;
|
|
301
|
+
constructor(ctx) {
|
|
302
|
+
this.leafClass = ctx.leafTransport?.import;
|
|
303
|
+
}
|
|
304
|
+
local(name) {
|
|
305
|
+
return `$${name}`;
|
|
306
|
+
}
|
|
307
|
+
entryName(comp) {
|
|
308
|
+
return phpMethodName(comp.name);
|
|
309
|
+
}
|
|
310
|
+
paramRef(port) {
|
|
311
|
+
return `$${phpParam(port)}`;
|
|
312
|
+
}
|
|
313
|
+
paramDecl(port, omittable) {
|
|
314
|
+
return omittable ? `mixed $${phpParam(port)} = null` : `mixed $${phpParam(port)}`;
|
|
315
|
+
}
|
|
316
|
+
defaultLeafSymbol(component) {
|
|
317
|
+
return `leaf_${sanitize(component)}`;
|
|
318
|
+
}
|
|
319
|
+
str = phpStr;
|
|
320
|
+
bool(b) {
|
|
321
|
+
return b ? "true" : "false";
|
|
322
|
+
}
|
|
323
|
+
intLit(dec) {
|
|
324
|
+
return phpInt(dec);
|
|
325
|
+
}
|
|
326
|
+
floatLit(n) {
|
|
327
|
+
return phpFloat(n);
|
|
328
|
+
}
|
|
329
|
+
recordLit = objLit;
|
|
330
|
+
mapLit = objLit;
|
|
331
|
+
batchItem = objLit;
|
|
332
|
+
payload = assocLit;
|
|
333
|
+
listLit(items) {
|
|
334
|
+
return `[${items.join(", ")}]`;
|
|
335
|
+
}
|
|
336
|
+
batchPayload(itemsExpr) {
|
|
337
|
+
return `['items' => ${itemsExpr}]`;
|
|
338
|
+
}
|
|
339
|
+
get(base, key) {
|
|
340
|
+
return prop(base, key);
|
|
341
|
+
}
|
|
342
|
+
has(base, key) {
|
|
343
|
+
return `\\property_exists(${base}, ${phpStr(key)})`;
|
|
344
|
+
}
|
|
345
|
+
getOrNone(base, key) {
|
|
346
|
+
return `(${prop(base, key)} ?? null)`;
|
|
347
|
+
}
|
|
348
|
+
isNone(e) {
|
|
349
|
+
return `(${e} === null)`;
|
|
350
|
+
}
|
|
351
|
+
isNotNone(e) {
|
|
352
|
+
return `(${e} !== null)`;
|
|
353
|
+
}
|
|
354
|
+
isRecord(e) {
|
|
355
|
+
return `(${e} instanceof \\stdClass)`;
|
|
356
|
+
}
|
|
357
|
+
isList(e) {
|
|
358
|
+
return `(\\is_array(${e}) && \\array_is_list(${e}))`;
|
|
359
|
+
}
|
|
360
|
+
isString(e) {
|
|
361
|
+
return `\\is_string(${e})`;
|
|
362
|
+
}
|
|
363
|
+
len(e) {
|
|
364
|
+
return `\\count(${e})`;
|
|
365
|
+
}
|
|
366
|
+
concatStr(parts) {
|
|
367
|
+
return `(${parts.join(" . ")})`;
|
|
368
|
+
}
|
|
369
|
+
messageWithInt(prefix, intExpr, suffix) {
|
|
370
|
+
return `(${phpStr(prefix)} . ${intExpr} . ${phpStr(suffix)})`;
|
|
371
|
+
}
|
|
372
|
+
not(e) {
|
|
373
|
+
return `(!${e})`;
|
|
374
|
+
}
|
|
375
|
+
and(a, b) {
|
|
376
|
+
return `(${a} && ${b})`;
|
|
377
|
+
}
|
|
378
|
+
lt(a, b) {
|
|
379
|
+
return `(${a} < ${b})`;
|
|
380
|
+
}
|
|
381
|
+
eq(a, b) {
|
|
382
|
+
return `(${a} === ${b})`;
|
|
383
|
+
}
|
|
384
|
+
index(list, i) {
|
|
385
|
+
return `${list}[${i}]`;
|
|
386
|
+
}
|
|
387
|
+
ternary(cond, t, e) {
|
|
388
|
+
return `(${cond} ? ${t} : ${e})`;
|
|
389
|
+
}
|
|
390
|
+
setHas(set, key) {
|
|
391
|
+
return `isset(${set}[${key}])`;
|
|
392
|
+
}
|
|
393
|
+
setAdd(set, key) {
|
|
394
|
+
return `${set}[${key}] = true;`;
|
|
395
|
+
}
|
|
396
|
+
zero(ref, plan) {
|
|
397
|
+
switch (ref.kind) {
|
|
398
|
+
case "scalar":
|
|
399
|
+
return ref.scalar === "string" ? `''` : ref.scalar === "int" ? "0" : ref.scalar === "float" ? "0.0" : ref.scalar === "bool" ? "false" : "null";
|
|
400
|
+
case "value":
|
|
401
|
+
case "opt":
|
|
402
|
+
return "null";
|
|
403
|
+
case "arr":
|
|
404
|
+
return "[]";
|
|
405
|
+
case "map":
|
|
406
|
+
return "new \\stdClass()";
|
|
407
|
+
case "named":
|
|
408
|
+
return objLit(findDecl(plan, ref.name).fields.map((f) => ({ key: f.name, expr: this.zero(f.type, plan) })));
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
assign(to, e) {
|
|
412
|
+
return `${to} = ${e};`;
|
|
413
|
+
}
|
|
414
|
+
append(list, e) {
|
|
415
|
+
return `${list}[] = ${e};`;
|
|
416
|
+
}
|
|
417
|
+
setField(rec, key, e) {
|
|
418
|
+
return `${prop(rec, key)} = ${e};`;
|
|
419
|
+
}
|
|
420
|
+
setKey(map, keyExpr, e) {
|
|
421
|
+
return `${map}->{${keyExpr}} = ${e};`;
|
|
422
|
+
}
|
|
423
|
+
copyWith(to, rec, key, e) {
|
|
424
|
+
return [`${to} = clone ${rec};`, `${prop(to, key)} = ${e};`];
|
|
425
|
+
}
|
|
426
|
+
copyWithout(to, rec, key) {
|
|
427
|
+
return [`${to} = clone ${rec};`, `unset(${prop(to, key)});`];
|
|
428
|
+
}
|
|
429
|
+
forEntries(k, v, m, body) {
|
|
430
|
+
return { forEach: { v: `${k} => ${v}`, over: `\\get_object_vars(${m})` }, body };
|
|
431
|
+
}
|
|
432
|
+
ret(e) {
|
|
433
|
+
return `return ${e};`;
|
|
434
|
+
}
|
|
435
|
+
raise(e, _cause) {
|
|
436
|
+
// the cause is chained by the error's constructor (opFailed passes it as `previous`).
|
|
437
|
+
return `throw ${e};`;
|
|
438
|
+
}
|
|
439
|
+
error(code, messageExpr) {
|
|
440
|
+
return `new ${ERR}(${phpStr(code)}, ${messageExpr})`;
|
|
441
|
+
}
|
|
442
|
+
opFailed(nodeId, policy, errLocal) {
|
|
443
|
+
this.used.add("opFailed");
|
|
444
|
+
return `self::opFailed(${phpStr(nodeId)}, ${phpStr(policy)}, ${errLocal})`;
|
|
445
|
+
}
|
|
446
|
+
deMismatch(model, field, expected, src) {
|
|
447
|
+
this.used.add("mismatch");
|
|
448
|
+
return `throw self::mismatch(${phpStr(model)}, ${phpStr(field)}, ${phpStr(expected)}, ${src});`;
|
|
449
|
+
}
|
|
450
|
+
deMissing(model, field, expected) {
|
|
451
|
+
this.used.add("missing");
|
|
452
|
+
return `throw self::missing(${phpStr(model)}, ${phpStr(field)}, ${phpStr(expected)});`;
|
|
453
|
+
}
|
|
454
|
+
deForbiddenKey(_keyExpr) {
|
|
455
|
+
return `throw new ${ERR}('FORBIDDEN_KEY', 'object key "__proto__" is forbidden (fail-closed)');`;
|
|
456
|
+
}
|
|
457
|
+
batchMismatch(code, prefix, wantExpr, _gotExpr) {
|
|
458
|
+
return `new ${ERR}(${phpStr(code)}, ${phpStr(prefix + " (want ")} . ${wantExpr} . ')')`;
|
|
459
|
+
}
|
|
460
|
+
deScalar(scalar, src, model, field, expected) {
|
|
461
|
+
const name = `de${scalar[0].toUpperCase()}${scalar.slice(1)}`;
|
|
462
|
+
this.used.add(name);
|
|
463
|
+
return `self::${name}(${src}, ${phpStr(model)}, ${phpStr(field)}, ${phpStr(expected)})`;
|
|
464
|
+
}
|
|
465
|
+
leafCall(sym, payloadExpr) {
|
|
466
|
+
return this.leafClass === undefined ? `${sym}(${payloadExpr})` : `\\${this.leafClass.replace(/^\\+/, "")}::${sym}(${payloadExpr})`;
|
|
467
|
+
}
|
|
468
|
+
submit() {
|
|
469
|
+
throw new GeneratorFailure("UNSUPPORTED_NODE_STRAIGHTLINE", "php native: a stage runs in ascending order (no worker pool)");
|
|
470
|
+
}
|
|
471
|
+
result() {
|
|
472
|
+
return this.submit();
|
|
473
|
+
}
|
|
474
|
+
pair() {
|
|
475
|
+
return this.submit();
|
|
476
|
+
}
|
|
477
|
+
unpack() {
|
|
478
|
+
return this.submit();
|
|
479
|
+
}
|
|
480
|
+
exprBackend(resolveHead, plan, fresh) {
|
|
481
|
+
const used = this.used;
|
|
482
|
+
return {
|
|
483
|
+
lang: "php",
|
|
484
|
+
freshTemp: () => `$${fresh()}`,
|
|
485
|
+
renderType: () => "",
|
|
486
|
+
scalarType: () => "",
|
|
487
|
+
strLit: phpStr,
|
|
488
|
+
boolLit: (b) => (b ? "true" : "false"),
|
|
489
|
+
intLit: phpInt,
|
|
490
|
+
floatLit: phpFloat,
|
|
491
|
+
fieldAccess(baseExpr, baseRef, path) {
|
|
492
|
+
let expr = baseExpr;
|
|
493
|
+
let ref = baseRef;
|
|
494
|
+
for (const f of path) {
|
|
495
|
+
const fld = ref.kind === "named" ? findDecl(plan, ref.name)?.fields.find((x) => x.name === f) : undefined;
|
|
496
|
+
if (fld === undefined)
|
|
497
|
+
throw new GeneratorFailure("UNSUPPORTED_NODE_STRAIGHTLINE", `php native expr: field '.${f}' is not a field of ${JSON.stringify(ref)}`);
|
|
498
|
+
expr = prop(expr, f);
|
|
499
|
+
ref = fld.type;
|
|
500
|
+
}
|
|
501
|
+
return { expr, ref };
|
|
502
|
+
},
|
|
503
|
+
resolveHead,
|
|
504
|
+
hoistFallible: (temp, _ty, call) => [`${temp} = ${call};`],
|
|
505
|
+
helperCall: (helper, args) => helperCall(used, helper, args),
|
|
506
|
+
binOp: (op, a, b) => `(${a} ${op === "==" ? "===" : op === "!=" ? "!==" : op} ${b})`,
|
|
507
|
+
concat: (parts) => `(${parts.join(" . ")})`,
|
|
508
|
+
notOp: (a) => `(!${a})`,
|
|
509
|
+
// the COMPLEMENT of a float ordering (an incomparable NaN pair makes both directions false — #309).
|
|
510
|
+
negPartialOrdCmp: (op, a, b) => `(!(${a} ${op === "lt" ? "<" : op === "le" ? "<=" : op === "gt" ? ">" : ">="} ${b}))`,
|
|
511
|
+
structLit: (_name, inits) => objLit(inits.map((i) => ({ key: i.field, expr: i.expr }))),
|
|
512
|
+
arrLit: (_ty, items) => `[${items.join(", ")}]`,
|
|
513
|
+
// the php wire IS the native value model: boxing into the opaque wire is the identity.
|
|
514
|
+
wireRowLit: objLit,
|
|
515
|
+
wireListLit: (items) => `[${items.join(", ")}]`,
|
|
516
|
+
mapLit: (_ty, entries) => objLit(entries),
|
|
517
|
+
wireBox: (item) => item.expr,
|
|
518
|
+
wireBoxEachElem: (src) => src.expr,
|
|
519
|
+
wireType: () => "",
|
|
520
|
+
optNone: () => "null",
|
|
521
|
+
optIsSome: (e) => `(${e} !== null)`,
|
|
522
|
+
optIsNone: (e) => `(${e} === null)`,
|
|
523
|
+
narrowUnwrap: (e) => e,
|
|
524
|
+
optSome: (inner) => ({ stmts: [], expr: inner }),
|
|
525
|
+
optMapWireBox: (src) => src.expr,
|
|
526
|
+
optUnwrapOr: (optExpr, d) => `(${optExpr} ?? ${d})`,
|
|
527
|
+
optCoalesceGuard: (temp, _ty, optExpr, bindTemp, dStmts, dExpr) => [
|
|
528
|
+
`${bindTemp} = ${optExpr};`,
|
|
529
|
+
`if (${bindTemp} !== null) {`,
|
|
530
|
+
` ${temp} = ${bindTemp};`,
|
|
531
|
+
`} else {`,
|
|
532
|
+
...indentLines(dStmts),
|
|
533
|
+
` ${temp} = ${dExpr};`,
|
|
534
|
+
`}`,
|
|
535
|
+
],
|
|
536
|
+
ternary: (c, t, e) => `(${c} ? ${t} : ${e})`,
|
|
537
|
+
shortCircuitBool: (op, temp, left, rStmts, rExpr) => op === "and"
|
|
538
|
+
? [`if (${left}) {`, ...indentLines(rStmts), ` ${temp} = ${rExpr};`, `} else {`, ` ${temp} = false;`, `}`]
|
|
539
|
+
: [`if (${left}) {`, ` ${temp} = true;`, `} else {`, ...indentLines(rStmts), ` ${temp} = ${rExpr};`, `}`],
|
|
540
|
+
condGuard: (temp, _ty, cond, tStmts, tExpr, eStmts, eExpr) => [
|
|
541
|
+
`if (${cond}) {`,
|
|
542
|
+
...indentLines(tStmts),
|
|
543
|
+
` ${temp} = ${tExpr};`,
|
|
544
|
+
`} else {`,
|
|
545
|
+
...indentLines(eStmts),
|
|
546
|
+
` ${temp} = ${eExpr};`,
|
|
547
|
+
`}`,
|
|
548
|
+
],
|
|
549
|
+
};
|
|
550
|
+
}
|
|
551
|
+
render(stmts, depth) {
|
|
552
|
+
const ind = " ".repeat(depth);
|
|
553
|
+
const out = [];
|
|
554
|
+
for (const st of stmts) {
|
|
555
|
+
if (typeof st === "string") {
|
|
556
|
+
for (const l of st.split("\n"))
|
|
557
|
+
out.push(`${ind}${l}`);
|
|
558
|
+
}
|
|
559
|
+
else if ("comment" in st) {
|
|
560
|
+
out.push(`${ind}// ${st.comment.replace(/\s+/g, " ")}`);
|
|
561
|
+
}
|
|
562
|
+
else if ("if" in st) {
|
|
563
|
+
out.push(`${ind}if (${st.if}) {`, ...this.render(st.then, depth + 1));
|
|
564
|
+
if (st.else !== undefined && st.else.length > 0)
|
|
565
|
+
out.push(`${ind}} else {`, ...this.render(st.else, depth + 1));
|
|
566
|
+
out.push(`${ind}}`);
|
|
567
|
+
}
|
|
568
|
+
else if ("forEach" in st) {
|
|
569
|
+
out.push(`${ind}foreach (${st.forEach.over} as ${st.forEach.v}) {`, ...this.render(st.body, depth + 1), `${ind}}`);
|
|
570
|
+
}
|
|
571
|
+
else if ("forEnum" in st) {
|
|
572
|
+
out.push(`${ind}foreach (${st.forEnum.over} as ${st.forEnum.i} => ${st.forEnum.v}) {`, ...this.render(st.body, depth + 1), `${ind}}`);
|
|
573
|
+
}
|
|
574
|
+
else if ("try" in st) {
|
|
575
|
+
out.push(`${ind}try {`, ...this.render(st.try, depth + 1), `${ind}} catch (\\Exception ${st.err}) {`, ...this.render(st.catch, depth + 1), `${ind}}`);
|
|
576
|
+
}
|
|
577
|
+
else {
|
|
578
|
+
throw new GeneratorFailure("UNSUPPORTED_NODE_STRAIGHTLINE", "php native: a stage runs in ascending order (no worker / pool statement)");
|
|
579
|
+
}
|
|
580
|
+
}
|
|
581
|
+
return out;
|
|
582
|
+
}
|
|
583
|
+
renderFunction(comp, params, body) {
|
|
584
|
+
return [`public static function ${this.entryName(comp)}(${params.join(", ")}): mixed`, `{`, ...this.render(body, 1), `}`].join("\n");
|
|
585
|
+
}
|
|
586
|
+
module(ctx, runners, leafSymbols, _uses) {
|
|
587
|
+
// php function and method names are case-INSENSITIVE: two transport symbols that differ only by case are
|
|
588
|
+
// ONE function, so the module would silently call one leaf's transport for the other.
|
|
589
|
+
const byKey = new Map();
|
|
590
|
+
for (const sym of leafSymbols) {
|
|
591
|
+
const prior = byKey.get(sym.toLowerCase());
|
|
592
|
+
if (prior !== undefined)
|
|
593
|
+
throw new GeneratorFailure("UNSUPPORTED_NODE_STRAIGHTLINE", `php native: the leaf transport symbols '${prior}' and '${sym}' differ only by case — php resolves them to ONE function (map them to distinct names with --leaf-transport).`);
|
|
594
|
+
byKey.set(sym.toLowerCase(), sym);
|
|
595
|
+
}
|
|
596
|
+
const need = new Set();
|
|
597
|
+
const add = (h) => {
|
|
598
|
+
if (need.has(h))
|
|
599
|
+
return;
|
|
600
|
+
need.add(h);
|
|
601
|
+
for (const d of HELPERS[h].deps)
|
|
602
|
+
add(d);
|
|
603
|
+
};
|
|
604
|
+
for (const h of this.used)
|
|
605
|
+
add(h);
|
|
606
|
+
const helpers = Object.keys(HELPERS).filter((h) => need.has(h)).map((h) => HELPERS[h].code);
|
|
607
|
+
const sv = ctx.specVersions;
|
|
608
|
+
const members = [
|
|
609
|
+
`/** The spec versions baked at generation time (provenance only — the module is runtime-free). */\npublic const EXPECTED_SPEC_VERSIONS = ['behavior' => ${sv.behavior}, 'expression' => ${sv.expression}, 'plan' => ${sv.plan}];`,
|
|
610
|
+
`/** The components on the native path (declaration order) — each a 1:1 entry <name>(<positional params>). */\npublic const COMPONENT_NAMES_NATIVE = [${ctx.ir.components.map((c) => phpStr(c.name)).join(", ")}];`,
|
|
611
|
+
...runners,
|
|
612
|
+
...helpers,
|
|
613
|
+
];
|
|
614
|
+
const indented = members.map((m) => m.split("\n").map((l) => (l === "" ? "" : ` ${l}`)).join("\n")).join("\n\n");
|
|
615
|
+
return `<?php
|
|
616
|
+
|
|
617
|
+
// GENERATED by behavior-contracts (php-native) — DO NOT EDIT.
|
|
618
|
+
// The runtime-free native module: every component is a native static method whose body is its execution
|
|
619
|
+
// plan unrolled into statements — no embedded IR, no interpreter, no bc runtime import. Each node calls the
|
|
620
|
+
// op-agnostic leaf transport directly with ONE payload (port name => value) and de-boxes the value it
|
|
621
|
+
// returns against the node's declared outType. \`require\` returns the module (an anonymous class); failures
|
|
622
|
+
// throw ${ERR}.
|
|
623
|
+
|
|
624
|
+
declare(strict_types=1);
|
|
625
|
+
|
|
626
|
+
namespace BehaviorContractsNative {
|
|
627
|
+
if (!\\class_exists(BehaviorError::class, false)) {
|
|
628
|
+
/**
|
|
629
|
+
* A failure of a generated native module: a stable \`failureCode\` (the codes the interpreter raises),
|
|
630
|
+
* the message, and the structured \`detail\` of scp-error.md "The Error Value" (null when the failure
|
|
631
|
+
* is not about a datum).
|
|
632
|
+
*/
|
|
633
|
+
final class BehaviorError extends \\RuntimeException
|
|
634
|
+
{
|
|
635
|
+
/** @param array<string,mixed>|null $detail */
|
|
636
|
+
public function __construct(
|
|
637
|
+
public readonly string $failureCode,
|
|
638
|
+
string $message,
|
|
639
|
+
public readonly ?array $detail = null,
|
|
640
|
+
?\\Throwable $previous = null,
|
|
641
|
+
) {
|
|
642
|
+
parent::__construct($message, 0, $previous);
|
|
643
|
+
}
|
|
644
|
+
}
|
|
645
|
+
}
|
|
646
|
+
}
|
|
647
|
+
|
|
648
|
+
namespace {
|
|
649
|
+
return new class {
|
|
650
|
+
${indented}
|
|
651
|
+
};
|
|
652
|
+
}
|
|
653
|
+
`;
|
|
654
|
+
}
|
|
655
|
+
}
|
|
656
|
+
function emit(ctx) {
|
|
657
|
+
return emitDynamicNative(ctx, new PhpBackend(ctx));
|
|
658
|
+
}
|
|
659
|
+
/** phpNativeEmitter — language id `php-native` (bc#330): the runtime-free php native module. */
|
|
660
|
+
export const phpNativeEmitter = {
|
|
661
|
+
language: "php-native",
|
|
662
|
+
classification: "codegen",
|
|
663
|
+
fileExtension: ".php",
|
|
664
|
+
defaultRuntimeImport: "",
|
|
665
|
+
filenameHint: "behaviors_native.php",
|
|
666
|
+
emit,
|
|
667
|
+
};
|
|
668
|
+
//# sourceMappingURL=emit-native-php.js.map
|