refactor: replace a few usages of V8 hidden properties (#29400)

This commit is contained in:
Milan Burda 2021-06-03 07:59:56 +02:00 коммит произвёл GitHub
Родитель bb6903543c
Коммит 8d0ed05c99
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
5 изменённых файлов: 20 добавлений и 17 удалений

Просмотреть файл

@ -3,7 +3,6 @@ import * as path from 'path';
import * as util from 'util';
const asar = process._linkedBinding('electron_common_asar');
const v8Util = process._linkedBinding('electron_common_v8_util');
const Module = require('module');
@ -787,6 +786,8 @@ export const wrapFsWithAsar = (fs: Record<string, any>) => {
overrideAPISync(childProcess, 'execFileSync');
};
const asarReady = new WeakSet();
// Lazily override the child_process APIs only when child_process is
// fetched the first time. We will eagerly override the child_process APIs
// when this env var is set so that stack traces generated inside node unit
@ -799,8 +800,8 @@ export const wrapFsWithAsar = (fs: Record<string, any>) => {
Module._load = (request: string, ...args: any[]) => {
const loadResult = originalModuleLoad(request, ...args);
if (request === 'child_process') {
if (!v8Util.getHiddenValue(loadResult, 'asar-ready')) {
v8Util.setHiddenValue(loadResult, 'asar-ready', true);
if (!asarReady.has(loadResult)) {
asarReady.add(loadResult);
// Just to make it obvious what we are dealing with here
const childProcess = loadResult;

Просмотреть файл

@ -1,10 +1,11 @@
import { BaseWindow, MenuItem, webContents, Menu as MenuType, BrowserWindow, MenuItemConstructorOptions } from 'electron/main';
import { sortMenuItems } from '@electron/internal/browser/api/menu-utils';
import { setApplicationMenuWasSet } from '@electron/internal/browser/default-menu';
const v8Util = process._linkedBinding('electron_common_v8_util');
const bindings = process._linkedBinding('electron_browser_menu');
const { Menu } = bindings as { Menu: typeof MenuType };
const checked = new WeakMap<MenuItem, boolean>();
let applicationMenu: MenuType | null = null;
let groupIdIndex = 0;
@ -60,7 +61,7 @@ Menu.prototype._menuWillShow = function () {
// Ensure radio groups have at least one menu item selected
for (const id of Object.keys(this.groupsMap)) {
const found = this.groupsMap[id].find(item => item.checked) || null;
if (!found) v8Util.setHiddenValue(this.groupsMap[id][0], 'checked', true);
if (!found) checked.set(this.groupsMap[id][0], true);
}
};
@ -169,7 +170,7 @@ Menu.setApplicationMenu = function (menu: MenuType) {
}
applicationMenu = menu;
v8Util.setHiddenValue(global, 'applicationMenuSet', true);
setApplicationMenuWasSet();
if (process.platform === 'darwin') {
if (!menu) return;
@ -275,15 +276,15 @@ function insertItemByType (this: MenuType, item: MenuItem, pos: number) {
this.groupsMap[item.groupId].push(item);
// Setting a radio menu item should flip other items in the group.
v8Util.setHiddenValue(item, 'checked', item.checked);
checked.set(item, item.checked);
Object.defineProperty(item, 'checked', {
enumerable: true,
get: () => v8Util.getHiddenValue(item, 'checked'),
get: () => checked.get(item),
set: () => {
this.groupsMap[item.groupId].forEach(other => {
if (other !== item) v8Util.setHiddenValue(other, 'checked', false);
if (other !== item) checked.set(other, false);
});
v8Util.setHiddenValue(item, 'checked', true);
checked.set(item, true);
}
});
this.insertRadioItem(pos, item.commandId, item.label, item.groupId);

Просмотреть файл

@ -1,12 +1,16 @@
import { app, Menu } from 'electron/main';
import { shell } from 'electron/common';
const v8Util = process._linkedBinding('electron_common_v8_util');
const isMac = process.platform === 'darwin';
let applicationMenuWasSet = false;
export const setApplicationMenuWasSet = () => {
applicationMenuWasSet = true;
};
export const setDefaultApplicationMenu = () => {
if (v8Util.getHiddenValue<boolean>(global, 'applicationMenuSet')) return;
if (applicationMenuWasSet) return;
const helpMenu: Electron.MenuItemConstructorOptions = {
role: 'help',

Просмотреть файл

@ -1,4 +1,3 @@
/* eslint no-eval: "off" */
/* global binding */
import * as events from 'events';
import { IPC_MESSAGES } from '@electron/internal/common/ipc-messages';

4
typings/internal-electron.d.ts поставляемый
Просмотреть файл

@ -113,9 +113,7 @@ declare namespace Electron {
_executeCommand(event: any, id: number): void;
_menuWillShow(): void;
commandsMap: Record<string, MenuItem>;
groupsMap: Record<string, {
checked: boolean;
}[]>;
groupsMap: Record<string, MenuItem[]>;
getItemCount(): number;
popupAt(window: BaseWindow, x: number, y: number, positioning: number, callback: () => void): void;
closePopupAt(id: number): void;