Skip to content

Commit

Permalink
sort classes in dependency order, or load lazily if needed, fixes #78
Browse files Browse the repository at this point in the history
this also starts using compiled code in dart_core.js (just copy+paste for Object)

visitClassDeclaration was getting rather long, so it's now refactored to make the flow more clear.

R=sigmund@google.com

Review URL: https://codereview.chromium.org/1016003003
  • Loading branch information
John Messerly committed Mar 19, 2015
1 parent 48d4a4e commit b62f4c0
Show file tree
Hide file tree
Showing 14 changed files with 3,759 additions and 3,558 deletions.
64 changes: 39 additions & 25 deletions pkg/dev_compiler/lib/runtime/dart_core.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,49 @@
// 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.

var dart_core;
(function (dart_core) {
var Object = (function () {
var constructor = function Object() {};
var core;
(function (core) {
'use strict';

constructor.prototype.toString = function toString() {
return 'Instance of ' + this.constructor.name;
};

constructor.prototype.noSuchMethod = function noSuchMethod(invocation) {
// TODO: add arguments when Invocation is defined
throw new NoSuchMethodError();
};

// TODO: implement ==

// TODO: implement hashCode

// TODO: implement runtimeType
// TODO(jmesserly): for now this is copy+paste from dart/core.js
class Object {
constructor() {
var name = this.constructor.name;
var init = this[name];
var result = void 0;
if (init)
result = init.apply(this, arguments);
return result === void 0 ? this : result;
}
['=='](other) {
return identical(this, other);
}
get hashCode() {
return _js_helper.Primitives.objectHashCode(this);
}
toString() {
return _js_helper.Primitives.objectToString(this);
}
noSuchMethod(invocation) {
throw new NoSuchMethodError(this, invocation.memberName, invocation.positionalArguments, invocation.namedArguments);
}
get runtimeType() {
return _js_helper.getRuntimeType(this);
}
}
core.Object = Object;

return constructor;
})();
dart_core.Object = Object;
// Function identical: (Object, Object) → bool
function identical(a, b) {
return _js_helper.Primitives.identicalImplementation(a, b);
}
core.identical = identical;

// Function print: (Object) → void
function print(obj) {
console.log(obj.toString());
}
dart_core.print = print;
core.print = print;

// Class NoSuchMethodError
var NoSuchMethodError = (function () {
Expand All @@ -39,7 +53,7 @@ var dart_core;
}
return NoSuchMethodError;
})();
dart_core.NoSuchMethodError = NoSuchMethodError;
core.NoSuchMethodError = NoSuchMethodError;

// Class UnimplementedError
var UnimplementedError = (function () {
Expand All @@ -49,5 +63,5 @@ var dart_core;
}
return UnimplementedError;
})();
dart_core.UnimplementedError = UnimplementedError;
})(dart_core || (dart_core = {}));
core.UnimplementedError = UnimplementedError;
})(core || (core = {}));
79 changes: 28 additions & 51 deletions pkg/dev_compiler/lib/runtime/dart_runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ var core = core || {int: { parse: Number }, print: e => console.log(e) };

var dart;
(function (dart) {
'use strict';

var defineProperty = Object.defineProperty;
var getOwnPropertyDescriptor = Object.getOwnPropertyDescriptor;
var getOwnPropertyNames = Object.getOwnPropertyNames;
Expand Down Expand Up @@ -172,14 +174,17 @@ var dart;
defineProperty(to, name, desc);
}

function defineLazyProperties(to, from) {
function defineLazy(to, from) {
var names = getOwnPropertyNames(from);
for (var i = 0; i < names.length; i++) {
var name = names[i];
defineLazyProperty(to, name, getOwnPropertyDescriptor(from, name));
}
}
dart.defineLazyProperties = defineLazyProperties;
// TODO(jmesserly): these are identical, but this makes it easier to grep for.
dart.defineLazyClass = defineLazy;
dart.defineLazyProperties = defineLazy;
dart.defineLazyClassGeneric = defineLazyProperty;

/**
* Copy properties from source to destination object.
Expand All @@ -205,33 +210,27 @@ var dart;
* superclass (prototype).
*/
function mixin(base/*, ...mixins*/) {
// Inherit statics from Base to simulate ES6 class inheritance
// Conceptually this is: `class Mixin extends base {}`
function Mixin() {
// TODO(jmesserly): since we're using initializers and not constructors,
// we can just skip directly to core.Object.
core.Object.apply(this, arguments);
}
Mixin.__proto__ = base;
Mixin.prototype = Object.create(base.prototype);
Mixin.prototype.constructor = Mixin;
// Copy each mixin, with later ones overwriting earlier entries.
var mixins = Array.prototype.slice.call(arguments, 1);
for (var i = 0; i < mixins.length; i++) {
copyProperties(Mixin.prototype, mixins[i].prototype);
}
// Create an initializer for the mixin, so when derived constructor calls
// super, we can correctly initialize base and mixins.
var baseCtor = base.prototype[base.name];
Mixin.prototype[base.name] = function() {
// Run mixin initializers. They cannot have arguments.
// Run them backwards so most-derived mixin is initialized first.
for (var i = mixins.length - 1; i >= 0; i--) {
var mixin = mixins[i];
mixin.prototype[mixin.name].call(this);
var mixins = Array.prototype.slice.call(arguments, 1);

// Create a class that will hold all of the mixin methods.
class Mixin extends base {
// Initializer method: run mixin initializers, then the base.
[base.name](/*...args*/) {
// Run mixin initializers. They cannot have arguments.
// Run them backwards so most-derived mixin is initialized first.
for (var i = mixins.length - 1; i >= 0; i--) {
var mixin = mixins[i];
mixin.prototype[mixin.name].call(this);
}
// Run base initializer.
base.prototype[base.name].apply(this, arguments);
}
// Run base initializer.
baseCtor.apply(this, arguments);
}
// Copy each mixin's methods, with later ones overwriting earlier entries.
for (var i = 0; i < mixins.length; i++) {
copyProperties(Mixin.prototype, mixins[i].prototype);
}
return Mixin;
}
Expand Down Expand Up @@ -314,8 +313,7 @@ var dart;
var value = resultMap;
for (var i = 0; i < length; i++) {
var arg = arguments[i];
// TODO(jmesserly): assume `dynamic` here?
if (arg === void 0) throw 'undefined is not allowed as a type argument';
if (arg === void 0) arg = dart.dynamic;

var map = value;
value = map.get(arg);
Expand All @@ -339,28 +337,7 @@ var dart;
}
dart.generic = generic;


/**
* Implements Dart constructor behavior. Because of V8 `super` [constructor
* restrictions](https://code.google.com/p/v8/issues/detail?id=3330#c65) we
* cannot currently emit actual ES6 constructors with super calls. Instead
* we use the same trick as named constructors, and do them as instance
* methods that perform initialization.
*/
// TODO(jmesserly): we'll need to rethink this once the ES6 spec and V8
// settles. See <https://github.com/dart-lang/dart-dev-compiler/issues/51>.
// Performance of this pattern is likely to be bad.
core.Object = function Object() {
// Get the class name for this instance.
var name = this.constructor.name;
// Call the default constructor.
var init = this[name];
var result = void 0;
if (init) result = init.apply(this, arguments);
return result === void 0 ? this : result;
};
// The initializer for Object
core.Object.prototype.Object = function() {};
core.Object.prototype.constructor = core.Object;
// TODO(jmesserly): this is just a placeholder.
dart.dynamic = Object.create(null);

})(dart || (dart = {}));
Loading

0 comments on commit b62f4c0

Please sign in to comment.