OLD | NEW |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, 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 library dart2js.js_emitter.startup_emitter.model_emitter; | 5 library dart2js.js_emitter.startup_emitter.model_emitter; |
6 | 6 |
7 import 'dart:convert' show JsonEncoder; | 7 import 'dart:convert' show JsonEncoder; |
8 | 8 |
9 import 'package:js_runtime/shared/embedded_names.dart' show | 9 import 'package:js_runtime/shared/embedded_names.dart' show |
10 CLASS_FIELDS_EXTRACTOR, | 10 CLASS_FIELDS_EXTRACTOR, |
(...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
201 Map<DeferredFragment, String> hunkHashes = | 201 Map<DeferredFragment, String> hunkHashes = |
202 writeDeferredFragments(deferredFragmentsCode); | 202 writeDeferredFragments(deferredFragmentsCode); |
203 | 203 |
204 // Now that we have written the deferred hunks, we can update the hash | 204 // Now that we have written the deferred hunks, we can update the hash |
205 // tokens in the main-fragment. | 205 // tokens in the main-fragment. |
206 deferredHashTokens.forEach((DeferredFragment key, | 206 deferredHashTokens.forEach((DeferredFragment key, |
207 _DeferredFragmentHash token) { | 207 _DeferredFragmentHash token) { |
208 token.setHash(hunkHashes[key]); | 208 token.setHash(hunkHashes[key]); |
209 }); | 209 }); |
210 | 210 |
211 writeMainFragment(mainFragment, mainCode); | 211 writeMainFragment(mainFragment, mainCode, |
| 212 isSplit: program.deferredFragments.isNotEmpty); |
212 | 213 |
213 if (backend.requiresPreamble && | 214 if (backend.requiresPreamble && |
214 !backend.htmlLibraryIsLoaded) { | 215 !backend.htmlLibraryIsLoaded) { |
215 reporter.reportHintMessage( | 216 reporter.reportHintMessage( |
216 NO_LOCATION_SPANNABLE, MessageKind.PREAMBLE); | 217 NO_LOCATION_SPANNABLE, MessageKind.PREAMBLE); |
217 } | 218 } |
218 | 219 |
219 if (compiler.deferredMapUri != null) { | 220 if (compiler.deferredMapUri != null) { |
220 writeDeferredMap(); | 221 writeDeferredMap(); |
221 } | 222 } |
(...skipping 20 matching lines...) Expand all Loading... |
242 Map<DeferredFragment, js.Expression> fragmentsCode) { | 243 Map<DeferredFragment, js.Expression> fragmentsCode) { |
243 Map<DeferredFragment, String> hunkHashes = <DeferredFragment, String>{}; | 244 Map<DeferredFragment, String> hunkHashes = <DeferredFragment, String>{}; |
244 | 245 |
245 fragmentsCode.forEach((DeferredFragment fragment, js.Expression code) { | 246 fragmentsCode.forEach((DeferredFragment fragment, js.Expression code) { |
246 hunkHashes[fragment] = writeDeferredFragment(fragment, code); | 247 hunkHashes[fragment] = writeDeferredFragment(fragment, code); |
247 }); | 248 }); |
248 | 249 |
249 return hunkHashes; | 250 return hunkHashes; |
250 } | 251 } |
251 | 252 |
| 253 js.Statement buildDeferredInitializerGlobal() { |
| 254 String global = deferredInitializersGlobal; |
| 255 return js.js.statement( |
| 256 "if (typeof($global) === 'undefined') var # = Object.create(null);", |
| 257 new js.VariableDeclaration(global, allowRename: false)); |
| 258 } |
| 259 |
252 // Writes the given [fragment]'s [code] into a file. | 260 // Writes the given [fragment]'s [code] into a file. |
253 // | 261 // |
254 // Updates the shared [outputBuffers] field with the output. | 262 // Updates the shared [outputBuffers] field with the output. |
255 void writeMainFragment(MainFragment fragment, js.Statement code) { | 263 void writeMainFragment(MainFragment fragment, js.Statement code, |
| 264 {bool isSplit}) { |
256 LineColumnCollector lineColumnCollector; | 265 LineColumnCollector lineColumnCollector; |
257 List<CodeOutputListener> codeOutputListeners; | 266 List<CodeOutputListener> codeOutputListeners; |
258 if (shouldGenerateSourceMap) { | 267 if (shouldGenerateSourceMap) { |
259 lineColumnCollector = new LineColumnCollector(); | 268 lineColumnCollector = new LineColumnCollector(); |
260 codeOutputListeners = <CodeOutputListener>[lineColumnCollector]; | 269 codeOutputListeners = <CodeOutputListener>[lineColumnCollector]; |
261 } | 270 } |
262 | 271 |
263 CodeOutput mainOutput = new StreamCodeOutput( | 272 CodeOutput mainOutput = new StreamCodeOutput( |
264 compiler.outputProvider('', 'js'), | 273 compiler.outputProvider('', 'js'), |
265 codeOutputListeners); | 274 codeOutputListeners); |
266 outputBuffers[fragment] = mainOutput; | 275 outputBuffers[fragment] = mainOutput; |
267 | 276 |
268 js.Program program = new js.Program([ | 277 js.Program program = new js.Program([ |
269 buildGeneratedBy(), | 278 buildGeneratedBy(), |
270 new js.Comment(HOOKS_API_USAGE), | 279 new js.Comment(HOOKS_API_USAGE), |
| 280 isSplit ? buildDeferredInitializerGlobal() : new js.Block.empty(), |
271 code]); | 281 code]); |
272 | 282 |
273 mainOutput.addBuffer(js.prettyPrint(program, compiler, | 283 mainOutput.addBuffer(js.prettyPrint(program, compiler, |
274 monitor: compiler.dumpInfoTask)); | 284 monitor: compiler.dumpInfoTask)); |
275 | 285 |
276 if (shouldGenerateSourceMap) { | 286 if (shouldGenerateSourceMap) { |
277 mainOutput.add( | 287 mainOutput.add( |
278 generateSourceMapTag(compiler.sourceMapUri, compiler.outputUri)); | 288 generateSourceMapTag(compiler.sourceMapUri, compiler.outputUri)); |
279 } | 289 } |
280 | 290 |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
314 // hunk is loaded. | 324 // hunk is loaded. |
315 // That function must be in a map from its hashcode to the function. Since | 325 // That function must be in a map from its hashcode to the function. Since |
316 // we don't know the hash before we actually emit the code we store the | 326 // we don't know the hash before we actually emit the code we store the |
317 // function in a temporary field first: | 327 // function in a temporary field first: |
318 // | 328 // |
319 // deferredInitializer.current = <pretty-printed code>; | 329 // deferredInitializer.current = <pretty-printed code>; |
320 // deferredInitializer[<hash>] = deferredInitializer.current; | 330 // deferredInitializer[<hash>] = deferredInitializer.current; |
321 | 331 |
322 js.Program program = new js.Program([ | 332 js.Program program = new js.Program([ |
323 buildGeneratedBy(), | 333 buildGeneratedBy(), |
| 334 buildDeferredInitializerGlobal(), |
324 js.js.statement('$deferredInitializersGlobal.current = #', code)]); | 335 js.js.statement('$deferredInitializersGlobal.current = #', code)]); |
325 | 336 |
326 output.addBuffer(js.prettyPrint(program, compiler, | 337 output.addBuffer(js.prettyPrint(program, compiler, |
327 monitor: compiler.dumpInfoTask)); | 338 monitor: compiler.dumpInfoTask)); |
328 | 339 |
329 // Make a unique hash of the code (before the sourcemaps are added) | 340 // Make a unique hash of the code (before the sourcemaps are added) |
330 // This will be used to retrieve the initializing function from the global | 341 // This will be used to retrieve the initializing function from the global |
331 // variable. | 342 // variable. |
332 String hash = hasher.getHash(); | 343 String hash = hasher.getHash(); |
333 | 344 |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
403 // Json does not support comments, so we embed the explanation in the | 414 // Json does not support comments, so we embed the explanation in the |
404 // data. | 415 // data. |
405 mapping["_comment"] = "This mapping shows which compiled `.js` files are " | 416 mapping["_comment"] = "This mapping shows which compiled `.js` files are " |
406 "needed for a given deferred library import."; | 417 "needed for a given deferred library import."; |
407 mapping.addAll(compiler.deferredLoadTask.computeDeferredMap()); | 418 mapping.addAll(compiler.deferredLoadTask.computeDeferredMap()); |
408 compiler.outputProvider(compiler.deferredMapUri.path, 'deferred_map') | 419 compiler.outputProvider(compiler.deferredMapUri.path, 'deferred_map') |
409 ..add(const JsonEncoder.withIndent(" ").convert(mapping)) | 420 ..add(const JsonEncoder.withIndent(" ").convert(mapping)) |
410 ..close(); | 421 ..close(); |
411 } | 422 } |
412 } | 423 } |
OLD | NEW |