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

Unified Diff: pkg/compiler/lib/src/js_emitter/old_emitter/emitter.dart

Issue 957343002: Make use of __proto__ when available. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 5 years, 10 months 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 side-by-side diff with in-line comments
Download patch
Index: pkg/compiler/lib/src/js_emitter/old_emitter/emitter.dart
diff --git a/pkg/compiler/lib/src/js_emitter/old_emitter/emitter.dart b/pkg/compiler/lib/src/js_emitter/old_emitter/emitter.dart
index a7b3e56d88b01d8ab2c303cc5b62afdb43d9b4ca..d97440f5e98ef3855c7ff0295751fa50722725b7 100644
--- a/pkg/compiler/lib/src/js_emitter/old_emitter/emitter.dart
+++ b/pkg/compiler/lib/src/js_emitter/old_emitter/emitter.dart
@@ -420,12 +420,31 @@ class OldEmitter implements Emitter {
/** Needs defineClass to be defined. */
jsAst.Expression buildInheritFrom() {
jsAst.Expression result = js(r"""
+ // If the browser supports changing the prototype via __proto__, we make
+ // use of that feature. Otherwise, we copy the properties into a new
+ // constructor.
+ (function () {
+ var cls = function () {};
+ cls.prototype = {'p': {}};
+ var object = new cls();
+ return object.__proto__ &&
+ object.__proto__.p === cls.prototype.p;
+ })() ?
+ function(constructor, superConstructor) {
+ var prototype = constructor.prototype;
+ prototype.__proto__ = superConstructor.prototype;
+ // Use a function for `true` here, as functions are stored in the
+ // hidden class and not as properties in the object.
+ prototype.constructor = constructor;
+ prototype[#operatorIsPrefix + constructor.name] = constructor;
+ return convertToFastObject(prototype);
+ } :
function() {
function tmp() {}
return function (constructor, superConstructor) {
tmp.prototype = superConstructor.prototype;
var object = new tmp();
- object.x = 0; delete object.x; // Make object slow.
+ convertToSlowObject(object);
var properties = constructor.prototype;
var members = Object.keys(properties);
for (var i = 0; i < members.length; i++) {
@@ -1108,6 +1127,20 @@ class OldEmitter implements Emitter {
output.add(N);
}
+ void emitConvertToSlowObjectFunction(CodeOutput output) {
+ jsAst.Statement convertToSlowObject = js.statement(r'''
+ function convertToSlowObject(properties) {
+ // Add and remove a property to make the object transition into hashmap
+ // mode.
+ properties.__MAGIC_SLOW_PROPERTY = 1;
+ delete properties.__MAGIC_SLOW_PROPERTY;
+ return properties;
+ }''');
+
+ output.addBuffer(jsAst.prettyPrint(convertToSlowObject, compiler));
+ output.add(N);
+ }
+
void writeLibraryDescriptors(CodeOutput output, LibraryElement library) {
var uri = "";
if (!compiler.enableMinification || backend.mustPreserveUris) {
@@ -1472,6 +1505,8 @@ class OldEmitter implements Emitter {
'${namer.currentIsolate}$_=${_}new ${namer.isolateName}()$N');
emitConvertToFastObjectFunction(mainOutput);
+ emitConvertToSlowObjectFunction(mainOutput);
+
for (String globalObject in Namer.reservedGlobalObjectNames) {
mainOutput.add('$globalObject = convertToFastObject($globalObject)$N');
}

Powered by Google App Engine
This is Rietveld 408576698