@@ -18,6 +18,12 @@ class JStack {
1818 static var mapper : SourceMap ;
1919 /** User-defined callback which will be invoked when sourceMap is loaded */
2020 static var onReadyCallback : Void -> Void ;
21+ /**
22+ * Full name of currently being executed js file.
23+ * If JStack fails to resolve current file name, then `currentFile` will be an empty string.
24+ */
25+ static var currentFile (get ,never ): String ;
26+ static var _currentFile : String = null ;
2127
2228 /** Native stack lines like: at /home/alex/Work/HaXe/jstack/build/js/test.js:729:22 */
2329 static var stackFile = ~/ ^ at (. +? js):([0-9 ] + ):([0-9 ] + )$ / ;
@@ -70,7 +76,10 @@ class JStack {
7076 mapper = new SourceMap (sourceMapData );
7177
7278 CallStack .wrapCallSite = function (site ) {
73- var pos = mapper .originalPositionFor (site .getLineNumber (), site .getColumnNumber ());
79+ var pos = null ;
80+ if (site .getFileName () == currentFile ) {
81+ pos = mapper .originalPositionFor (site .getLineNumber (), site .getColumnNumber ());
82+ }
7483 return new StackPos (site , pos );
7584 }
7685
@@ -176,7 +185,7 @@ class JStack {
176185 switch (item ) {
177186 case Module (line ) if (stackFile .match (line )):
178187 var file = stackFile .matched (1 );
179- if (file != currentFile () ) return item ;
188+ if (file != currentFile ) return item ;
180189
181190 var line = Std .parseInt (stackFile .matched (2 ));
182191 var column = Std .parseInt (stackFile .matched (3 ));
@@ -186,7 +195,7 @@ class JStack {
186195
187196 case Module (line ) if (stackFunctionFile .match (line )):
188197 var file = stackFunctionFile .matched (2 );
189- if (file != currentFile () ) return item ;
198+ if (file != currentFile ) return item ;
190199
191200 var line = Std .parseInt (stackFunctionFile .matched (3 ));
192201 var column = Std .parseInt (stackFunctionFile .matched (4 ));
@@ -206,14 +215,20 @@ class JStack {
206215 }
207216 }
208217
209- /**
210- Returns full name of currently being executed js file.
211- **/
212- static function currentFile () : Null <String > {
213- if (isNode ()) {
214- return untyped __js__ (' __filename' );
218+ static function get_currentFile () : String {
219+ if (_currentFile == null ) {
220+ if (isNode ()) {
221+ _currentFile = untyped __js__ (' __filename' );
222+ } else {
223+ var erFile = ~/ \( (. +? ):([0-9 ] + ):([0-9 ] + )\) $ / ;
224+ if (erFile .match (new Error ().stack )) {
225+ _currentFile = erFile .matched (1 );
226+ } else {
227+ _currentFile = ' ' ;
228+ }
229+ }
215230 }
216- return null ;
231+ return _currentFile ;
217232 }
218233}
219234
@@ -234,7 +249,7 @@ class StackPos {
234249 }
235250
236251 public function getFunctionName () return js .getFunctionName ();
237- public function getFileName () return (hx == null || hx .originalLine == null ? js .getFileName () : hx .source );
252+ public function getFileName () return (hx == null || hx .source == null ? js .getFileName () : hx .source );
238253 public function getLineNumber () return (hx == null || hx .originalLine == null ? js .getLineNumber () : hx .originalLine );
239254
240255}
0 commit comments