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

Unified Diff: sdk/lib/_internal/compiler/js_lib/preambles/d8.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 side-by-side diff with in-line comments
Download patch
Index: sdk/lib/_internal/compiler/js_lib/preambles/d8.js
diff --git a/sdk/lib/_internal/compiler/js_lib/preambles/d8.js b/sdk/lib/_internal/compiler/js_lib/preambles/d8.js
index a48f918cf704bde5d69786c81e3453b10fdffad2..18823e15d9dbaa7953d439d40a6415c493b289c7 100644
--- a/sdk/lib/_internal/compiler/js_lib/preambles/d8.js
+++ b/sdk/lib/_internal/compiler/js_lib/preambles/d8.js
@@ -281,6 +281,43 @@ if (typeof global != "undefined") self = global; // Node.js.
self.clearInterval = cancelTimer;
self.scheduleImmediate = addTask;
+ function computeCurrentScript() {
+ try {
+ throw new Error();
+ } catch(e) {
+ var stack = e.stack;
+ // The V8 stack looks like:
+ // at computeCurrentScript (preambles/d8.js:286:13)
+ // at Object.currentScript (preambles/d8.js:308:31)
+ // at init.currentScript (/tmp/foo.js:308:19)
+ // at /tmp/foo.js:320:7
+ // at /tmp/foo.js:331:4
+ var re = new RegExp("^ *at [^(]*\\((.*):[0-9]*:[0-9]*\\)$", "mg");
+ var lastMatch = null;
+ do {
+ var match = re.exec(stack);
+ if (match != null) lastMatch = match;
+ } while (match != null);
+ return lastMatch[1];
+ }
+ }
+
+ // Adding a 'document' is dangerous since it invalidates the 'typeof document'
+ // test to see if we are running in the browser. It means that the runtime
+ // needs to do more precise checks.
+ // Note that we can't run "currentScript" right away, since that would give
+ // us the location of the preamble file. Instead we wait for the first access
+ // which should happen just before invoking main. At this point we are in
+ // the main file and setting the currentScript property is correct.
+ var cachedCurrentScript = null;
+ self.document = { get currentScript() {
+ if (cachedCurrentScript == null) {
+ cachedCurrentScript = {src: computeCurrentScript()};
+ }
+ return cachedCurrentScript;
+ }
+ };
+
// Support for deferred loading.
self.dartDeferredLibraryLoader = function(uri, successCallback, errorCallback) {
try {

Powered by Google App Engine
This is Rietveld 408576698