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 (function (dart_utils) { |
| 11 'use strict'; |
| 12 |
| 13 const defineProperty = Object.defineProperty; |
| 14 const getOwnPropertyDescriptor = Object.getOwnPropertyDescriptor; |
| 15 const getOwnPropertyNames = Object.getOwnPropertyNames; |
| 16 const getOwnPropertySymbols = Object.getOwnPropertySymbols; |
| 17 |
| 18 const hasOwnProperty = Object.prototype.hasOwnProperty; |
| 19 |
| 20 const slice = [].slice; |
| 21 |
| 22 |
| 23 /** This error indicates a bug in the runtime or the compiler. |
| 24 */ |
| 25 function throwError(message) { |
| 26 throw Error(message); |
| 27 } |
| 28 dart_utils.throwError = throwError; |
| 29 |
| 30 function assert(condition) { |
| 31 if (!condition) throwError("The compiler is broken: failed assert"); |
| 32 } |
| 33 dart_utils.assert = assert; |
| 34 |
| 35 function getOwnNamesAndSymbols(obj) { |
| 36 return getOwnPropertyNames(obj).concat(getOwnPropertySymbols(obj)); |
| 37 } |
| 38 dart_utils.getOwnNamesAndSymbols = getOwnNamesAndSymbols; |
| 39 |
| 40 function safeGetOwnProperty(obj, name) { |
| 41 let desc = getOwnPropertyDescriptor(obj, name); |
| 42 if (desc) return desc.value; |
| 43 } |
| 44 dart_utils.safeGetOwnProperty = safeGetOwnProperty; |
| 45 |
| 46 /** |
| 47 * Defines a lazy property. |
| 48 * After initial get or set, it will replace itself with a value property. |
| 49 */ |
| 50 // TODO(jmesserly): is this the best implementation for JS engines? |
| 51 // TODO(jmesserly): reusing descriptor objects has been shown to improve |
| 52 // performance in other projects (e.g. webcomponents.js ShadowDOM polyfill). |
| 53 function defineLazyProperty(to, name, desc) { |
| 54 let init = desc.get; |
| 55 let writable = !!desc.set; |
| 56 function lazySetter(value) { |
| 57 defineProperty(to, name, { value: value, writable: writable }); |
| 58 } |
| 59 function lazyGetter() { |
| 60 // Clear the init function to detect circular initialization. |
| 61 let f = init; |
| 62 if (f === null) { |
| 63 throwError('circular initialization for field ' + name); |
| 64 } |
| 65 init = null; |
| 66 |
| 67 // Compute and store the value. |
| 68 let value = f(); |
| 69 lazySetter(value); |
| 70 return value; |
| 71 } |
| 72 desc.get = lazyGetter; |
| 73 desc.configurable = true; |
| 74 if (writable) desc.set = lazySetter; |
| 75 defineProperty(to, name, desc); |
| 76 } |
| 77 dart_utils.defineLazyProperty = defineLazyProperty; |
| 78 |
| 79 function defineLazy(to, from) { |
| 80 for (let name of getOwnNamesAndSymbols(from)) { |
| 81 defineLazyProperty(to, name, getOwnPropertyDescriptor(from, name)); |
| 82 } |
| 83 } |
| 84 dart_utils.defineLazy = defineLazy; |
| 85 |
| 86 function defineMemoizedGetter(obj, name, get) { |
| 87 let cache = null; |
| 88 function getter() { |
| 89 if (cache != null) return cache; |
| 90 cache = get(); |
| 91 get = null; |
| 92 return cache; |
| 93 } |
| 94 defineProperty(obj, name, {get: getter, configurable: true}); |
| 95 } |
| 96 dart_utils.defineMemoizedGetter = defineMemoizedGetter; |
| 97 |
| 98 function copyTheseProperties(to, from, names) { |
| 99 for (let name of names) { |
| 100 defineProperty(to, name, getOwnPropertyDescriptor(from, name)); |
| 101 } |
| 102 return to; |
| 103 } |
| 104 dart_utils.copyTheseProperties = copyTheseProperties; |
| 105 |
| 106 /** |
| 107 * Copy properties from source to destination object. |
| 108 * This operation is commonly called `mixin` in JS. |
| 109 */ |
| 110 function copyProperties(to, from) { |
| 111 return copyTheseProperties(to, from, getOwnNamesAndSymbols(from)); |
| 112 } |
| 113 dart_utils.copyProperties = copyProperties; |
| 114 |
| 115 })(dart_utils || (dart_utils = {})); |
OLD | NEW |