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 dart2js.js_emitter; | 5 part of dart2js.js_emitter; |
6 | 6 |
7 /** | 7 /** |
8 * Generates the code for all used classes in the program. Static fields (even | 8 * Generates the code for all used classes in the program. Static fields (even |
9 * in classes) are ignored, since they can be treated as non-class elements. | 9 * in classes) are ignored, since they can be treated as non-class elements. |
10 * | 10 * |
(...skipping 956 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
967 Element appMain, | 967 Element appMain, |
968 Element isolateMain) { | 968 Element isolateMain) { |
969 String mainAccess = "${namer.isolateStaticClosureAccess(appMain)}"; | 969 String mainAccess = "${namer.isolateStaticClosureAccess(appMain)}"; |
970 // Since we pass the closurized version of the main method to | 970 // Since we pass the closurized version of the main method to |
971 // the isolate method, we must make sure that it exists. | 971 // the isolate method, we must make sure that it exists. |
972 return "${namer.isolateAccess(isolateMain)}($mainAccess)"; | 972 return "${namer.isolateAccess(isolateMain)}($mainAccess)"; |
973 } | 973 } |
974 | 974 |
975 jsAst.Expression generateDispatchPropertyInitialization() { | 975 jsAst.Expression generateDispatchPropertyInitialization() { |
976 return js('!#', js.fun([], [ | 976 return js('!#', js.fun([], [ |
977 js('var objectProto = Object.prototype'), | 977 |
| 978 // On V8, the 'intern' function converts a string to a symbol, which |
| 979 // makes property access much faster. |
| 980 new jsAst.FunctionDeclaration(new jsAst.VariableDeclaration('intern'), |
| 981 js.fun(['s'], [ |
| 982 js('var o = {}'), |
| 983 js('o[s] = 1'), |
| 984 js.return_(js('Object.keys(convertToFastObject(o))[0]'))])), |
| 985 |
| 986 // To ensure that different programs loaded into the same context (page) |
| 987 // use distinct dispatch properies, we place an object on `Object` to |
| 988 // contain the names already in use. |
| 989 js('var tableProperty = "___dart_dispatch_property_names_"'), |
| 990 js('var usedProperties = Object[tableProperty] ||' |
| 991 '(Object[tableProperty] = Object.create(null))'), |
| 992 |
| 993 js('var rootProperty = "${generateDispatchPropertyName()}"'), |
978 js.for_('var i = 0', null, 'i++', [ | 994 js.for_('var i = 0', null, 'i++', [ |
979 js('var property = "${generateDispatchPropertyName(0)}"'), | 995 js('property = intern(rootProperty + "_" + i + "_")'), |
980 js.if_('i > 0', js('property = rootProperty + "_" + i')), | 996 js.if_('!(property in usedProperties)', [ |
981 js.if_('!(property in objectProto)', | 997 js('usedProperties[property] = 1'), |
982 js.return_( | 998 js.return_( |
983 js('init.dispatchPropertyName = property')))])])()); | 999 js('init.dispatchPropertyName = property'))])])])()); |
984 } | 1000 } |
985 | 1001 |
986 String generateDispatchPropertyName(int seed) { | 1002 String generateDispatchPropertyName() { |
987 // TODO(sra): MD5 of contributing source code or URIs? | 1003 // TODO(sra): MD5 of contributing source code or URIs? |
988 return '___dart_dispatch_record_ZxYxX_${seed}_'; | 1004 return '___dart_dispatch_record_ZxYxX_'; |
989 } | 1005 } |
990 | 1006 |
991 emitMain(CodeBuffer buffer) { | 1007 emitMain(CodeBuffer buffer) { |
992 if (compiler.isMockCompilation) return; | 1008 if (compiler.isMockCompilation) return; |
993 Element main = compiler.mainFunction; | 1009 Element main = compiler.mainFunction; |
994 String mainCall = null; | 1010 String mainCall = null; |
995 if (compiler.hasIsolateSupport()) { | 1011 if (compiler.hasIsolateSupport()) { |
996 Element isolateMain = | 1012 Element isolateMain = |
997 compiler.isolateHelperLibrary.find(Compiler.START_ROOT_ISOLATE); | 1013 compiler.isolateHelperLibrary.find(Compiler.START_ROOT_ISOLATE); |
998 mainCall = buildIsolateSetup(buffer, main, isolateMain); | 1014 mainCall = buildIsolateSetup(buffer, main, isolateMain); |
(...skipping 603 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1602 String sourceMap = buildSourceMap(mainBuffer, compiledFile); | 1618 String sourceMap = buildSourceMap(mainBuffer, compiledFile); |
1603 compiler.outputProvider(name, 'js.map') | 1619 compiler.outputProvider(name, 'js.map') |
1604 ..add(sourceMap) | 1620 ..add(sourceMap) |
1605 ..close(); | 1621 ..close(); |
1606 } | 1622 } |
1607 | 1623 |
1608 void registerReadTypeVariable(TypeVariableElement element) { | 1624 void registerReadTypeVariable(TypeVariableElement element) { |
1609 readTypeVariables.add(element); | 1625 readTypeVariables.add(element); |
1610 } | 1626 } |
1611 } | 1627 } |
OLD | NEW |