Browse Source

Lua : new std Math and related logging improvements

Lua has slightly different ways of referring to the concept of infinity
and NaN.  It also will print them out a bit differently.  This commit
standardizes the behavior against the base haxe Math class, and makes
some changes to the instance tracer method in Boot.hx.
Justin Donaldson 10 years ago
parent
commit
cc6901fe2f
2 changed files with 20 additions and 9 deletions
  1. 7 2
      std/lua/Boot.hx
  2. 13 7
      std/lua/_std/Math.hx

+ 7 - 2
std/lua/Boot.hx

@@ -79,11 +79,16 @@ class Boot {
 	}
 
 	@:ifFeature("may_print_enum")
-	private static function __string_rec(o, s = '') {
+	private static function __string_rec(o : Dynamic, s = '') {
 		untyped {
 			switch(__type__(o)){
 				case "nil": return "null";
-				case"number" : return untyped tostring(o);
+				case"number" : {
+					if (o == Math.INFINITY) return "Infinity";
+					else if (o == Math.NEGATIVE_INFINITY) return "-Infinity";
+					else if (o != o) return "NaN";
+					else return untyped tostring(o);
+				}
 				case "boolean" : return untyped tostring(o);
 				case "string": return o;
 				case "userdata": return "<userdata>";

+ 13 - 7
std/lua/_std/Math.hx

@@ -24,23 +24,27 @@ package;
 // Can't enable @:coreApi because some fields are now inline getters
 // @:coreApi
 @:keepInit
+@:native("_G.math")
 extern class Math
 {
-	static var PI(default,null) : Float;
+	static var PI(get,null) : Float;
+	private static inline function get_PI () : Float {
+		return untyped Math.pi;
+	}
 
 	static var NEGATIVE_INFINITY(get, null) : Float;
 	private static inline function get_NEGATIVE_INFINITY () : Float {
-		return -(untyped __js__("Infinity"));
+		return untyped Math.huge; 
 	}
 
 	static var POSITIVE_INFINITY(get,null) : Float;
 	private static inline function get_POSITIVE_INFINITY () : Float {
-		return (untyped __js__("Infinity"));
+		return untyped -Math.huge; 
 	}
 
 	static var NaN(get, null) : Float;
 	private static inline function get_NaN () : Float {
-		return (untyped __js__("NaN"));
+		return 0/0; 
 	}
 
 	static function abs(v:Float):Float;
@@ -57,7 +61,9 @@ extern class Math
 	static function min(a:Float, b:Float):Float;
 	static function pow(v:Float, exp:Float):Float;
 	static function random() : Float;
-	static function round(v:Float):Int;
+	static inline function round(v:Float):Int {
+		return Std.int(v + 0.5);
+	}
 	static function sin(v:Float):Float;
 	static function sqrt(v:Float):Float;
 	static function tan(v:Float):Float;
@@ -75,11 +81,11 @@ extern class Math
 	}
 
 	static inline function isFinite( f : Float ) : Bool {
-		return (untyped __js__("isFinite"))(f);
+		return (f > Math.NEGATIVE_INFINITY && f < Math.POSITIVE_INFINITY);
 	}
 
 	static inline function isNaN( f : Float ) : Bool {
-		return (untyped __js__("isNaN"))(f);
+		return (f != f);
 	}
 
 	static function __init__() : Void {