| OLD | NEW |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 part of dart2js.js_emitter; | 5 part of dart2js.js_emitter; |
| 6 | 6 |
| 7 | 7 |
| 8 class OldEmitter implements Emitter { | 8 class OldEmitter implements Emitter { |
| 9 final Compiler compiler; | 9 final Compiler compiler; |
| 10 final CodeEmitterTask task; | 10 final CodeEmitterTask task; |
| (...skipping 249 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 260 // Example: | 260 // Example: |
| 261 // defineClass("A", ["x", "y"], { | 261 // defineClass("A", ["x", "y"], { |
| 262 // foo$1: function(y) { | 262 // foo$1: function(y) { |
| 263 // print(this.x + y); | 263 // print(this.x + y); |
| 264 // }, | 264 // }, |
| 265 // bar$2: function(t, v) { | 265 // bar$2: function(t, v) { |
| 266 // this.x = t - v; | 266 // this.x = t - v; |
| 267 // }, | 267 // }, |
| 268 // }); | 268 // }); |
| 269 | 269 |
| 270 var defineClass = js('''function(name, cls, fields) { | 270 var defineClass = js('''function(name, fields) { |
| 271 var accessors = []; | 271 var accessors = []; |
| 272 | 272 |
| 273 var str = "function " + cls + "("; | 273 var str = "function " + name + "("; |
| 274 var body = ""; | 274 var body = ""; |
| 275 | 275 |
| 276 for (var i = 0; i < fields.length; i++) { | 276 for (var i = 0; i < fields.length; i++) { |
| 277 if(i != 0) str += ", "; | 277 if(i != 0) str += ", "; |
| 278 | 278 |
| 279 var field = generateAccessor(fields[i], accessors, cls); | 279 var field = generateAccessor(fields[i], accessors, name); |
| 280 var parameter = "parameter_" + field; | 280 var parameter = "parameter_" + field; |
| 281 str += parameter; | 281 str += parameter; |
| 282 body += ("this." + field + " = " + parameter + ";\\n"); | 282 body += ("this." + field + " = " + parameter + ";\\n"); |
| 283 } | 283 } |
| 284 str += ") {\\n" + body + "}\\n"; | 284 str += ") {\\n" + body + "}\\n"; |
| 285 str += cls + ".builtin\$cls=\\"" + name + "\\";\\n"; | 285 str += name + ".builtin\$cls=\\"" + name + "\\";\\n"; |
| 286 str += "\$desc=\$collectedClasses." + cls + ";\\n"; | 286 str += "\$desc=\$collectedClasses." + name + ";\\n"; |
| 287 str += "if(\$desc instanceof Array) \$desc = \$desc[1];\\n"; | 287 str += "if(\$desc instanceof Array) \$desc = \$desc[1];\\n"; |
| 288 str += cls + ".prototype = \$desc;\\n"; | 288 str += name + ".prototype = \$desc;\\n"; |
| 289 if (typeof defineClass.name != "string") { | 289 if (typeof defineClass.name != "string") { |
| 290 str += cls + ".name=\\"" + cls + "\\";\\n"; | 290 str += name + ".name=\\"" + name + "\\";\\n"; |
| 291 } | 291 } |
| 292 str += accessors.join(""); | 292 str += accessors.join(""); |
| 293 | 293 |
| 294 return str; | 294 return str; |
| 295 }'''); | 295 }'''); |
| 296 // Declare a function called "generateAccessor". This is used in | 296 // Declare a function called "generateAccessor". This is used in |
| 297 // defineClassFunction (it's a local declaration in init()). | 297 // defineClassFunction (it's a local declaration in init()). |
| 298 List<jsAst.Node> saveDefineClass = []; |
| 299 if (compiler.hasIncrementalSupport) { |
| 300 saveDefineClass.add( |
| 301 js(r'self.$dart_unsafe_eval.defineClass = defineClass')); |
| 302 } |
| 298 return [ | 303 return [ |
| 299 generateAccessorFunction, | 304 generateAccessorFunction, |
| 300 js('$generateAccessorHolder = generateAccessor'), | 305 js('$generateAccessorHolder = generateAccessor'), |
| 301 new jsAst.FunctionDeclaration( | 306 new jsAst.FunctionDeclaration( |
| 302 new jsAst.VariableDeclaration('defineClass'), defineClass) ]; | 307 new jsAst.VariableDeclaration('defineClass'), defineClass) ] |
| 308 ..addAll(saveDefineClass); |
| 303 } | 309 } |
| 304 | 310 |
| 305 /** Needs defineClass to be defined. */ | 311 /** Needs defineClass to be defined. */ |
| 306 jsAst.Expression buildInheritFrom() { | 312 jsAst.Expression buildInheritFrom() { |
| 307 jsAst.Expression result = js(r''' | 313 jsAst.Expression result = js(r''' |
| 308 function() { | 314 function() { |
| 309 var hasOwnProperty = Object.prototype.hasOwnProperty; | 315 var hasOwnProperty = Object.prototype.hasOwnProperty; |
| 310 return function (constructor, superConstructor) { | 316 return function (constructor, superConstructor) { |
| 311 var object = Object.create(superConstructor.prototype); | 317 var object = Object.create(superConstructor.prototype); |
| 312 var properties = constructor.prototype; | 318 var properties = constructor.prototype; |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 411 var constructorsList = []; | 417 var constructorsList = []; |
| 412 } | 418 } |
| 413 | 419 |
| 414 for (var cls in collectedClasses) { | 420 for (var cls in collectedClasses) { |
| 415 var desc = collectedClasses[cls]; | 421 var desc = collectedClasses[cls]; |
| 416 if (desc instanceof Array) desc = desc[1]; | 422 if (desc instanceof Array) desc = desc[1]; |
| 417 | 423 |
| 418 /* The 'fields' are either a constructor function or a | 424 /* The 'fields' are either a constructor function or a |
| 419 * string encoding fields, constructor and superclass. Gets the | 425 * string encoding fields, constructor and superclass. Gets the |
| 420 * superclass and fields in the format | 426 * superclass and fields in the format |
| 421 * '[name/]Super;field1,field2' | 427 * 'Super;field1,field2' |
| 422 * from the CLASS_DESCRIPTOR_PROPERTY property on the descriptor. | 428 * from the CLASS_DESCRIPTOR_PROPERTY property on the descriptor. |
| 423 * The 'name/' is optional and contains the name that should be used | |
| 424 * when printing the runtime type string. It is used, for example, | |
| 425 * to print the runtime type JSInt as 'int'. | |
| 426 */ | 429 */ |
| 427 var classData = desc["${namer.classDescriptorProperty}"], | 430 var classData = desc["${namer.classDescriptorProperty}"], |
| 428 supr, name = cls, fields = classData; | 431 supr, fields = classData; |
| 429 if (#hasRetainedMetadata) | 432 if (#hasRetainedMetadata) |
| 430 if (typeof classData == "object" && | 433 if (typeof classData == "object" && |
| 431 classData instanceof Array) { | 434 classData instanceof Array) { |
| 432 classData = fields = classData[0]; | 435 classData = fields = classData[0]; |
| 433 } | 436 } |
| 434 if (typeof classData == "string") { | |
| 435 var split = classData.split("/"); | |
| 436 if (split.length == 2) { | |
| 437 name = split[0]; | |
| 438 fields = split[1]; | |
| 439 } | |
| 440 } | |
| 441 | |
| 442 var s = fields.split(";"); | 437 var s = fields.split(";"); |
| 443 fields = s[1] == "" ? [] : s[1].split(","); | 438 fields = s[1] == "" ? [] : s[1].split(","); |
| 444 supr = s[0]; | 439 supr = s[0]; |
| 445 split = supr.split(":"); | 440 split = supr.split(":"); |
| 446 if (split.length == 2) { | 441 if (split.length == 2) { |
| 447 supr = split[0]; | 442 supr = split[0]; |
| 448 var functionSignature = split[1]; | 443 var functionSignature = split[1]; |
| 449 if (functionSignature) | 444 if (functionSignature) |
| 450 desc.\$signature = (function(s) { | 445 desc.\$signature = (function(s) { |
| 451 return function(){ return #metadata[s]; }; | 446 return function(){ return #metadata[s]; }; |
| 452 })(functionSignature); | 447 })(functionSignature); |
| 453 } | 448 } |
| 454 | 449 |
| 455 if (#needsMixinSupport) | 450 if (#needsMixinSupport) |
| 456 if (supr && supr.indexOf("+") > 0) { | 451 if (supr && supr.indexOf("+") > 0) { |
| 457 s = supr.split("+"); | 452 s = supr.split("+"); |
| 458 supr = s[0]; | 453 supr = s[0]; |
| 459 var mixin = collectedClasses[s[1]]; | 454 var mixin = collectedClasses[s[1]]; |
| 460 if (mixin instanceof Array) mixin = mixin[1]; | 455 if (mixin instanceof Array) mixin = mixin[1]; |
| 461 for (var d in mixin) { | 456 for (var d in mixin) { |
| 462 if (hasOwnProperty.call(mixin, d) && | 457 if (hasOwnProperty.call(mixin, d) && |
| 463 !hasOwnProperty.call(desc, d)) | 458 !hasOwnProperty.call(desc, d)) |
| 464 desc[d] = mixin[d]; | 459 desc[d] = mixin[d]; |
| 465 } | 460 } |
| 466 } | 461 } |
| 467 | 462 |
| 468 if (typeof dart_precompiled != "function") { | 463 if (typeof dart_precompiled != "function") { |
| 469 combinedConstructorFunction += defineClass(name, cls, fields); | 464 combinedConstructorFunction += defineClass(cls, fields); |
| 470 constructorsList.push(cls); | 465 constructorsList.push(cls); |
| 471 } | 466 } |
| 472 if (supr) pendingClasses[cls] = supr; | 467 if (supr) pendingClasses[cls] = supr; |
| 473 } | 468 } |
| 474 | 469 |
| 475 if (typeof dart_precompiled != "function") { | 470 if (typeof dart_precompiled != "function") { |
| 476 combinedConstructorFunction += | 471 combinedConstructorFunction += |
| 477 "return [\\n " + constructorsList.join(",\\n ") + "\\n]"; | 472 "return [\\n " + constructorsList.join(",\\n ") + "\\n]"; |
| 478 var constructors = | 473 var constructors = |
| 479 new Function("\$collectedClasses", combinedConstructorFunction) | 474 new Function("\$collectedClasses", combinedConstructorFunction) |
| (...skipping 1527 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2007 for (Element element in compiler.enqueuer.codegen.newlyEnqueuedElements) { | 2002 for (Element element in compiler.enqueuer.codegen.newlyEnqueuedElements) { |
| 2008 if (element.isInstanceMember) { | 2003 if (element.isInstanceMember) { |
| 2009 cachedClassBuilders.remove(element.enclosingClass); | 2004 cachedClassBuilders.remove(element.enclosingClass); |
| 2010 | 2005 |
| 2011 nativeEmitter.cachedBuilders.remove(element.enclosingClass); | 2006 nativeEmitter.cachedBuilders.remove(element.enclosingClass); |
| 2012 | 2007 |
| 2013 } | 2008 } |
| 2014 } | 2009 } |
| 2015 } | 2010 } |
| 2016 } | 2011 } |
| OLD | NEW |