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 /** | 7 /** |
8 * A function element that represents a closure call. The signature is copied | 8 * A function element that represents a closure call. The signature is copied |
9 * from the given element. | 9 * from the given element. |
10 */ | 10 */ |
(...skipping 2930 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2941 } | 2941 } |
2942 | 2942 |
2943 String assembleProgram() { | 2943 String assembleProgram() { |
2944 measure(() { | 2944 measure(() { |
2945 // Compute the required type checks to know which classes need a | 2945 // Compute the required type checks to know which classes need a |
2946 // 'is$' method. | 2946 // 'is$' method. |
2947 computeRequiredTypeChecks(); | 2947 computeRequiredTypeChecks(); |
2948 | 2948 |
2949 computeNeededClasses(); | 2949 computeNeededClasses(); |
2950 | 2950 |
2951 mainBuffer.add(buildGeneratedBy()); | 2951 mainBuffer.add(GENERATED_BY); |
2952 addComment(HOOKS_API_USAGE, mainBuffer); | 2952 addComment(HOOKS_API_USAGE, mainBuffer); |
2953 mainBuffer.add('function ${namer.isolateName}()$_{}\n'); | 2953 mainBuffer.add('function ${namer.isolateName}()$_{}\n'); |
2954 mainBuffer.add('init()$N$n'); | 2954 mainBuffer.add('init()$N$n'); |
2955 // Shorten the code by using [namer.CURRENT_ISOLATE] as temporary. | 2955 // Shorten the code by using [namer.CURRENT_ISOLATE] as temporary. |
2956 isolateProperties = namer.CURRENT_ISOLATE; | 2956 isolateProperties = namer.CURRENT_ISOLATE; |
2957 mainBuffer.add( | 2957 mainBuffer.add( |
2958 'var $isolateProperties$_=$_$isolatePropertiesName$N'); | 2958 'var $isolateProperties$_=$_$isolatePropertiesName$N'); |
2959 | 2959 |
2960 if (!regularClasses.isEmpty || | 2960 if (!regularClasses.isEmpty || |
2961 !deferredClasses.isEmpty || | 2961 !deferredClasses.isEmpty || |
(...skipping 204 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3166 String code = buffer.getText(); | 3166 String code = buffer.getText(); |
3167 compiler.outputProvider('part', 'js') | 3167 compiler.outputProvider('part', 'js') |
3168 ..add(code) | 3168 ..add(code) |
3169 ..close(); | 3169 ..close(); |
3170 outputSourceMap(buffer, compiler.assembledCode, 'part'); | 3170 outputSourceMap(buffer, compiler.assembledCode, 'part'); |
3171 } | 3171 } |
3172 | 3172 |
3173 void emitDeferredPreambleWhenEmpty(CodeBuffer buffer) { | 3173 void emitDeferredPreambleWhenEmpty(CodeBuffer buffer) { |
3174 if (!buffer.isEmpty) return; | 3174 if (!buffer.isEmpty) return; |
3175 | 3175 |
3176 buffer.write(buildGeneratedBy()); | 3176 buffer.write(GENERATED_BY); |
3177 | 3177 |
3178 buffer.write('var old${namer.CURRENT_ISOLATE}$_=' | 3178 buffer.write('var old${namer.CURRENT_ISOLATE}$_=' |
3179 '$_${namer.CURRENT_ISOLATE}$N'); | 3179 '$_${namer.CURRENT_ISOLATE}$N'); |
3180 | 3180 |
3181 // TODO(ahe): This defines a lot of properties on the | 3181 // TODO(ahe): This defines a lot of properties on the |
3182 // Isolate.prototype object. We know this will turn it into a | 3182 // Isolate.prototype object. We know this will turn it into a |
3183 // slow object in V8, so instead we should do something similar to | 3183 // slow object in V8, so instead we should do something similar to |
3184 // Isolate.$finishIsolateConstructor. | 3184 // Isolate.$finishIsolateConstructor. |
3185 buffer.write('${namer.CURRENT_ISOLATE}$_=' | 3185 buffer.write('${namer.CURRENT_ISOLATE}$_=' |
3186 '$_${namer.isolateName}.prototype$N$n'); | 3186 '$_${namer.isolateName}.prototype$N$n'); |
3187 } | 3187 } |
3188 | 3188 |
3189 String buildGeneratedBy() { | |
3190 var suffix = ''; | |
3191 if (BUILD_ID != null) suffix = ' version: $BUILD_ID'; | |
3192 return '// Generated by dart2js, the Dart to JavaScript compiler$suffix.\n'; | |
3193 } | |
3194 | |
3195 String buildSourceMap(CodeBuffer buffer, SourceFile compiledFile) { | 3189 String buildSourceMap(CodeBuffer buffer, SourceFile compiledFile) { |
3196 SourceMapBuilder sourceMapBuilder = | 3190 SourceMapBuilder sourceMapBuilder = |
3197 new SourceMapBuilder(compiler.sourceMapUri); | 3191 new SourceMapBuilder(compiler.sourceMapUri); |
3198 buffer.forEachSourceLocation(sourceMapBuilder.addMapping); | 3192 buffer.forEachSourceLocation(sourceMapBuilder.addMapping); |
3199 return sourceMapBuilder.build(compiledFile); | 3193 return sourceMapBuilder.build(compiledFile); |
3200 } | 3194 } |
3201 | 3195 |
3202 void outputSourceMap(CodeBuffer buffer, String code, String name) { | 3196 void outputSourceMap(CodeBuffer buffer, String code, String name) { |
3203 if (!generateSourceMap) return; | 3197 if (!generateSourceMap) return; |
3204 SourceFile compiledFile = new SourceFile(null, compiler.assembledCode); | 3198 SourceFile compiledFile = new SourceFile(null, compiler.assembledCode); |
3205 String sourceMap = buildSourceMap(mainBuffer, compiledFile); | 3199 String sourceMap = buildSourceMap(mainBuffer, compiledFile); |
3206 compiler.outputProvider(name, 'js.map') | 3200 compiler.outputProvider(name, 'js.map') |
3207 ..add(sourceMap) | 3201 ..add(sourceMap) |
3208 ..close(); | 3202 ..close(); |
3209 } | 3203 } |
3210 | 3204 |
3211 bool isDeferred(Element element) { | 3205 bool isDeferred(Element element) { |
3212 return compiler.deferredLoadTask.isDeferred(element); | 3206 return compiler.deferredLoadTask.isDeferred(element); |
3213 } | 3207 } |
3214 | 3208 |
3215 // TODO(ahe): Remove this when deferred loading is fully implemented. | 3209 // TODO(ahe): Remove this when deferred loading is fully implemented. |
3216 void warnNotImplemented(Element element, String message) { | 3210 void warnNotImplemented(Element element, String message) { |
3217 compiler.reportMessage(compiler.spanFromSpannable(element), | 3211 compiler.reportMessage(compiler.spanFromSpannable(element), |
3218 MessageKind.GENERIC.error({'text': message}), | 3212 MessageKind.GENERIC.error({'text': message}), |
3219 api.Diagnostic.WARNING); | 3213 api.Diagnostic.WARNING); |
3220 } | 3214 } |
3221 } | 3215 } |
3222 | 3216 |
| 3217 const String GENERATED_BY = """ |
| 3218 // Generated by dart2js, the Dart to JavaScript compiler. |
| 3219 """; |
| 3220 |
3223 const String HOOKS_API_USAGE = """ | 3221 const String HOOKS_API_USAGE = """ |
3224 // The code supports the following hooks: | 3222 // The code supports the following hooks: |
3225 // dartPrint(message) - if this function is defined it is called | 3223 // dartPrint(message) - if this function is defined it is called |
3226 // instead of the Dart [print] method. | 3224 // instead of the Dart [print] method. |
3227 // dartMainRunner(main) - if this function is defined, the Dart [main] | 3225 // dartMainRunner(main) - if this function is defined, the Dart [main] |
3228 // method will not be invoked directly. | 3226 // method will not be invoked directly. |
3229 // Instead, a closure that will invoke [main] is | 3227 // Instead, a closure that will invoke [main] is |
3230 // passed to [dartMainRunner]. | 3228 // passed to [dartMainRunner]. |
3231 """; | 3229 """; |
3232 | 3230 |
(...skipping 20 matching lines...) Expand all Loading... |
3253 functions.push(property); | 3251 functions.push(property); |
3254 } else { | 3252 } else { |
3255 $$[property] = element; | 3253 $$[property] = element; |
3256 classes.push(property); | 3254 classes.push(property); |
3257 classes.push(element[""]); | 3255 classes.push(element[""]); |
3258 } | 3256 } |
3259 } | 3257 } |
3260 libraries.push([name, uri, classes, functions]); | 3258 libraries.push([name, uri, classes, functions]); |
3261 } | 3259 } |
3262 })'''; | 3260 })'''; |
OLD | NEW |