Skip to content

Commit b90794e

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

File tree

6 files changed

+44
-25
lines changed

6 files changed

+44
-25
lines changed

src/compiler/compiler.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ module Setup = struct
180180
) com.defines.values;
181181
Buffer.truncate buffer (Buffer.length buffer - 1);
182182
Common.log com (Buffer.contents buffer);
183-
com.callbacks#run com.error_ext com.callbacks#get_before_typer_create;
183+
Common.run_callbacks com.error_ext com.callbacks#get_before_typer_create;
184184
TyperEntry.create com macros
185185

186186
let executable_path() =
@@ -310,7 +310,7 @@ let do_type ctx mctx actx display_file_dot_path =
310310
CommonCache.lock_signature com "after_init_macros";
311311
Option.may (fun mctx -> MacroContext.finalize_macro_api tctx mctx) mctx;
312312
(try begin
313-
com.callbacks#run com.error_ext com.callbacks#get_after_init_macros;
313+
Common.run_callbacks com.error_ext com.callbacks#get_after_init_macros;
314314
run_or_diagnose ctx (fun () ->
315315
if com.display.dms_kind <> DMNone then DisplayTexpr.check_display_file tctx cs;
316316
List.iter (fun cpath ->
@@ -419,7 +419,7 @@ let compile ctx actx callbacks =
419419
ServerMessage.compiler_stage com;
420420
end;
421421
Sys.catch_break false;
422-
com.callbacks#run com.error_ext com.callbacks#get_after_generation;
422+
Common.run_callbacks com.error_ext com.callbacks#get_after_generation;
423423
if not actx.no_output then begin
424424
List.iter (fun c ->
425425
let r = run_command ctx c in

src/context/common.ml

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,23 @@ type stats = {
4545
s_macros_called : int ref;
4646
}
4747

48+
let rec run_callbacks handle_error r v =
49+
match !r with
50+
| [] ->
51+
()
52+
| l ->
53+
r := [];
54+
List.iter (fun f -> try f v with Error.Error err -> handle_error err) (List.rev l);
55+
run_callbacks handle_error r v
56+
4857
class compiler_callbacks = object(self)
4958
val before_typer_create = ref [];
5059
val after_init_macros = ref [];
5160
val mutable after_typing = [];
5261
val before_save = ref [];
5362
val after_save = ref [];
63+
val before_save_only_new = ref [];
64+
val after_save_only_new = ref [];
5465
val after_filters = ref [];
5566
val after_generation = ref [];
5667
val mutable null_safety_report = [];
@@ -70,6 +81,12 @@ class compiler_callbacks = object(self)
7081
method add_after_save (f : unit -> unit) : unit =
7182
after_save := f :: !after_save
7283

84+
method add_before_save_only_new (f : module_type list -> unit) : unit =
85+
before_save_only_new := f :: !before_save_only_new
86+
87+
method add_after_save_only_new (f : module_type list -> unit) : unit =
88+
after_save_only_new := f :: !after_save_only_new
89+
7390
method add_after_filters (f : unit -> unit) : unit =
7491
after_filters := f :: !after_filters
7592

@@ -79,20 +96,13 @@ class compiler_callbacks = object(self)
7996
method add_null_safety_report (f : (string*pos) list -> unit) : unit =
8097
null_safety_report <- f :: null_safety_report
8198

82-
method run handle_error r =
83-
match !r with
84-
| [] ->
85-
()
86-
| l ->
87-
r := [];
88-
List.iter (fun f -> try f() with Error.Error err -> handle_error err) (List.rev l);
89-
self#run handle_error r
90-
9199
method get_before_typer_create = before_typer_create
92100
method get_after_init_macros = after_init_macros
93101
method get_after_typing = after_typing
94102
method get_before_save = before_save
95103
method get_after_save = after_save
104+
method get_before_save_only_new = before_save_only_new
105+
method get_after_save_only_new = after_save_only_new
96106
method get_after_filters = after_filters
97107
method get_after_generation = after_generation
98108
method get_null_safety_report = null_safety_report

src/filters/filters.ml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ let destruction (com : Common.context) scom ectx detail_times main rename_locals
268268
)
269269
);
270270

271-
com.callbacks#run com.error_ext com.callbacks#get_after_filters;
271+
Common.run_callbacks com.error_ext com.callbacks#get_after_filters;
272272
Common.enter_stage com CFilteringDone
273273

274274
let update_cache_dependencies ~close_monomorphs scom t =
@@ -511,7 +511,8 @@ let run com ectx main before_destruction =
511511
)
512512
);
513513
with_timer com.timer_ctx detail_times "callbacks" None (fun () ->
514-
com.callbacks#run com.error_ext com.callbacks#get_before_save;
514+
Common.run_callbacks com.error_ext com.callbacks#get_before_save;
515+
Common.run_callbacks 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 () ->
@@ -526,7 +527,8 @@ let run com ectx main before_destruction =
526527
);
527528
Common.enter_stage com CSaveDone;
528529
with_timer com.timer_ctx detail_times "callbacks" None (fun () ->
529-
com.callbacks#run com.error_ext com.callbacks#get_after_save;
530+
Common.run_callbacks com.error_ext com.callbacks#get_after_save;
531+
Common.run_callbacks 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 `onlyNew` 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, onlyNew:Bool = false) {
453+
load("on_generate", 3)(callback, persistent, onlyNew);
452454
}
453455

454456
/**

0 commit comments

Comments
 (0)