-
-
Notifications
You must be signed in to change notification settings - Fork 75
Expand file tree
/
Copy pathjs-emitter.mjs
More file actions
442 lines (399 loc) · 16 KB
/
Copy pathjs-emitter.mjs
File metadata and controls
442 lines (399 loc) · 16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
// @ts-check
/**
* @typedef {import('../types.mjs').BridgeType} BridgeType
* @typedef {import('../types.mjs').Param} Param
* @typedef {import('../types.mjs').ImportedFunc} ImportedFunc
* @typedef {import('../types.mjs').ImportedClass} ImportedClass
* @typedef {import('../types.mjs').Var} Var
* @typedef {import('../types.mjs').Operation} Operation
* @typedef {import('../types.mjs').TestFunc} TestFunc
* @typedef {import('../types.mjs').TestCase} TestCase
*/
/**
* Generate a default JS constant for a given BridgeType.
* Used when no parameter matches the return type.
* @param {BridgeType} type
* @returns {*}
*/
function defaultJSValue(type) {
switch (type.kind) {
case "int": return 0;
case "float": return 0.0;
case "double": return 0.0;
case "string": return "";
case "bool": return false;
case "jsObject": return {};
case "void": return undefined;
case "nullable": return null;
case "array": return [];
case "dictionary": return {};
case "importedClass": return {};
default: return undefined;
}
}
/**
* Check if two BridgeTypes are structurally equal.
* @param {BridgeType} a
* @param {BridgeType} b
* @returns {boolean}
*/
function typesEqual(a, b) {
if (a.kind !== b.kind) return false;
switch (a.kind) {
case "nullable":
return typesEqual(a.wrapped, /** @type {Extract<BridgeType, {kind:"nullable"}>} */ (b).wrapped);
case "array":
return typesEqual(a.element, /** @type {Extract<BridgeType, {kind:"array"}>} */ (b).element);
case "dictionary":
return typesEqual(a.value, /** @type {Extract<BridgeType, {kind:"dictionary"}>} */ (b).value);
case "importedClass":
return a.name === /** @type {Extract<BridgeType, {kind:"importedClass"}>} */ (b).name;
default:
return true;
}
}
/**
* Returns the assertion strategy for a type.
* @param {BridgeType} type
* @returns {"deep"|"strict"|"float"}
*/
function assertionKind(type) {
switch (type.kind) {
case "float":
return "float";
case "array":
case "dictionary":
return "deep";
case "nullable":
return assertionKind(type.wrapped);
default:
// importedClass + jsObject use strictEqual (reference identity is preserved)
return "strict";
}
}
/**
* Format a JS value as a source literal string.
* @param {*} val
* @returns {string}
*/
function jsLiteral(val) {
if (val === null) return "null";
if (val === undefined) return "undefined";
if (typeof val === "string") return JSON.stringify(val);
if (typeof val === "number") return String(val);
if (typeof val === "boolean") return String(val);
if (Array.isArray(val)) return `[${val.map(jsLiteral).join(", ")}]`;
if (typeof val === "object") {
const entries = Object.entries(val);
if (entries.length === 0) return "{}";
return `{ ${entries.map(([k, v]) => `${JSON.stringify(k)}: ${jsLiteral(v)}`).join(", ")} }`;
}
return String(val);
}
/**
* Generate the JS implementation body for an imported function.
* Strategy: return the first param that matches the return type, else return a default.
* @param {ImportedFunc} func
* @returns {string}
*/
function emitImportFuncImpl(func) {
const paramNames = func.params.map(p => p.name);
const paramList = paramNames.join(", ");
if (func.returnType.kind === "void") {
return `(${paramList}) => {}`;
}
// If return type is string and we have multiple string params, concatenate them
if (func.returnType.kind === "string") {
const stringParams = func.params.filter(p => p.kind === "string" || p.type.kind === "string");
if (stringParams.length > 1) {
return `(${paramList}) => ${stringParams.map(p => p.name).join(" + ")}`;
}
}
// If return type is numeric (int/float/double) and we have multiple numeric params, sum them
if (func.returnType.kind === "int" || func.returnType.kind === "float" || func.returnType.kind === "double") {
const numericParams = func.params.filter(p =>
p.type.kind === "int" || p.type.kind === "float" || p.type.kind === "double"
);
if (numericParams.length > 1) {
return `(${paramList}) => ${numericParams.map(p => p.name).join(" + ")}`;
}
}
// Return first param matching return type
for (const p of func.params) {
if (typesEqual(p.type, func.returnType)) {
return `(${paramList}) => ${p.name}`;
}
}
// No match: return a constant
const def = defaultJSValue(func.returnType);
return `(${paramList}) => ${jsLiteral(def)}`;
}
/**
* Generate the JS class implementation for an imported class.
* Constructor stores params as _p0, _p1, etc.
* @param {ImportedClass} cls
* @returns {string}
*/
function emitImportClassImpl(cls) {
const paramNames = cls.constructorParams.map(p => p.name);
const paramList = paramNames.join(", ");
const assigns = paramNames.map((n, i) => `this._p${i} = ${n};`).join(" ");
return `class ${cls.name} { constructor(${paramList}) { ${assigns} } }`;
}
/**
* Evaluate what the imported function returns for given JS argument values.
* Must mirror the logic in emitImportFuncImpl.
* @param {ImportedFunc} func
* @param {*[]} argValues
* @returns {*}
*/
function evalImportFunc(func, argValues) {
if (func.returnType.kind === "void") return undefined;
// String concatenation case
if (func.returnType.kind === "string") {
const stringIndices = func.params
.map((p, i) => ({ p, i }))
.filter(({ p }) => p.type.kind === "string");
if (stringIndices.length > 1) {
return stringIndices.map(({ i }) => argValues[i]).join("");
}
}
// Numeric sum case
if (func.returnType.kind === "int" || func.returnType.kind === "float" || func.returnType.kind === "double") {
const numericIndices = func.params
.map((p, i) => ({ p, i }))
.filter(({ p }) =>
p.type.kind === "int" || p.type.kind === "float" || p.type.kind === "double"
);
if (numericIndices.length > 1) {
return numericIndices.reduce((acc, { i }) => acc + argValues[i], 0);
}
}
// Identity on first matching param
for (let i = 0; i < func.params.length; i++) {
if (typesEqual(func.params[i].type, func.returnType)) {
return argValues[i];
}
}
return defaultJSValue(func.returnType);
}
/**
* Evaluate what constructing an imported class yields.
* @param {ImportedClass} cls
* @param {*[]} argValues
* @returns {Record<string, *>}
*/
function evalConstruct(cls, argValues) {
/** @type {Record<string, *>} */
const obj = {};
for (let i = 0; i < argValues.length; i++) {
obj[`_p${i}`] = argValues[i];
}
return obj;
}
/**
* Result of tracing a test function.
* @typedef {{ value: *, sourceExpr: string }} TraceResult
*/
/**
* Trace through a test function's operations to compute:
* - the expected return value (for value-type assertions)
* - the source expression to compare against (for reference-type assertions,
* this is a variable name so we can assert reference identity)
*
* @param {TestFunc} func
* @param {*[]} argValues - JS values for the function params
* @param {string[]} argVarNames - emitted variable names (e.g. ["_a0", "_a1"])
* @param {TestCase} testCase
* @returns {TraceResult}
*/
function traceTestFunc(func, argValues, argVarNames, testCase) {
/** @type {Map<string, *>} */
const vars = new Map();
/** @type {Map<string, string>} */
const varSources = new Map();
for (let i = 0; i < func.params.length; i++) {
vars.set(func.params[i].name, argValues[i]);
varSources.set(func.params[i].name, argVarNames[i]);
}
for (const op of func.ops) {
switch (op.kind) {
case "literal": {
const val = JSON.parse(op.jsValue);
vars.set(op.var.name, val);
varSources.set(op.var.name, jsLiteral(val));
break;
}
case "callImport": {
const callArgs = op.args.map(a => vars.get(a.name));
const result = evalImportFunc(op.func, callArgs);
vars.set(op.var.name, result);
const argSrcs = op.args.map(a => varSources.get(a.name) ?? jsLiteral(vars.get(a.name)));
varSources.set(op.var.name, traceImportSource(op.func, argSrcs));
break;
}
case "construct": {
const ctorArgs = op.args.map(a => vars.get(a.name));
vars.set(op.var.name, evalConstruct(op.class, ctorArgs));
// Constructs always create a new object — no way to reference-match from outside
const ctorSrcs = op.args.map(a => varSources.get(a.name) ?? jsLiteral(vars.get(a.name)));
varSources.set(op.var.name, `new ${op.class.name}(${ctorSrcs.join(", ")})`);
break;
}
case "return": {
const val = vars.get(op.value.name);
const src = varSources.get(op.value.name) ?? jsLiteral(val);
return { value: val, sourceExpr: src };
}
}
}
return { value: undefined, sourceExpr: "undefined" };
}
/**
* Determine what source expression an import function returns.
* Must mirror emitImportFuncImpl / evalImportFunc.
* @param {ImportedFunc} func
* @param {string[]} argSources
* @returns {string}
*/
function traceImportSource(func, argSources) {
if (func.returnType.kind === "void") return "undefined";
if (func.returnType.kind === "string") {
const idx = func.params.map((p, i) => ({ p, i })).filter(({ p }) => p.type.kind === "string");
if (idx.length > 1) return idx.map(({ i }) => argSources[i]).join(" + ");
}
if (func.returnType.kind === "int" || func.returnType.kind === "float" || func.returnType.kind === "double") {
const idx = func.params.map((p, i) => ({ p, i })).filter(({ p }) =>
p.type.kind === "int" || p.type.kind === "float" || p.type.kind === "double"
);
if (idx.length > 1) return idx.map(({ i }) => argSources[i]).join(" + ");
}
// Identity: return first param matching return type
for (let i = 0; i < func.params.length; i++) {
if (typesEqual(func.params[i].type, func.returnType)) return argSources[i];
}
return jsLiteral(defaultJSValue(func.returnType));
}
/**
* Generate test argument source strings and runtime values for a list of params.
* @param {Param[]} params
* @param {DeclEnv} env
* @returns {{ sources: string[], values: *[] }}
*/
function generateTestArgs(params, env) {
const sources = [];
const values = [];
for (let i = 0; i < params.length; i++) {
const { source, value } = generateArg(params[i].type, i, env);
sources.push(source);
values.push(value);
}
return { sources, values };
}
/**
* Generate a deterministic test arg: both the JS source string and the runtime value.
* @param {BridgeType} type
* @param {number} index
* @param {DeclEnv} env
* @returns {{ source: string, value: * }}
*/
function generateArg(type, index, env) {
switch (type.kind) {
case "int": { const v = 10 + index; return { source: String(v), value: v }; }
case "float": { const v = 1.5 + index; return { source: String(v), value: v }; }
case "double": { const v = 2.5 + index; return { source: String(v), value: v }; }
case "string": { const v = `s${index}`; return { source: JSON.stringify(v), value: v }; }
case "bool": { const v = index % 2 === 0; return { source: String(v), value: v }; }
case "jsObject": return { source: "{}", value: {} };
case "void": return { source: "undefined", value: undefined };
case "nullable": return generateArg(type.wrapped, index, env);
case "array": {
const inner = generateArg(type.element, 0, env);
return { source: `[${inner.source}]`, value: [inner.value] };
}
case "dictionary": {
const inner = generateArg(type.value, 0, env);
const key = `k${index}`;
return { source: `{ ${JSON.stringify(key)}: ${inner.source} }`, value: { [key]: inner.value } };
}
case "importedClass": {
const cls = env.importedClasses.find(c => c.name === type.name);
if (!cls) return { source: "{}", value: {} };
// Construct with deterministic args
const ctorArgs = cls.constructorParams.map((p, ci) => generateArg(p.type, ci, env));
const ctorSources = ctorArgs.map(a => a.source).join(", ");
const ctorValues = ctorArgs.map(a => a.value);
return {
source: `new ${cls.name}(${ctorSources})`,
value: evalConstruct(cls, ctorValues),
};
}
default: return { source: "null", value: null };
}
}
/**
* Generate the complete harness.mjs file for a test case.
* @param {TestCase} testCase
* @returns {string}
*/
export function emitJS(testCase) {
const lines = [];
lines.push(`import { instantiate } from "./instantiate.js";`);
lines.push(`import { defaultNodeSetup } from "./platforms/node.js";`);
lines.push(`import assert from "node:assert/strict";`);
lines.push("");
// Hoist class definitions so they're in scope for both getImports and assertions
for (const cls of testCase.env.importedClasses) {
lines.push(emitImportClassImpl(cls));
}
if (testCase.env.importedClasses.length > 0) lines.push("");
// Build getImports body
const importEntries = [];
for (const func of testCase.env.importedFuncs) {
importEntries.push(` ${func.name}: ${emitImportFuncImpl(func)}`);
}
for (const cls of testCase.env.importedClasses) {
importEntries.push(` ${cls.name}: ${cls.name}`);
}
lines.push(`const nodeOptions = await defaultNodeSetup();`);
lines.push(`const { exports } = await instantiate({`);
lines.push(` ...nodeOptions,`);
lines.push(` getImports() {`);
lines.push(` return {`);
lines.push(importEntries.join(",\n"));
lines.push(` };`);
lines.push(` }`);
lines.push(`});`);
lines.push("");
// Test assertions
for (const tf of testCase.testFuncs) {
const { sources: argSources, values: argValues } = generateTestArgs(tf.params, testCase.env);
// Emit args as named variables so reference-type assertions can
// compare against the same variable (preserving identity).
const argVarNames = tf.params.map((_, i) => `_${tf.name}_a${i}`);
for (let i = 0; i < tf.params.length; i++) {
lines.push(`const ${argVarNames[i]} = ${argSources[i]};`);
}
const argStr = argVarNames.join(", ");
if (tf.returnType.kind === "void") {
lines.push(`exports.${tf.name}(${argStr});`);
lines.push("");
continue;
}
const { value: expected, sourceExpr } = traceTestFunc(tf, argValues, argVarNames, testCase);
const kind = assertionKind(tf.returnType);
if (kind === "float") {
lines.push(`{ const _r = exports.${tf.name}(${argStr}); assert.ok(Math.abs(_r - ${jsLiteral(expected)}) < Math.abs(${jsLiteral(expected)} * 1e-6) + 1e-6, "float mismatch: " + _r + " vs ${jsLiteral(expected)}"); }`);
} else if (kind === "deep") {
lines.push(`assert.deepStrictEqual(exports.${tf.name}(${argStr}), ${sourceExpr});`);
} else {
// strictEqual — works for primitives and reference identity
lines.push(`assert.strictEqual(exports.${tf.name}(${argStr}), ${sourceExpr});`);
}
lines.push("");
}
lines.push("");
lines.push(`console.log("All assertions passed.");`);
lines.push("");
return lines.join("\n");
}