| 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 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 47 String get dynamicFunctionTableName { | 47 String get dynamicFunctionTableName { |
| 48 Element element = compiler.findHelper('dynamicFunctionTable'); | 48 Element element = compiler.findHelper('dynamicFunctionTable'); |
| 49 return backend.namer.isolateAccess(element); | 49 return backend.namer.isolateAccess(element); |
| 50 } | 50 } |
| 51 | 51 |
| 52 String get typeNameOfName { | 52 String get typeNameOfName { |
| 53 Element element = compiler.findHelper('getTypeNameOf'); | 53 Element element = compiler.findHelper('getTypeNameOf'); |
| 54 return backend.namer.isolateAccess(element); | 54 return backend.namer.isolateAccess(element); |
| 55 } | 55 } |
| 56 | 56 |
| 57 String get defPropName { | 57 jsAst.Expression get defPropFunction { |
| 58 Element element = compiler.findHelper('defineProperty'); | 58 Element element = compiler.findHelper('defineProperty'); |
| 59 return backend.namer.isolateAccess(element); | 59 return backend.namer.elementAccess(element); |
| 60 } | 60 } |
| 61 | 61 |
| 62 String get toStringHelperName { | 62 String get toStringHelperName { |
| 63 Element element = compiler.findHelper('toStringForNativeObject'); | 63 Element element = compiler.findHelper('toStringForNativeObject'); |
| 64 return backend.namer.isolateAccess(element); | 64 return backend.namer.isolateAccess(element); |
| 65 } | 65 } |
| 66 | 66 |
| 67 String get hashCodeHelperName { | 67 String get hashCodeHelperName { |
| 68 Element element = compiler.findHelper('hashCodeForNativeObject'); | 68 Element element = compiler.findHelper('hashCodeForNativeObject'); |
| 69 return backend.namer.isolateAccess(element); | 69 return backend.namer.isolateAccess(element); |
| (...skipping 278 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 348 // specializations. | 348 // specializations. |
| 349 } | 349 } |
| 350 | 350 |
| 351 void potentiallyConvertDartClosuresToJs( | 351 void potentiallyConvertDartClosuresToJs( |
| 352 List<jsAst.Statement> statements, | 352 List<jsAst.Statement> statements, |
| 353 FunctionElement member, | 353 FunctionElement member, |
| 354 List<jsAst.Parameter> stubParameters) { | 354 List<jsAst.Parameter> stubParameters) { |
| 355 FunctionSignature parameters = member.functionSignature; | 355 FunctionSignature parameters = member.functionSignature; |
| 356 Element converter = | 356 Element converter = |
| 357 compiler.findHelper('convertDartClosureToJS'); | 357 compiler.findHelper('convertDartClosureToJS'); |
| 358 String closureConverter = backend.namer.isolateAccess(converter); | 358 jsAst.Expression closureConverter = backend.namer.elementAccess(converter); |
| 359 Set<String> stubParameterNames = new Set<String>.from( | |
| 360 stubParameters.map((param) => param.name)); | |
| 361 parameters.forEachParameter((ParameterElement parameter) { | 359 parameters.forEachParameter((ParameterElement parameter) { |
| 362 String name = parameter.name; | 360 String name = parameter.name; |
| 363 // If [name] is not in [stubParameters], then the parameter is an optional | 361 // If [name] is not in [stubParameters], then the parameter is an optional |
| 364 // parameter that was not provided for this stub. | 362 // parameter that was not provided for this stub. |
| 365 for (jsAst.Parameter stubParameter in stubParameters) { | 363 for (jsAst.Parameter stubParameter in stubParameters) { |
| 366 if (stubParameter.name == name) { | 364 if (stubParameter.name == name) { |
| 367 DartType type = parameter.type.unalias(compiler); | 365 DartType type = parameter.type.unalias(compiler); |
| 368 if (type is FunctionType) { | 366 if (type is FunctionType) { |
| 369 // The parameter type is a function type either directly or through | 367 // The parameter type is a function type either directly or through |
| 370 // typedef(s). | 368 // typedef(s). |
| 371 FunctionType functionType = type; | 369 FunctionType functionType = type; |
| 372 int arity = functionType.computeArity(); | 370 int arity = functionType.computeArity(); |
| 373 statements.add( | 371 statements.add( |
| 374 js('$name = $closureConverter($name, $arity)').toStatement()); | 372 js.statement('# = #(#, $arity)', |
| 373 [name, closureConverter, name])); |
| 375 break; | 374 break; |
| 376 } | 375 } |
| 377 } | 376 } |
| 378 } | 377 } |
| 379 }); | 378 }); |
| 380 } | 379 } |
| 381 | 380 |
| 382 List<jsAst.Statement> generateParameterStubStatements( | 381 List<jsAst.Statement> generateParameterStubStatements( |
| 383 Element member, | 382 Element member, |
| 384 bool isInterceptedMethod, | 383 bool isInterceptedMethod, |
| (...skipping 26 matching lines...) Expand all Loading... |
| 411 | 410 |
| 412 if (isInterceptedMethod) { | 411 if (isInterceptedMethod) { |
| 413 receiver = argumentsBuffer[0]; | 412 receiver = argumentsBuffer[0]; |
| 414 arguments = argumentsBuffer.sublist(1, | 413 arguments = argumentsBuffer.sublist(1, |
| 415 indexOfLastOptionalArgumentInParameters + 1); | 414 indexOfLastOptionalArgumentInParameters + 1); |
| 416 } else { | 415 } else { |
| 417 receiver = js('this'); | 416 receiver = js('this'); |
| 418 arguments = argumentsBuffer.sublist(0, | 417 arguments = argumentsBuffer.sublist(0, |
| 419 indexOfLastOptionalArgumentInParameters + 1); | 418 indexOfLastOptionalArgumentInParameters + 1); |
| 420 } | 419 } |
| 421 statements.add(new jsAst.Return(receiver[target](arguments))); | 420 statements.add( |
| 421 js.statement('return #.#(#)', [receiver, target, arguments])); |
| 422 | 422 |
| 423 return statements; | 423 return statements; |
| 424 } | 424 } |
| 425 | 425 |
| 426 bool isSupertypeOfNativeClass(Element element) { | 426 bool isSupertypeOfNativeClass(Element element) { |
| 427 if (element.isTypeVariable()) { | 427 if (element.isTypeVariable()) { |
| 428 compiler.internalError(element, "Is check for type variable."); | 428 compiler.internalError(element, "Is check for type variable."); |
| 429 return false; | 429 return false; |
| 430 } | 430 } |
| 431 if (element.computeType(compiler).unalias(compiler) is FunctionType) { | 431 if (element.computeType(compiler).unalias(compiler) is FunctionType) { |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 470 // If the native emitter has been asked to take care of the | 470 // If the native emitter has been asked to take care of the |
| 471 // noSuchMethod handlers, we do that now. | 471 // noSuchMethod handlers, we do that now. |
| 472 if (handleNoSuchMethod) { | 472 if (handleNoSuchMethod) { |
| 473 emitter.nsmEmitter.emitNoSuchMethodHandlers(addProperty); | 473 emitter.nsmEmitter.emitNoSuchMethodHandlers(addProperty); |
| 474 } | 474 } |
| 475 } | 475 } |
| 476 | 476 |
| 477 // If we have any properties to add to Object.prototype, we run | 477 // If we have any properties to add to Object.prototype, we run |
| 478 // through them and add them using defineProperty. | 478 // through them and add them using defineProperty. |
| 479 if (!objectProperties.isEmpty) { | 479 if (!objectProperties.isEmpty) { |
| 480 jsAst.Expression init = | 480 jsAst.Expression init = js(r''' |
| 481 js.fun(['table'], | 481 (function(table) { |
| 482 new jsAst.ForIn( | 482 for(var key in table) |
| 483 new jsAst.VariableDeclarationList( | 483 #(Object.prototype, key, table[key]); |
| 484 [new jsAst.VariableInitialization( | 484 })(#)''', |
| 485 new jsAst.VariableDeclaration('key'), | 485 [ defPropFunction, |
| 486 null)]), | 486 new jsAst.ObjectInitializer(objectProperties)]); |
| 487 js('table'), | |
| 488 new jsAst.ExpressionStatement( | |
| 489 js('$defPropName(Object.prototype, key, table[key])'))))( | |
| 490 new jsAst.ObjectInitializer(objectProperties)); | |
| 491 | 487 |
| 492 if (emitter.compiler.enableMinification) targetBuffer.add(';'); | 488 if (emitter.compiler.enableMinification) targetBuffer.add(';'); |
| 493 targetBuffer.add(jsAst.prettyPrint( | 489 targetBuffer.add(jsAst.prettyPrint( |
| 494 new jsAst.ExpressionStatement(init), compiler)); | 490 new jsAst.ExpressionStatement(init), compiler)); |
| 495 targetBuffer.add('\n'); | 491 targetBuffer.add('\n'); |
| 496 } | 492 } |
| 497 | 493 |
| 498 targetBuffer.add(nativeBuffer); | 494 targetBuffer.add(nativeBuffer); |
| 499 targetBuffer.add('\n'); | 495 targetBuffer.add('\n'); |
| 500 } | 496 } |
| 501 } | 497 } |
| OLD | NEW |