@@ -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