Skip to content

Commit a966d96

Browse files
support php7 target
1 parent ad2be20 commit a966d96

File tree

5 files changed

+270
-160
lines changed

5 files changed

+270
-160
lines changed

extraParams.hxml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
-D source-map
12
--macro include('jstack.JStack')
23
--macro keep('jstack.JStack')
34
--macro jstack.Tools.addInjectMetaToMain()

src/jstack/JStack.hx

Lines changed: 6 additions & 158 deletions
Original file line numberDiff line numberDiff line change
@@ -1,163 +1,11 @@
11
package jstack;
22

3-
#if (js && (debug || JSTACK_FORCE))
3+
#if (debug || JSTACK_FORCE)
44

5-
import haxe.CallStack;
6-
import sourcemap.SourcePos;
7-
import js.Lib;
8-
import haxe.io.Path;
9-
import js.Browser;
10-
import haxe.Http;
11-
12-
using StringTools;
13-
14-
/**
15-
* Handles source map
16-
*/
17-
class JStack {
18-
19-
/** Create instance just to invoke `inject()` */
20-
static private var instance = new JStack();
21-
/** User-defined callback which will be invoked when sourceMap is loaded */
22-
static private var onReadyCallback : Void->Void;
23-
24-
/** Indicates if source map is loaded */
25-
public var ready (default,null) : Bool = false;
26-
27-
28-
/**
29-
* Invoke `callback` when source map is loaded.
30-
* A call to this method is automatically injected in `static main()` function of your app.
31-
* You don't need to use this method manually.
32-
*/
33-
static public function onReady (callback:Void->Void) : Void
34-
{
35-
onReadyCallback = callback;
36-
if (instance.ready) callback();
37-
}
38-
39-
40-
private function new ()
41-
{
42-
inject();
43-
}
44-
45-
46-
/**
47-
* Loads source map and injects hooks into `haxe.CallStack`.
48-
* It's asynchronous process so haxe-related positions in call stack might be not available right away.
49-
*/
50-
public function inject () : Void
51-
{
52-
loadSourceMap(function (sourceMapData:String) {
53-
var mapper = new SourceMap(sourceMapData);
54-
55-
CallStack.wrapCallSite = function (site) {
56-
var pos = mapper.originalPositionFor(site.getLineNumber(), site.getColumnNumber());
57-
return new StackPos(site, pos);
58-
}
59-
60-
ready = true;
61-
if (onReadyCallback != null) {
62-
onReadyCallback();
63-
}
64-
});
65-
}
66-
67-
68-
/**
69-
* Load source map and pass it to `callback`
70-
*/
71-
private function loadSourceMap (callback : String->Void) : Void
72-
{
73-
if (untyped __js__("typeof window != 'undefined'")) {
74-
loadInBrowser(callback);
75-
} else {
76-
loadInNode(callback);
77-
}
78-
}
79-
80-
81-
/**
82-
* Do the job in browser environment
83-
*/
84-
private function loadInBrowser (callback:String->Void) : Void
85-
{
86-
var file = getCurrentDirInBrowser() + '/' + Tools.getSourceMapFileName();
87-
var http = new Http(file);
88-
89-
http.onError = function (error:String) {
90-
trace(error);
91-
}
92-
http.onData = function (sourceMap:String) {
93-
callback(sourceMap);
94-
}
95-
http.request();
96-
}
97-
98-
99-
/**
100-
* Do the job in nodejs environment
101-
*/
102-
public function loadInNode (callback:String->Void) : Void
103-
{
104-
var dir : String = untyped __js__("__dirname");
105-
var fs = untyped __js__("require('fs')");
106-
107-
fs.readFile(dir + '/' + Tools.getSourceMapFileName(), function(error, sourceMap) {
108-
if (error != null) {
109-
trace(error);
110-
} else {
111-
callback(sourceMap);
112-
}
113-
});
114-
}
115-
116-
117-
/**
118-
* Scans DOM for <script> tags to find current script directory
119-
*/
120-
public function getCurrentDirInBrowser () : String
121-
{
122-
var file = Tools.getOutputFileName();
123-
var scripts = Browser.document.getElementsByTagName('script');
124-
125-
var fullPath = './$file';
126-
for (i in 0...scripts.length) {
127-
var src = scripts.item(i).attributes.getNamedItem('src');
128-
if (src != null && src.value.indexOf(file) >= 0) {
129-
fullPath = src.value;
130-
}
131-
}
132-
var path = new Path(fullPath);
133-
134-
return path.dir;
135-
}
136-
137-
}
138-
139-
140-
/**
141-
* Represents call stack position
142-
*/
143-
@:keep
144-
private class StackPos
145-
{
146-
/** JS side */
147-
private var js : Dynamic;
148-
/** HX side */
149-
private var hx : Dynamic;
150-
151-
public function new (js:Dynamic, hx:SourcePos)
152-
{
153-
this.js = js;
154-
this.hx = hx;
155-
}
156-
157-
public function getFunctionName () return js.getFunctionName();
158-
public function getFileName () return (hx == null || hx.originalLine == null ? js.getFileName() : hx.source);
159-
public function getLineNumber () return (hx == null || hx.originalLine == null ? js.getLineNumber() : hx.originalLine);
160-
161-
}
5+
#if js
6+
typedef JStack = jstack.js.JStack;
7+
#elseif (php && php7)
8+
typedef JStack = jstack.php7.JStack;
9+
#end
16210

16311
#end

src/jstack/Tools.hx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ class Tools {
2525
**/
2626
static public function addInjectMetaToMain() : Void
2727
{
28-
#if (display || !debug)
28+
#if (display || !(debug || JSTACK_FORCE))
2929
return;
3030
#end
31-
if (Context.definedValue('js') == null) return;
31+
if (!Context.defined('js') && !Context.defined('php7')) return;
3232

3333
var main : String = null;
3434
var args = Sys.args();

src/jstack/js/JStack.hx

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
package jstack.js;
2+
3+
import haxe.CallStack;
4+
import sourcemap.SourcePos;
5+
import js.Lib;
6+
import haxe.io.Path;
7+
import js.Browser;
8+
import haxe.Http;
9+
10+
using StringTools;
11+
12+
/**
13+
* Handles source map
14+
*/
15+
class JStack {
16+
17+
/** Create instance just to invoke `inject()` */
18+
static private var instance = new JStack();
19+
/** User-defined callback which will be invoked when sourceMap is loaded */
20+
static private var onReadyCallback : Void->Void;
21+
22+
/** Indicates if source map is loaded */
23+
public var ready (default,null) : Bool = false;
24+
25+
26+
/**
27+
* Invoke `callback` when source map is loaded.
28+
* A call to this method is automatically injected in `static main()` function of your app.
29+
* You don't need to use this method manually.
30+
*/
31+
static public function onReady (callback:Void->Void) : Void
32+
{
33+
onReadyCallback = callback;
34+
if (instance.ready) callback();
35+
}
36+
37+
38+
private function new ()
39+
{
40+
inject();
41+
}
42+
43+
44+
/**
45+
* Loads source map and injects hooks into `haxe.CallStack`.
46+
* It's asynchronous process so haxe-related positions in call stack might be not available right away.
47+
*/
48+
public function inject () : Void
49+
{
50+
loadSourceMap(function (sourceMapData:String) {
51+
var mapper = new SourceMap(sourceMapData);
52+
53+
CallStack.wrapCallSite = function (site) {
54+
var pos = mapper.originalPositionFor(site.getLineNumber(), site.getColumnNumber());
55+
return new StackPos(site, pos);
56+
}
57+
58+
ready = true;
59+
if (onReadyCallback != null) {
60+
onReadyCallback();
61+
}
62+
});
63+
}
64+
65+
66+
/**
67+
* Load source map and pass it to `callback`
68+
*/
69+
private function loadSourceMap (callback : String->Void) : Void
70+
{
71+
if (untyped __js__("typeof window != 'undefined'")) {
72+
loadInBrowser(callback);
73+
} else {
74+
loadInNode(callback);
75+
}
76+
}
77+
78+
79+
/**
80+
* Do the job in browser environment
81+
*/
82+
private function loadInBrowser (callback:String->Void) : Void
83+
{
84+
var file = getCurrentDirInBrowser() + '/' + Tools.getSourceMapFileName();
85+
var http = new Http(file);
86+
87+
http.onError = function (error:String) {
88+
trace(error);
89+
}
90+
http.onData = function (sourceMap:String) {
91+
callback(sourceMap);
92+
}
93+
http.request();
94+
}
95+
96+
97+
/**
98+
* Do the job in nodejs environment
99+
*/
100+
public function loadInNode (callback:String->Void) : Void
101+
{
102+
var dir : String = untyped __js__("__dirname");
103+
var fs = untyped __js__("require('fs')");
104+
105+
fs.readFile(dir + '/' + Tools.getSourceMapFileName(), function(error, sourceMap) {
106+
if (error != null) {
107+
trace(error);
108+
} else {
109+
callback(sourceMap);
110+
}
111+
});
112+
}
113+
114+
115+
/**
116+
* Scans DOM for <script> tags to find current script directory
117+
*/
118+
public function getCurrentDirInBrowser () : String
119+
{
120+
var file = Tools.getOutputFileName();
121+
var scripts = Browser.document.getElementsByTagName('script');
122+
123+
var fullPath = './$file';
124+
for (i in 0...scripts.length) {
125+
var src = scripts.item(i).attributes.getNamedItem('src');
126+
if (src != null && src.value.indexOf(file) >= 0) {
127+
fullPath = src.value;
128+
}
129+
}
130+
var path = new Path(fullPath);
131+
132+
return path.dir;
133+
}
134+
135+
}
136+
137+
138+
/**
139+
* Represents call stack position
140+
*/
141+
@:keep
142+
private class StackPos
143+
{
144+
/** JS side */
145+
private var js : Dynamic;
146+
/** HX side */
147+
private var hx : Dynamic;
148+
149+
public function new (js:Dynamic, hx:SourcePos)
150+
{
151+
this.js = js;
152+
this.hx = hx;
153+
}
154+
155+
public function getFunctionName () return js.getFunctionName();
156+
public function getFileName () return (hx == null || hx.originalLine == null ? js.getFileName() : hx.source);
157+
public function getLineNumber () return (hx == null || hx.originalLine == null ? js.getLineNumber() : hx.originalLine);
158+
159+
}

0 commit comments

Comments
 (0)