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