Skip to content

Commit

Permalink
added -D JSTACK_MAIN for custom entry points (fixes #4)
Browse files Browse the repository at this point in the history
  • Loading branch information
RealyUniqueName committed Mar 1, 2017
1 parent 497a33a commit 2345986
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 17 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,8 @@ haxelib install jstack

## Usage
Just add JStack to compilation with `-lib jstack` compiler flag.

If you don't have `-main` in your build config, then you need to specify entry point like this:
```
-D JSTACK_MAIN=my.SomeClass.entryPoint
```
2 changes: 1 addition & 1 deletion extraParams.hxml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
--macro include('jstack.JStack')
--macro keep('jstack.JStack')
--macro jstack.Tools.addInjectMetaToMain()
--macro jstack.Tools.addInjectMetaToEntryPoint()
4 changes: 2 additions & 2 deletions haxelib.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
"license" : "MIT",
"tags" : ["js", "php7", "stack", "callstack", "stacktrace"],
"description" : "Friendly stack traces for JS and PHP7 targets. Makes them point to haxe sources.",
"version" : "2.1.1",
"releasenote" : "Handle uncaught exceptions in nodejs",
"version" : "2.2.0",
"releasenote" : "Custom entry point with -D JSTACK_MAIN=my.SomeClass.entryPoint",
"classPath" : "src",
"contributors" : ["RealyUniqueName"],
"dependencies" : {
Expand Down
49 changes: 35 additions & 14 deletions src/jstack/Tools.hx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class Tools {
/**
Inject `json.JStack.onReady()` into app entry point, so that app will not start untill source map is ready.
**/
static public function addInjectMetaToMain() : Void
static public function addInjectMetaToEntryPoint() : Void
{
#if (display || !(debug || JSTACK_FORCE))
return;
Expand All @@ -32,42 +32,63 @@ class Tools {
Compiler.define('js_source_map');
Compiler.define('source_map');

var main : String = null;
var args = Sys.args();
for (i in 0...args.length) {
if(args[i] == '-main') {
main = args[i + 1];
break;
var entryClass : String = null;
var entryMethod : String = null;

if (Context.defined('JSTACK_MAIN')) {
var jstackMain = Context.definedValue('JSTACK_MAIN');

if (jstackMain == null || jstackMain.length == 0 || jstackMain == '1') {
Context.error('JSTACK_MAIN should have a value. E.g.: -D JSTACK_MAIN=my.SomeClass.entryPoint', (macro {}).pos);
}

var parts = jstackMain.split('.');
if (parts.length < 2) {
Context.error('JSTACK_MAIN value should have a class name and a function name. E.g.: -D JSTACK_MAIN=my.SomeClass.entryPoint', (macro {}).pos);
}
entryMethod = parts.pop();
entryClass = parts.join('.');
}
if (main == null) {
Context.warning('JStack: Failed to find entry point. Did you specify `-main` directive?', (macro {}).pos);

if (entryClass == null) {
var args = Sys.args();
for (i in 0...args.length) {
if(args[i] == '-main') {
entryClass = args[i + 1];
entryMethod = 'main';
break;
}
}
}

if (entryClass == null) {
Context.warning('JStack: Failed to find entry point. Did you specify `-main` or `-D JSTACK_MAIN`?', (macro {}).pos);
return;
}

Compiler.addMetadata('@:build(jstack.Tools.injectInMain())', main);
Compiler.addMetadata('@:build(jstack.Tools.injectInEntryPoint("$entryMethod"))', entryClass);
}
#end

macro static public function injectInMain() : Array<Field>
macro static public function injectInEntryPoint(method:String) : Array<Field>
{
var fields = Context.getBuildFields();
var injected = false;

for (field in fields) {
if (field.name != 'main') continue;
if (field.name != method) continue;

switch (field.kind) {
case FFun(fn):
fn.expr = macro jstack.JStack.onReady(function() ${fn.expr});
injected = true;
case _:
Context.error('JStack: Failed to inject JStack in `main` function.', field.pos);
Context.error('JStack: Failed to inject JStack in `$method` function.', field.pos);
}
}

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

return fields;
Expand Down

0 comments on commit 2345986

Please sign in to comment.