fix(ptc): isolate image property descriptors from prototypes
This commit is contained in:
1 parent
4c55f03a57
commit
9e61299fdb
2 files changed
+36
-7
No files matched your search
@@ -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
|
||||
|
||||
@@ -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 },
|
||||
])
|
||||
})
|
||||
Reference in new issue
Block a user