1
0
mirror of https://github.com/gryf/pentadactyl-pm.git synced 2025-12-27 04:22:26 +01:00

Use Object.defineProperty instead of __defineGetter__/__defineSetter__

In Firefox 48+, the __defineSetter__/__defineSetter__ is deprecated,
so use Object.defineProperty instead.
This commit is contained in:
Zheng Chaoping
2016-05-04 20:00:59 +08:00
parent 1a4290d92a
commit 195ea78efb
20 changed files with 291 additions and 146 deletions

View File

@@ -26,8 +26,7 @@ try {
catch (e) {}
let objproto = Object.prototype;
var { __lookupGetter__, __lookupSetter__, __defineGetter__, __defineSetter__,
hasOwnProperty, propertyIsEnumerable } = objproto;
var { hasOwnProperty, propertyIsEnumerable } = objproto;
var hasOwnProp = Function.call.bind(hasOwnProperty);
@@ -780,7 +779,7 @@ function memoize(obj, key, getter) {
if (arguments.length == 1) {
let res = update(Object.create(obj), obj);
for (let prop of Object.getOwnPropertyNames(obj)) {
let get = __lookupGetter__.call(obj, prop);
let get = Object.getOwnPropertyDescriptor(obj, prop).get;
if (get)
memoize(res, prop, get);
}
@@ -847,8 +846,12 @@ function update(target) {
let func = desc.value.wrapped || desc.value;
if (!func.superapply) {
func.__defineGetter__("super", function get_super() {
return Object.getPrototypeOf(target)[k];
Object.defineProperty(func, "super", {
get: function get_super() {
return Object.getPrototypeOf(target)[k];
},
enumerable: true,
configurable: true
});
func.superapply = function superapply(self, args) {
@@ -1169,7 +1172,11 @@ Class.prototype = {
if (typeof desc.value === "function") {
let func = desc.value.wrapped || desc.value;
if (!func.superapply) {
func.__defineGetter__("super", () => Object.getPrototypeOf(this)[k]);
Object.defineProperty(func, "super", {
get: () => Object.getPrototypeOf(this)[k],
enumerable: true,
configurable: true
});
func.superapply = function superapply(self, args) {
let meth = Object.getPrototypeOf(self)[k];
@@ -1403,11 +1410,15 @@ function Struct(...args) {
members: Ary.toObject(args.map((v, k) => [v, k])),
});
args.forEach(function (name, i) {
Struct.prototype.__defineGetter__(name, function () {
return this[i];
});
Struct.prototype.__defineSetter__(name, function (val) {
this[i] = val;
Object.defineProperty(Struct.prototype, name, {
get() {
return this[i];
},
set(val) {
this[i] = val;
},
enumerable: true,
configurable: true
});
});
return Struct;
@@ -1467,11 +1478,15 @@ var StructBase = Class("StructBase", Array, {
*/
defaultValue: function defaultValue(key, val) {
let i = this.prototype.members[key];
this.prototype.__defineGetter__(i, function () {
return this[i] = val.call(this);
});
this.prototype.__defineSetter__(i, function (value) {
return Class.replaceProperty(this, i, value);
Object.defineProperty(this.prototype, i, {
get() {
return this[i] = val.call(this);
},
set(value) {
Class.replaceProperty(this, i, value);
},
enumerable: true,
configurable: true
});
return this;
},

View File

@@ -61,7 +61,13 @@ update(Bookmark.prototype, {
}
});
Bookmark.prototype.members.uri = Bookmark.prototype.members.url;
Bookmark.setter = function (key, func) { return this.prototype.__defineSetter__(key, func); };
Bookmark.setter = function (key, func) {
return Object.defineProperty(this.prototype, key, {
set: func,
enumerable: true,
configurable: true
});
};
Bookmark.setter("url", function (val) { this.uri = isString(val) ? newURI(val) : val; });
Bookmark.setter("title", function (val) { services.bookmarks.setItemTitle(this.id, val); });
Bookmark.setter("post", function (val) { bookmarkcache.annotate(this.id, bookmarkcache.POST, val); });

View File

@@ -62,8 +62,12 @@ var CompletionContext = Class("CompletionContext", {
["anchored", "compare", "editor", "_filter", "filterFunc", "forceAnchored", "top"]
.forEach(key => self[key] = parent[key]);
self.__defineGetter__("value", function get_value() {
return this.top.value;
Object.defineProperty(self, "value", {
get() {
return this.top.value;
},
enumerable: true,
configurable: true
});
self.offset = parent.offset;
@@ -90,11 +94,15 @@ var CompletionContext = Class("CompletionContext", {
if (self != this)
return self;
["_caret", "contextList", "maxItems", "onUpdate", "selectionTypes", "tabPressed", "updateAsync", "value"].forEach(function fe(key) {
self.__defineGetter__(key, function () {
return this.top[key];
});
self.__defineSetter__(key, function (val) {
this.top[key] = val;
Object.defineProperty(self, key, {
get() {
return this.top[key];
},
set(val) {
this.top[key] = val;
},
enumerable: true,
configurable: true
});
});
}
@@ -175,19 +183,29 @@ var CompletionContext = Class("CompletionContext", {
* @property {CompletionContext} The top-level completion context.
*/
this.top = this;
this.__defineGetter__("incomplete", function get_incomplete() {
return this._incomplete ||
this.contextList.some(c => c.parent && c.incomplete);
});
this.__defineGetter__("waitingForTab", function get_waitingForTab() {
return this._waitingForTab ||
this.contextList.some(c => c.parent && c.waitingForTab);
});
this.__defineSetter__("incomplete", function get_incomplete(val) {
this._incomplete = val;
});
this.__defineSetter__("waitingForTab", function get_waitingForTab(val) {
this._waitingForTab = val;
Object.defineProperties(this, {
"incomplete": {
get() {
return this._incomplete ||
this.contextList.some(c => c.parent && c.incomplete);
},
set(val) {
this._incomplete = val;
},
enumerable: true,
configurable: true
},
"waitingForTab": {
get() {
return this._waitingForTab ||
this.contextList.some(c => c.parent && c.waitingForTab);
},
set(val) {
this._waitingForTab = val;
},
enumerable: true,
configurable: true
}
});
this.reset();
}
@@ -432,17 +450,22 @@ var CompletionContext = Class("CompletionContext", {
// This is only allowed to be a simple accessor, and shouldn't
// reference any variables. Don't bother with eval context.
v = Function("i", "return i" + v);
let descriptor = {
enumerable: true,
configurable: true
};
if (typeof v == "function")
res.__defineGetter__(k, function p_gf() {
descriptor.get = function p_gf() {
return Class.replaceProperty(this, k, v.call(this, this.item, self));
});
};
else
res.__defineGetter__(k, function p_gp() {
descriptor.get = function p_gp() {
return Class.replaceProperty(this, k, this.item[v]);
});
res.__defineSetter__(k, function p_s(val) {
};
descriptor.set = function p_s(val) {
Class.replaceProperty(this, k, val);
});
};
Object.defineProperty(res, k, descriptor);
}
return res;
},
@@ -811,8 +834,12 @@ var CompletionContext = Class("CompletionContext", {
split: function split(name, obj, fn, ...args) {
let context = this.fork(name);
let alias = prop => {
context.__defineGetter__(prop, () => this[prop]);
context.__defineSetter__(prop, val => this[prop] = val);
Object.defineProperty(context, prop, {
get: () => this[prop],
set: val => this[prop] = val,
enumerable: true,
configurable: true,
});
};
alias("_cache");
alias("_completions");

View File

@@ -459,8 +459,12 @@ var Contexts = Module("contexts", {
group = this.Group(name, description, filter, persist);
this.groupList.unshift(group);
this.groupMap[name] = group;
this.hiveProto.__defineGetter__(name, function () {
return group[this._hive];
Object.defineProperty(this.hiveProto, name, {
get() {
return group[this._hive];
},
enumerable: true,
configurable: true
});
}

View File

@@ -17,27 +17,31 @@ var Highlight = Struct("class", "selector", "sites",
"value", "extends", "agent",
"base", "baseClass", "style");
Highlight.liveProperty = function (name, prop) {
this.prototype.__defineGetter__(name, function () {
return this.get(name);
});
this.prototype.__defineSetter__(name, function (val) {
if (isObject(val) && name !== "style") {
if (isArray(val))
val = Array.slice(val);
else
val = update({}, val);
Object.freeze(val);
}
this.set(name, val);
Object.defineProperty(this.prototype, name, {
get() {
return this.get(name);
},
set(val) {
if (isObject(val) && name !== "style") {
if (isArray(val))
val = Array.slice(val);
else
val = update({}, val);
Object.freeze(val);
}
this.set(name, val);
if (name === "value" || name === "extends")
for (let h of highlight)
if (h.extends.indexOf(this.class) >= 0)
h.style.css = h.css;
if (name === "value" || name === "extends")
for (let h of highlight)
if (h.extends.indexOf(this.class) >= 0)
h.style.css = h.css;
this.style[prop || name] = this[prop || name];
if (this.onChange)
this.onChange();
this.style[prop || name] = this[prop || name];
if (this.onChange)
this.onChange();
},
enumerable: true,
configurable: true,
});
};
Highlight.liveProperty("agent");
@@ -155,19 +159,23 @@ var Highlights = Module("Highlight", {
if (!old && obj.base && obj.base.style.enabled)
obj.style.enabled = true;
else
this.loaded.__defineSetter__(obj.class, function () {
Object.defineProperty(this, obj.class, {
value: true,
configurable: true,
enumerable: true,
writable: true
});
Object.defineProperty(this.loaded, obj.class, {
set() {
Object.defineProperty(this, obj.class, {
value: true,
configurable: true,
enumerable: true,
writable: true
});
if (obj.class === obj.baseClass)
for (let h of highlight)
if (h.baseClass === obj.class)
this[h.class] = true;
obj.style.enabled = true;
if (obj.class === obj.baseClass)
for (let h of highlight)
if (h.baseClass === obj.class)
this[h.class] = true;
obj.style.enabled = true;
},
enumerable: true,
configurable: true
});
return obj;
},

View File

@@ -886,8 +886,11 @@ unlet s:cpo_save
sep = sep || " ";
let width = 0;
let lines = [];
lines.__defineGetter__("last", function () {
return this[this.length - 1];
Object.defineProperty(lines, "last", {
get() {
return this[this.length - 1];
},
configurable: true
});
for (let item of values(items.array || items)) {

View File

@@ -253,15 +253,19 @@ overlay.overlayWindow(Object.keys(config.overlays),
this.initDependencies(className);
}
else
modules.__defineGetter__(className, () => {
let module = modules.jsmodules[className];
Class.replaceProperty(modules, className, module);
if (module.reallyInit)
module.reallyInit(); // :(
Object.defineProperty(modules, className, {
get: () => {
let module = modules.jsmodules[className];
Class.replaceProperty(modules, className, module);
if (module.reallyInit)
module.reallyInit(); // :(
if (!module.lazyDepends)
self.initDependencies(className);
return module;
if (!module.lazyDepends)
this.initDependencies(className);
return module;
},
enumerable: true,
configurable: true,
});
}, this);
},
@@ -380,9 +384,12 @@ overlay.overlayWindow(Object.keys(config.overlays),
Module.list.forEach(mod => {
if (!mod.frobbed) {
modules.__defineGetter__(mod.className, () => {
delete modules[mod.className];
return this.loadModule(mod.className, null, Components.stack.caller);
Object.defineProperty(modules, mod.className, {
get: () => {
return this.loadModule(mod.className, null, Components.stack.caller);
},
enumerable: true,
configurable: true
});
Object.keys(mod.prototype.INIT)
.forEach(name => { this.deferInit(name, mod.prototype.INIT, mod); });

View File

@@ -1060,11 +1060,15 @@ var Options = Module("options", {
memoize(this._options, this._options.length, closure);
// quickly access options with options["wildmode"]:
this.__defineGetter__(name, function () {
return this._optionMap[name].value;
});
this.__defineSetter__(name, function (value) {
this._optionMap[name].value = value;
Object.defineProperty(this, name, {
get() {
return this._optionMap[name].value;
},
set(value) {
this._optionMap[name].value = value;
},
enumerable: true,
configurable: true
});
}
};

View File

@@ -160,7 +160,7 @@ var Services = Module("Services", {
add: function add(name, class_, ifaces, meth) {
const self = this;
this.services[name] = { method: meth, class: class_, interfaces: Array.concat(ifaces || []) };
if (name in this && ifaces && !this.__lookupGetter__(name) && !(this[name] instanceof Ci.nsISupports))
if (name in this && ifaces && !Object.getOwnPropertyDescriptor(this, name).get && !(this[name] instanceof Ci.nsISupports))
throw TypeError();
memoize(this, name, () => self._create(name));
},

View File

@@ -31,8 +31,20 @@ var StoreBase = Class("StoreBase", {
this._load = load;
this._options = options;
this.__defineGetter__("store", () => store);
this.__defineGetter__("name", () => name);
Object.defineProperties(this, {
"store": {
value: store,
writable: false,
enumerable: true,
configurable: true,
},
"name": {
value: name,
writable: false,
enumerable: true,
configurable: true,
}
});
for (let [k, v] of iter(options))
if (this.OPTIONS.indexOf(k) >= 0)
this[k] = v;
@@ -301,8 +313,12 @@ var Storage = Module("Storage", {
this.keys[key] = new constructor(key, params.store, load, params);
this.keys[key].timer = new Timer(1000, 10000, () => this.save(key));
this.__defineGetter__(key, function () {
return this.keys[key];
Object.defineProperty(this, key, {
get() {
return this.keys[key];
},
enumerable: true,
configurable: true,
});
}
return this.keys[key];

View File

@@ -19,14 +19,20 @@ var namespace = "@namespace html " + JSON.stringify(XHTML) + ";\n" +
var Sheet = Struct("name", "id", "sites", "css", "hive", "agent");
Sheet.liveProperty = function (name) {
let i = this.prototype.members[name];
this.prototype.__defineGetter__(name, function () { return this[i]; });
this.prototype.__defineSetter__(name, function (val) {
if (isArray(val))
val = Array.slice(val);
if (isArray(val))
Object.freeze(val);
this[i] = val;
this.enabled = this.enabled;
Object.defineProperty(this.prototype, name, {
get() {
return this[i];
},
set(val) {
if (isArray(val))
val = Array.slice(val);
if (isArray(val))
Object.freeze(val);
this[i] = val;
this.enabled = this.enabled;
},
enumerable: true,
configurable: true
});
};
Sheet.liveProperty("agent");

View File

@@ -283,8 +283,11 @@ var Util = Module("Util", XPCOM([Ci.nsIObserver, Ci.nsISupportsWeakReference]),
compileFormat: function compileFormat(format) {
let stack = [frame()];
stack.__defineGetter__("top", function () {
return this[this.length - 1];
Object.defineProperty(stack, "top", {
get() {
return this[this.length - 1];
},
configurable: true
});
function frame() {
@@ -370,8 +373,11 @@ var Util = Module("Util", XPCOM([Ci.nsIObserver, Ci.nsISupportsWeakReference]),
*/
compileMacro: function compileMacro(macro, keepUnknown) {
let stack = [frame()];
stack.__defineGetter__("top", function () {
return this[this.length - 1];
Object.defineProperty(stack, "top", {
get() {
return this[this.length - 1];
},
configurable: true,
});
let unknown = identity;