Skip to content

Commit

Permalink
Convert dart_utils.js to input_sdk/lib/_internal/utils.dart (#310)
Browse files Browse the repository at this point in the history
  • Loading branch information
ochafik committed Dec 1, 2015
1 parent 7f85055 commit 6410051
Show file tree
Hide file tree
Showing 24 changed files with 295 additions and 104 deletions.
5 changes: 3 additions & 2 deletions pkg/dev_compiler/lib/runtime/dart/_classes.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@
// TODO(leafp): Consider splitting some of this out.
dart_library.library('dart/_classes', null, /* Imports */[
], /* Lazy Imports */[
'dart/_utils',
'dart/core',
'dart/_interceptors',
'dart/_types',
'dart/_rtti',
], function(exports, core, _interceptors, types, rtti) {
], function(exports, dart_utils, core, _interceptors, types, rtti) {
'use strict';

const assert = dart_utils.assert;
const assert = dart_utils.assert_;
const copyProperties = dart_utils.copyProperties;
const copyTheseProperties = dart_utils.copyTheseProperties;
const defineMemoizedGetter = dart_utils.defineMemoizedGetter;
Expand Down
3 changes: 2 additions & 1 deletion pkg/dev_compiler/lib/runtime/dart/_operations.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/
dart_library.library('dart/_operations', null, /* Imports */[
], /* Lazy Imports */[
'dart/_utils',
'dart/async',
'dart/collection',
'dart/core',
Expand All @@ -15,7 +16,7 @@ dart_library.library('dart/_operations', null, /* Imports */[
'dart/_errors',
'dart/_rtti',
'dart/_types'
], function(exports, async, collection, core, _js_helper, classes, errors, rtti,
], function(exports, dart_utils, async, collection, core, _js_helper, classes, errors, rtti,
types) {
'use strict';

Expand Down
3 changes: 2 additions & 1 deletion pkg/dev_compiler/lib/runtime/dart/_rtti.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@

dart_library.library('dart/_rtti', null, /* Imports */[
], /* Lazy Imports */[
'dart/_utils',
'dart/core',
'dart/_types'
], function(exports, core, types) {
], function(exports, dart_utils, core, types) {
'use strict';

const defineLazyProperty = dart_utils.defineLazyProperty;
Expand Down
5 changes: 3 additions & 2 deletions pkg/dev_compiler/lib/runtime/dart/_runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// BSD-style license that can be found in the LICENSE file.

dart_library.library('dart/_runtime', null, /* Imports */[
'dart/_utils',
'dart/_classes',
'dart/_errors',
'dart/_generators',
Expand All @@ -11,7 +12,7 @@ dart_library.library('dart/_runtime', null, /* Imports */[
'dart/_types',
], /* Lazy Imports */[
'dart/_js_helper'
], function(exports, classes, errors, generators, operations, rtti, types,
], function(exports, dart_utils, classes, errors, generators, operations, rtti, types,
_js_helper) {
'use strict';

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

// From dart_utils
exportFrom(dart_utils, ['copyProperties', 'export']);
exportFrom(dart_utils, ['copyProperties', 'export_']);
// Renames
exports.defineLazyClass = _export(dart_utils.defineLazy);
exports.defineLazyProperties = _export(dart_utils.defineLazy);
Expand Down
9 changes: 5 additions & 4 deletions pkg/dev_compiler/lib/runtime/dart/_types.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@

dart_library.library('dart/_types', null, /* Imports */[
], /* Lazy Imports */[
'dart/_utils',
'dart/core',
'dart/_classes',
'dart/_rtti'
], function(exports, core, classes, rtti) {
], function(exports, dart_utils, core, classes, rtti) {
'use strict';

const getOwnPropertyNames = Object.getOwnPropertyNames;

const assert = dart_utils.assert;
const assert = dart_utils.assert_;

/**
* Types in dart are represented at runtime as follows.
Expand All @@ -24,12 +25,12 @@ dart_library.library('dart/_types', null, /* Imports */[
* If the type is the result of instantiating a generic class,
* then the "classes" module manages the association between the
* instantiated class and the original class declaration
* and the type arguments with which it was instantiated. This
* and the type arguments with which it was instantiated. This
* assocation can be queried via the "classes" module".
*
* - All other types are represented as instances of class TypeRep,
* defined in this module.
* - Dynamic, Void, and Bottom are singleton instances of sentinal
* - Dynamic, Void, and Bottom are singleton instances of sentinal
* classes.
* - Function types are instances of subclasses of AbstractFunctionType.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,70 +1,42 @@
// Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

/* This library defines a set of general javascript utilities for us
* by the Dart runtime.
*/

var dart_utils =
typeof module != "undefined" && module.exports || {};

(function (dart_utils) {
dart_library.library('dart/_utils', null, /* Imports */[
], /* Lazy imports */[
], function(exports, dart) {
'use strict';

const defineProperty = Object.defineProperty;
const getOwnPropertyDescriptor = Object.getOwnPropertyDescriptor;
const getOwnPropertyNames = Object.getOwnPropertyNames;
const getOwnPropertySymbols = Object.getOwnPropertySymbols;

const hasOwnProperty = Object.prototype.hasOwnProperty;

class StrongModeError extends Error {
constructor(message) {
super(message);
const StrongModeError = (function() {
function StrongModeError(message) {
Error.call(this);
this.message = message;
}
}

/** This error indicates a strong mode specific failure.
*/
;
Object.setPrototypeOf(StrongModeError.prototype, Error.prototype);
return StrongModeError;
})();
function throwStrongModeError(message) {
throw new StrongModeError(message);
}
dart_utils.throwStrongModeError = throwStrongModeError;

/** This error indicates a bug in the runtime or the compiler.
*/
function throwInternalError(message) {
throw Error(message);
}
dart_utils.throwInternalError = throwInternalError;

function assert(condition) {
if (!condition) throwInternalError("The compiler is broken: failed assert");
function assert_(condition) {
if (!condition)
throwInternalError("The compiler is broken: failed assert");
}
dart_utils.assert = assert;

function getOwnNamesAndSymbols(obj) {
return getOwnPropertyNames(obj).concat(getOwnPropertySymbols(obj));
}
dart_utils.getOwnNamesAndSymbols = getOwnNamesAndSymbols;

function safeGetOwnProperty(obj, name) {
let desc = getOwnPropertyDescriptor(obj, name);
if (desc) return desc.value;
if (desc)
return desc.value;
}
dart_utils.safeGetOwnProperty = safeGetOwnProperty;

/**
* Defines a lazy property.
* After initial get or set, it will replace itself with a value property.
*/
// TODO(jmesserly): reusing descriptor objects has been shown to improve
// performance in other projects (e.g. webcomponents.js ShadowDOM polyfill).
function defineLazyProperty(to, name, desc) {
let init = desc.get;
let value = null;

function lazySetter(x) {
init = null;
value = x;
Expand All @@ -73,61 +45,62 @@ var dart_utils =
throwInternalError('circular initialization for field ' + name);
}
function lazyGetter() {
if (init == null) return value;

// Compute and store the value, guarding against reentry.
if (init == null)
return value;
let f = init;
init = circularInitError;
lazySetter(f());
return value;
}
desc.get = lazyGetter;
desc.configurable = true;
if (desc.set) desc.set = lazySetter;
if (desc.set)
desc.set = lazySetter;
return defineProperty(to, name, desc);
}
dart_utils.defineLazyProperty = defineLazyProperty;

function defineLazy(to, from) {
for (let name of getOwnNamesAndSymbols(from)) {
defineLazyProperty(to, name, getOwnPropertyDescriptor(from, name));
}
}
dart_utils.defineLazy = defineLazy;

function defineMemoizedGetter(obj, name, getter) {
return defineLazyProperty(obj, name, {get: getter});
}
dart_utils.defineMemoizedGetter = defineMemoizedGetter;

function copyTheseProperties(to, from, names) {
for (let name of names) {
defineProperty(to, name, getOwnPropertyDescriptor(from, name));
}
return to;
}
dart_utils.copyTheseProperties = copyTheseProperties;

/**
* Copy properties from source to destination object.
* This operation is commonly called `mixin` in JS.
*/
function copyProperties(to, from) {
return copyTheseProperties(to, from, getOwnNamesAndSymbols(from));
}
dart_utils.copyProperties = copyProperties;

/** Exports from one Dart module to another. */
function export_(to, from, show, hide) {
if (show == void 0) {
show = getOwnNamesAndSymbols(from);
}
if (hide != void 0) {
var hideMap = new Set(hide);
show = show.filter((k) => !hideMap.has(k));
show = show.filter(k => !hideMap.has(k));
}
return copyTheseProperties(to, from, show);
}
dart_utils.export = export_;

})(dart_utils);
// Exports:
exports.defineProperty = defineProperty;
exports.getOwnPropertyDescriptor = getOwnPropertyDescriptor;
exports.getOwnPropertyNames = getOwnPropertyNames;
exports.getOwnPropertySymbols = getOwnPropertySymbols;
exports.hasOwnProperty = hasOwnProperty;
exports.StrongModeError = StrongModeError;
exports.throwStrongModeError = throwStrongModeError;
exports.throwInternalError = throwInternalError;
exports.assert_ = assert_;
exports.getOwnNamesAndSymbols = getOwnNamesAndSymbols;
exports.safeGetOwnProperty = safeGetOwnProperty;
exports.defineLazyProperty = defineLazyProperty;
exports.defineLazy = defineLazy;
exports.defineMemoizedGetter = defineMemoizedGetter;
exports.copyTheseProperties = copyTheseProperties;
exports.copyProperties = copyProperties;
exports.export_ = export_;
});
11 changes: 8 additions & 3 deletions pkg/dev_compiler/lib/runtime/dart_library.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ var dart_library =
(function (dart_library) {
'use strict';

/** Note that we cannot use dart_utils.throwInternalError from here. */
function throwLibraryError(message) {
throw Error(message);
}

// Module support. This is a simplified module system for Dart.
// Longer term, we can easily migrate to an existing JS module system:
// ES6, AMD, RequireJS, ....
Expand Down Expand Up @@ -48,7 +53,7 @@ var dart_library =
for (let name of list) {
let lib = libraries[name];
if (!lib) {
dart_utils.throwInternalError('Library not available: ' + name);
throwLibraryError('Library not available: ' + name);
}
results.push(handler(lib));
}
Expand All @@ -58,7 +63,7 @@ var dart_library =
load(inheritedPendingSet) {
// Check for cycles
if (this._state == LibraryLoader.LOADING) {
dart_utils.throwInternalError('Circular dependence on library: '
throwLibraryError('Circular dependence on library: '
+ this._name);
} else if (this._state >= LibraryLoader.LOADED) {
return this._library;
Expand Down Expand Up @@ -107,7 +112,7 @@ var dart_library =
let loader = libraries[libraryName];
// TODO(vsm): A user might call this directly from JS (as we do in tests).
// We may want a different error type.
if (!loader) dart_utils.throwInternalError('Library not found: ' + libraryName);
if (!loader) throwLibraryError('Library not found: ' + libraryName);
return loader.load();
}
dart_library.import = import_;
Expand Down
Loading

0 comments on commit 6410051

Please sign in to comment.