diff --git a/packages/ptc-runtime/ptc-runtime-node/src/process.ts b/packages/ptc-runtime/ptc-runtime-node/src/process.ts index 6aa13e5033..3ac1f66ac9 100644 --- a/packages/ptc-runtime/ptc-runtime-node/src/process.ts +++ b/packages/ptc-runtime/ptc-runtime-node/src/process.ts @@ -13,7 +13,7 @@ import type { ProgramEvaluator, ProgramImportHandler, ProgramImportResult } from import { PROGRAM_PROTOCOL_VERSION, type ProgramResult, type HostMethods, type ProgramMethods } from './protocol.ts' const capturedObjectCreate = Object.create -const capturedObjectDefineProperties = Object.defineProperties +const capturedObjectDefineProperty = Object.defineProperty const capturedObjectFreeze = Object.freeze const nativeConsole = globalThis.console @@ -85,12 +85,12 @@ export async function runNodeMain( } const decodeImage = (wire: PtcImageWire): PtcJsonValue => { const image = capturedObjectCreate(Image.prototype) as Image - capturedObjectDefineProperties(image, { - attachmentId: { enumerable: true, value: wire.attachmentId }, - path: { enumerable: true, value: wire.path }, - width: { enumerable: true, value: wire.width }, - height: { enumerable: true, value: wire.height }, - }) + for (const key of ['attachmentId', 'path', 'width', 'height'] as const) { + const descriptor = capturedObjectCreate(null) as PropertyDescriptor + descriptor.enumerable = true + descriptor.value = wire[key] + capturedObjectDefineProperty(image, key, descriptor) + } capturedObjectFreeze(image) images.set(image, wire.id) return image diff --git a/packages/ptc-runtime/ptc-runtime-node/tests/output.spec.ts b/packages/ptc-runtime/ptc-runtime-node/tests/output.spec.ts index 3dc039f259..81100dd3d7 100644 --- a/packages/ptc-runtime/ptc-runtime-node/tests/output.spec.ts +++ b/packages/ptc-runtime/ptc-runtime-node/tests/output.spec.ts @@ -263,3 +263,32 @@ describe.each(modes)('program output in $executionMode with retention $scriptPer expect((await run(completion('42'), false)).value).toBe(42) }) }) + +it.each(['get', 'set'])('decodes Images after a program changes the %s prototype descriptor field', async (field) => { + const { runtime } = await mountRuntime(new Context(), { executionMode: 'per-call' }) + const image = new PtcImage('/images/prototype.png', { + attachmentId: AttachmentId('sha256:prototype'), mediaType: 'image/png', bytes: 64, width: 8, height: 4, + }) + const result = await runtime.run(runtime.resolve({ + program: ` + const field = ${JSON.stringify(field)}; + const original = Object.getOwnPropertyDescriptor(Object.prototype, field); + try { + Object.defineProperty(Object.prototype, field, { configurable: true, value: () => 1 }); + const image = await tools.image({}); + inspect(image.attachmentId, image.path, image.width, image.height, Object.isFrozen(image)); + inspect(image); + } finally { + Reflect.deleteProperty(Object.prototype, field); + if (original !== undefined) Object.defineProperty(Object.prototype, field, original); + } + `, + bindings: [{ global: 'tools', functions: { image: () => Promise.resolve(image) } }], + discardCompletion: true, + })) + expect(result.error).toBeUndefined() + expect(result.output).toEqual([ + { type: 'text', text: 'sha256:prototype /images/prototype.png 8 4 true' }, + { type: 'image', attachment: image.attachment }, + ]) +})