| 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 909 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 920 /// `function(args) { $.startRootIsolate(X.main$closure(), args); }` | 920 /// `function(args) { $.startRootIsolate(X.main$closure(), args); }` |
| 921 jsAst.Expression buildIsolateSetupClosure(Element appMain, | 921 jsAst.Expression buildIsolateSetupClosure(Element appMain, |
| 922 Element isolateMain) { | 922 Element isolateMain) { |
| 923 jsAst.Expression mainAccess = isolateStaticClosureAccess(appMain); | 923 jsAst.Expression mainAccess = isolateStaticClosureAccess(appMain); |
| 924 // Since we pass the closurized version of the main method to | 924 // Since we pass the closurized version of the main method to |
| 925 // the isolate method, we must make sure that it exists. | 925 // the isolate method, we must make sure that it exists. |
| 926 return js('function(a){ #(#, a); }', | 926 return js('function(a){ #(#, a); }', |
| 927 [backend.emitter.staticFunctionAccess(isolateMain), mainAccess]); | 927 [backend.emitter.staticFunctionAccess(isolateMain), mainAccess]); |
| 928 } | 928 } |
| 929 | 929 |
| 930 /** | |
| 931 * Emits code that sets the `isolateTag embedded global to a unique string. | |
| 932 */ | |
| 933 jsAst.Expression generateIsolateAffinityTagInitialization() { | |
| 934 jsAst.Expression getIsolateTagAccess = | |
| 935 generateEmbeddedGlobalAccess(embeddedNames.GET_ISOLATE_TAG); | |
| 936 jsAst.Expression isolateTagAccess = | |
| 937 generateEmbeddedGlobalAccess(embeddedNames.ISOLATE_TAG); | |
| 938 | |
| 939 return js(''' | |
| 940 !function() { | |
| 941 // On V8, the 'intern' function converts a string to a symbol, which | |
| 942 // makes property access much faster. | |
| 943 function intern(s) { | |
| 944 var o = {}; | |
| 945 o[s] = 1; | |
| 946 return Object.keys(convertToFastObject(o))[0]; | |
| 947 } | |
| 948 | |
| 949 #getIsolateTag = function(name) { | |
| 950 return intern("___dart_" + name + #isolateTag); | |
| 951 }; | |
| 952 | |
| 953 // To ensure that different programs loaded into the same context (page) | |
| 954 // use distinct dispatch properies, we place an object on `Object` to | |
| 955 // contain the names already in use. | |
| 956 var tableProperty = "___dart_isolate_tags_"; | |
| 957 var usedProperties = Object[tableProperty] || | |
| 958 (Object[tableProperty] = Object.create(null)); | |
| 959 | |
| 960 var rootProperty = "_${generateIsolateTagRoot()}"; | |
| 961 for (var i = 0; ; i++) { | |
| 962 var property = intern(rootProperty + "_" + i + "_"); | |
| 963 if (!(property in usedProperties)) { | |
| 964 usedProperties[property] = 1; | |
| 965 #isolateTag = property; | |
| 966 break; | |
| 967 } | |
| 968 } | |
| 969 }() | |
| 970 ''', | |
| 971 {'getIsolateTag': getIsolateTagAccess, 'isolateTag': isolateTagAccess}); | |
| 972 } | |
| 973 | |
| 974 jsAst.Expression generateDispatchPropertyNameInitialization() { | |
| 975 jsAst.Expression dispatchPropertyNameAccess = | |
| 976 generateEmbeddedGlobalAccess(embeddedNames.DISPATCH_PROPERTY_NAME); | |
| 977 jsAst.Expression getIsolateTagAccess = | |
| 978 generateEmbeddedGlobalAccess(embeddedNames.GET_ISOLATE_TAG); | |
| 979 return js('# = #("dispatch_record")', | |
| 980 [dispatchPropertyNameAccess, | |
| 981 getIsolateTagAccess]); | |
| 982 } | |
| 983 | |
| 984 String generateIsolateTagRoot() { | |
| 985 // TODO(sra): MD5 of contributing source code or URIs? | |
| 986 return 'ZxYxX'; | |
| 987 } | |
| 988 | |
| 989 emitMain(CodeBuffer buffer) { | 930 emitMain(CodeBuffer buffer) { |
| 990 if (compiler.isMockCompilation) return; | 931 if (compiler.isMockCompilation) return; |
| 991 Element main = compiler.mainFunction; | 932 Element main = compiler.mainFunction; |
| 992 jsAst.Expression mainCallClosure = null; | 933 jsAst.Expression mainCallClosure = null; |
| 993 if (compiler.hasIsolateSupport) { | 934 if (compiler.hasIsolateSupport) { |
| 994 Element isolateMain = | 935 Element isolateMain = |
| 995 backend.isolateHelperLibrary.find(JavaScriptBackend.START_ROOT_ISOLATE); | 936 backend.isolateHelperLibrary.find(JavaScriptBackend.START_ROOT_ISOLATE); |
| 996 mainCallClosure = buildIsolateSetupClosure(main, isolateMain); | 937 mainCallClosure = buildIsolateSetupClosure(main, isolateMain); |
| 997 } else if (compiler.hasIncrementalSupport) { | 938 } else if (compiler.hasIncrementalSupport) { |
| 998 mainCallClosure = js( | 939 mainCallClosure = js( |
| 999 'function() { return #(); }', | 940 'function() { return #(); }', |
| 1000 backend.emitter.staticFunctionAccess(main)); | 941 backend.emitter.staticFunctionAccess(main)); |
| 1001 } else { | 942 } else { |
| 1002 mainCallClosure = backend.emitter.staticFunctionAccess(main); | 943 mainCallClosure = backend.emitter.staticFunctionAccess(main); |
| 1003 } | 944 } |
| 1004 | 945 |
| 946 NativeGenerator nativeGenerator = |
| 947 new NativeGenerator(generateEmbeddedGlobalAccess); |
| 1005 if (backend.needToInitializeIsolateAffinityTag) { | 948 if (backend.needToInitializeIsolateAffinityTag) { |
| 1006 buffer.write( | 949 buffer.write( |
| 1007 jsAst.prettyPrint(generateIsolateAffinityTagInitialization(), | 950 jsAst.prettyPrint( |
| 1008 compiler, monitor: compiler.dumpInfoTask)); | 951 nativeGenerator.generateIsolateAffinityTagInitialization(), |
| 952 compiler, monitor: compiler.dumpInfoTask)); |
| 1009 buffer.write(N); | 953 buffer.write(N); |
| 1010 } | 954 } |
| 1011 if (backend.needToInitializeDispatchProperty) { | 955 if (backend.needToInitializeDispatchProperty) { |
| 1012 assert(backend.needToInitializeIsolateAffinityTag); | 956 assert(backend.needToInitializeIsolateAffinityTag); |
| 1013 buffer.write( | 957 buffer.write( |
| 1014 jsAst.prettyPrint(generateDispatchPropertyNameInitialization(), | 958 jsAst.prettyPrint( |
| 959 nativeGenerator.generateDispatchPropertyNameInitialization(), |
| 1015 compiler, monitor: compiler.dumpInfoTask)); | 960 compiler, monitor: compiler.dumpInfoTask)); |
| 1016 buffer.write(N); | 961 buffer.write(N); |
| 1017 } | 962 } |
| 1018 | 963 |
| 1019 jsAst.Expression currentScriptAccess = | 964 jsAst.Expression currentScriptAccess = |
| 1020 generateEmbeddedGlobalAccess(embeddedNames.CURRENT_SCRIPT); | 965 generateEmbeddedGlobalAccess(embeddedNames.CURRENT_SCRIPT); |
| 1021 | 966 |
| 1022 addComment('BEGIN invoke [main].', buffer); | 967 addComment('BEGIN invoke [main].', buffer); |
| 1023 // This code finds the currently executing script by listening to the | 968 // This code finds the currently executing script by listening to the |
| 1024 // onload event of all script tags and getting the first script which | 969 // onload event of all script tags and getting the first script which |
| (...skipping 1069 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2094 for (Element element in compiler.enqueuer.codegen.newlyEnqueuedElements) { | 2039 for (Element element in compiler.enqueuer.codegen.newlyEnqueuedElements) { |
| 2095 if (element.isInstanceMember) { | 2040 if (element.isInstanceMember) { |
| 2096 cachedClassBuilders.remove(element.enclosingClass); | 2041 cachedClassBuilders.remove(element.enclosingClass); |
| 2097 | 2042 |
| 2098 nativeEmitter.cachedBuilders.remove(element.enclosingClass); | 2043 nativeEmitter.cachedBuilders.remove(element.enclosingClass); |
| 2099 | 2044 |
| 2100 } | 2045 } |
| 2101 } | 2046 } |
| 2102 } | 2047 } |
| 2103 } | 2048 } |
| OLD | NEW |