Skip to content

Commit 2345986

Browse files
added -D JSTACK_MAIN for custom entry points (fixes #4)
1 parent 497a33a commit 2345986

File tree

4 files changed

+43
-17
lines changed

4 files changed

+43
-17
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,8 @@ haxelib install jstack
1313

1414
## Usage
1515
Just add JStack to compilation with `-lib jstack` compiler flag.
16+
17+
If you don't have `-main` in your build config, then you need to specify entry point like this:
18+
```
19+
-D JSTACK_MAIN=my.SomeClass.entryPoint
20+
```

extraParams.hxml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
--macro include('jstack.JStack')
22
--macro keep('jstack.JStack')
3-
--macro jstack.Tools.addInjectMetaToMain()
3+
--macro jstack.Tools.addInjectMetaToEntryPoint()

haxelib.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
"license" : "MIT",
55
"tags" : ["js", "php7", "stack", "callstack", "stacktrace"],
66
"description" : "Friendly stack traces for JS and PHP7 targets. Makes them point to haxe sources.",
7-
"version" : "2.1.1",
8-
"releasenote" : "Handle uncaught exceptions in nodejs",
7+
"version" : "2.2.0",
8+
"releasenote" : "Custom entry point with -D JSTACK_MAIN=my.SomeClass.entryPoint",
99
"classPath" : "src",
1010
"contributors" : ["RealyUniqueName"],
1111
"dependencies" : {

src/jstack/Tools.hx

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class Tools {
2323
/**
2424
Inject `json.JStack.onReady()` into app entry point, so that app will not start untill source map is ready.
2525
**/
26-
static public function addInjectMetaToMain() : Void
26+
static public function addInjectMetaToEntryPoint() : Void
2727
{
2828
#if (display || !(debug || JSTACK_FORCE))
2929
return;
@@ -32,42 +32,63 @@ class Tools {
3232
Compiler.define('js_source_map');
3333
Compiler.define('source_map');
3434

35-
var main : String = null;
36-
var args = Sys.args();
37-
for (i in 0...args.length) {
38-
if(args[i] == '-main') {
39-
main = args[i + 1];
40-
break;
35+
var entryClass : String = null;
36+
var entryMethod : String = null;
37+
38+
if (Context.defined('JSTACK_MAIN')) {
39+
var jstackMain = Context.definedValue('JSTACK_MAIN');
40+
41+
if (jstackMain == null || jstackMain.length == 0 || jstackMain == '1') {
42+
Context.error('JSTACK_MAIN should have a value. E.g.: -D JSTACK_MAIN=my.SomeClass.entryPoint', (macro {}).pos);
43+
}
44+
45+
var parts = jstackMain.split('.');
46+
if (parts.length < 2) {
47+
Context.error('JSTACK_MAIN value should have a class name and a function name. E.g.: -D JSTACK_MAIN=my.SomeClass.entryPoint', (macro {}).pos);
4148
}
49+
entryMethod = parts.pop();
50+
entryClass = parts.join('.');
4251
}
43-
if (main == null) {
44-
Context.warning('JStack: Failed to find entry point. Did you specify `-main` directive?', (macro {}).pos);
52+
53+
if (entryClass == null) {
54+
var args = Sys.args();
55+
for (i in 0...args.length) {
56+
if(args[i] == '-main') {
57+
entryClass = args[i + 1];
58+
entryMethod = 'main';
59+
break;
60+
}
61+
}
62+
}
63+
64+
if (entryClass == null) {
65+
Context.warning('JStack: Failed to find entry point. Did you specify `-main` or `-D JSTACK_MAIN`?', (macro {}).pos);
4566
return;
4667
}
4768

48-
Compiler.addMetadata('@:build(jstack.Tools.injectInMain())', main);
69+
Compiler.addMetadata('@:build(jstack.Tools.injectInEntryPoint("$entryMethod"))', entryClass);
4970
}
5071
#end
5172

52-
macro static public function injectInMain() : Array<Field>
73+
macro static public function injectInEntryPoint(method:String) : Array<Field>
5374
{
5475
var fields = Context.getBuildFields();
5576
var injected = false;
5677

5778
for (field in fields) {
58-
if (field.name != 'main') continue;
79+
if (field.name != method) continue;
5980

6081
switch (field.kind) {
6182
case FFun(fn):
6283
fn.expr = macro jstack.JStack.onReady(function() ${fn.expr});
6384
injected = true;
6485
case _:
65-
Context.error('JStack: Failed to inject JStack in `main` function.', field.pos);
86+
Context.error('JStack: Failed to inject JStack in `$method` function.', field.pos);
6687
}
6788
}
6889

6990
if (!injected) {
70-
Context.error('JStack: Failed to find static function main.', (macro {}).pos);
91+
Context.error('JStack: Failed to find entry point method "$method".', (macro {}).pos);
7192
}
7293

7394
return fields;

0 commit comments

Comments
 (0)