| 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 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 109 * @param {!Array.<string>} scriptNames | 109 * @param {!Array.<string>} scriptNames |
| 110 * @param {string=} base | 110 * @param {string=} base |
| 111 * @return {!Promise.<undefined>} | 111 * @return {!Promise.<undefined>} |
| 112 */ | 112 */ |
| 113 function loadScriptsPromise(scriptNames, base) | 113 function loadScriptsPromise(scriptNames, base) |
| 114 { | 114 { |
| 115 /** @type {!Array<!Promise<undefined>>} */ | 115 /** @type {!Array<!Promise<undefined>>} */ |
| 116 var promises = []; | 116 var promises = []; |
| 117 /** @type {!Array<string>} */ | 117 /** @type {!Array<string>} */ |
| 118 var urls = []; | 118 var urls = []; |
| 119 var pauseSourceEvaluation = false; |
| 119 var sources = new Array(scriptNames.length); | 120 var sources = new Array(scriptNames.length); |
| 120 var scriptToEval = 0; | 121 var scriptToEval = 0; |
| 121 for (var i = 0; i < scriptNames.length; ++i) { | 122 for (var i = 0; i < scriptNames.length; ++i) { |
| 122 var scriptName = scriptNames[i]; | 123 var scriptName = scriptNames[i]; |
| 123 var sourceURL = (base || self._importScriptPathPrefix) + scriptName; | 124 var sourceURL = (base || self._importScriptPathPrefix) + scriptName; |
| 124 | 125 |
| 125 var schemaIndex = sourceURL.indexOf("://") + 3; | 126 var schemaIndex = sourceURL.indexOf("://") + 3; |
| 126 var pathIndex = sourceURL.indexOf("/", schemaIndex); | 127 var pathIndex = sourceURL.indexOf("/", schemaIndex); |
| 127 if (pathIndex === -1) | 128 if (pathIndex === -1) |
| 128 pathIndex = sourceURL.length; | 129 pathIndex = sourceURL.length; |
| 129 sourceURL = sourceURL.substring(0, pathIndex) + normalizePath(sourceURL.
substring(pathIndex)); | 130 sourceURL = sourceURL.substring(0, pathIndex) + normalizePath(sourceURL.
substring(pathIndex)); |
| 130 | 131 |
| 131 if (_loadedScripts[sourceURL]) | 132 if (_loadedScripts[sourceURL]) |
| 132 continue; | 133 continue; |
| 133 urls.push(sourceURL); | 134 urls.push(sourceURL); |
| 134 promises.push(loadResourcePromise(sourceURL).then(scriptSourceLoaded.bin
d(null, i), scriptSourceLoaded.bind(null, i, undefined))); | 135 var promise = loadResourcePromise(sourceURL) |
| 136 .then(scriptSource => scriptSource || "") |
| 137 .catch(e => ""); |
| 138 promises.push(promise); |
| 135 } | 139 } |
| 136 return Promise.all(promises).then(undefined); | 140 var allSourcesPromise = /** @type {!Promise<!Array<string>>} */(Promise.all(
promises)); |
| 141 return allSourcesPromise.then(evaluateSources); |
| 137 | 142 |
| 138 /** | 143 /** |
| 139 * @param {number} scriptNumber | 144 * @param {!Array<string>} sources |
| 140 * @param {string=} scriptSource | 145 * @return {!Promise} |
| 141 */ | 146 */ |
| 142 function scriptSourceLoaded(scriptNumber, scriptSource) | 147 function evaluateSources(sources) |
| 143 { | 148 { |
| 144 sources[scriptNumber] = scriptSource || ""; | 149 var result = Promise.resolve(); |
| 145 // Eval scripts as fast as possible. | 150 for (var i = 0; i < sources.length; ++i) |
| 146 while (typeof sources[scriptToEval] !== "undefined") { | 151 result = result.then(evaluateScript.bind(null, urls[i], sources[i]))
; |
| 147 evaluateScript(urls[scriptToEval], sources[scriptToEval]); | 152 return result; |
| 148 ++scriptToEval; | |
| 149 } | |
| 150 } | 153 } |
| 151 | 154 |
| 152 /** | 155 /** |
| 153 * @param {string} sourceURL | 156 * @param {string} sourceURL |
| 154 * @param {string=} scriptSource | 157 * @param {string=} scriptSource |
| 158 * @return {?Promise} |
| 155 */ | 159 */ |
| 156 function evaluateScript(sourceURL, scriptSource) | 160 function evaluateScript(sourceURL, scriptSource) |
| 157 { | 161 { |
| 158 _loadedScripts[sourceURL] = true; | 162 _loadedScripts[sourceURL] = true; |
| 159 if (!scriptSource) { | 163 if (!scriptSource) { |
| 160 // Do not reject, as this is normal in the hosted mode. | 164 // Do not reject, as this is normal in the hosted mode. |
| 161 console.error("Empty response arrived for script '" + sourceURL + "'
"); | 165 console.error("Empty response arrived for script '" + sourceURL + "'
"); |
| 162 return; | 166 return null; |
| 163 } | 167 } |
| 164 self.eval(scriptSource + "\n//# sourceURL=" + sourceURL); | 168 var result = self.eval(scriptSource + "\n//# sourceURL=" + sourceURL); |
| 169 return result instanceof Promise ? result : null; |
| 165 } | 170 } |
| 166 } | 171 } |
| 167 | 172 |
| 168 (function() { | 173 (function() { |
| 169 var baseUrl = self.location ? self.location.origin + self.location.pathname
: ""; | 174 var baseUrl = self.location ? self.location.origin + self.location.pathname
: ""; |
| 170 self._importScriptPathPrefix = baseUrl.substring(0, baseUrl.lastIndexOf("/")
+ 1); | 175 self._importScriptPathPrefix = baseUrl.substring(0, baseUrl.lastIndexOf("/")
+ 1); |
| 171 })(); | 176 })(); |
| 172 | 177 |
| 173 /** | 178 /** |
| 174 * @constructor | 179 * @constructor |
| (...skipping 1003 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1178 { | 1183 { |
| 1179 var sourceURL = self.location.href; | 1184 var sourceURL = self.location.href; |
| 1180 if (self.location.search) | 1185 if (self.location.search) |
| 1181 sourceURL = sourceURL.replace(self.location.search, ""); | 1186 sourceURL = sourceURL.replace(self.location.search, ""); |
| 1182 sourceURL = sourceURL.substring(0, sourceURL.lastIndexOf("/") + 1) + path; | 1187 sourceURL = sourceURL.substring(0, sourceURL.lastIndexOf("/") + 1) + path; |
| 1183 return "\n/*# sourceURL=" + sourceURL + " */"; | 1188 return "\n/*# sourceURL=" + sourceURL + " */"; |
| 1184 } | 1189 } |
| 1185 | 1190 |
| 1186 /** @type {!Runtime} */ | 1191 /** @type {!Runtime} */ |
| 1187 var runtime; | 1192 var runtime; |
| OLD | NEW |