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 library dart._utils; |
| 6 |
| 7 import 'dart:_foreign_helper' show JS; |
| 8 |
| 9 /// This library defines a set of general javascript utilities for us |
| 10 /// by the Dart runtime. |
| 11 /// TODO(ochafik): Rewrite some of these in Dart when possible. |
| 12 |
| 13 const defineProperty = JS('', 'Object.defineProperty'); |
| 14 const getOwnPropertyDescriptor = JS('', 'Object.getOwnPropertyDescriptor'); |
| 15 const getOwnPropertyNames = JS('', 'Object.getOwnPropertyNames'); |
| 16 const getOwnPropertySymbols = JS('', 'Object.getOwnPropertySymbols'); |
| 17 |
| 18 const hasOwnProperty = JS('', 'Object.prototype.hasOwnProperty'); |
| 19 |
| 20 // TODO(ochafik): Add ES6 class syntax support to JS intrinsics to avoid this. |
| 21 const StrongModeError = JS('', '''(function() { |
| 22 function StrongModeError(message) { |
| 23 Error.call(this); |
| 24 this.message = message; |
| 25 }; |
| 26 Object.setPrototypeOf(StrongModeError.prototype, Error.prototype); |
| 27 return StrongModeError; |
| 28 })()'''); |
| 29 |
| 30 /// This error indicates a strong mode specific failure. |
| 31 void throwStrongModeError(String message) => JS('', '''((message) => { |
| 32 throw new StrongModeError(message); |
| 33 })(#)''', message); |
| 34 |
| 35 /// This error indicates a bug in the runtime or the compiler. |
| 36 void throwInternalError(String message) => JS('', '''((message) => { |
| 37 throw Error(message); |
| 38 })(#)''', message); |
| 39 |
| 40 // TODO(ochafik): Re-introduce a @JS annotation in the SDK (same as package:js) |
| 41 // so that this is named 'assert' in JavaScript. |
| 42 void assert_(bool condition) => JS('', '''((condition) => { |
| 43 if (!condition) throwInternalError("The compiler is broken: failed assert"); |
| 44 })(#)''', condition); |
| 45 |
| 46 getOwnNamesAndSymbols(obj) => JS('', '''((obj) => { |
| 47 return getOwnPropertyNames(obj).concat(getOwnPropertySymbols(obj)); |
| 48 })(#)''', obj); |
| 49 |
| 50 safeGetOwnProperty(obj, String name) => JS('', '''((obj, name) => { |
| 51 let desc = getOwnPropertyDescriptor(obj, name); |
| 52 if (desc) return desc.value; |
| 53 })(#, #)''', obj, name); |
| 54 |
| 55 /// Defines a lazy 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 |
| 58 // performance in other projects (e.g. webcomponents.js ShadowDOM polyfill). |
| 59 defineLazyProperty(to, name, desc) => JS('', '''((to, name, desc) => { |
| 60 let init = desc.get; |
| 61 let value = null; |
| 62 |
| 63 function lazySetter(x) { |
| 64 init = null; |
| 65 value = x; |
| 66 } |
| 67 function circularInitError() { |
| 68 throwInternalError('circular initialization for field ' + name); |
| 69 } |
| 70 function lazyGetter() { |
| 71 if (init == null) return value; |
| 72 |
| 73 // Compute and store the value, guarding against reentry. |
| 74 let f = init; |
| 75 init = circularInitError; |
| 76 lazySetter(f()); |
| 77 return value; |
| 78 } |
| 79 desc.get = lazyGetter; |
| 80 desc.configurable = true; |
| 81 if (desc.set) desc.set = lazySetter; |
| 82 return defineProperty(to, name, desc); |
| 83 })(#, #, #)''', to, name, desc); |
| 84 |
| 85 void defineLazy(to, from) => JS('', '''((to, from) => { |
| 86 for (let name of getOwnNamesAndSymbols(from)) { |
| 87 defineLazyProperty(to, name, getOwnPropertyDescriptor(from, name)); |
| 88 } |
| 89 })(#, #)''', to, from); |
| 90 |
| 91 defineMemoizedGetter(obj, String name, getter) => |
| 92 JS('', '''((obj, name, getter) => { |
| 93 return defineLazyProperty(obj, name, {get: getter}); |
| 94 })(#, #, #)''', obj, name, getter); |
| 95 |
| 96 copyTheseProperties(to, from, names) => JS('', '''((to, from, names) => { |
| 97 for (let name of names) { |
| 98 defineProperty(to, name, getOwnPropertyDescriptor(from, name)); |
| 99 } |
| 100 return to; |
| 101 })(#, #, #)''', to, from, names); |
| 102 |
| 103 /// Copy properties from source to destination object. |
| 104 /// This operation is commonly called `mixin` in JS. |
| 105 copyProperties(to, from) => JS('', '''((to, from) => { |
| 106 return copyTheseProperties(to, from, getOwnNamesAndSymbols(from)); |
| 107 })(#, #)''', to, from); |
| 108 |
| 109 /// Exports from one Dart module to another. |
| 110 // TODO(ochafik): Re-introduce a @JS annotation in the SDK (same as package:js) |
| 111 // so that this is named 'export' in JavaScript. |
| 112 export_(to, from, show, hide) => JS('', '''((to, from, show, hide) => { |
| 113 if (show == void 0) { |
| 114 show = getOwnNamesAndSymbols(from); |
| 115 } |
| 116 if (hide != void 0) { |
| 117 var hideMap = new Set(hide); |
| 118 show = show.filter((k) => !hideMap.has(k)); |
| 119 } |
| 120 return copyTheseProperties(to, from, show); |
| 121 })(#, #, #, #)''', to, from, show, hide); |
OLD | NEW |