Bug 1608276 - Part 1: Do not use global this when loading subprocess scripts. r=mossop

Differential Revision: https://phabricator.services.mozilla.com/D144700
This commit is contained in:
Tooru Fujisawa 2022-04-26 12:48:05 +00:00
Родитель 3511ca17e1
Коммит d049dc7b13
8 изменённых файлов: 43 добавлений и 38 удалений

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

@ -20,12 +20,14 @@ ChromeUtils.defineModuleGetter(
"resource://gre/modules/Timer.jsm"
);
var obj = {};
Services.scriptloader.loadSubScript(
"resource://gre/modules/subprocess/subprocess_shared.js",
this
obj
);
/* global SubprocessConstants */
const { SubprocessConstants, ArrayBuffer_transfer } = obj;
var EXPORTED_SYMBOLS = ["BaseProcess", "PromiseWorker", "SubprocessConstants"];
const BUFFER_SIZE = 32768;
@ -396,9 +398,9 @@ class InputPipe extends Pipe {
let buffer = this.buffers[0];
this.buffers[0] = buffer.slice(length);
result = ArrayBuffer.transfer(buffer, length);
result = ArrayBuffer_transfer(buffer, length);
} else {
result = ArrayBuffer.transfer(this.buffers.shift(), length);
result = ArrayBuffer_transfer(this.buffers.shift(), length);
let u8result = new Uint8Array(result);
while (byteLength < length) {

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

@ -5,29 +5,28 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";
/* exported Library, SubprocessConstants */
/* exported ArrayBuffer_transfer, Library, SubprocessConstants */
if (!ArrayBuffer.transfer) {
/**
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer/transfer
*
* @param {ArrayBuffer} buffer
* @param {integer} [size = buffer.byteLength]
* @returns {ArrayBuffer}
*/
ArrayBuffer.transfer = function(buffer, size = buffer.byteLength) {
let u8out = new Uint8Array(size);
let u8buffer = new Uint8Array(buffer, 0, Math.min(size, buffer.byteLength));
u8out.set(u8buffer);
return u8out.buffer;
};
}
/**
* Returns a new ArrayBuffer whose contents have been taken from the `buffer`'s
* data and then is either truncated or zero-extended by `size`. If `size` is
* undefined, the `byteLength` of the `buffer` is used. This operation leaves
* `buffer` in a detached state.
*
* @param {ArrayBuffer} buffer
* @param {integer} [size = buffer.byteLength]
* @returns {ArrayBuffer}
*/
var ArrayBuffer_transfer = function(buffer, size = buffer.byteLength) {
let u8out = new Uint8Array(size);
let u8buffer = new Uint8Array(buffer, 0, Math.min(size, buffer.byteLength));
u8out.set(u8buffer);
return u8out.buffer;
};
var libraries = {};
class Library {
var Library = class Library {
constructor(name, names, definitions) {
if (name in libraries) {
return libraries[name];
@ -66,14 +65,14 @@ class Library {
},
});
}
}
};
/**
* Holds constants which apply to various Subprocess operations.
* @namespace
* @lends Subprocess
*/
const SubprocessConstants = {
var SubprocessConstants = {
/**
* @property {integer} ERROR_END_OF_FILE
* The operation failed because the end of the file was reached.

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

@ -5,12 +5,13 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";
/* exported libc */
/* exported LIBC, libc */
// This file is loaded into the same scope as subprocess_unix.jsm
/* import-globals-from subprocess_shared.js */
/* import-globals-from subprocess_unix.jsm */
const LIBC = OS.Constants.libc;
var LIBC = OS.Constants.libc;
const LIBC_CHOICES = ["libc.so", "libSystem.B.dylib", "a.out"];

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

@ -5,9 +5,10 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";
/* exported LIBC, Win, createPipe, libc */
/* exported LIBC, Win, createPipe, libc, win32 */
// This file is loaded into the same scope as subprocess_win.jsm
/* import-globals-from subprocess_shared.js */
/* import-globals-from subprocess_win.jsm */
const LIBC = OS.Constants.libc;

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

@ -18,17 +18,18 @@ const { BaseProcess, PromiseWorker } = ChromeUtils.import(
"resource://gre/modules/subprocess/subprocess_common.jsm"
);
/* import-globals-from subprocess_shared.js */
/* import-globals-from subprocess_shared_unix.js */
var obj = {};
Services.scriptloader.loadSubScript(
"resource://gre/modules/subprocess/subprocess_shared.js",
this
obj
);
Services.scriptloader.loadSubScript(
"resource://gre/modules/subprocess/subprocess_shared_unix.js",
this
obj
);
const { SubprocessConstants, libc, LIBC } = obj;
class UnixPromiseWorker extends PromiseWorker {
constructor(...args) {
super(...args);

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

@ -31,17 +31,18 @@ XPCOMUtils.defineLazyServiceGetter(
"nsIEnvironment"
);
/* import-globals-from subprocess_shared.js */
/* import-globals-from subprocess_shared_win.js */
var obj = {};
Services.scriptloader.loadSubScript(
"resource://gre/modules/subprocess/subprocess_shared.js",
this
obj
);
Services.scriptloader.loadSubScript(
"resource://gre/modules/subprocess/subprocess_shared_win.js",
this
obj
);
const { SubprocessConstants, libc, win32 } = obj;
class WinPromiseWorker extends PromiseWorker {
constructor(...args) {
super(...args);

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

@ -137,7 +137,7 @@ class InputPipe extends Pipe {
}
if (read < buffer.byteLength) {
return ArrayBuffer.transfer(buffer, read);
return ArrayBuffer_transfer(buffer, read);
}
return buffer;

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

@ -224,7 +224,7 @@ class InputPipe extends Pipe {
if (read == buffer.byteLength) {
resolve(buffer);
} else {
resolve(ArrayBuffer.transfer(buffer, read));
resolve(ArrayBuffer_transfer(buffer, read));
}
if (this.pending.length) {