| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 js_backend; | 5 part of js_backend; |
| 6 | 6 |
| 7 class NativeEmitter { | 7 class NativeEmitter { |
| 8 | 8 |
| 9 CodeEmitterTask emitter; | 9 CodeEmitterTask emitter; |
| 10 CodeBuffer nativeBuffer; | 10 CodeBuffer nativeBuffer; |
| (...skipping 231 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 242 // If a method is overridden, we must check if the prototype of 'this' has the | 242 // If a method is overridden, we must check if the prototype of 'this' has the |
| 243 // method available. Otherwise, we may end up calling the method from the | 243 // method available. Otherwise, we may end up calling the method from the |
| 244 // super class. If the method is not available, we make a direct call to | 244 // super class. If the method is not available, we make a direct call to |
| 245 // Object.prototype.$methodName. This method will patch the prototype of | 245 // Object.prototype.$methodName. This method will patch the prototype of |
| 246 // 'this' to the real method. | 246 // 'this' to the real method. |
| 247 jsAst.Statement generateMethodBodyWithPrototypeCheck( | 247 jsAst.Statement generateMethodBodyWithPrototypeCheck( |
| 248 String methodName, | 248 String methodName, |
| 249 jsAst.Statement body, | 249 jsAst.Statement body, |
| 250 List<jsAst.Parameter> parameters) { | 250 List<jsAst.Parameter> parameters) { |
| 251 return js.if_( | 251 return js.if_( |
| 252 js['Object']['getPrototypeOf']('this')['hasOwnProperty']( | 252 js['(Object.getPrototypeOf(this)).hasOwnProperty("$methodName")'], |
| 253 js.string(methodName)), | |
| 254 body, | 253 body, |
| 255 js.return_( | 254 js.return_( |
| 256 js['Object']['prototype'][methodName]['call']( | 255 js['Object.prototype.$methodName.call']( |
| 257 <jsAst.Expression>[js['this']]..addAll( | 256 <jsAst.Expression>[js['this']]..addAll( |
| 258 parameters.map((param) => js[param.name]))))); | 257 parameters.map((param) => js[param.name]))))); |
| 259 } | 258 } |
| 260 | 259 |
| 261 jsAst.Block generateMethodBodyWithPrototypeCheckForElement( | 260 jsAst.Block generateMethodBodyWithPrototypeCheckForElement( |
| 262 FunctionElement element, | 261 FunctionElement element, |
| 263 jsAst.Block body, | 262 jsAst.Block body, |
| 264 List<jsAst.Parameter> parameters) { | 263 List<jsAst.Parameter> parameters) { |
| 265 ElementKind kind = element.kind; | 264 ElementKind kind = element.kind; |
| 266 if (kind != ElementKind.FUNCTION && | 265 if (kind != ElementKind.FUNCTION && |
| (...skipping 205 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 472 // attach to the JS Object prototype these methods that return | 471 // attach to the JS Object prototype these methods that return |
| 473 // false, and will be overridden by subclasses when they have to | 472 // false, and will be overridden by subclasses when they have to |
| 474 // return true. | 473 // return true. |
| 475 void emitIsChecks() { | 474 void emitIsChecks() { |
| 476 for (ClassElement element in | 475 for (ClassElement element in |
| 477 Elements.sortedByPosition(emitter.checkedClasses)) { | 476 Elements.sortedByPosition(emitter.checkedClasses)) { |
| 478 if (!requiresNativeIsCheck(element)) continue; | 477 if (!requiresNativeIsCheck(element)) continue; |
| 479 if (element.isObject(compiler)) continue; | 478 if (element.isObject(compiler)) continue; |
| 480 String name = backend.namer.operatorIs(element); | 479 String name = backend.namer.operatorIs(element); |
| 481 addProperty(name, | 480 addProperty(name, |
| 482 js.fun([], js.return_(new jsAst.LiteralBool(false)))); | 481 js.fun([], js.return_(js['false']))); |
| 483 } | 482 } |
| 484 } | 483 } |
| 485 emitIsChecks(); | 484 emitIsChecks(); |
| 486 | 485 |
| 487 jsAst.Expression makeCallOnThis(String functionName) { | 486 jsAst.Expression makeCallOnThis(String functionName) { |
| 488 return js.fun([], js.return_(js[functionName]('this'))); | 487 return js.fun([], js.return_(js['$functionName(this)'])); |
| 489 } | 488 } |
| 490 | 489 |
| 491 // In order to have the toString method on every native class, | 490 // In order to have the toString method on every native class, |
| 492 // we must patch the JS Object prototype with a helper method. | 491 // we must patch the JS Object prototype with a helper method. |
| 493 String toStringName = backend.namer.publicInstanceMethodNameByArity( | 492 String toStringName = backend.namer.publicInstanceMethodNameByArity( |
| 494 const SourceString('toString'), 0); | 493 const SourceString('toString'), 0); |
| 495 addProperty(toStringName, makeCallOnThis(toStringHelperName)); | 494 addProperty(toStringName, makeCallOnThis(toStringHelperName)); |
| 496 | 495 |
| 497 // Same as above, but for hashCode. | 496 // Same as above, but for hashCode. |
| 498 String hashCodeName = | 497 String hashCodeName = |
| 499 backend.namer.publicGetterName(const SourceString('hashCode')); | 498 backend.namer.publicGetterName(const SourceString('hashCode')); |
| 500 addProperty(hashCodeName, makeCallOnThis(hashCodeHelperName)); | 499 addProperty(hashCodeName, makeCallOnThis(hashCodeHelperName)); |
| 501 | 500 |
| 502 // Same as above, but for operator==. | 501 // Same as above, but for operator==. |
| 503 String equalsName = backend.namer.publicInstanceMethodNameByArity( | 502 String equalsName = backend.namer.publicInstanceMethodNameByArity( |
| 504 const SourceString('=='), 1); | 503 const SourceString('=='), 1); |
| 505 addProperty(equalsName, js.fun(['a'], | 504 addProperty(equalsName, js.fun(['a'], |
| 506 js.return_(js.strictEquals(new jsAst.This(), js['a'])))); | 505 js.return_(js['this === a']))); |
| 507 | 506 |
| 508 // If the native emitter has been asked to take care of the | 507 // If the native emitter has been asked to take care of the |
| 509 // noSuchMethod handlers, we do that now. | 508 // noSuchMethod handlers, we do that now. |
| 510 if (handleNoSuchMethod) { | 509 if (handleNoSuchMethod) { |
| 511 emitter.emitNoSuchMethodHandlers(addProperty); | 510 emitter.emitNoSuchMethodHandlers(addProperty); |
| 512 } | 511 } |
| 513 | 512 |
| 514 // If we have any properties to add to Object.prototype, we run | 513 // If we have any properties to add to Object.prototype, we run |
| 515 // through them and add them using defineProperty. | 514 // through them and add them using defineProperty. |
| 516 if (!objectProperties.isEmpty) { | 515 if (!objectProperties.isEmpty) { |
| 517 jsAst.Expression init = | 516 jsAst.Expression init = |
| 518 js.fun(['table'], | 517 js.fun(['table'], |
| 519 new jsAst.ForIn( | 518 new jsAst.ForIn( |
| 520 new jsAst.VariableDeclarationList( | 519 new jsAst.VariableDeclarationList( |
| 521 [new jsAst.VariableInitialization( | 520 [new jsAst.VariableInitialization( |
| 522 new jsAst.VariableDeclaration('key'), | 521 new jsAst.VariableDeclaration('key'), |
| 523 null)]), | 522 null)]), |
| 524 js['table'], | 523 js['table'], |
| 525 new jsAst.ExpressionStatement( | 524 new jsAst.ExpressionStatement( |
| 526 js[defPropName]( | 525 js['$defPropName(Object.prototype, key, table[key])'])))( |
| 527 [js['Object']['prototype'], | |
| 528 js['key'], | |
| 529 new jsAst.PropertyAccess(js['table'], | |
| 530 js['key'])]))))( | |
| 531 new jsAst.ObjectInitializer(objectProperties)); | 526 new jsAst.ObjectInitializer(objectProperties)); |
| 532 | 527 |
| 533 if (emitter.compiler.enableMinification) targetBuffer.add(';'); | 528 if (emitter.compiler.enableMinification) targetBuffer.add(';'); |
| 534 targetBuffer.add(jsAst.prettyPrint( | 529 targetBuffer.add(jsAst.prettyPrint( |
| 535 new jsAst.ExpressionStatement(init), compiler)); | 530 new jsAst.ExpressionStatement(init), compiler)); |
| 536 targetBuffer.add('\n'); | 531 targetBuffer.add('\n'); |
| 537 } | 532 } |
| 538 | 533 |
| 539 targetBuffer.add(nativeBuffer); | 534 targetBuffer.add(nativeBuffer); |
| 540 targetBuffer.add('\n'); | 535 targetBuffer.add('\n'); |
| 541 } | 536 } |
| 542 } | 537 } |
| OLD | NEW |