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 303 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
314 // The sets could be much smaller if we could make assumptions about the | 314 // The sets could be much smaller if we could make assumptions about the |
315 // cls tags of other classes (which are constructor names or part of the | 315 // cls tags of other classes (which are constructor names or part of the |
316 // result of Object.protocls.toString). For example, if objects that are | 316 // result of Object.protocls.toString). For example, if objects that are |
317 // Dart objects could be easily excluded, then we might be able to simplify | 317 // Dart objects could be easily excluded, then we might be able to simplify |
318 // the test, replacing dozens of HTMLxxxElement classes with the regexp | 318 // the test, replacing dozens of HTMLxxxElement classes with the regexp |
319 // /HTML.*Element/. | 319 // /HTML.*Element/. |
320 | 320 |
321 // Temporary variables for common substrings. | 321 // Temporary variables for common substrings. |
322 List<String> varNames = <String>[]; | 322 List<String> varNames = <String>[]; |
323 // var -> expression | 323 // var -> expression |
324 Map<String, js.Expression> varDefns = <String, js.Expression>{}; | 324 Map<dynamic, js.Expression> varDefns = new Map<dynamic, js.Expression>(); |
325 // tag -> expression (a string or a variable) | 325 // tag -> expression (a string or a variable) |
326 Map<ClassElement, js.Expression> tagDefns = | 326 Map<ClassElement, js.Expression> tagDefns = |
327 new Map<ClassElement, js.Expression>(); | 327 new Map<ClassElement, js.Expression>(); |
328 | 328 |
329 String makeExpression(ClassElement classElement) { | 329 js.Expression makeExpression(ClassElement classElement) { |
330 // Expression fragments for this set of cls keys. | 330 // Expression fragments for this set of cls keys. |
331 List<js.Expression> expressions = <js.Expression>[]; | 331 List<js.Expression> expressions = <js.Expression>[]; |
332 // TODO: Remove if cls is abstract. | 332 // TODO: Remove if cls is abstract. |
333 List<String> subtags = [toNativeName(classElement)]; | 333 List<String> subtags = [toNativeName(classElement)]; |
334 void walk(ClassElement cls) { | 334 void walk(ClassElement cls) { |
335 for (final ClassElement subclass in getDirectSubclasses(cls)) { | 335 for (final ClassElement subclass in getDirectSubclasses(cls)) { |
336 ClassElement tag = subclass; | 336 ClassElement tag = subclass; |
337 String existing = tagDefns[tag]; | 337 var existing = tagDefns[tag]; |
338 if (existing == null) { | 338 if (existing == null) { |
339 subtags.add(toNativeName(tag)); | 339 subtags.add(toNativeName(tag)); |
340 walk(subclass); | 340 walk(subclass); |
341 } else { | 341 } else { |
342 if (varDefns.containsKey(existing)) { | 342 if (varDefns.containsKey(existing)) { |
343 expressions.add(existing); | 343 expressions.add(existing); |
344 } else { | 344 } else { |
345 String varName = 'v${varNames.length}_${tag.name.slowToString()}'; | 345 String varName = 'v${varNames.length}_${tag.name.slowToString()}'; |
346 varNames.add(varName); | 346 varNames.add(varName); |
347 varDefns[varName] = existing; | 347 varDefns[varName] = existing; |
(...skipping 21 matching lines...) Expand all Loading... |
369 } | 369 } |
370 | 370 |
371 for (final ClassElement classElement in dispatchClasses) { | 371 for (final ClassElement classElement in dispatchClasses) { |
372 tagDefns[classElement] = makeExpression(classElement); | 372 tagDefns[classElement] = makeExpression(classElement); |
373 } | 373 } |
374 | 374 |
375 // Write out a thunk that builds the metadata. | 375 // Write out a thunk that builds the metadata. |
376 if (!tagDefns.isEmpty) { | 376 if (!tagDefns.isEmpty) { |
377 List<js.Statement> statements = <js.Statement>[]; | 377 List<js.Statement> statements = <js.Statement>[]; |
378 | 378 |
379 List<js.Expression> initializations = <js.Expression>[]; | 379 List<js.VariableInitialization> initializations = |
| 380 <js.VariableInitialization>[]; |
380 for (final String varName in varNames) { | 381 for (final String varName in varNames) { |
381 initializations.add( | 382 initializations.add( |
382 new js.VariableInitialization( | 383 new js.VariableInitialization( |
383 new js.VariableDeclaration(varName), | 384 new js.VariableDeclaration(varName), |
384 varDefns[varName])); | 385 varDefns[varName])); |
385 } | 386 } |
386 if (!initializations.isEmpty) { | 387 if (!initializations.isEmpty) { |
387 statements.add( | 388 statements.add( |
388 new js.ExpressionStatement( | 389 new js.ExpressionStatement( |
389 new js.VariableDeclarationList(initializations))); | 390 new js.VariableDeclarationList(initializations))); |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
499 if (!first) targetBuffer.add(",\n"); | 500 if (!first) targetBuffer.add(",\n"); |
500 targetBuffer.add(" $name: $function"); | 501 targetBuffer.add(" $name: $function"); |
501 first = false; | 502 first = false; |
502 }); | 503 }); |
503 targetBuffer.add("\n});\n\n"); | 504 targetBuffer.add("\n});\n\n"); |
504 } | 505 } |
505 targetBuffer.add(nativeBuffer); | 506 targetBuffer.add(nativeBuffer); |
506 targetBuffer.add('\n'); | 507 targetBuffer.add('\n'); |
507 } | 508 } |
508 } | 509 } |
OLD | NEW |