haxe/src/haxe/MainLoop.cs
// Generated by Haxe 4.3.7

#pragma warning disable 109, 114, 219, 429, 168, 162
namespace haxe {
	public class MainEvent : global::haxe.lang.HxObject {
		
		public MainEvent(global::haxe.lang.EmptyObject empty) {
		}
		
		
		public MainEvent(global::haxe.lang.Function f, int p) {
			global::haxe.MainEvent.__hx_ctor_haxe_MainEvent(this, f, p);
		}
		
		
		protected static void __hx_ctor_haxe_MainEvent(global::haxe.MainEvent __hx_this, global::haxe.lang.Function f, int p) {
			__hx_this.isBlocking = true;
			{
				__hx_this.f = f;
				__hx_this.priority = p;
				__hx_this.nextRun = global::Math.NEGATIVE_INFINITY;
			}
			
		}
		
		
		public global::haxe.lang.Function f;
		
		public global::haxe.MainEvent prev;
		
		public global::haxe.MainEvent next;
		
		public bool isBlocking;
		
		public double nextRun;
		
		public int priority;
		
		public virtual void delay(global::haxe.lang.Null<double> t) {
			this.nextRun = ( ( ! (t.hasValue) ) ? (global::Math.NEGATIVE_INFINITY) : (( global::haxe.Timer.stamp() + (t).@value )) );
		}
		
		
		public void call() {
			if (( this.f != null )) {
				this.f.__hx_invoke0_o();
			}
			
		}
		
		
		public virtual void stop() {
			if (( this.f == null )) {
				return;
			}
			
			this.f = null;
			this.nextRun = global::Math.NEGATIVE_INFINITY;
			if (( this.prev == null )) {
				global::haxe.MainLoop.pending = this.next;
			}
			else {
				this.prev.next = this.next;
			}
			
			if (( this.next != null )) {
				this.next.prev = this.prev;
			}
			
		}
		
		
		public override double __hx_setField_f(string field, int hash, double @value, bool handleProperties) {
			unchecked {
				switch (hash) {
					case 993950564:
					{
						this.priority = ((int) (@value) );
						return @value;
					}
					
					
					case 625308696:
					{
						this.nextRun = ((double) (@value) );
						return @value;
					}
					
					
					default:
					{
						return base.__hx_setField_f(field, hash, @value, handleProperties);
					}
					
				}
				
			}
		}
		
		
		public override object __hx_setField(string field, int hash, object @value, bool handleProperties) {
			unchecked {
				switch (hash) {
					case 993950564:
					{
						this.priority = ((int) (global::haxe.lang.Runtime.toInt(@value)) );
						return @value;
					}
					
					
					case 625308696:
					{
						this.nextRun = ((double) (global::haxe.lang.Runtime.toDouble(@value)) );
						return @value;
					}
					
					
					case 341784511:
					{
						this.isBlocking = global::haxe.lang.Runtime.toBool(@value);
						return @value;
					}
					
					
					case 1224901875:
					{
						this.next = ((global::haxe.MainEvent) (@value) );
						return @value;
					}
					
					
					case 1247723251:
					{
						this.prev = ((global::haxe.MainEvent) (@value) );
						return @value;
					}
					
					
					case 102:
					{
						this.f = ((global::haxe.lang.Function) (@value) );
						return @value;
					}
					
					
					default:
					{
						return base.__hx_setField(field, hash, @value, handleProperties);
					}
					
				}
				
			}
		}
		
		
		public override object __hx_getField(string field, int hash, bool throwErrors, bool isCheck, bool handleProperties) {
			unchecked {
				switch (hash) {
					case 1281093634:
					{
						return ((global::haxe.lang.Function) (new global::haxe.lang.Closure(this, "stop", 1281093634)) );
					}
					
					
					case 1102715038:
					{
						return ((global::haxe.lang.Function) (new global::haxe.lang.Closure(this, "call", 1102715038)) );
					}
					
					
					case 1462163331:
					{
						return ((global::haxe.lang.Function) (new global::haxe.lang.Closure(this, "delay", 1462163331)) );
					}
					
					
					case 993950564:
					{
						return this.priority;
					}
					
					
					case 625308696:
					{
						return this.nextRun;
					}
					
					
					case 341784511:
					{
						return this.isBlocking;
					}
					
					
					case 1224901875:
					{
						return this.next;
					}
					
					
					case 1247723251:
					{
						return this.prev;
					}
					
					
					case 102:
					{
						return this.f;
					}
					
					
					default:
					{
						return base.__hx_getField(field, hash, throwErrors, isCheck, handleProperties);
					}
					
				}
				
			}
		}
		
		
		public override double __hx_getField_f(string field, int hash, bool throwErrors, bool handleProperties) {
			unchecked {
				switch (hash) {
					case 993950564:
					{
						return ((double) (this.priority) );
					}
					
					
					case 625308696:
					{
						return this.nextRun;
					}
					
					
					default:
					{
						return base.__hx_getField_f(field, hash, throwErrors, handleProperties);
					}
					
				}
				
			}
		}
		
		
		public override object __hx_invokeField(string field, int hash, object[] dynargs) {
			unchecked {
				switch (hash) {
					case 1281093634:
					{
						this.stop();
						break;
					}
					
					
					case 1102715038:
					{
						this.call();
						break;
					}
					
					
					case 1462163331:
					{
						this.delay(global::haxe.lang.Null<object>.ofDynamic<double>(dynargs[0]));
						break;
					}
					
					
					default:
					{
						return base.__hx_invokeField(field, hash, dynargs);
					}
					
				}
				
				return null;
			}
		}
		
		
		public override void __hx_getFields(global::Array<string> baseArr) {
			baseArr.push("priority");
			baseArr.push("nextRun");
			baseArr.push("isBlocking");
			baseArr.push("next");
			baseArr.push("prev");
			baseArr.push("f");
			base.__hx_getFields(baseArr);
		}
		
		
	}
}



#pragma warning disable 109, 114, 219, 429, 168, 162
namespace haxe {
	public class MainLoop : global::haxe.lang.HxObject {
		
		public MainLoop(global::haxe.lang.EmptyObject empty) {
		}
		
		
		public MainLoop() {
			global::haxe.MainLoop.__hx_ctor_haxe_MainLoop(this);
		}
		
		
		protected static void __hx_ctor_haxe_MainLoop(global::haxe.MainLoop __hx_this) {
		}
		
		
		public static global::haxe.MainEvent pending;
		
		
		
		public static int get_threadCount() {
			return global::haxe.EntryPoint.threadCount;
		}
		
		
		public static bool hasEvents() {
			global::haxe.MainEvent p = global::haxe.MainLoop.pending;
			while (( p != null )) {
				if (p.isBlocking) {
					return true;
				}
				
				p = p.next;
			}
			
			return false;
		}
		
		
		public static void addThread(global::haxe.lang.Function f) {
			global::haxe.EntryPoint.addThread(f);
		}
		
		
		public static void runInMainThread(global::haxe.lang.Function f) {
			global::haxe.EntryPoint.runInMainThread(f);
		}
		
		
		public static global::haxe.MainEvent @add(global::haxe.lang.Function f, global::haxe.lang.Null<int> priority) {
			int priority1 = ( ( ! (priority.hasValue) ) ? (0) : ((priority).@value) );
			if (( f == null )) {
				throw ((global::System.Exception) (global::haxe.Exception.thrown("Event function is null")) );
			}
			
			global::haxe.MainEvent e = new global::haxe.MainEvent(((global::haxe.lang.Function) (f) ), ((int) (priority1) ));
			global::haxe.MainEvent head = global::haxe.MainLoop.pending;
			if (( head != null )) {
				head.prev = e;
			}
			
			e.next = head;
			global::haxe.MainLoop.pending = e;
			return e;
		}
		
		
		public static void sortEvents() {
			unchecked {
				global::haxe.MainEvent list = global::haxe.MainLoop.pending;
				if (( list == null )) {
					return;
				}
				
				int insize = 1;
				int nmerges = default(int);
				int psize = 0;
				int qsize = 0;
				global::haxe.MainEvent p = null;
				global::haxe.MainEvent q = null;
				global::haxe.MainEvent e = null;
				global::haxe.MainEvent tail = null;
				while (true) {
					p = list;
					list = null;
					tail = null;
					nmerges = 0;
					while (( p != null )) {
						 ++ nmerges;
						q = p;
						psize = 0;
						{
							int _g = 0;
							int _g1 = insize;
							while (( _g < _g1 )) {
								int i = _g++;
								 ++ psize;
								q = q.next;
								if (( q == null )) {
									break;
								}
								
							}
							
						}
						
						qsize = insize;
						while (( ( psize > 0 ) || ( ( qsize > 0 ) && ( q != null ) ) )) {
							if (( psize == 0 )) {
								e = q;
								q = q.next;
								 -- qsize;
							}
							else if (( ( ( qsize == 0 ) || ( q == null ) ) || (( ( p.priority > q.priority ) || ( ( p.priority == q.priority ) && ( p.nextRun <= q.nextRun ) ) )) )) {
								e = p;
								p = p.next;
								 -- psize;
							}
							else {
								e = q;
								q = q.next;
								 -- qsize;
							}
							
							if (( tail != null )) {
								tail.next = e;
							}
							else {
								list = e;
							}
							
							e.prev = tail;
							tail = e;
						}
						
						p = q;
					}
					
					tail.next = null;
					if (( nmerges <= 1 )) {
						break;
					}
					
					insize *= 2;
				}
				
				list.prev = null;
				global::haxe.MainLoop.pending = list;
			}
		}
		
		
		public static double tick() {
			global::haxe.MainLoop.sortEvents();
			global::haxe.MainEvent e = global::haxe.MainLoop.pending;
			double now = global::haxe.Timer.stamp();
			double wait = 1e9;
			while (( e != null )) {
				global::haxe.MainEvent next = e.next;
				double wt = ( e.nextRun - now );
				if (( wt <= 0 )) {
					wait = ((double) (0) );
					e.call();
				}
				else if (( wait > wt )) {
					wait = wt;
				}
				
				e = next;
			}
			
			return wait;
		}
		
		
	}
}