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

Unified Diff: dart/sdk/lib/_internal/compiler/implementation/lib/isolate_helper.dart

Issue 12288029: Improve IsolateNatives.computeThisScript(). (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge
Patch Set: Fix regular expression that matches on IE, Chrome, and Safari Created 7 years, 10 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
« no previous file with comments | « no previous file | dart/tests/compiler/dart2js_native/compute_this_script_test.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: dart/sdk/lib/_internal/compiler/implementation/lib/isolate_helper.dart
diff --git a/dart/sdk/lib/_internal/compiler/implementation/lib/isolate_helper.dart b/dart/sdk/lib/_internal/compiler/implementation/lib/isolate_helper.dart
index 54bbdc140c44b724ca5e6aee8cf8bdd7c229f86d..d25c058fbc80ad2449528eb3a4cd616d56953a0c 100644
--- a/dart/sdk/lib/_internal/compiler/implementation/lib/isolate_helper.dart
+++ b/dart/sdk/lib/_internal/compiler/implementation/lib/isolate_helper.dart
@@ -406,23 +406,45 @@ class IsolateNatives {
* JavaScript workers.
*/
static String computeThisScript() {
- // TODO(7369): Find a cross-platform non-brittle way of getting the
- // currently running script.
- var scripts = JS('', r"document.getElementsByTagName('script')");
- // The scripts variable only contains the scripts that have already been
- // executed. The last one is the currently running script.
- for (int i = 0, len = JS('int', '#.length', scripts); i < len; i++) {
- var script = JS('', '#[#]', scripts, i);
- var src = JS('String|Null', '# && #.src', script, script);
- // Filter out the test controller script, and the Dart
- // bootstrap script.
- if (src != null
- && !src.endsWith('test_controller.js')
- && !src.endsWith('dart.js')) {
- return src;
- }
+ // TODO(ahe): The following works in Firefox during loading of the
+ // script, and is being considered for the standard.
+ // if (JS('String', 'typeof document') == 'object') {
+ // var currentScript = JS('', 'document.currentScript');
+ // if (JS('String', 'typeof #', currentScript) == 'object') {
+ // return JS('String', '#.src', currentScript);
+ // }
+ // }
+
+ var stack = JS('String|Null', 'new Error().stack');
+ if (stack == null) {
+ // According to Internet Explorer documentation, the stack
+ // property is not set until the exception is thrown.
+ stack = JS('String',
+ '(function() {'
+ 'try { throw new Error() } catch(e) { return e.stack }'
+ '})()');
}
- return null;
+ var pattern, matches;
+
+ // This pattern matches V8, Chrome, and Internet Explorer stack
+ // traces that look like this:
+ // Error
+ // at methodName (URI:LINE:COLUMN)
+ pattern = JS('',
+ r'new RegExp("^ *at [^(]*\\((.*):[0-9]*:[0-9]*\\)$", "m")');
+
+
+ matches = JS('=List|Null', '#.match(#)', stack, pattern);
+ if (matches != null) return JS('String', '#[1]', matches);
+
+ // This pattern matches Firefox stack traces that look like this:
+ // methodName@URI:LINE
+ pattern = JS('', r'new RegExp("^[^@]*@(.*):[0-9]*$", "m")');
+
+ matches = JS('=List|Null', '#.match(#)', stack, pattern);
+ if (matches != null) return JS('String', '#[1]', matches);
+
+ throw new UnsupportedError('Cannot extract URI from "$stack"');
}
static computeGlobalThis() => JS('', 'function() { return this; }()');
« no previous file with comments | « no previous file | dart/tests/compiler/dart2js_native/compute_this_script_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698