Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(712)

Side by Side Diff: pkg/compiler/lib/src/js_emitter/old_emitter/emitter.dart

Issue 906713002: Extract invoke-main stub generator. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Rebase and remove whitespace. Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 896 matching lines...) Expand 10 before | Expand all | Expand 10 after
907 backend.rti.getFunctionThatReturnsNullName]), 907 backend.rti.getFunctionThatReturnsNullName]),
908 compiler, monitor: compiler.dumpInfoTask)); 908 compiler, monitor: compiler.dumpInfoTask));
909 output.add(N); 909 output.add(N);
910 } 910 }
911 911
912 jsAst.Expression generateFunctionThatReturnsNull() { 912 jsAst.Expression generateFunctionThatReturnsNull() {
913 return js("#.#", [backend.namer.currentIsolate, 913 return js("#.#", [backend.namer.currentIsolate,
914 backend.rti.getFunctionThatReturnsNullName]); 914 backend.rti.getFunctionThatReturnsNullName]);
915 } 915 }
916 916
917 /// Returns the code equivalent to: 917 emitMain(CodeOutput output, jsAst.Statement invokeMain) {
918 /// `function(args) { $.startRootIsolate(X.main$closure(), args); }`
919 jsAst.Expression buildIsolateSetupClosure(Element appMain,
920 Element isolateMain) {
921 jsAst.Expression mainAccess = isolateStaticClosureAccess(appMain);
922 // Since we pass the closurized version of the main method to
923 // the isolate method, we must make sure that it exists.
924 return js('function(a){ #(#, a); }',
925 [backend.emitter.staticFunctionAccess(isolateMain), mainAccess]);
926 }
927
928 emitMain(CodeOutput output) {
929 if (compiler.isMockCompilation) return; 918 if (compiler.isMockCompilation) return;
930 Element main = compiler.mainFunction;
931 jsAst.Expression mainCallClosure = null;
932 if (compiler.hasIsolateSupport) {
933 Element isolateMain =
934 backend.isolateHelperLibrary.find(JavaScriptBackend.START_ROOT_ISOLATE);
935 mainCallClosure = buildIsolateSetupClosure(main, isolateMain);
936 } else if (compiler.hasIncrementalSupport) {
937 mainCallClosure = js(
938 'function() { return #(); }',
939 backend.emitter.staticFunctionAccess(main));
940 } else {
941 mainCallClosure = backend.emitter.staticFunctionAccess(main);
942 }
943 919
944 if (NativeGenerator.needsIsolateAffinityTagInitialization(backend)) { 920 if (NativeGenerator.needsIsolateAffinityTagInitialization(backend)) {
945 jsAst.Statement nativeBoilerPlate = 921 jsAst.Statement nativeBoilerPlate =
946 NativeGenerator.generateIsolateAffinityTagInitialization( 922 NativeGenerator.generateIsolateAffinityTagInitialization(
947 backend, 923 backend,
948 generateEmbeddedGlobalAccess, 924 generateEmbeddedGlobalAccess,
949 js("convertToFastObject", [])); 925 js("convertToFastObject", []));
950 output.addBuffer(jsAst.prettyPrint( 926 output.addBuffer(jsAst.prettyPrint(
951 nativeBoilerPlate, compiler, monitor: compiler.dumpInfoTask)); 927 nativeBoilerPlate, compiler, monitor: compiler.dumpInfoTask));
952 } 928 }
953 929
954 jsAst.Expression currentScriptAccess =
955 generateEmbeddedGlobalAccess(embeddedNames.CURRENT_SCRIPT);
956
957 addComment('BEGIN invoke [main].', output); 930 addComment('BEGIN invoke [main].', output);
958 // This code finds the currently executing script by listening to the
959 // onload event of all script tags and getting the first script which
960 // finishes. Since onload is called immediately after execution this should
961 // not substantially change execution order.
962 jsAst.Statement invokeMain = js.statement('''
963 (function (callback) {
964 if (typeof document === "undefined") {
965 callback(null);
966 return;
967 }
968 if (document.currentScript) {
969 callback(document.currentScript);
970 return;
971 }
972
973 var scripts = document.scripts;
974 function onLoad(event) {
975 for (var i = 0; i < scripts.length; ++i) {
976 scripts[i].removeEventListener("load", onLoad, false);
977 }
978 callback(event.target);
979 }
980 for (var i = 0; i < scripts.length; ++i) {
981 scripts[i].addEventListener("load", onLoad, false);
982 }
983 })(function(currentScript) {
984 #currentScript = currentScript;
985
986 if (typeof dartMainRunner === "function") {
987 dartMainRunner(#mainCallClosure, []);
988 } else {
989 #mainCallClosure([]);
990 }
991 })''', {'currentScript': currentScriptAccess,
992 'mainCallClosure': mainCallClosure});
993
994 output.add(';');
995 output.addBuffer(jsAst.prettyPrint(invokeMain, 931 output.addBuffer(jsAst.prettyPrint(invokeMain,
996 compiler, monitor: compiler.dumpInfoTask)); 932 compiler, monitor: compiler.dumpInfoTask));
997 output.add(N); 933 output.add(N);
998 addComment('END invoke [main].', output); 934 addComment('END invoke [main].', output);
999 } 935 }
1000 936
1001 void emitInitFunction(CodeOutput output) { 937 void emitInitFunction(CodeOutput output) {
1002 String isolate = namer.currentIsolate; 938 String isolate = namer.currentIsolate;
1003 jsAst.Expression allClassesAccess = 939 jsAst.Expression allClassesAccess =
1004 generateEmbeddedGlobalAccess(embeddedNames.ALL_CLASSES); 940 generateEmbeddedGlobalAccess(embeddedNames.ALL_CLASSES);
(...skipping 346 matching lines...) Expand 10 before | Expand all | Expand 10 after
1351 ClassBuilder libraryBuilder = getElementDescriptor(libraryElement); 1287 ClassBuilder libraryBuilder = getElementDescriptor(libraryElement);
1352 for (Class cls in library.classes) { 1288 for (Class cls in library.classes) {
1353 emitClass(cls, libraryBuilder); 1289 emitClass(cls, libraryBuilder);
1354 } 1290 }
1355 1291
1356 classEmitter.emitFields(library, libraryBuilder, emitStatics: true); 1292 classEmitter.emitFields(library, libraryBuilder, emitStatics: true);
1357 } 1293 }
1358 1294
1359 void emitMainOutputUnit(Program program, 1295 void emitMainOutputUnit(Program program,
1360 Map<OutputUnit, String> deferredLoadHashes) { 1296 Map<OutputUnit, String> deferredLoadHashes) {
1361 Fragment mainFragment = program.fragments.first; 1297 MainFragment mainFragment = program.fragments.first;
1362 OutputUnit mainOutputUnit = mainFragment.outputUnit; 1298 OutputUnit mainOutputUnit = mainFragment.outputUnit;
1363 1299
1364 LineColumnCollector lineColumnCollector; 1300 LineColumnCollector lineColumnCollector;
1365 List<CodeOutputListener> codeOutputListeners; 1301 List<CodeOutputListener> codeOutputListeners;
1366 if (generateSourceMap) { 1302 if (generateSourceMap) {
1367 lineColumnCollector = new LineColumnCollector(); 1303 lineColumnCollector = new LineColumnCollector();
1368 codeOutputListeners = <CodeOutputListener>[lineColumnCollector]; 1304 codeOutputListeners = <CodeOutputListener>[lineColumnCollector];
1369 } 1305 }
1370 1306
1371 CodeOutput mainOutput = 1307 CodeOutput mainOutput =
(...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after
1565 + String(Object.getOwnPropertyNames($object).length) 1501 + String(Object.getOwnPropertyNames($object).length)
1566 + ", fast properties " + HasFastProperties($object)); 1502 + ", fast properties " + HasFastProperties($object));
1567 } 1503 }
1568 '''); 1504 ''');
1569 } 1505 }
1570 } 1506 }
1571 1507
1572 jsAst.FunctionDeclaration precompiledFunctionAst = 1508 jsAst.FunctionDeclaration precompiledFunctionAst =
1573 buildCspPrecompiledFunctionFor(mainOutputUnit); 1509 buildCspPrecompiledFunctionFor(mainOutputUnit);
1574 emitInitFunction(mainOutput); 1510 emitInitFunction(mainOutput);
1575 emitMain(mainOutput); 1511 emitMain(mainOutput, mainFragment.invokeMain);
1576 mainOutput.add('})()\n'); 1512 mainOutput.add('})()\n');
1577 1513
1578 if (compiler.useContentSecurityPolicy) { 1514 if (compiler.useContentSecurityPolicy) {
1579 mainOutput.addBuffer( 1515 mainOutput.addBuffer(
1580 jsAst.prettyPrint( 1516 jsAst.prettyPrint(
1581 precompiledFunctionAst, 1517 precompiledFunctionAst,
1582 compiler, 1518 compiler,
1583 monitor: compiler.dumpInfoTask, 1519 monitor: compiler.dumpInfoTask,
1584 allowVariableMinification: false)); 1520 allowVariableMinification: false));
1585 } 1521 }
(...skipping 467 matching lines...) Expand 10 before | Expand all | Expand 10 after
2053 for (Element element in compiler.enqueuer.codegen.newlyEnqueuedElements) { 1989 for (Element element in compiler.enqueuer.codegen.newlyEnqueuedElements) {
2054 if (element.isInstanceMember) { 1990 if (element.isInstanceMember) {
2055 cachedClassBuilders.remove(element.enclosingClass); 1991 cachedClassBuilders.remove(element.enclosingClass);
2056 1992
2057 nativeEmitter.cachedBuilders.remove(element.enclosingClass); 1993 nativeEmitter.cachedBuilders.remove(element.enclosingClass);
2058 1994
2059 } 1995 }
2060 } 1996 }
2061 } 1997 }
2062 } 1998 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698