Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(150)

Side by Side Diff: sdk/lib/_internal/compiler/js_lib/preambles/jsshell.js

Issue 1130293002: dart2js: compute the currentScript in the preamble for d8 and jsshell. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 JSShell. 5 // Javascript preamble, that lets the output of dart2js run on JSShell.
6 6
7 (function(self) { 7 (function(self) {
8 // Using strict mode to avoid accidentally defining global variables. 8 // Using strict mode to avoid accidentally defining global variables.
9 "use strict"; // Should be first statement of this function. 9 "use strict"; // Should be first statement of this function.
10 10
11 // Location (Uri.base) 11 // Location (Uri.base)
12 12
13 var workingDirectory = os.getenv("PWD"); 13 var workingDirectory = os.getenv("PWD");
14 14
15 // Global properties. "self" refers to the global object, so adding a 15 // Global properties. "self" refers to the global object, so adding a
16 // property to "self" defines a global variable. 16 // property to "self" defines a global variable.
17 self.self = self; 17 self.self = self;
18 18
19 self.location = { href: "file://" + workingDirectory + "/" }; 19 self.location = { href: "file://" + workingDirectory + "/" };
20 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
21 // Support for deferred loading. 61 // Support for deferred loading.
22 self.dartDeferredLibraryLoader = function(uri, successCallback, errorCallback) { 62 self.dartDeferredLibraryLoader = function(uri, successCallback, errorCallback) {
23 try { 63 try {
24 load(uri); 64 load(uri);
25 successCallback(); 65 successCallback();
26 } catch (error) { 66 } catch (error) {
27 errorCallback(error); 67 errorCallback(error);
28 } 68 }
29 }; 69 };
30 })(this) 70 })(this)
31 71
32 var getKeys = function(obj){ 72 var getKeys = function(obj){
33 var keys = []; 73 var keys = [];
34 for(var key in obj){ 74 for(var key in obj){
35 keys.push(key); 75 keys.push(key);
36 } 76 }
37 return keys; 77 return keys;
38 } 78 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698