OLD | NEW |
| (Empty) |
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 | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 // Javascript preamble, that lets the output of dart2js run on JSShell. | |
6 | |
7 (function(self) { | |
8 // Using strict mode to avoid accidentally defining global variables. | |
9 "use strict"; // Should be first statement of this function. | |
10 | |
11 // Location (Uri.base) | |
12 | |
13 var workingDirectory = os.getenv("PWD"); | |
14 | |
15 // Global properties. "self" refers to the global object, so adding a | |
16 // property to "self" defines a global variable. | |
17 self.self = self; | |
18 | |
19 self.location = { href: "file://" + workingDirectory + "/" }; | |
20 | |
21 function computeCurrentScript() { | |
22 try { | |
23 throw new Error(); | |
24 } catch(e) { | |
25 var stack = e.stack; | |
26 print(stack); | |
27 // The jsshell stack looks like: | |
28 // computeCurrentScript@...preambles/jsshell.js:23:13 | |
29 // self.document.currentScript@...preambles/jsshell.js:53:37 | |
30 // @/tmp/foo.js:308:1 | |
31 // @/tmp/foo.js:303:1 | |
32 // @/tmp/foo.js:5:1 | |
33 var re = new RegExp("^.*@(.*):[0-9]*:[0-9]*$", "mg"); | |
34 var lastMatch = null; | |
35 do { | |
36 var match = re.exec(stack); | |
37 if (match != null) lastMatch = match; | |
38 } while (match != null); | |
39 return lastMatch[1]; | |
40 } | |
41 } | |
42 | |
43 // Adding a 'document' is dangerous since it invalidates the 'typeof document' | |
44 // test to see if we are running in the browser. It means that the runtime | |
45 // needs to do more precise checks. | |
46 // Note that we can't run "currentScript" right away, since that would give | |
47 // us the location of the preamble file. Instead we wait for the first access | |
48 // which should happen just before invoking main. At this point we are in | |
49 // the main file and setting the currentScript property is correct. | |
50 // Note that we cannot use `thisFileName()`, since that would give us the | |
51 // preamble and not the script file. | |
52 var cachedCurrentScript = null; | |
53 self.document = { get currentScript() { | |
54 if (cachedCurrentScript == null) { | |
55 cachedCurrentScript = {src: computeCurrentScript()}; | |
56 } | |
57 return cachedCurrentScript; | |
58 } | |
59 }; | |
60 | |
61 // Support for deferred loading. | |
62 self.dartDeferredLibraryLoader = function(uri, successCallback, errorCallback)
{ | |
63 try { | |
64 load(uri); | |
65 successCallback(); | |
66 } catch (error) { | |
67 errorCallback(error); | |
68 } | |
69 }; | |
70 })(this) | |
71 | |
72 var getKeys = function(obj){ | |
73 var keys = []; | |
74 for(var key in obj){ | |
75 keys.push(key); | |
76 } | |
77 return keys; | |
78 } | |
OLD | NEW |