Not using proxy for toString broke google

For #698, #699, #700, #701
This commit is contained in:
kkapsner 2024-04-07 23:59:49 +02:00
parent dacc578e12
commit b8c6115603
1 changed files with 35 additions and 17 deletions

View File

@ -141,23 +141,35 @@
};
const proxies = new Map();
const changedToStrings = new WeakMap();
scope.createProxyFunction = function createProxyFunction(window, original, replacement){
if (!changedToStrings.get(window)){
const functionPrototype = scope.getWrapped(window).Function.prototype;
const originalToString = functionPrototype.toString;
changedToStrings.set(window, originalToString);
const alteredToString = scope.exportFunctionWithName(function toString(){
return proxies.get(this) || originalToString.call(this);
}, window, "toString");
proxies.set(alteredToString, originalToString.call(originalToString));
scope.changeProperty(window, "toString", {
object: functionPrototype,
name: "toString",
type: "value",
changed: alteredToString
});
const changedWindowsForProxies = new WeakMap();
function setupWindowForProxies(window){
if (changedWindowsForProxies.get(window)){
return;
}
const wrappedWindow = scope.getWrapped(window);
const functionPrototype = wrappedWindow.Function.prototype;
const originalToString = functionPrototype.toString;
changedWindowsForProxies.set(window, originalToString);
const alteredToString = scope.createProxyFunction(
window,
originalToString,
function toString(){
if (proxies.has(this)){
return proxies.get(this).string;
}
return originalToString.call(scope.getWrapped(this));
}
);
scope.changeProperty(window, "toString", {
object: functionPrototype,
name: "toString",
type: "value",
changed: alteredToString
});
}
scope.createProxyFunction = function createProxyFunction(window, original, replacement){
setupWindowForProxies(window);
const handler = scope.getWrapped(window).Object.create(null);
handler.apply = scope.exportFunctionWithName(function(target, thisArgs, args){
try {
@ -175,7 +187,13 @@
}
}, window, "");
const proxy = new window.Proxy(original, handler);
proxies.set(proxy, changedToStrings.get(window).call(original));
const proxyData = {
original: original,
wrappedOriginal: scope.getWrapped(original),
string: changedWindowsForProxies.get(window).call(original),
};
proxies.set(proxy, proxyData);
proxies.set(scope.getWrapped(proxy), proxyData);
return scope.getWrapped(proxy);
};