6d79d99ee09e296ba9391141952a33eb1be07f98
[WebKit-https.git] / PerformanceTests / Speedometer / resources / todomvc / dependency-examples / flight / bower_components / flight / lib / compose.js
1 // ==========================================
2 // Copyright 2013 Twitter, Inc
3 // Licensed under The MIT License
4 // http://opensource.org/licenses/MIT
5 // ==========================================
6
7 define(
8
9   [
10     './utils',
11     './debug'
12   ],
13
14   function(utils, debug) {
15     'use strict';
16
17     //enumerables are shims - getOwnPropertyDescriptor shim doesn't work
18     var canWriteProtect = debug.enabled && !utils.isEnumerable(Object, 'getOwnPropertyDescriptor');
19     //whitelist of unlockable property names
20     var dontLock = ['mixedIn'];
21
22     if (canWriteProtect) {
23       //IE8 getOwnPropertyDescriptor is built-in but throws exeption on non DOM objects
24       try {
25         Object.getOwnPropertyDescriptor(Object, 'keys');
26       } catch(e) {
27         canWriteProtect = false;
28       }
29     }
30
31     function setPropertyWritability(obj, isWritable) {
32       if (!canWriteProtect) {
33         return;
34       }
35
36       var props = Object.create(null);
37
38       Object.keys(obj).forEach(
39         function (key) {
40           if (dontLock.indexOf(key) < 0) {
41             var desc = Object.getOwnPropertyDescriptor(obj, key);
42             desc.writable = isWritable;
43             props[key] = desc;
44           }
45         }
46       );
47
48       Object.defineProperties(obj, props);
49     }
50
51     function unlockProperty(obj, prop, op) {
52       var writable;
53
54       if (!canWriteProtect || !obj.hasOwnProperty(prop)) {
55         op.call(obj);
56         return;
57       }
58
59       writable = Object.getOwnPropertyDescriptor(obj, prop).writable;
60       Object.defineProperty(obj, prop, { writable: true });
61       op.call(obj);
62       Object.defineProperty(obj, prop, { writable: writable });
63     }
64
65     function mixin(base, mixins) {
66       base.mixedIn = base.hasOwnProperty('mixedIn') ? base.mixedIn : [];
67
68       mixins.forEach(function(mixin) {
69         if (base.mixedIn.indexOf(mixin) == -1) {
70           setPropertyWritability(base, false);
71           mixin.call(base);
72           base.mixedIn.push(mixin);
73         }
74       });
75
76       setPropertyWritability(base, true);
77     }
78
79     return {
80       mixin: mixin,
81       unlockProperty: unlockProperty
82     };
83
84   }
85 );