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];
}
}