addon-sdk/source/lib/sdk/deprecated/cortex.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 "use strict";
michael@0 6
michael@0 7 module.metadata = {
michael@0 8 "stability": "deprecated"
michael@0 9 };
michael@0 10
michael@0 11 // `var` is being used in the module in order to make it reusable in
michael@0 12 // environments in which `let` and `const` is not yet supported.
michael@0 13
michael@0 14 // Returns `object`'s property value, where `name` is a name of the property.
michael@0 15 function get(object, name) {
michael@0 16 return object[name];
michael@0 17 }
michael@0 18
michael@0 19 // Assigns `value` to the `object`'s property, where `name` is the name of the
michael@0 20 // property.
michael@0 21 function set(object, name, value) {
michael@0 22 return object[name] = value;
michael@0 23 }
michael@0 24
michael@0 25 /**
michael@0 26 * Given an `object` containing a property with the given `name`, create
michael@0 27 * a property descriptor that can be used to define alias/proxy properties
michael@0 28 * on other objects. A change in the value of an alias will propagate
michael@0 29 * to the aliased property and vice versa.
michael@0 30 */
michael@0 31 function createAliasProperty(object, name) {
michael@0 32 // Getting own property descriptor of an `object` for the given `name` as
michael@0 33 // we are going to create proxy analog.
michael@0 34 var property = Object.getOwnPropertyDescriptor(object, name);
michael@0 35 var descriptor = {
michael@0 36 configurable: property.configurable,
michael@0 37 enumerable: property.enumerable,
michael@0 38 alias: true
michael@0 39 };
michael@0 40
michael@0 41 // If the original property has a getter and/or setter, bind a
michael@0 42 // corresponding getter/setter in the alias descriptor to the original
michael@0 43 // object, so the `this` object in the getter/setter is the original object
michael@0 44 // rather than the alias.
michael@0 45 if ("get" in property && property.get)
michael@0 46 descriptor.get = property.get.bind(object);
michael@0 47 if ("set" in property && property.set)
michael@0 48 descriptor.set = property.set.bind(object);
michael@0 49
michael@0 50 // If original property was a value property.
michael@0 51 if ("value" in property) {
michael@0 52 // If original property is a method using it's `object` bounded copy.
michael@0 53 if (typeof property.value === "function") {
michael@0 54 descriptor.value = property.value.bind(object);
michael@0 55 // Also preserving writability of the original property.
michael@0 56 descriptor.writable = property.writable;
michael@0 57 }
michael@0 58
michael@0 59 // If the original property was just a data property, we create proxy
michael@0 60 // accessors using our custom get/set functions to propagate changes to the
michael@0 61 // original `object` and vice versa.
michael@0 62 else {
michael@0 63 descriptor.get = get.bind(null, object, name);
michael@0 64 descriptor.set = set.bind(null, object, name);
michael@0 65 }
michael@0 66 }
michael@0 67 return descriptor;
michael@0 68 }
michael@0 69
michael@0 70 // Defines property on `object` object with a name `alias` if given if not
michael@0 71 // defaults to `name` that represents an alias of `source[name]`. If aliased
michael@0 72 // property was an assessor or a method `this` pseudo-variable will be `source`
michael@0 73 // when invoked. If aliased property was a data property changes on any of the
michael@0 74 // aliases will propagate to the `source[name]` and also other way round.
michael@0 75 function defineAlias(source, target, name, alias) {
michael@0 76 return Object.defineProperty(target, alias || name,
michael@0 77 createAliasProperty(source, name));
michael@0 78 }
michael@0 79
michael@0 80 /**
michael@0 81 * Function takes any `object` and returns a proxy for its own public
michael@0 82 * properties. By default properties are considered to be public if they don't
michael@0 83 * start with `"_"`, but default behavior can be overridden if needed, by
michael@0 84 * passing array of public property `names` as a second argument. By default
michael@0 85 * returned object will be direct decedent of the given `object`'s prototype,
michael@0 86 * but this can be overridden by passing third optional argument, that will be
michael@0 87 * used as `prototype` instead.
michael@0 88 * @param {Object} object
michael@0 89 * Object to create cortex for.
michael@0 90 * @param {String[]} [names]
michael@0 91 * Optional array of public property names.
michael@0 92 * @param {Object} [prototype]
michael@0 93 * Optional argument that will be used as `prototype` of the returned object,
michael@0 94 * if not provided `Object.getPrototypeOf(object)` is used instead.
michael@0 95 */
michael@0 96 exports.Cortex = function Cortex(object, names, prototype) {
michael@0 97 // Creating a cortex object from the given `prototype`, if one was not
michael@0 98 // provided then `prototype` of a given `object` is used. This allows
michael@0 99 // consumer to define expected behavior `instanceof`. In common case
michael@0 100 // `prototype` argument can be omitted to preserve same behavior of
michael@0 101 // `instanceof` as on original `object`.
michael@0 102 var cortex = Object.create(prototype || Object.getPrototypeOf(object));
michael@0 103 // Creating alias properties on the `cortex` object for all the own
michael@0 104 // properties of the original `object` that are contained in `names` array.
michael@0 105 // If `names` array is not provided then all the properties that don't
michael@0 106 // start with `"_"` are aliased.
michael@0 107 Object.getOwnPropertyNames(object).forEach(function (name) {
michael@0 108 if ((!names && "_" !== name.charAt(0)) || (names && ~names.indexOf(name)))
michael@0 109 defineAlias(object, cortex, name);
michael@0 110 });
michael@0 111 return cortex;
michael@0 112 }

mercurial