Skip to content

Commit 6410051

Browse files
author
Olivier Chafik
committed
Convert dart_utils.js to input_sdk/lib/_internal/utils.dart (#310)
BUG= [email protected], [email protected] Review URL: https://codereview.chromium.org/1486473002 .
1 parent 7f85055 commit 6410051

24 files changed

+295
-104
lines changed

pkg/dev_compiler/lib/runtime/dart/_classes.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,15 @@
1212
// TODO(leafp): Consider splitting some of this out.
1313
dart_library.library('dart/_classes', null, /* Imports */[
1414
], /* Lazy Imports */[
15+
'dart/_utils',
1516
'dart/core',
1617
'dart/_interceptors',
1718
'dart/_types',
1819
'dart/_rtti',
19-
], function(exports, core, _interceptors, types, rtti) {
20+
], function(exports, dart_utils, core, _interceptors, types, rtti) {
2021
'use strict';
2122

22-
const assert = dart_utils.assert;
23+
const assert = dart_utils.assert_;
2324
const copyProperties = dart_utils.copyProperties;
2425
const copyTheseProperties = dart_utils.copyTheseProperties;
2526
const defineMemoizedGetter = dart_utils.defineMemoizedGetter;

pkg/dev_compiler/lib/runtime/dart/_operations.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*/
88
dart_library.library('dart/_operations', null, /* Imports */[
99
], /* Lazy Imports */[
10+
'dart/_utils',
1011
'dart/async',
1112
'dart/collection',
1213
'dart/core',
@@ -15,7 +16,7 @@ dart_library.library('dart/_operations', null, /* Imports */[
1516
'dart/_errors',
1617
'dart/_rtti',
1718
'dart/_types'
18-
], function(exports, async, collection, core, _js_helper, classes, errors, rtti,
19+
], function(exports, dart_utils, async, collection, core, _js_helper, classes, errors, rtti,
1920
types) {
2021
'use strict';
2122

pkg/dev_compiler/lib/runtime/dart/_rtti.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@
88

99
dart_library.library('dart/_rtti', null, /* Imports */[
1010
], /* Lazy Imports */[
11+
'dart/_utils',
1112
'dart/core',
1213
'dart/_types'
13-
], function(exports, core, types) {
14+
], function(exports, dart_utils, core, types) {
1415
'use strict';
1516

1617
const defineLazyProperty = dart_utils.defineLazyProperty;

pkg/dev_compiler/lib/runtime/dart/_runtime.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// BSD-style license that can be found in the LICENSE file.
44

55
dart_library.library('dart/_runtime', null, /* Imports */[
6+
'dart/_utils',
67
'dart/_classes',
78
'dart/_errors',
89
'dart/_generators',
@@ -11,7 +12,7 @@ dart_library.library('dart/_runtime', null, /* Imports */[
1112
'dart/_types',
1213
], /* Lazy Imports */[
1314
'dart/_js_helper'
14-
], function(exports, classes, errors, generators, operations, rtti, types,
15+
], function(exports, dart_utils, classes, errors, generators, operations, rtti, types,
1516
_js_helper) {
1617
'use strict';
1718

@@ -64,7 +65,7 @@ dart_library.library('dart/_runtime', null, /* Imports */[
6465
]);
6566

6667
// From dart_utils
67-
exportFrom(dart_utils, ['copyProperties', 'export']);
68+
exportFrom(dart_utils, ['copyProperties', 'export_']);
6869
// Renames
6970
exports.defineLazyClass = _export(dart_utils.defineLazy);
7071
exports.defineLazyProperties = _export(dart_utils.defineLazy);

pkg/dev_compiler/lib/runtime/dart/_types.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,16 @@
77

88
dart_library.library('dart/_types', null, /* Imports */[
99
], /* Lazy Imports */[
10+
'dart/_utils',
1011
'dart/core',
1112
'dart/_classes',
1213
'dart/_rtti'
13-
], function(exports, core, classes, rtti) {
14+
], function(exports, dart_utils, core, classes, rtti) {
1415
'use strict';
1516

1617
const getOwnPropertyNames = Object.getOwnPropertyNames;
1718

18-
const assert = dart_utils.assert;
19+
const assert = dart_utils.assert_;
1920

2021
/**
2122
* Types in dart are represented at runtime as follows.
@@ -24,12 +25,12 @@ dart_library.library('dart/_types', null, /* Imports */[
2425
* If the type is the result of instantiating a generic class,
2526
* then the "classes" module manages the association between the
2627
* instantiated class and the original class declaration
27-
* and the type arguments with which it was instantiated. This
28+
* and the type arguments with which it was instantiated. This
2829
* assocation can be queried via the "classes" module".
2930
*
3031
* - All other types are represented as instances of class TypeRep,
3132
* defined in this module.
32-
* - Dynamic, Void, and Bottom are singleton instances of sentinal
33+
* - Dynamic, Void, and Bottom are singleton instances of sentinal
3334
* classes.
3435
* - Function types are instances of subclasses of AbstractFunctionType.
3536
*
Lines changed: 40 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,42 @@
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) {
1+
dart_library.library('dart/_utils', null, /* Imports */[
2+
], /* Lazy imports */[
3+
], function(exports, dart) {
134
'use strict';
14-
155
const defineProperty = Object.defineProperty;
166
const getOwnPropertyDescriptor = Object.getOwnPropertyDescriptor;
177
const getOwnPropertyNames = Object.getOwnPropertyNames;
188
const getOwnPropertySymbols = Object.getOwnPropertySymbols;
19-
209
const hasOwnProperty = Object.prototype.hasOwnProperty;
21-
22-
class StrongModeError extends Error {
23-
constructor(message) {
24-
super(message);
10+
const StrongModeError = (function() {
11+
function StrongModeError(message) {
12+
Error.call(this);
13+
this.message = message;
2514
}
26-
}
27-
28-
/** This error indicates a strong mode specific failure.
29-
*/
15+
;
16+
Object.setPrototypeOf(StrongModeError.prototype, Error.prototype);
17+
return StrongModeError;
18+
})();
3019
function throwStrongModeError(message) {
3120
throw new StrongModeError(message);
3221
}
33-
dart_utils.throwStrongModeError = throwStrongModeError;
34-
35-
/** This error indicates a bug in the runtime or the compiler.
36-
*/
3722
function throwInternalError(message) {
3823
throw Error(message);
3924
}
40-
dart_utils.throwInternalError = throwInternalError;
41-
42-
function assert(condition) {
43-
if (!condition) throwInternalError("The compiler is broken: failed assert");
25+
function assert_(condition) {
26+
if (!condition)
27+
throwInternalError("The compiler is broken: failed assert");
4428
}
45-
dart_utils.assert = assert;
46-
4729
function getOwnNamesAndSymbols(obj) {
4830
return getOwnPropertyNames(obj).concat(getOwnPropertySymbols(obj));
4931
}
50-
dart_utils.getOwnNamesAndSymbols = getOwnNamesAndSymbols;
51-
5232
function safeGetOwnProperty(obj, name) {
5333
let desc = getOwnPropertyDescriptor(obj, name);
54-
if (desc) return desc.value;
34+
if (desc)
35+
return desc.value;
5536
}
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).
6437
function defineLazyProperty(to, name, desc) {
6538
let init = desc.get;
6639
let value = null;
67-
6840
function lazySetter(x) {
6941
init = null;
7042
value = x;
@@ -73,61 +45,62 @@ var dart_utils =
7345
throwInternalError('circular initialization for field ' + name);
7446
}
7547
function lazyGetter() {
76-
if (init == null) return value;
77-
78-
// Compute and store the value, guarding against reentry.
48+
if (init == null)
49+
return value;
7950
let f = init;
8051
init = circularInitError;
8152
lazySetter(f());
8253
return value;
8354
}
8455
desc.get = lazyGetter;
8556
desc.configurable = true;
86-
if (desc.set) desc.set = lazySetter;
57+
if (desc.set)
58+
desc.set = lazySetter;
8759
return defineProperty(to, name, desc);
8860
}
89-
dart_utils.defineLazyProperty = defineLazyProperty;
90-
9161
function defineLazy(to, from) {
9262
for (let name of getOwnNamesAndSymbols(from)) {
9363
defineLazyProperty(to, name, getOwnPropertyDescriptor(from, name));
9464
}
9565
}
96-
dart_utils.defineLazy = defineLazy;
97-
9866
function defineMemoizedGetter(obj, name, getter) {
9967
return defineLazyProperty(obj, name, {get: getter});
10068
}
101-
dart_utils.defineMemoizedGetter = defineMemoizedGetter;
102-
10369
function copyTheseProperties(to, from, names) {
10470
for (let name of names) {
10571
defineProperty(to, name, getOwnPropertyDescriptor(from, name));
10672
}
10773
return to;
10874
}
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-
*/
11575
function copyProperties(to, from) {
11676
return copyTheseProperties(to, from, getOwnNamesAndSymbols(from));
11777
}
118-
dart_utils.copyProperties = copyProperties;
119-
120-
/** Exports from one Dart module to another. */
12178
function export_(to, from, show, hide) {
12279
if (show == void 0) {
12380
show = getOwnNamesAndSymbols(from);
12481
}
12582
if (hide != void 0) {
12683
var hideMap = new Set(hide);
127-
show = show.filter((k) => !hideMap.has(k));
84+
show = show.filter(k => !hideMap.has(k));
12885
}
12986
return copyTheseProperties(to, from, show);
13087
}
131-
dart_utils.export = export_;
132-
133-
})(dart_utils);
88+
// Exports:
89+
exports.defineProperty = defineProperty;
90+
exports.getOwnPropertyDescriptor = getOwnPropertyDescriptor;
91+
exports.getOwnPropertyNames = getOwnPropertyNames;
92+
exports.getOwnPropertySymbols = getOwnPropertySymbols;
93+
exports.hasOwnProperty = hasOwnProperty;
94+
exports.StrongModeError = StrongModeError;
95+
exports.throwStrongModeError = throwStrongModeError;
96+
exports.throwInternalError = throwInternalError;
97+
exports.assert_ = assert_;
98+
exports.getOwnNamesAndSymbols = getOwnNamesAndSymbols;
99+
exports.safeGetOwnProperty = safeGetOwnProperty;
100+
exports.defineLazyProperty = defineLazyProperty;
101+
exports.defineLazy = defineLazy;
102+
exports.defineMemoizedGetter = defineMemoizedGetter;
103+
exports.copyTheseProperties = copyTheseProperties;
104+
exports.copyProperties = copyProperties;
105+
exports.export_ = export_;
106+
});

pkg/dev_compiler/lib/runtime/dart_library.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ var dart_library =
1111
(function (dart_library) {
1212
'use strict';
1313

14+
/** Note that we cannot use dart_utils.throwInternalError from here. */
15+
function throwLibraryError(message) {
16+
throw Error(message);
17+
}
18+
1419
// Module support. This is a simplified module system for Dart.
1520
// Longer term, we can easily migrate to an existing JS module system:
1621
// ES6, AMD, RequireJS, ....
@@ -48,7 +53,7 @@ var dart_library =
4853
for (let name of list) {
4954
let lib = libraries[name];
5055
if (!lib) {
51-
dart_utils.throwInternalError('Library not available: ' + name);
56+
throwLibraryError('Library not available: ' + name);
5257
}
5358
results.push(handler(lib));
5459
}
@@ -58,7 +63,7 @@ var dart_library =
5863
load(inheritedPendingSet) {
5964
// Check for cycles
6065
if (this._state == LibraryLoader.LOADING) {
61-
dart_utils.throwInternalError('Circular dependence on library: '
66+
throwLibraryError('Circular dependence on library: '
6267
+ this._name);
6368
} else if (this._state >= LibraryLoader.LOADED) {
6469
return this._library;
@@ -107,7 +112,7 @@ var dart_library =
107112
let loader = libraries[libraryName];
108113
// TODO(vsm): A user might call this directly from JS (as we do in tests).
109114
// We may want a different error type.
110-
if (!loader) dart_utils.throwInternalError('Library not found: ' + libraryName);
115+
if (!loader) throwLibraryError('Library not found: ' + libraryName);
111116
return loader.load();
112117
}
113118
dart_library.import = import_;

0 commit comments

Comments
 (0)