Chromium Code Reviews| 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 promises.push(loadResourcePromise(sourceURL).then(scriptSourceLoaded.bin d(null, i), scriptSourceLoaded.bind(null, i, undefined))); |
| 135 } | 136 } |
| 136 return Promise.all(promises).then(undefined); | 137 return Promise.all(promises).then(undefined); |
| 137 | 138 |
| 138 /** | 139 /** |
| 139 * @param {number} scriptNumber | 140 * @param {number} scriptNumber |
| 140 * @param {string=} scriptSource | 141 * @param {string=} scriptSource |
| 141 */ | 142 */ |
| 142 function scriptSourceLoaded(scriptNumber, scriptSource) | 143 function scriptSourceLoaded(scriptNumber, scriptSource) |
| 143 { | 144 { |
| 144 sources[scriptNumber] = scriptSource || ""; | 145 sources[scriptNumber] = scriptSource || ""; |
| 146 evaluateLoadedSources(); | |
| 147 } | |
| 148 | |
| 149 function evaluateLoadedSources() | |
| 150 { | |
| 145 // Eval scripts as fast as possible. | 151 // Eval scripts as fast as possible. |
|
pfeldman
2016/07/07 04:13:34
optional: We could sacrifice this optimization for
lushnikov
2016/07/07 21:26:22
Done.
| |
| 146 while (typeof sources[scriptToEval] !== "undefined") { | 152 while (!pauseSourceEvaluation && typeof sources[scriptToEval] !== "undef ined") { |
| 147 evaluateScript(urls[scriptToEval], sources[scriptToEval]); | 153 var asyncEvalPromise = evaluateScript(urls[scriptToEval], sources[sc riptToEval]); |
| 148 ++scriptToEval; | 154 ++scriptToEval; |
| 155 if (asyncEvalPromise) { | |
| 156 pauseSourceEvaluation = true; | |
| 157 asyncEvalPromise.then(onAsyncEvalCompleted); | |
| 158 } | |
| 149 } | 159 } |
| 150 } | 160 } |
| 151 | 161 |
| 162 function onAsyncEvalCompleted() | |
| 163 { | |
| 164 pauseSourceEvaluation = false; | |
| 165 evaluateLoadedSources(); | |
| 166 } | |
| 167 | |
| 152 /** | 168 /** |
| 153 * @param {string} sourceURL | 169 * @param {string} sourceURL |
| 154 * @param {string=} scriptSource | 170 * @param {string=} scriptSource |
| 171 * @return {?Promise} | |
| 155 */ | 172 */ |
| 156 function evaluateScript(sourceURL, scriptSource) | 173 function evaluateScript(sourceURL, scriptSource) |
| 157 { | 174 { |
| 158 _loadedScripts[sourceURL] = true; | 175 _loadedScripts[sourceURL] = true; |
| 159 if (!scriptSource) { | 176 if (!scriptSource) { |
| 160 // Do not reject, as this is normal in the hosted mode. | 177 // Do not reject, as this is normal in the hosted mode. |
| 161 console.error("Empty response arrived for script '" + sourceURL + "' "); | 178 console.error("Empty response arrived for script '" + sourceURL + "' "); |
| 162 return; | 179 return null; |
| 163 } | 180 } |
| 181 self.runtime.onAsyncEvalCompletion = null; | |
|
pfeldman
2016/07/07 04:13:34
nuke
lushnikov
2016/07/07 21:26:22
Done.
| |
| 164 self.eval(scriptSource + "\n//# sourceURL=" + sourceURL); | 182 self.eval(scriptSource + "\n//# sourceURL=" + sourceURL); |
|
pfeldman
2016/07/07 04:13:34
var promise = self.eval(scriptSource + "\n//# sour
lushnikov
2016/07/07 21:26:22
Done.
| |
| 183 return self.runtime.onAsyncEvalCompletion; | |
| 165 } | 184 } |
| 166 } | 185 } |
| 167 | 186 |
| 168 (function() { | 187 (function() { |
| 169 var baseUrl = self.location ? self.location.origin + self.location.pathname : ""; | 188 var baseUrl = self.location ? self.location.origin + self.location.pathname : ""; |
| 170 self._importScriptPathPrefix = baseUrl.substring(0, baseUrl.lastIndexOf("/") + 1); | 189 self._importScriptPathPrefix = baseUrl.substring(0, baseUrl.lastIndexOf("/") + 1); |
| 171 })(); | 190 })(); |
| 172 | 191 |
| 173 /** | 192 /** |
| 174 * @constructor | 193 * @constructor |
| (...skipping 1003 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1178 { | 1197 { |
| 1179 var sourceURL = self.location.href; | 1198 var sourceURL = self.location.href; |
| 1180 if (self.location.search) | 1199 if (self.location.search) |
| 1181 sourceURL = sourceURL.replace(self.location.search, ""); | 1200 sourceURL = sourceURL.replace(self.location.search, ""); |
| 1182 sourceURL = sourceURL.substring(0, sourceURL.lastIndexOf("/") + 1) + path; | 1201 sourceURL = sourceURL.substring(0, sourceURL.lastIndexOf("/") + 1) + path; |
| 1183 return "\n/*# sourceURL=" + sourceURL + " */"; | 1202 return "\n/*# sourceURL=" + sourceURL + " */"; |
| 1184 } | 1203 } |
| 1185 | 1204 |
| 1186 /** @type {!Runtime} */ | 1205 /** @type {!Runtime} */ |
| 1187 var runtime; | 1206 var runtime; |
| OLD | NEW |