Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2)

Side by Side Diff: tool/input_sdk/private/utils.dart

Issue 1486473002: Convert dart_utils.js to input_sdk/lib/_internal/utils.dart (#310) (Closed) Base URL: git@github.com:dart-lang/dev_compiler.git@master
Patch Set: Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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
12 const defineProperty = JS('', 'Object.defineProperty');
13 const getOwnPropertyDescriptor = JS('', 'Object.getOwnPropertyDescriptor');
14 const getOwnPropertyNames = JS('', 'Object.getOwnPropertyNames');
15 const getOwnPropertySymbols = JS('', 'Object.getOwnPropertySymbols');
16
17 const hasOwnProperty = JS('', 'Object.prototype.hasOwnProperty');
18
19 // TODO(ochafik): Add ES6 class syntax support to JS intrinsics to avoid this.
Jennifer Messerly 2015/11/30 19:00:03 ah, yeah, good idea :)
20 const StrongModeError = JS('', '''(function() {
21 function StrongModeError(message) {
22 Error.call(this);
23 this.message = message;
24 };
25 Object.setPrototypeOf(StrongModeError.prototype, Error.prototype);
26 return StrongModeError;
27 })()''');
28
29 /// This error indicates a strong mode specific failure.
30 void throwStrongModeError(message) => JS('', '''(function() {
Jennifer Messerly 2015/11/30 19:00:03 should we explicitly pass in "message"? The curren
ochafik 2015/12/01 14:41:03 Now passing args with the `foo(x) => JS('', '((x)
31 throw new StrongModeError(message);
32 })()''');
33
34 /// This error indicates a bug in the runtime or the compiler.
35 void throwInternalError(message) => JS('', '''(function() {
36 throw Error(message);
37 })()''');
38
39 void assert_(bool condition) => JS('', '''(function() {
40 if (!condition) throwInternalError("The compiler is broken: failed assert");
41 })()''');
42
43 getOwnNamesAndSymbols(obj) => JS('', '''(function() {
44 return getOwnPropertyNames(obj).concat(getOwnPropertySymbols(obj));
45 })()''');
46
47 safeGetOwnProperty(obj, name) => JS('', '''(function() {
48 let desc = getOwnPropertyDescriptor(obj, name);
Jennifer Messerly 2015/11/30 19:00:03 I wonder if some of these could be written in Dart
ochafik 2015/12/01 14:41:03 Yes definitely, I would expect more "idiomatic" re
49 if (desc) return desc.value;
50 })()''');
51
52 /// Defines a lazy property.
53 /// After initial get or set, it will replace itself with a value property.
54 // TODO(jmesserly): reusing descriptor objects has been shown to improve
55 // performance in other projects (e.g. webcomponents.js ShadowDOM polyfill).
56 defineLazyProperty(to, name, desc) => JS('', '''(function() {
57 let init = desc.get;
58 let value = null;
59
60 function lazySetter(x) {
61 init = null;
62 value = x;
63 }
64 function circularInitError() {
65 throwInternalError('circular initialization for field ' + name);
66 }
67 function lazyGetter() {
68 if (init == null) return value;
69
70 // Compute and store the value, guarding against reentry.
71 let f = init;
72 init = circularInitError;
73 lazySetter(f());
74 return value;
75 }
76 desc.get = lazyGetter;
77 desc.configurable = true;
78 if (desc.set) desc.set = lazySetter;
79 return defineProperty(to, name, desc);
80 })()''');
81
82 void defineLazy(to, from) => JS('', '''(function() {
83 for (let name of getOwnNamesAndSymbols(from)) {
84 defineLazyProperty(to, name, getOwnPropertyDescriptor(from, name));
85 }
86 })()''');
87
88 defineMemoizedGetter(obj, name, getter) => JS('', '''(function() {
89 return defineLazyProperty(obj, name, {get: getter});
90 })()''');
91
92 copyTheseProperties(to, from, names) => JS('', '''(function() {
93 for (let name of names) {
94 defineProperty(to, name, getOwnPropertyDescriptor(from, name));
95 }
96 return to;
97 })()''');
98
99 /// Copy properties from source to destination object.
100 /// This operation is commonly called `mixin` in JS.
101 copyProperties(to, from) => JS('', '''(function() {
102 return copyTheseProperties(to, from, getOwnNamesAndSymbols(from));
103 })()''');
104
105 /// Exports from one Dart module to another.
106 export_(to, from, show, hide) => JS('', '''(function() {
107 if (show == void 0) {
108 show = getOwnNamesAndSymbols(from);
109 }
110 if (hide != void 0) {
111 var hideMap = new Set(hide);
112 show = show.filter((k) => !hideMap.has(k));
113 }
114 return copyTheseProperties(to, from, show);
115 })()''');
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698