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 // Javascript preamble, that lets the output of dart2js run on V8's d8 shell. | 5 // Javascript preamble, that lets the output of dart2js run on V8's d8 shell. |
6 | 6 |
7 // Node wraps files and provides them with a different `this`. The global | 7 // Node wraps files and provides them with a different `this`. The global |
8 // `this` can be accessed through `global`. | 8 // `this` can be accessed through `global`. |
9 | 9 |
10 var self = this; | 10 var self = this; |
(...skipping 263 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
274 // Initialize. | 274 // Initialize. |
275 var action = function() { main(args); } | 275 var action = function() { main(args); } |
276 eventLoop(action); | 276 eventLoop(action); |
277 }; | 277 }; |
278 self.setTimeout = addTimer; | 278 self.setTimeout = addTimer; |
279 self.clearTimeout = cancelTimer; | 279 self.clearTimeout = cancelTimer; |
280 self.setInterval = addInterval; | 280 self.setInterval = addInterval; |
281 self.clearInterval = cancelTimer; | 281 self.clearInterval = cancelTimer; |
282 self.scheduleImmediate = addTask; | 282 self.scheduleImmediate = addTask; |
283 | 283 |
| 284 function computeCurrentScript() { |
| 285 try { |
| 286 throw new Error(); |
| 287 } catch(e) { |
| 288 var stack = e.stack; |
| 289 // The V8 stack looks like: |
| 290 // at computeCurrentScript (preambles/d8.js:286:13) |
| 291 // at Object.currentScript (preambles/d8.js:308:31) |
| 292 // at init.currentScript (/tmp/foo.js:308:19) |
| 293 // at /tmp/foo.js:320:7 |
| 294 // at /tmp/foo.js:331:4 |
| 295 var re = new RegExp("^ *at [^(]*\\((.*):[0-9]*:[0-9]*\\)$", "mg"); |
| 296 var lastMatch = null; |
| 297 do { |
| 298 var match = re.exec(stack); |
| 299 if (match != null) lastMatch = match; |
| 300 } while (match != null); |
| 301 return lastMatch[1]; |
| 302 } |
| 303 } |
| 304 |
| 305 // Adding a 'document' is dangerous since it invalidates the 'typeof document' |
| 306 // test to see if we are running in the browser. It means that the runtime |
| 307 // needs to do more precise checks. |
| 308 // Note that we can't run "currentScript" right away, since that would give |
| 309 // us the location of the preamble file. Instead we wait for the first access |
| 310 // which should happen just before invoking main. At this point we are in |
| 311 // the main file and setting the currentScript property is correct. |
| 312 var cachedCurrentScript = null; |
| 313 self.document = { get currentScript() { |
| 314 if (cachedCurrentScript == null) { |
| 315 cachedCurrentScript = {src: computeCurrentScript()}; |
| 316 } |
| 317 return cachedCurrentScript; |
| 318 } |
| 319 }; |
| 320 |
284 // Support for deferred loading. | 321 // Support for deferred loading. |
285 self.dartDeferredLibraryLoader = function(uri, successCallback, errorCallback)
{ | 322 self.dartDeferredLibraryLoader = function(uri, successCallback, errorCallback)
{ |
286 try { | 323 try { |
287 load(uri); | 324 load(uri); |
288 successCallback(); | 325 successCallback(); |
289 } catch (error) { | 326 } catch (error) { |
290 errorCallback(error); | 327 errorCallback(error); |
291 } | 328 } |
292 }; | 329 }; |
293 })(self); | 330 })(self); |
OLD | NEW |