| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | |
| 2 // for details. All rights reserved. Use of this source code is governed by a | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 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'; | |
| 14 | |
| 15 const defineProperty = Object.defineProperty; | |
| 16 const getOwnPropertyDescriptor = Object.getOwnPropertyDescriptor; | |
| 17 const getOwnPropertyNames = Object.getOwnPropertyNames; | |
| 18 const getOwnPropertySymbols = Object.getOwnPropertySymbols; | |
| 19 | |
| 20 const hasOwnProperty = Object.prototype.hasOwnProperty; | |
| 21 | |
| 22 class StrongModeError extends Error { | |
| 23 constructor(message) { | |
| 24 super(message); | |
| 25 } | |
| 26 } | |
| 27 | |
| 28 /** This error indicates a strong mode specific failure. | |
| 29 */ | |
| 30 function throwStrongModeError(message) { | |
| 31 throw new StrongModeError(message); | |
| 32 } | |
| 33 dart_utils.throwStrongModeError = throwStrongModeError; | |
| 34 | |
| 35 /** This error indicates a bug in the runtime or the compiler. | |
| 36 */ | |
| 37 function throwInternalError(message) { | |
| 38 throw Error(message); | |
| 39 } | |
| 40 dart_utils.throwInternalError = throwInternalError; | |
| 41 | |
| 42 function assert(condition) { | |
| 43 if (!condition) throwInternalError("The compiler is broken: failed assert"); | |
| 44 } | |
| 45 dart_utils.assert = assert; | |
| 46 | |
| 47 function getOwnNamesAndSymbols(obj) { | |
| 48 return getOwnPropertyNames(obj).concat(getOwnPropertySymbols(obj)); | |
| 49 } | |
| 50 dart_utils.getOwnNamesAndSymbols = getOwnNamesAndSymbols; | |
| 51 | |
| 52 function safeGetOwnProperty(obj, name) { | |
| 53 let desc = getOwnPropertyDescriptor(obj, name); | |
| 54 if (desc) return desc.value; | |
| 55 } | |
| 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) { | |
| 65 let init = desc.get; | |
| 66 let value = null; | |
| 67 | |
| 68 function lazySetter(x) { | |
| 69 init = null; | |
| 70 value = x; | |
| 71 } | |
| 72 function circularInitError() { | |
| 73 throwInternalError('circular initialization for field ' + name); | |
| 74 } | |
| 75 function lazyGetter() { | |
| 76 if (init == null) return value; | |
| 77 | |
| 78 // Compute and store the value, guarding against reentry. | |
| 79 let f = init; | |
| 80 init = circularInitError; | |
| 81 lazySetter(f()); | |
| 82 return value; | |
| 83 } | |
| 84 desc.get = lazyGetter; | |
| 85 desc.configurable = true; | |
| 86 if (desc.set) desc.set = lazySetter; | |
| 87 return defineProperty(to, name, desc); | |
| 88 } | |
| 89 dart_utils.defineLazyProperty = defineLazyProperty; | |
| 90 | |
| 91 function defineLazy(to, from) { | |
| 92 for (let name of getOwnNamesAndSymbols(from)) { | |
| 93 defineLazyProperty(to, name, getOwnPropertyDescriptor(from, name)); | |
| 94 } | |
| 95 } | |
| 96 dart_utils.defineLazy = defineLazy; | |
| 97 | |
| 98 function defineMemoizedGetter(obj, name, getter) { | |
| 99 return defineLazyProperty(obj, name, {get: getter}); | |
| 100 } | |
| 101 dart_utils.defineMemoizedGetter = defineMemoizedGetter; | |
| 102 | |
| 103 function copyTheseProperties(to, from, names) { | |
| 104 for (let name of names) { | |
| 105 defineProperty(to, name, getOwnPropertyDescriptor(from, name)); | |
| 106 } | |
| 107 return to; | |
| 108 } | |
| 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) { | |
| 116 return copyTheseProperties(to, from, getOwnNamesAndSymbols(from)); | |
| 117 } | |
| 118 dart_utils.copyProperties = copyProperties; | |
| 119 | |
| 120 /** Exports from one Dart module to another. */ | |
| 121 function export_(to, from, show, hide) { | |
| 122 if (show == void 0) { | |
| 123 show = getOwnNamesAndSymbols(from); | |
| 124 } | |
| 125 if (hide != void 0) { | |
| 126 var hideMap = new Set(hide); | |
| 127 show = show.filter((k) => !hideMap.has(k)); | |
| 128 } | |
| 129 return copyTheseProperties(to, from, show); | |
| 130 } | |
| 131 dart_utils.export = export_; | |
| 132 | |
| 133 })(dart_utils); | |
| OLD | NEW |