OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright (C) 2014 Google Inc. All rights reserved. | 2 * Copyright (C) 2014 Google Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
6 * met: | 6 * met: |
7 * | 7 * |
8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
(...skipping 10 matching lines...) Expand all Loading... | |
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
29 */ | 29 */ |
30 | 30 |
31 var allDescriptors = []; | |
31 var _importedScripts = {}; | 32 var _importedScripts = {}; |
32 | 33 |
33 /** | 34 /** |
34 * @param {string} url | 35 * @param {string} url |
35 * @return {string} | 36 * @return {string} |
36 */ | 37 */ |
37 function loadResource(url) | 38 function loadResource(url) |
38 { | 39 { |
39 var xhr = new XMLHttpRequest(); | 40 var xhr = new XMLHttpRequest(); |
40 xhr.open("GET", url, false); | 41 xhr.open("GET", url, false); |
41 try { | 42 try { |
42 xhr.send(null); | 43 xhr.send(null); |
43 } catch (e) { | 44 } catch (e) { |
44 console.error(url + " -> " + new Error().stack); | 45 console.error(url + " -> " + new Error().stack); |
45 throw e; | 46 throw e; |
46 } | 47 } |
47 return xhr.responseText; | 48 return xhr.responseText; |
48 } | 49 } |
49 | 50 |
50 /** | 51 /** |
51 * This function behavior depends on the "debug_devtools" flag value. | |
52 * - In debug mode it loads scripts synchronously via xhr request. | |
53 * - In release mode every occurrence of "importScript" in the js files | |
54 * that have been whitelisted in the build system gets replaced with | |
55 * the script source code on the compilation phase. | |
56 * The build system will throw an exception if it finds an importScript() call | |
57 * in other files. | |
58 * | |
59 * To load scripts lazily in release mode call "loadScript" function. | |
60 * @param {string} scriptName | 52 * @param {string} scriptName |
61 */ | 53 */ |
62 function importScript(scriptName) | 54 function loadScript(scriptName) |
63 { | 55 { |
64 var sourceURL = self._importScriptPathPrefix + scriptName; | 56 var sourceURL = self._importScriptPathPrefix + scriptName; |
65 if (_importedScripts[sourceURL]) | 57 if (_importedScripts[sourceURL]) |
66 return; | 58 return; |
67 _importedScripts[sourceURL] = true; | 59 _importedScripts[sourceURL] = true; |
68 var scriptSource = loadResource(sourceURL); | 60 var scriptSource = loadResource(sourceURL); |
69 if (!scriptSource) | 61 if (!scriptSource) |
70 throw "empty response arrived for script '" + sourceURL + "'"; | 62 throw "empty response arrived for script '" + sourceURL + "'"; |
71 var oldPrefix = self._importScriptPathPrefix; | 63 var oldPrefix = self._importScriptPathPrefix; |
72 self._importScriptPathPrefix += scriptName.substring(0, scriptName.lastIndex Of("/") + 1); | 64 self._importScriptPathPrefix += scriptName.substring(0, scriptName.lastIndex Of("/") + 1); |
73 try { | 65 try { |
74 self.eval(scriptSource + "\n//# sourceURL=" + sourceURL); | 66 self.eval(scriptSource + "\n//# sourceURL=" + sourceURL); |
75 } finally { | 67 } finally { |
76 self._importScriptPathPrefix = oldPrefix; | 68 self._importScriptPathPrefix = oldPrefix; |
77 } | 69 } |
78 } | 70 } |
79 | 71 |
80 (function() { | 72 (function() { |
81 var baseUrl = location.origin + location.pathname; | 73 var baseUrl = location.origin + location.pathname; |
82 self._importScriptPathPrefix = baseUrl.substring(0, baseUrl.lastIndexOf("/") + 1); | 74 self._importScriptPathPrefix = baseUrl.substring(0, baseUrl.lastIndexOf("/") + 1); |
83 })(); | 75 })(); |
84 | 76 |
85 var loadScript = importScript; | |
86 | |
87 /** | 77 /** |
88 * @constructor | 78 * @constructor |
89 * @param {!Array.<!Runtime.ModuleDescriptor>} descriptors | |
90 */ | 79 */ |
91 var Runtime = function(descriptors) | 80 var Runtime = function() |
92 { | 81 { |
93 /** | 82 /** |
94 * @type {!Array.<!Runtime.Module>} | 83 * @type {!Array.<!Runtime.Module>} |
95 */ | 84 */ |
96 this._modules = []; | 85 this._modules = []; |
97 /** | 86 /** |
98 * @type {!Object.<string, !Runtime.Module>} | 87 * @type {!Object.<string, !Runtime.Module>} |
99 */ | 88 */ |
100 this._modulesMap = {}; | 89 this._modulesMap = {}; |
101 /** | 90 /** |
102 * @type {!Array.<!Runtime.Extension>} | 91 * @type {!Array.<!Runtime.Extension>} |
103 */ | 92 */ |
104 this._extensions = []; | 93 this._extensions = []; |
105 | 94 |
106 /** | 95 /** |
107 * @type {!Object.<string, !function(new:Object)>} | 96 * @type {!Object.<string, !function(new:Object)>} |
108 */ | 97 */ |
109 this._cachedTypeClasses = {}; | 98 this._cachedTypeClasses = {}; |
110 | 99 |
111 /** | 100 /** |
112 * @type {!Object.<string, !Runtime.ModuleDescriptor>} | 101 * @type {!Object.<string, !Runtime.ModuleDescriptor>} |
113 */ | 102 */ |
114 this._descriptorsMap = {}; | 103 this._descriptorsMap = {}; |
115 for (var i = 0; i < descriptors.length; ++i) | 104 for (var i = 0; i < allDescriptors.length; ++i) |
116 this._descriptorsMap[descriptors[i]["name"]] = descriptors[i]; | 105 this._descriptorsMap[allDescriptors[i]["name"]] = allDescriptors[i]; |
117 } | 106 } |
118 | 107 |
119 /** | 108 /** |
120 * @param {string} moduleName | 109 * @param {string} moduleName |
121 * @return {!Worker} | 110 * @return {!Worker} |
122 */ | 111 */ |
123 Runtime.startWorker = function(moduleName) | 112 Runtime.startWorker = function(moduleName) |
124 { | 113 { |
125 return new Worker(moduleName + "/_module.js"); | 114 if (allDescriptors.length) |
115 return new Worker(moduleName + ".js"); | |
116 var content = loadResource(moduleName + "/module.json"); | |
117 if (!content) | |
118 throw new Error("Worker is not defined: " + moduleName + " " + new Error ().stack); | |
119 var workerJSON = JSON.parse(content); | |
120 var workerString = ""; | |
121 var scripts = workerJSON["scripts"]; | |
122 for (var i = 0; i < scripts.length; ++i) | |
123 workerString += loadResource(moduleName + "/" + scripts[i]) + ";"; | |
124 var blob = new Blob([workerString], { type: "text/javascript" }); | |
vsevik
2014/08/14 16:43:47
This will make debugging workers harder since all
| |
125 var workerURL = window.URL.createObjectURL(blob); | |
126 var worker = new Worker(workerURL); | |
127 window.URL.revokeObjectURL(workerURL); | |
128 return worker; | |
126 } | 129 } |
127 | 130 |
128 Runtime.prototype = { | 131 Runtime.prototype = { |
129 /** | 132 /** |
130 * @param {!Array.<string>} configuration | 133 * @param {!Array.<string>} configuration |
131 */ | 134 */ |
132 registerModules: function(configuration) | 135 registerModules: function(configuration) |
133 { | 136 { |
134 for (var i = 0; i < configuration.length; ++i) | 137 for (var i = 0; i < configuration.length; ++i) |
135 this._registerModule(configuration[i]); | 138 this._registerModule(configuration[i]); |
(...skipping 301 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
437 console.assert(false, "Module " + this._name + " is loaded from itse lf: " + new Error().stack); | 440 console.assert(false, "Module " + this._name + " is loaded from itse lf: " + new Error().stack); |
438 Error.stackTraceLimit = oldStackTraceLimit; | 441 Error.stackTraceLimit = oldStackTraceLimit; |
439 return; | 442 return; |
440 } | 443 } |
441 | 444 |
442 this._isLoading = true; | 445 this._isLoading = true; |
443 var dependencies = this._descriptor.dependencies; | 446 var dependencies = this._descriptor.dependencies; |
444 for (var i = 0; dependencies && i < dependencies.length; ++i) | 447 for (var i = 0; dependencies && i < dependencies.length; ++i) |
445 this._manager.loadModule(dependencies[i]); | 448 this._manager.loadModule(dependencies[i]); |
446 if (this._descriptor.scripts) | 449 if (this._descriptor.scripts) |
447 loadScript(this._name + "/_module.js"); | 450 loadScript(this._name + ".js"); |
448 this._isLoading = false; | 451 this._isLoading = false; |
449 this._loaded = true; | 452 this._loaded = true; |
450 } | 453 } |
451 } | 454 } |
452 | 455 |
453 /** | 456 /** |
454 * @constructor | 457 * @constructor |
455 * @param {!Runtime.Module} module | 458 * @param {!Runtime.Module} module |
456 * @param {!Runtime.ExtensionDescriptor} descriptor | 459 * @param {!Runtime.ExtensionDescriptor} descriptor |
457 */ | 460 */ |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
523 this._instance = new constructorFunction(); | 526 this._instance = new constructorFunction(); |
524 } | 527 } |
525 return this._instance; | 528 return this._instance; |
526 } | 529 } |
527 } | 530 } |
528 | 531 |
529 /** | 532 /** |
530 * @type {!Runtime} | 533 * @type {!Runtime} |
531 */ | 534 */ |
532 var runtime; | 535 var runtime; |
OLD | NEW |