Fix toString for proxies

This commit is contained in:
kkapsner 2021-02-21 11:00:33 +01:00
parent aa3f9d878d
commit a7d02efd09
1 changed files with 32 additions and 6 deletions

View File

@ -136,14 +136,40 @@
}
};
const proxies = new Map();
const changedToStrings = new WeakMap();
scope.createProxyFunction = function createProxyFunction(window, original, replacement){
const wrappedWindow = scope.getWrapped(window);
const handler = wrappedWindow.Object.create(wrappedWindow.Object);
handler.apply = scope.exportFunctionWithName(function(target, thisArgs, args){
// console.log(target, thisArgs, args);
return replacement.apply(thisArgs, args);
if (!changedToStrings.get(window)){
changedToStrings.set(window, true);
const functionPrototype = scope.getWrapped(window).Function.prototype;
const toString = functionPrototype.toString;
scope.changeProperty(window, "toString", {
object: functionPrototype,
name: "toString",
type: "value",
changed: scope.createProxyFunction(
window,
toString,
function(){
return proxies.get(this) || toString.call(this);
}
)
});
}
const handler = scope.getWrapped(window).Object.create(null);
handler.apply = scope.exportFunctionWithName(function(_target, thisArgs, args){
try {
return args.length?
replacement.call(thisArgs, ...args):
replacement.call(thisArgs);
}
catch (error){
return original.apply(thisArgs, args);
}
}, window, "");
return new wrappedWindow.Proxy(original, handler);
const proxy = new window.Proxy(original, handler);
proxies.set(proxy, original.toString());
return scope.getWrapped(proxy);
};
const changedPropertiesByWindow = new WeakMap();