extensionUtils: Stop using RegExp for stack parsing
We currently use a regular expression to extract the exact file path from a stack line. That RE is no longer accurate: - we assume a line number at the end, but at one point the column number was added as well - stacks from ES modules use file:// URIs instead of plain paths Luckily that doesn't matter: We don't want to access any actual files, so all we need is a path that can be traversed and that contains the UUID. We can get that with simple string manipulation, so avoid the regex overhead. Part-of: <https://gitlab.gnome.org/GNOME/gnome-shell/-/merge_requests/2832>
This commit is contained in:
parent
c2c4d84fc1
commit
3289b79433
@ -77,35 +77,28 @@ function getCurrentExtension() {
|
|||||||
if (_extension)
|
if (_extension)
|
||||||
return _extension;
|
return _extension;
|
||||||
|
|
||||||
let stack = new Error().stack.split('\n');
|
const basePath = '/gnome-shell/extensions/';
|
||||||
let extensionStackLine;
|
|
||||||
|
|
||||||
// Search for an occurrence of an extension stack frame
|
// Search for an occurrence of an extension stack frame
|
||||||
// Start at 1 because 0 is the stack frame of this function
|
// Start at 1 because 0 is the stack frame of this function
|
||||||
for (let i = 1; i < stack.length; i++) {
|
const [, ...stack] = new Error().stack.split('\n');
|
||||||
if (stack[i].includes('/gnome-shell/extensions/')) {
|
const extensionLine = stack.find(
|
||||||
extensionStackLine = stack[i];
|
line => line.includes(basePath));
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (!extensionStackLine)
|
|
||||||
return null;
|
|
||||||
|
|
||||||
// The stack line is like:
|
if (!extensionLine)
|
||||||
// init([object Object])@/home/user/data/gnome-shell/extensions/u@u.id/prefs.js:8
|
|
||||||
//
|
|
||||||
// In the case that we're importing from
|
|
||||||
// module scope, the first field is blank:
|
|
||||||
// @/home/user/data/gnome-shell/extensions/u@u.id/prefs.js:8
|
|
||||||
let match = new RegExp('@(.+):\\d+').exec(extensionStackLine);
|
|
||||||
if (!match)
|
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
// local import, as the module is used from outside the gnome-shell process
|
// local import, as the module is used from outside the gnome-shell process
|
||||||
// as well (not this function though)
|
// as well (not this function though)
|
||||||
let extensionManager = imports.ui.main.extensionManager;
|
let extensionManager = imports.ui.main.extensionManager;
|
||||||
|
|
||||||
let path = match[1];
|
// The exact stack line differs depending on where the function
|
||||||
|
// was called (function or module scope), and whether it's called
|
||||||
|
// from a module or legacy import (file:// URI vs. plain path).
|
||||||
|
//
|
||||||
|
// We don't have to care about the exact composition, all we need
|
||||||
|
// is a string that can be traversed as path and contains the UUID
|
||||||
|
const path = extensionLine.slice(extensionLine.indexOf(basePath));
|
||||||
let file = Gio.File.new_for_path(path);
|
let file = Gio.File.new_for_path(path);
|
||||||
|
|
||||||
// Walk up the directory tree, looking for an extension with
|
// Walk up the directory tree, looking for an extension with
|
||||||
|
Loading…
Reference in New Issue
Block a user