OLD | NEW |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 dart_library.library('dart/_utils', null, /* Imports */[ |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 ], /* Lazy imports */[ |
3 // BSD-style license that can be found in the LICENSE file. | 3 ], function(exports, dart) { |
4 | |
5 /* This library defines a set of general javascript utilities for us | |
6 * by the Dart runtime. | |
7 */ | |
8 | |
9 var dart_utils = | |
10 typeof module != "undefined" && module.exports || {}; | |
11 | |
12 (function (dart_utils) { | |
13 'use strict'; | 4 'use strict'; |
14 | |
15 const defineProperty = Object.defineProperty; | 5 const defineProperty = Object.defineProperty; |
16 const getOwnPropertyDescriptor = Object.getOwnPropertyDescriptor; | 6 const getOwnPropertyDescriptor = Object.getOwnPropertyDescriptor; |
17 const getOwnPropertyNames = Object.getOwnPropertyNames; | 7 const getOwnPropertyNames = Object.getOwnPropertyNames; |
18 const getOwnPropertySymbols = Object.getOwnPropertySymbols; | 8 const getOwnPropertySymbols = Object.getOwnPropertySymbols; |
19 | |
20 const hasOwnProperty = Object.prototype.hasOwnProperty; | 9 const hasOwnProperty = Object.prototype.hasOwnProperty; |
21 | 10 const StrongModeError = (function() { |
22 class StrongModeError extends Error { | 11 function StrongModeError(message) { |
23 constructor(message) { | 12 Error.call(this); |
24 super(message); | 13 this.message = message; |
25 } | 14 } |
26 } | 15 ; |
27 | 16 Object.setPrototypeOf(StrongModeError.prototype, Error.prototype); |
28 /** This error indicates a strong mode specific failure. | 17 return StrongModeError; |
29 */ | 18 })(); |
30 function throwStrongModeError(message) { | 19 function throwStrongModeError(message) { |
31 throw new StrongModeError(message); | 20 throw new StrongModeError(message); |
32 } | 21 } |
33 dart_utils.throwStrongModeError = throwStrongModeError; | |
34 | |
35 /** This error indicates a bug in the runtime or the compiler. | |
36 */ | |
37 function throwInternalError(message) { | 22 function throwInternalError(message) { |
38 throw Error(message); | 23 throw Error(message); |
39 } | 24 } |
40 dart_utils.throwInternalError = throwInternalError; | 25 function assert_(condition) { |
41 | 26 if (!condition) |
42 function assert(condition) { | 27 throwInternalError("The compiler is broken: failed assert"); |
43 if (!condition) throwInternalError("The compiler is broken: failed assert"); | |
44 } | 28 } |
45 dart_utils.assert = assert; | |
46 | |
47 function getOwnNamesAndSymbols(obj) { | 29 function getOwnNamesAndSymbols(obj) { |
48 return getOwnPropertyNames(obj).concat(getOwnPropertySymbols(obj)); | 30 return getOwnPropertyNames(obj).concat(getOwnPropertySymbols(obj)); |
49 } | 31 } |
50 dart_utils.getOwnNamesAndSymbols = getOwnNamesAndSymbols; | |
51 | |
52 function safeGetOwnProperty(obj, name) { | 32 function safeGetOwnProperty(obj, name) { |
53 let desc = getOwnPropertyDescriptor(obj, name); | 33 let desc = getOwnPropertyDescriptor(obj, name); |
54 if (desc) return desc.value; | 34 if (desc) |
| 35 return desc.value; |
55 } | 36 } |
56 dart_utils.safeGetOwnProperty = safeGetOwnProperty; | |
57 | |
58 /** | |
59 * Defines a lazy property. | |
60 * After initial get or set, it will replace itself with a value property. | |
61 */ | |
62 // TODO(jmesserly): reusing descriptor objects has been shown to improve | |
63 // performance in other projects (e.g. webcomponents.js ShadowDOM polyfill). | |
64 function defineLazyProperty(to, name, desc) { | 37 function defineLazyProperty(to, name, desc) { |
65 let init = desc.get; | 38 let init = desc.get; |
66 let value = null; | 39 let value = null; |
67 | |
68 function lazySetter(x) { | 40 function lazySetter(x) { |
69 init = null; | 41 init = null; |
70 value = x; | 42 value = x; |
71 } | 43 } |
72 function circularInitError() { | 44 function circularInitError() { |
73 throwInternalError('circular initialization for field ' + name); | 45 throwInternalError('circular initialization for field ' + name); |
74 } | 46 } |
75 function lazyGetter() { | 47 function lazyGetter() { |
76 if (init == null) return value; | 48 if (init == null) |
77 | 49 return value; |
78 // Compute and store the value, guarding against reentry. | |
79 let f = init; | 50 let f = init; |
80 init = circularInitError; | 51 init = circularInitError; |
81 lazySetter(f()); | 52 lazySetter(f()); |
82 return value; | 53 return value; |
83 } | 54 } |
84 desc.get = lazyGetter; | 55 desc.get = lazyGetter; |
85 desc.configurable = true; | 56 desc.configurable = true; |
86 if (desc.set) desc.set = lazySetter; | 57 if (desc.set) |
| 58 desc.set = lazySetter; |
87 return defineProperty(to, name, desc); | 59 return defineProperty(to, name, desc); |
88 } | 60 } |
89 dart_utils.defineLazyProperty = defineLazyProperty; | |
90 | |
91 function defineLazy(to, from) { | 61 function defineLazy(to, from) { |
92 for (let name of getOwnNamesAndSymbols(from)) { | 62 for (let name of getOwnNamesAndSymbols(from)) { |
93 defineLazyProperty(to, name, getOwnPropertyDescriptor(from, name)); | 63 defineLazyProperty(to, name, getOwnPropertyDescriptor(from, name)); |
94 } | 64 } |
95 } | 65 } |
96 dart_utils.defineLazy = defineLazy; | |
97 | |
98 function defineMemoizedGetter(obj, name, getter) { | 66 function defineMemoizedGetter(obj, name, getter) { |
99 return defineLazyProperty(obj, name, {get: getter}); | 67 return defineLazyProperty(obj, name, {get: getter}); |
100 } | 68 } |
101 dart_utils.defineMemoizedGetter = defineMemoizedGetter; | |
102 | |
103 function copyTheseProperties(to, from, names) { | 69 function copyTheseProperties(to, from, names) { |
104 for (let name of names) { | 70 for (let name of names) { |
105 defineProperty(to, name, getOwnPropertyDescriptor(from, name)); | 71 defineProperty(to, name, getOwnPropertyDescriptor(from, name)); |
106 } | 72 } |
107 return to; | 73 return to; |
108 } | 74 } |
109 dart_utils.copyTheseProperties = copyTheseProperties; | |
110 | |
111 /** | |
112 * Copy properties from source to destination object. | |
113 * This operation is commonly called `mixin` in JS. | |
114 */ | |
115 function copyProperties(to, from) { | 75 function copyProperties(to, from) { |
116 return copyTheseProperties(to, from, getOwnNamesAndSymbols(from)); | 76 return copyTheseProperties(to, from, getOwnNamesAndSymbols(from)); |
117 } | 77 } |
118 dart_utils.copyProperties = copyProperties; | |
119 | |
120 /** Exports from one Dart module to another. */ | |
121 function export_(to, from, show, hide) { | 78 function export_(to, from, show, hide) { |
122 if (show == void 0) { | 79 if (show == void 0) { |
123 show = getOwnNamesAndSymbols(from); | 80 show = getOwnNamesAndSymbols(from); |
124 } | 81 } |
125 if (hide != void 0) { | 82 if (hide != void 0) { |
126 var hideMap = new Set(hide); | 83 var hideMap = new Set(hide); |
127 show = show.filter((k) => !hideMap.has(k)); | 84 show = show.filter(k => !hideMap.has(k)); |
128 } | 85 } |
129 return copyTheseProperties(to, from, show); | 86 return copyTheseProperties(to, from, show); |
130 } | 87 } |
131 dart_utils.export = export_; | 88 // Exports: |
132 | 89 exports.defineProperty = defineProperty; |
133 })(dart_utils); | 90 exports.getOwnPropertyDescriptor = getOwnPropertyDescriptor; |
| 91 exports.getOwnPropertyNames = getOwnPropertyNames; |
| 92 exports.getOwnPropertySymbols = getOwnPropertySymbols; |
| 93 exports.hasOwnProperty = hasOwnProperty; |
| 94 exports.StrongModeError = StrongModeError; |
| 95 exports.throwStrongModeError = throwStrongModeError; |
| 96 exports.throwInternalError = throwInternalError; |
| 97 exports.assert_ = assert_; |
| 98 exports.getOwnNamesAndSymbols = getOwnNamesAndSymbols; |
| 99 exports.safeGetOwnProperty = safeGetOwnProperty; |
| 100 exports.defineLazyProperty = defineLazyProperty; |
| 101 exports.defineLazy = defineLazy; |
| 102 exports.defineMemoizedGetter = defineMemoizedGetter; |
| 103 exports.copyTheseProperties = copyTheseProperties; |
| 104 exports.copyProperties = copyProperties; |
| 105 exports.export_ = export_; |
| 106 }); |
OLD | NEW |