Skip to content

Commit 6182a0d

Browse files
Support for php7 target
1 parent a966d96 commit 6182a0d

File tree

5 files changed

+36
-13
lines changed

5 files changed

+36
-13
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
# JStack
22

3-
This library automatically transforms `haxe.CallStack.callStack()` and `haxe.CallStack.exceptionStack()` results to make them point at Haxe sources instead of generated js file.
4-
The only supported target is `js`.
3+
This library automatically transforms `haxe.CallStack.callStack()`, `haxe.CallStack.exceptionStack()` and uncaught exceptions where possible to make them point at Haxe sources instead of generated js or php files.
4+
The only supported targets are `js` and `php7`.
55

6-
Works only in debug mode or when `-D JSTACK_FORCE` is enabled on js target.
7-
Does not affect your app if compiled without `-debug` and `-D JSTACK_FORCE` flags or to some other target.
6+
Works only in debug mode or when `-D JSTACK_FORCE`.
7+
Does not affect your app if compiled without `-debug` and `-D JSTACK_FORCE` flags or to unsupported target.
88

99
In debug mode Haxe generates a source map, which is utilized by JStack using [source-map library](https://github.com/mozilla/source-map) (bundled with JStack and embded automatically)
1010

extraParams.hxml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
-D source-map
21
--macro include('jstack.JStack')
32
--macro keep('jstack.JStack')
43
--macro jstack.Tools.addInjectMetaToMain()

haxelib.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
"url" : "https://github.com/RealyUniqueName/JStack",
44
"license" : "MIT",
55
"tags" : ["js", "stack", "callstack", "stacktrace"],
6-
"description" : "Friendly stack traces for JS target. Makes haxe.CallStack point to haxe sources.",
7-
"version" : "2.0.0",
8-
"releasenote" : "Use `sourcemap` haxe lib instead of js lib.",
6+
"description" : "Friendly stack traces for JS and PHP7 targets. Makes them point to haxe sources.",
7+
"version" : "2.1.0",
8+
"releasenote" : "Support for php7 target",
99
"classPath" : "src",
1010
"contributors" : ["RealyUniqueName"],
1111
"dependencies" : {

src/jstack/Tools.hx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ class Tools {
2929
return;
3030
#end
3131
if (!Context.defined('js') && !Context.defined('php7')) return;
32+
Compiler.define('js_source_map');
33+
Compiler.define('source_map');
3234

3335
var main : String = null;
3436
var args = Sys.args();

src/jstack/php7/JStack.hx

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@ import php.Global.*;
55
import haxe.CallStack;
66
import sourcemap.SourcePos;
77

8-
using StringTools;
8+
using Type;
99

1010
/**
1111
* Handles source map
1212
*/
1313
class JStack {
1414
static var sourceMaps = new Map<String,SourceMap>();
15+
static var mapPositionDefined : Bool = false;
1516

1617
/**
1718
* Invokes initialization.
@@ -30,14 +31,25 @@ class JStack {
3031
@:access(haxe.CallStack)
3132
static public dynamic function uncaughtExceptionHandler (e:Throwable) : String {
3233
var stack = CallStack.makeStack(getNativeStack(e));
33-
var error = e.getMessage() + CallStack.toString(stack.map(mapStackItem)) + '\n';
34+
if (!mapPositionDefined) {
35+
stack = stack.map(mapStackItem);
36+
}
37+
var error = e.getMessage() + CallStack.toString(stack) + '\n';
3438
return error;
3539
}
3640

3741
/**
3842
* Initialization
3943
*/
4044
static function init () {
45+
//CallStack.exceptionStack() and CallStack.callStack();
46+
var phpClassName = Boot.getPhpName(CallStack.getClassName());
47+
mapPositionDefined = property_exists(phpClassName, 'mapPosition');
48+
if (mapPositionDefined) {
49+
Syntax.setStaticField(phpClassName, 'mapPosition', mapPosition);
50+
}
51+
52+
//Uncaught exceptions
4153
var userDefined = set_exception_handler(_uncaughtExceptionHandler);
4254
//we have a user-defined handler, don't overwrite it.
4355
if (userDefined != null) set_exception_handler(userDefined);
@@ -59,18 +71,28 @@ class JStack {
5971
static inline function mapStackItem (item:StackItem) : StackItem {
6072
switch (item) {
6173
case FilePos(symbol, file, line):
62-
var map = getSourceMap(file);
63-
if (map == null) {
74+
var pos = mapPosition(file, line);
75+
if (pos == null) {
6476
return item;
6577
} else {
66-
var pos = map.originalPositionFor(line);
6778
return FilePos(symbol, pos.source, pos.originalLine);
6879
}
6980
case _:
7081
return item;
7182
}
7283
}
7384

85+
/**
86+
* Map position in generated php file to original position in source hx file.
87+
* @param file - Generated php file name.
88+
* @param line - Line in generated php file name.
89+
* @return SourcePos
90+
*/
91+
static inline function mapPosition (file:String, line:Int) : Null<SourcePos> {
92+
var map = getSourceMap(file);
93+
return (map == null ? null : map.originalPositionFor(line));
94+
}
95+
7496
/**
7597
* Handles source map parsers for each file.
7698
* @param file - A name of a generated php file.

0 commit comments

Comments
 (0)