shellDBus: Await Eval() result

The Eval() method currently evaluates the provided string, and
returns the result immediately. This isn't useful when a promise
is returned, which has become much more likely now that accessing
any module requires import().

Simply await the result, to handle both sync and async code.

Part-of: <https://gitlab.gnome.org/GNOME/gnome-shell/-/merge_requests/3021>
This commit is contained in:
Florian Müllner 2023-11-17 18:31:32 +01:00 committed by Marge Bot
parent eb8861e9b8
commit f2601e6888

View File

@ -56,17 +56,22 @@ export class GnomeShell {
* If evaluation fails, then the return value will be * If evaluation fails, then the return value will be
* [false, JSON.stringify(exception)]; * [false, JSON.stringify(exception)];
* *
* @param {string} code A string containing JavaScript code * @async
* @returns {Array} * @param {...any} params - method parameters
* @param {Gio.DBusMethodInvocation} invocation - the invocation
* @returns {void}
*/ */
Eval(code) { async EvalAsync(params, invocation) {
if (!global.context.unsafe_mode) if (!global.context.unsafe_mode) {
return [false, '']; invocation.return_value(new GLib.Variant('(bs)', [false, '']));
return;
}
const [code] = params;
let returnValue; let returnValue;
let success; let success;
try { try {
returnValue = JSON.stringify(eval(code)); returnValue = JSON.stringify(await eval(code));
// A hack; DBus doesn't have null/undefined // A hack; DBus doesn't have null/undefined
if (returnValue === undefined) if (returnValue === undefined)
returnValue = ''; returnValue = '';
@ -75,7 +80,8 @@ export class GnomeShell {
returnValue = `${e}`; returnValue = `${e}`;
success = false; success = false;
} }
return [success, returnValue]; invocation.return_value(
new GLib.Variant('(bs)', [success, returnValue]));
} }
/** /**