| 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 903 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 914 backend.rti.getFunctionThatReturnsNullName]), | 914 backend.rti.getFunctionThatReturnsNullName]), |
| 915 compiler, monitor: compiler.dumpInfoTask)); | 915 compiler, monitor: compiler.dumpInfoTask)); |
| 916 output.add(N); | 916 output.add(N); |
| 917 } | 917 } |
| 918 | 918 |
| 919 jsAst.Expression generateFunctionThatReturnsNull() { | 919 jsAst.Expression generateFunctionThatReturnsNull() { |
| 920 return js("#.#", [backend.namer.currentIsolate, | 920 return js("#.#", [backend.namer.currentIsolate, |
| 921 backend.rti.getFunctionThatReturnsNullName]); | 921 backend.rti.getFunctionThatReturnsNullName]); |
| 922 } | 922 } |
| 923 | 923 |
| 924 /// Returns the code equivalent to: | 924 emitMain(CodeOutput output, jsAst.Statement invokeMain) { |
| 925 /// `function(args) { $.startRootIsolate(X.main$closure(), args); }` | |
| 926 jsAst.Expression buildIsolateSetupClosure(Element appMain, | |
| 927 Element isolateMain) { | |
| 928 jsAst.Expression mainAccess = isolateStaticClosureAccess(appMain); | |
| 929 // Since we pass the closurized version of the main method to | |
| 930 // the isolate method, we must make sure that it exists. | |
| 931 return js('function(a){ #(#, a); }', | |
| 932 [backend.emitter.staticFunctionAccess(isolateMain), mainAccess]); | |
| 933 } | |
| 934 | |
| 935 emitMain(CodeOutput output) { | |
| 936 if (compiler.isMockCompilation) return; | 925 if (compiler.isMockCompilation) return; |
| 937 Element main = compiler.mainFunction; | |
| 938 jsAst.Expression mainCallClosure = null; | |
| 939 if (compiler.hasIsolateSupport) { | |
| 940 Element isolateMain = | |
| 941 backend.isolateHelperLibrary.find(JavaScriptBackend.START_ROOT_ISOLATE); | |
| 942 mainCallClosure = buildIsolateSetupClosure(main, isolateMain); | |
| 943 } else if (compiler.hasIncrementalSupport) { | |
| 944 mainCallClosure = js( | |
| 945 'function() { return #(); }', | |
| 946 backend.emitter.staticFunctionAccess(main)); | |
| 947 } else { | |
| 948 mainCallClosure = backend.emitter.staticFunctionAccess(main); | |
| 949 } | |
| 950 | 926 |
| 951 if (NativeGenerator.needsIsolateAffinityTagInitialization(backend)) { | 927 if (NativeGenerator.needsIsolateAffinityTagInitialization(backend)) { |
| 952 jsAst.Statement nativeBoilerPlate = | 928 jsAst.Statement nativeBoilerPlate = |
| 953 NativeGenerator.generateIsolateAffinityTagInitialization( | 929 NativeGenerator.generateIsolateAffinityTagInitialization( |
| 954 backend, | 930 backend, |
| 955 generateEmbeddedGlobalAccess, | 931 generateEmbeddedGlobalAccess, |
| 956 js("convertToFastObject", [])); | 932 js("convertToFastObject", [])); |
| 957 output.addBuffer(jsAst.prettyPrint( | 933 output.addBuffer(jsAst.prettyPrint( |
| 958 nativeBoilerPlate, compiler, monitor: compiler.dumpInfoTask)); | 934 nativeBoilerPlate, compiler, monitor: compiler.dumpInfoTask)); |
| 959 } | 935 } |
| 960 | 936 |
| 961 jsAst.Expression currentScriptAccess = | 937 output.add(';'); |
| 962 generateEmbeddedGlobalAccess(embeddedNames.CURRENT_SCRIPT); | |
| 963 | |
| 964 addComment('BEGIN invoke [main].', output); | 938 addComment('BEGIN invoke [main].', output); |
| 965 // This code finds the currently executing script by listening to the | |
| 966 // onload event of all script tags and getting the first script which | |
| 967 // finishes. Since onload is called immediately after execution this should | |
| 968 // not substantially change execution order. | |
| 969 jsAst.Statement invokeMain = js.statement(''' | |
| 970 (function (callback) { | |
| 971 if (typeof document === "undefined") { | |
| 972 callback(null); | |
| 973 return; | |
| 974 } | |
| 975 if (document.currentScript) { | |
| 976 callback(document.currentScript); | |
| 977 return; | |
| 978 } | |
| 979 | |
| 980 var scripts = document.scripts; | |
| 981 function onLoad(event) { | |
| 982 for (var i = 0; i < scripts.length; ++i) { | |
| 983 scripts[i].removeEventListener("load", onLoad, false); | |
| 984 } | |
| 985 callback(event.target); | |
| 986 } | |
| 987 for (var i = 0; i < scripts.length; ++i) { | |
| 988 scripts[i].addEventListener("load", onLoad, false); | |
| 989 } | |
| 990 })(function(currentScript) { | |
| 991 #currentScript = currentScript; | |
| 992 | |
| 993 if (typeof dartMainRunner === "function") { | |
| 994 dartMainRunner(#mainCallClosure, []); | |
| 995 } else { | |
| 996 #mainCallClosure([]); | |
| 997 } | |
| 998 })''', {'currentScript': currentScriptAccess, | |
| 999 'mainCallClosure': mainCallClosure}); | |
| 1000 | |
| 1001 output.add(';'); | |
| 1002 output.addBuffer(jsAst.prettyPrint(invokeMain, | 939 output.addBuffer(jsAst.prettyPrint(invokeMain, |
| 1003 compiler, monitor: compiler.dumpInfoTask)); | 940 compiler, monitor: compiler.dumpInfoTask)); |
| 1004 output.add(N); | 941 output.add(N); |
| 1005 addComment('END invoke [main].', output); | 942 addComment('END invoke [main].', output); |
| 1006 } | 943 } |
| 1007 | 944 |
| 1008 void emitInitFunction(CodeOutput output) { | 945 void emitInitFunction(CodeOutput output) { |
| 1009 String isolate = namer.currentIsolate; | 946 String isolate = namer.currentIsolate; |
| 1010 jsAst.Expression allClassesAccess = | 947 jsAst.Expression allClassesAccess = |
| 1011 generateEmbeddedGlobalAccess(embeddedNames.ALL_CLASSES); | 948 generateEmbeddedGlobalAccess(embeddedNames.ALL_CLASSES); |
| (...skipping 346 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1358 ClassBuilder libraryBuilder = getElementDescriptor(libraryElement); | 1295 ClassBuilder libraryBuilder = getElementDescriptor(libraryElement); |
| 1359 for (Class cls in library.classes) { | 1296 for (Class cls in library.classes) { |
| 1360 emitClass(cls, libraryBuilder); | 1297 emitClass(cls, libraryBuilder); |
| 1361 } | 1298 } |
| 1362 | 1299 |
| 1363 classEmitter.emitFields(library, libraryBuilder, emitStatics: true); | 1300 classEmitter.emitFields(library, libraryBuilder, emitStatics: true); |
| 1364 } | 1301 } |
| 1365 | 1302 |
| 1366 void emitMainOutputUnit(Program program, | 1303 void emitMainOutputUnit(Program program, |
| 1367 Map<OutputUnit, String> deferredLoadHashes) { | 1304 Map<OutputUnit, String> deferredLoadHashes) { |
| 1368 Fragment mainFragment = program.fragments.first; | 1305 MainFragment mainFragment = program.fragments.first; |
| 1369 OutputUnit mainOutputUnit = mainFragment.outputUnit; | 1306 OutputUnit mainOutputUnit = mainFragment.outputUnit; |
| 1370 | 1307 |
| 1371 LineColumnCollector lineColumnCollector; | 1308 LineColumnCollector lineColumnCollector; |
| 1372 List<CodeOutputListener> codeOutputListeners; | 1309 List<CodeOutputListener> codeOutputListeners; |
| 1373 if (generateSourceMap) { | 1310 if (generateSourceMap) { |
| 1374 lineColumnCollector = new LineColumnCollector(); | 1311 lineColumnCollector = new LineColumnCollector(); |
| 1375 codeOutputListeners = <CodeOutputListener>[lineColumnCollector]; | 1312 codeOutputListeners = <CodeOutputListener>[lineColumnCollector]; |
| 1376 } | 1313 } |
| 1377 | 1314 |
| 1378 CodeOutput mainOutput = | 1315 CodeOutput mainOutput = |
| (...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1572 + String(Object.getOwnPropertyNames($object).length) | 1509 + String(Object.getOwnPropertyNames($object).length) |
| 1573 + ", fast properties " + HasFastProperties($object)); | 1510 + ", fast properties " + HasFastProperties($object)); |
| 1574 } | 1511 } |
| 1575 '''); | 1512 '''); |
| 1576 } | 1513 } |
| 1577 } | 1514 } |
| 1578 | 1515 |
| 1579 jsAst.FunctionDeclaration precompiledFunctionAst = | 1516 jsAst.FunctionDeclaration precompiledFunctionAst = |
| 1580 buildCspPrecompiledFunctionFor(mainOutputUnit); | 1517 buildCspPrecompiledFunctionFor(mainOutputUnit); |
| 1581 emitInitFunction(mainOutput); | 1518 emitInitFunction(mainOutput); |
| 1582 emitMain(mainOutput); | 1519 emitMain(mainOutput, mainFragment.invokeMain); |
| 1583 mainOutput.add('})()\n'); | 1520 mainOutput.add('})()\n'); |
| 1584 | 1521 |
| 1585 if (compiler.useContentSecurityPolicy) { | 1522 if (compiler.useContentSecurityPolicy) { |
| 1586 mainOutput.addBuffer( | 1523 mainOutput.addBuffer( |
| 1587 jsAst.prettyPrint( | 1524 jsAst.prettyPrint( |
| 1588 precompiledFunctionAst, | 1525 precompiledFunctionAst, |
| 1589 compiler, | 1526 compiler, |
| 1590 monitor: compiler.dumpInfoTask, | 1527 monitor: compiler.dumpInfoTask, |
| 1591 allowVariableMinification: false)); | 1528 allowVariableMinification: false)); |
| 1592 } | 1529 } |
| (...skipping 467 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2060 for (Element element in compiler.enqueuer.codegen.newlyEnqueuedElements) { | 1997 for (Element element in compiler.enqueuer.codegen.newlyEnqueuedElements) { |
| 2061 if (element.isInstanceMember) { | 1998 if (element.isInstanceMember) { |
| 2062 cachedClassBuilders.remove(element.enclosingClass); | 1999 cachedClassBuilders.remove(element.enclosingClass); |
| 2063 | 2000 |
| 2064 nativeEmitter.cachedBuilders.remove(element.enclosingClass); | 2001 nativeEmitter.cachedBuilders.remove(element.enclosingClass); |
| 2065 | 2002 |
| 2066 } | 2003 } |
| 2067 } | 2004 } |
| 2068 } | 2005 } |
| 2069 } | 2006 } |
| OLD | NEW |