refactor: replace V8 hidden values with WeakMap / WeakSet (#26659)

This commit is contained in:
Milan Burda 2020-11-24 22:11:39 +01:00 committed by GitHub
parent 0be6c92aa9
commit c8d77cae4a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 29 additions and 19 deletions

View file

@ -1,11 +1,11 @@
import { WebContents } from 'electron/main';
const v8Util = process._linkedBinding('electron_common_v8_util');
const getOwnerKey = (webContents: WebContents, contextId: string) => {
return `${webContents.id}-${contextId}`;
};
const electronIds = new WeakMap<Object, number>();
class ObjectsRegistry {
private nextId: number = 0
@ -81,14 +81,14 @@ class ObjectsRegistry {
// Private: Saves the object into storage and assigns an ID for it.
saveToStorage (object: any) {
let id: number = v8Util.getHiddenValue(object, 'electronId');
let id = electronIds.get(object);
if (!id) {
id = ++this.nextId;
this.storage[id] = {
count: 0,
object: object
};
v8Util.setHiddenValue(object, 'electronId', id);
electronIds.set(object, id);
}
return id;
}
@ -101,7 +101,7 @@ class ObjectsRegistry {
}
pointer.count -= 1;
if (pointer.count === 0) {
v8Util.deleteHiddenValue(pointer.object, 'electronId');
electronIds.delete(pointer.object);
delete this.storage[id];
}
}

View file

@ -56,6 +56,8 @@ function setCachedRendererFunction (id: RendererFunctionId, wc: electron.WebCont
return value;
}
const locationInfo = new WeakMap<Object, string>();
// Return the description of object's members:
const getObjectMembers = function (object: any): ObjectMember[] {
let names = Object.getOwnPropertyNames(object);
@ -186,7 +188,7 @@ const throwRPCError = function (message: string) {
};
const removeRemoteListenersAndLogWarning = (sender: any, callIntoRenderer: (...args: any[]) => void) => {
const location = v8Util.getHiddenValue(callIntoRenderer, 'location');
const location = locationInfo.get(callIntoRenderer);
let message = 'Attempting to call a function in a renderer window that has been closed or released.' +
`\nFunction provided here: ${location}`;
@ -269,7 +271,7 @@ const unwrapArgs = function (sender: electron.WebContents, frameId: number, cont
removeRemoteListenersAndLogWarning(this, callIntoRenderer);
}
};
v8Util.setHiddenValue(callIntoRenderer, 'location', meta.location);
locationInfo.set(callIntoRenderer, meta.location);
Object.defineProperty(callIntoRenderer, 'length', { value: meta.length });
setCachedRendererFunction(objectId, sender, frameId, callIntoRenderer);