Skip to content

Commit 6255e08

Browse files
Fix toString DDGProxy response (#324)
* Fix toString DDGProxy response
1 parent 254b4d7 commit 6255e08

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

integration-test/test-utils.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* Tests for utils
3+
*/
4+
import { setup } from './helpers/harness.js'
5+
6+
describe('Ensure utils behave as expected', () => {
7+
let browser
8+
let server
9+
let teardown
10+
let setupServer
11+
let gotoAndWait
12+
beforeAll(async () => {
13+
({ browser, setupServer, teardown, gotoAndWait } = await setup({ withExtension: true }))
14+
server = setupServer()
15+
})
16+
afterAll(async () => {
17+
await server?.close()
18+
await teardown()
19+
})
20+
21+
it('should toString DDGProxy correctly', async () => {
22+
const port = server.address().port
23+
const page = await browser.newPage()
24+
await gotoAndWait(page, `http://localhost:${port}/blank.html`, { platform: { name: 'extension' } })
25+
const toStringResult = await page.evaluate('HTMLCanvasElement.prototype.getContext.toString()')
26+
expect(toStringResult).toEqual('function getContext() { [native code] }')
27+
28+
const toStringToStringResult = await page.evaluate('HTMLCanvasElement.prototype.getContext.toString.toString()')
29+
expect(toStringToStringResult).toEqual('function toString() { [native code] }')
30+
31+
/* TOFIX: This is failing because the toString() call is being proxied and the result is not what we expect
32+
const callToStringToStringResult = await page.evaluate('String.toString.call(HTMLCanvasElement.prototype.getContext.toString)')
33+
expect(callToStringToStringResult).toEqual('function toString() { [native code] }')
34+
*/
35+
})
36+
})

src/utils.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,16 +377,29 @@ export class DDGProxy {
377377
}
378378
return proxyObject.apply(...args)
379379
}
380+
const getMethod = (target, prop, receiver) => {
381+
if (prop === 'toString') {
382+
const method = Reflect.get(target, prop, receiver).bind(target)
383+
Object.defineProperty(method, 'toString', {
384+
value: String.toString.bind(String.toString),
385+
enumerable: false
386+
})
387+
return method
388+
}
389+
return DDGReflect.get(target, prop, receiver)
390+
}
380391
if (hasMozProxies) {
381392
this._native = objectScope[property]
382393
const handler = new globalObj.wrappedJSObject.Object()
383394
handler.apply = exportFunction(outputHandler, globalObj)
395+
handler.get = exportFunction(getMethod, globalObj)
384396
// @ts-ignore
385397
this.internal = new globalObj.wrappedJSObject.Proxy(objectScope.wrappedJSObject[property], handler)
386398
} else {
387399
this._native = objectScope[property]
388400
const handler = {}
389401
handler.apply = outputHandler
402+
handler.get = getMethod
390403
this.internal = new globalObj.Proxy(objectScope[property], handler)
391404
}
392405
}

0 commit comments

Comments
 (0)