Skip to content

Commit ab8f082

Browse files
Add only_new option to haxe.macro.Context.onGenerate
1 parent b6a2db6 commit ab8f082

File tree

5 files changed

+38
-10
lines changed

5 files changed

+38
-10
lines changed

src/context/common.ml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ class compiler_callbacks = object(self)
5151
val mutable after_typing = [];
5252
val before_save = ref [];
5353
val after_save = ref [];
54+
val before_save_only_new = ref [];
55+
val after_save_only_new = ref [];
5456
val after_filters = ref [];
5557
val after_generation = ref [];
5658
val mutable null_safety_report = [];
@@ -70,6 +72,12 @@ class compiler_callbacks = object(self)
7072
method add_after_save (f : unit -> unit) : unit =
7173
after_save := f :: !after_save
7274

75+
method add_before_save_only_new (f : module_type list -> unit) : unit =
76+
before_save_only_new := f :: !before_save_only_new
77+
78+
method add_after_save_only_new (f : module_type list -> unit) : unit =
79+
after_save_only_new := f :: !after_save_only_new
80+
7381
method add_after_filters (f : unit -> unit) : unit =
7482
after_filters := f :: !after_filters
7583

@@ -88,11 +96,22 @@ class compiler_callbacks = object(self)
8896
List.iter (fun f -> try f() with Error.Error err -> handle_error err) (List.rev l);
8997
self#run handle_error r
9098

99+
method run_with_types handle_error r (tl: module_type list) =
100+
match !r with
101+
| [] ->
102+
()
103+
| l ->
104+
r := [];
105+
List.iter (fun f -> try f tl with Error.Error err -> handle_error err) (List.rev l);
106+
self#run_with_types handle_error r tl
107+
91108
method get_before_typer_create = before_typer_create
92109
method get_after_init_macros = after_init_macros
93110
method get_after_typing = after_typing
94111
method get_before_save = before_save
95112
method get_after_save = after_save
113+
method get_before_save_only_new = before_save_only_new
114+
method get_after_save_only_new = after_save_only_new
96115
method get_after_filters = after_filters
97116
method get_after_generation = after_generation
98117
method get_null_safety_report = null_safety_report

src/filters/filters.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,7 @@ let run com ectx main before_destruction =
512512
);
513513
with_timer com.timer_ctx detail_times "callbacks" None (fun () ->
514514
com.callbacks#run com.error_ext com.callbacks#get_before_save;
515+
com.callbacks#run_with_types com.error_ext com.callbacks#get_before_save_only_new new_types;
515516
);
516517
Common.enter_stage com CSaveStart;
517518
with_timer com.timer_ctx detail_times "save state" None (fun () ->
@@ -527,6 +528,7 @@ let run com ectx main before_destruction =
527528
Common.enter_stage com CSaveDone;
528529
with_timer com.timer_ctx detail_times "callbacks" None (fun () ->
529530
com.callbacks#run com.error_ext com.callbacks#get_after_save;
531+
com.callbacks#run_with_types com.error_ext com.callbacks#get_after_save_only_new new_types;
530532
);
531533
before_destruction();
532534
destruction com scom ectx detail_times main rename_locals_config com.types all_types_array

src/macro/macroApi.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ type 'value compiler_api = {
3131
include_module : string -> unit;
3232
after_init_macros : (unit -> unit) -> unit;
3333
after_typing : (module_type list -> unit) -> unit;
34-
on_generate : (Type.t list -> unit) -> bool -> unit;
34+
on_generate : (Type.t list -> unit) -> bool -> bool -> unit;
3535
after_generate : (unit -> unit) -> unit;
3636
on_type_not_found : (string -> 'value) -> unit;
3737
parse_string : string -> Globals.pos -> bool -> Ast.expr;
@@ -1901,9 +1901,9 @@ let macro_api ccom get_api =
19011901
(get_api()).after_typing (fun tl -> ignore(f [encode_array (List.map encode_module_type tl)]));
19021902
vnull
19031903
);
1904-
"on_generate", vfun2 (fun f b ->
1904+
"on_generate", vfun3 (fun f persistent only_new ->
19051905
let f = prepare_callback f 1 in
1906-
(get_api()).on_generate (fun tl -> ignore(f [encode_array (List.map encode_type tl)])) (decode_bool b);
1906+
(get_api()).on_generate (fun tl -> ignore(f [encode_array (List.map encode_type tl)])) (decode_bool persistent) (decode_bool only_new);
19071907
vnull
19081908
);
19091909
"on_after_generate", vfun1 (fun f ->

src/typing/macroContext.ml

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -149,10 +149,15 @@ let make_macro_com_api com mcom p =
149149
macro_timer com.timer_ctx timer_level ["afterTyping"] None f tl;
150150
)
151151
);
152-
on_generate = (fun f b ->
153-
(if b then com.callbacks#add_before_save else com.callbacks#add_after_save) (fun() ->
154-
macro_timer com.timer_ctx timer_level ["onGenerate"] None f (List.map type_of_module_type com.types);
155-
)
152+
on_generate = (fun f persistent only_new ->
153+
if only_new then
154+
(if persistent then com.callbacks#add_before_save_only_new else com.callbacks#add_after_save_only_new) (fun tl ->
155+
macro_timer com.timer_ctx timer_level ["onGenerate"] None f (List.map type_of_module_type tl);
156+
)
157+
else
158+
(if persistent then com.callbacks#add_before_save else com.callbacks#add_after_save) (fun () ->
159+
macro_timer com.timer_ctx timer_level ["onGenerate"] None f (List.map type_of_module_type com.types);
160+
)
156161
);
157162
after_generate = (fun f ->
158163
com.callbacks#add_after_generation (fun() ->

std/haxe/macro/Context.hx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,9 @@ class Context {
436436
compiler's typing phase, just before its generation phase.
437437
438438
The callback receives an `Array` containing all types which are about
439-
to be generated. Modifications are limited to metadata, it is mainly
439+
to be generated, unless `only_new` is set to `true` in which case the callback
440+
only receives types that were not cached by the compilation server.
441+
Modifications are limited to metadata, it is mainly
440442
intended to obtain information.
441443
442444
By default, the callback is made before types are stored in the compilation
@@ -447,8 +449,8 @@ class Context {
447449
448450
*Note*: the callback is still invoked when generation is disabled with `--no-output`.
449451
**/
450-
public static function onGenerate(callback:Array<Type>->Void, persistent:Bool = true) {
451-
load("on_generate", 2)(callback, persistent);
452+
public static function onGenerate(callback:Array<Type>->Void, persistent:Bool = true, only_new:Bool = false) {
453+
load("on_generate", 3)(callback, persistent, only_new);
452454
}
453455

454456
/**

0 commit comments

Comments
 (0)