mirror of
https://github.com/edu4rdshl/gnome-shell.git
synced 2026-07-17 23:24:51 +00:00
injectionManager: Support overriding vfuncs
Overriding vfuncs can be useful, in particular when we convert to ES modules, and exported symbols cannot easily be swapped out. Adapt overrideMethod() to work correctly in that case as well. Part-of: <https://gitlab.gnome.org/GNOME/gnome-shell/-/merge_requests/2809>
This commit is contained in:
parent
f70a75a905
commit
a332771562
2 changed files with 57 additions and 2 deletions
|
|
@ -1,5 +1,7 @@
|
|||
const JsUnit = imports.jsUnit;
|
||||
|
||||
import GObject from 'gi://GObject';
|
||||
|
||||
import '../../js/ui/environment.js';
|
||||
|
||||
import {InjectionManager} from '../../js/extensions/extension.js';
|
||||
|
|
@ -16,6 +18,23 @@ class Object1 {
|
|||
}
|
||||
}
|
||||
|
||||
class GObject1 extends GObject.Object {
|
||||
static [GObject.properties] = {
|
||||
'plonked': GObject.ParamSpec.boolean(
|
||||
'plonked', '', '',
|
||||
GObject.ParamFlags.READWRITE,
|
||||
false),
|
||||
};
|
||||
|
||||
static {
|
||||
GObject.registerClass(this);
|
||||
}
|
||||
|
||||
plonk() {
|
||||
this.set_property('plonked', true);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {object} object to modify
|
||||
*/
|
||||
|
|
@ -91,3 +110,28 @@ JsUnit.assertEquals(obj.getNumber(), 42);
|
|||
JsUnit.assertEquals(obj.getCount(), obj.count);
|
||||
JsUnit.assert(obj.count > 0);
|
||||
JsUnit.assertRaises(() => obj.getOtherNumber());
|
||||
|
||||
// GObject injections
|
||||
const gobj = new GObject1();
|
||||
let vfuncCalled;
|
||||
|
||||
injectionManager.overrideMethod(
|
||||
GObject1.prototype, 'vfunc_set_property', originalMethod => {
|
||||
return function (...args) {
|
||||
// eslint-disable-next-line no-invalid-this
|
||||
originalMethod.apply(this, args);
|
||||
vfuncCalled = true;
|
||||
};
|
||||
});
|
||||
|
||||
// gobj is now modified
|
||||
vfuncCalled = false;
|
||||
gobj.plonk();
|
||||
JsUnit.assertTrue(vfuncCalled);
|
||||
|
||||
injectionManager.clear();
|
||||
|
||||
// gobj is unmodified again
|
||||
vfuncCalled = false;
|
||||
gobj.plonk();
|
||||
JsUnit.assertFalse(vfuncCalled);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue