Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion typescript/src/ts/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ export function createModuleTextFromProgram(jsonObject: object): Result<string>
return `[${arrayToString(obj)}]`;
}
else {
return `{ ${Object.keys(obj).map(key => `${JSON.stringify(key)}: ${exprToString(obj[key])}`).join(", ")} }`;
return `{ ${Object.keys(obj).map(key => `${key === "__proto__" ? '["__proto__"]' : JSON.stringify(key)}: ${exprToString(obj[key])}`).join(", ")} }`;
}
hasError = true;
return "";
Expand Down
22 changes: 22 additions & 0 deletions typescript/test/program.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,29 @@
import { describe, it } from "node:test";
import assert from "node:assert/strict";
import { stripTypeScriptTypes } from "node:module";
import { runInNewContext } from "node:vm";
import { evaluateJsonProgram, createModuleTextFromProgram, Program, FunctionCall } from "../dist/ts/index.js";

describe("generated JSON object property names", () => {
for (const value of ["kept", { label: "kept" }, null]) {
it(`preserves an own __proto__ field with value ${JSON.stringify(value)}`, async () => {
const input = JSON.parse(`{"__proto__": ${JSON.stringify(value)}, "value": 7}`);
const program: Program = { "@steps": [{ "@func": "identity", "@args": [input] }] };
const module = createModuleTextFromProgram(program);
assert.ok(module.success);
// The schema import is used only for type checking, not by the host API.
const source = stripTypeScriptTypes(module.data.replace(/^import[^\n]*\n/, ""));
const generated = runInNewContext(source + "; program") as
(api: { identity: (value: unknown) => unknown }) => object;
const result = generated({ identity: value => value });
assert.ok(Object.prototype.hasOwnProperty.call(result, "__proto__"));
assert.equal(JSON.stringify(result), JSON.stringify(input));
const interpreted = await evaluateJsonProgram(program, async (_name, args) => args[0]);
assert.equal(JSON.stringify(result), JSON.stringify(interpreted));
});
}
});

// ---------------------------------------------------------------------------
// evaluateJsonProgram result-reference bounds checking
// ---------------------------------------------------------------------------
Expand Down