Pārlūkot izejas kodu

haxe.macro.Context.timer(id)
closes #8814

Aleksandr Kuzmenko 6 gadi atpakaļ
vecāks
revīzija
b4233983a5
3 mainītis faili ar 27 papildinājumiem un 0 dzēšanām
  1. 4 0
      src/core/timer.ml
  2. 5 0
      src/macro/macroApi.ml
  3. 18 0
      std/haxe/macro/Context.hx

+ 4 - 0
src/core/timer.ml

@@ -77,6 +77,10 @@ let timer id =
 	) else
 		(fun() -> ())
 
+let current_id() =
+	match !curtime with
+	| [] -> None
+	| t :: _ -> Some t.id
 
 let rec close_times() =
 	let now = get_time() in

+ 5 - 0
src/macro/macroApi.ml

@@ -1951,6 +1951,11 @@ let macro_api ccom get_api =
 			);
 			vnull
 		);
+		"timer", vfun1 (fun id ->
+			let full_id = (Option.default [] (Timer.current_id())) @ [decode_string id] in
+			let stop = Timer.timer full_id in
+			vfun0 (fun() -> stop(); vnull)
+		);
 	]
 
 

+ 18 - 0
std/haxe/macro/Context.hx

@@ -585,6 +585,24 @@ class Context {
 		load("register_module_dependency", 2)(modulePath, externFile);
 	}
 
+	/**
+		Creates a timer which will be printed in the compilation report
+		if `--times` compilation argument is set.
+
+		Note that a timer may be omitted from the report if the amount of time
+		measured is too small.
+
+		This method immediately starts a timer and returns a function to stop it:
+		```
+		var stopTimer = haxe.macro.Context.timer("my heavy task");
+		runTask();
+		stopTimer();
+		```
+	**/
+	public static function timer(id:String):()->Void {
+		return load("timer", 1)(id);
+	}
+
 	@:deprecated
 	public static function registerModuleReuseCall(modulePath:String, macroCall:String) {
 		throw "This method is no longer supported. See https://github.com/HaxeFoundation/haxe/issues/5746";