scripting: Don't use legacy API

The current scripting module makes heavy use of pre-standardized
iterator/generator/promise APIs, at least for some of those support
was pulled in SpiderMonkey 58.

Port to the new standardized replacements to get the module back into
a working state.

https://gitlab.gnome.org/GNOME/gnome-shell/merge_requests/440
This commit is contained in:
Florian Müllner 2019-02-12 17:11:24 +01:00 committed by Georges Basile Stavracas Neto
parent 0427a782be
commit 611605a67f

View File

@ -37,16 +37,13 @@ const { loadInterfaceXML } = imports.misc.fileUtils;
* 'yield Scripting.sleep(500);' * 'yield Scripting.sleep(500);'
*/ */
function sleep(milliseconds) { function sleep(milliseconds) {
let cb; return new Promise(resolve => {
let id = Mainloop.timeout_add(milliseconds, () => {
let id = Mainloop.timeout_add(milliseconds, () => { resolve();
if (cb) return GLib.SOURCE_REMOVE;
cb(); });
return GLib.SOURCE_REMOVE; GLib.Source.set_name_by_id(id, '[gnome-shell] sleep');
}); });
GLib.Source.set_name_by_id(id, '[gnome-shell] sleep');
return callback => { cb = callback; };
} }
/** /**
@ -57,14 +54,9 @@ function sleep(milliseconds) {
* 'yield Scripting.waitLeisure();' * 'yield Scripting.waitLeisure();'
*/ */
function waitLeisure() { function waitLeisure() {
let cb; return new Promise(resolve => {
global.run_at_leisure(resolve);
global.run_at_leisure(() => {
if (cb)
cb();
}); });
return callback => { cb = callback; };
} }
const PerfHelperIface = loadInterfaceXML('org.gnome.Shell.PerfHelper'); const PerfHelperIface = loadInterfaceXML('org.gnome.Shell.PerfHelper');
@ -82,25 +74,16 @@ function _getPerfHelper() {
} }
function _callRemote(obj, method, ...args) { function _callRemote(obj, method, ...args) {
let cb; return new Promise((resolve, reject) => {
let errcb; args.push((result, excp) => {
if (excp)
reject(excp);
else
resolve();
});
args.push((result, excp) => { method.apply(obj, args);
if (excp) {
if (errcb)
errcb(excp);
} else {
if (cb)
cb();
}
}); });
method.apply(obj, args);
return (callback, error_callback) => {
cb = callback;
errcb = error_callback;
};
} }
/** /**
@ -192,27 +175,6 @@ function collectStatistics() {
Shell.PerfLog.get_default().collect_statistics(); Shell.PerfLog.get_default().collect_statistics();
} }
function _step(g, finish, onError) {
try {
let waitFunction = g.next();
waitFunction(() => {
_step(g, finish, onError);
},
err => {
if (onError)
onError(err);
});
} catch (err) {
if (err instanceof StopIteration) {
if (finish)
finish();
} else {
if (onError)
onError(err);
}
}
}
function _collect(scriptModule, outputFile) { function _collect(scriptModule, outputFile) {
let eventHandlers = {}; let eventHandlers = {};
@ -348,23 +310,23 @@ function _collect(scriptModule, outputFile) {
* After running the script and collecting statistics from the * After running the script and collecting statistics from the
* event log, GNOME Shell will exit. * event log, GNOME Shell will exit.
**/ **/
function runPerfScript(scriptModule, outputFile) { async function runPerfScript(scriptModule, outputFile) {
Shell.PerfLog.get_default().set_enabled(true); Shell.PerfLog.get_default().set_enabled(true);
let g = scriptModule.run(); for (let step of scriptModule.run()) {
try {
await step;
} catch (err) {
log(`Script failed: ${err}\n${err.stack}`);
Meta.exit(Meta.ExitCode.ERROR);
}
}
_step(g, try {
() => { _collect(scriptModule, outputFile);
try { } catch (err) {
_collect(scriptModule, outputFile); log(`Script failed: ${err}\n${err.stack}`);
} catch (err) { Meta.exit(Meta.ExitCode.ERROR);
log("Script failed: " + err + "\n" + err.stack); }
Meta.exit(Meta.ExitCode.ERROR); Meta.exit(Meta.ExitCode.SUCCESS);
}
Meta.exit(Meta.ExitCode.SUCCESS);
},
err => {
log("Script failed: " + err + "\n" + err.stack);
Meta.exit(Meta.ExitCode.ERROR);
});
} }