Skip to content

Commit d2663fe

Browse files
inject jstack.JStack.onReady() automatically (fixes #3)
1 parent a0a5e7d commit d2663fe

File tree

5 files changed

+59
-26
lines changed

5 files changed

+59
-26
lines changed

README.md

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,12 @@ The only supported target is `js`.
55

66
Works only in debug mode on js target. Does not affect your app if compiled without `-debug` flag or to some other target.
77

8-
In debug mode Haxe generates a source map, which is utilized by JStack using [source-map library](https://github.com/mozilla/source-map).
8+
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)
99

1010
## Installation
1111
```haxe
1212
haxelib install jstack
1313
```
1414

1515
## Usage
16-
In most cases all you need to do is just add JStack to compilation with `-lib jstack` compiler flag.
17-
18-
### Accessing call stack before exiting `static main()`
19-
Source map loading is an asynchronous process, so if you need to access call stack right after your app started, you have to wait until source map is loaded:
20-
```haxe
21-
jstack.JStack.onReady(function () {
22-
//app start here
23-
});
24-
```
16+
Just add JStack to compilation with `-lib jstack` compiler flag.

extraParams.hxml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1-
--macro include('jstack')
2-
--macro keep('jstack.JStack')
1+
--macro include('jstack.JStack')
2+
--macro keep('jstack.JStack')
3+
--macro jstack.Tools.addInjectmetaToMain()

haxelib.json

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,9 @@
44
"license" : "MIT",
55
"tags" : ["js", "stack", "callstack", "stacktrace"],
66
"description" : "Friendly stack traces for JS target. Makes haxe.CallStack point to haxe sources.",
7-
"version" : "0.1.1",
8-
"releasenote" : "Public API stubs for not supported targets. ",
7+
"version" : "1.0.0",
8+
"releasenote" : "jstack.JStack.onReady() in automatically injected now.",
99
"classPath" : "src",
1010
"contributors" : ["RealyUniqueName"],
11-
"dependencies" : {
12-
13-
}
11+
"dependencies" : {}
1412
}

src/jstack/JStack.hx

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ class JStack {
2525

2626

2727
/**
28-
* Invoke `callback` when source map is loaded
28+
* Invoke `callback` when source map is loaded.
29+
* A call to this method is autonmatically injected in `static main()` function of your app.
30+
* You don't need to use this method manually.
2931
*/
3032
static public function onReady (callback:Void->Void) : Void
3133
{
@@ -173,12 +175,4 @@ private class StackPos
173175

174176
}
175177

176-
#else
177-
178-
/**
179-
* Public API stub for cases when jstack should not work.
180-
*/
181-
class JStack {
182-
static public function onReady (callback:Void->Void) haxe.Timer.delay(callback, 0);
183-
}
184178
#end

src/jstack/Tools.hx

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,56 @@ using haxe.io.Path;
1717
* Macro tools
1818
*/
1919
class Tools {
20+
#if macro
2021
static private inline var SOURCE_MAP_LIB_FILE = '../../js/source-map.min.js';
2122

23+
/**
24+
Inject `json.JStack.onReady()` into app entry point, so that app will not start untill source map is ready.
25+
**/
26+
static public function addInjectmetaToMain() : Void
27+
{
28+
if (Compiler.getDefine('display') != null) return;
29+
if (Compiler.getDefine('debug') == null || Compiler.getDefine('js') == null) return;
30+
31+
var main : String = null;
32+
var args = Sys.args();
33+
for (i in 0...args.length) {
34+
if(args[i] == '-main') {
35+
main = args[i + 1];
36+
break;
37+
}
38+
}
39+
if (main == null) {
40+
Context.warning('Failed to find entry point. Did you specify `-main` directive?', Context.currentPos());
41+
return;
42+
}
43+
44+
Compiler.addMetadata('@:build(jstack.Tools.injectInMain())', main);
45+
}
46+
#end
47+
48+
macro static public function injectInMain() : Array<Field>
49+
{
50+
var fields = Context.getBuildFields();
51+
var injected = false;
52+
53+
for (field in fields) {
54+
if (field.name != 'main') continue;
55+
56+
switch (field.kind) {
57+
case FFun(fn):
58+
fn.expr = macro jstack.JStack.onReady(function() ${fn.expr});
59+
case _:
60+
Context.error('Failed to inject JStack in `main` function.', field.pos);
61+
}
62+
}
63+
64+
if (!injected) {
65+
Context.error('Failed to find static function main.', Context.currentPos());
66+
}
67+
68+
return fields;
69+
}
2270

2371
/**
2472
* Returns file name of generated output

0 commit comments

Comments
 (0)