Browse Source

[lua] add int clamp behavior that more closely matches js, etc. Use this as default for Std.int

Justin Donaldson 8 years ago
parent
commit
cdab01efa5
4 changed files with 17 additions and 11 deletions
  1. 12 4
      src/generators/genlua.ml
  2. 1 3
      std/haxe/io/Input.hx
  3. 2 2
      std/lua/Boot.hx
  4. 2 2
      std/lua/_std/Std.hx

+ 12 - 4
src/generators/genlua.ml

@@ -1940,12 +1940,20 @@ let generate com =
 
 	if has_feature ctx "use._bitop" || has_feature ctx "lua.Boot.clamp" then begin
 	    println ctx "local _hx_bit_raw = require 'bit32'";
-	    println ctx "local function _hx_bit_clamp(v) return _hx_bit_raw.band(v, 2147483647 ) - _hx_bit_raw.band(v, 2147483648) end";
+	    println ctx "local function _hx_bit_clamp(v) ";
+	    println ctx "  if v <= 2147483647 and v >= -2147483648 then";
+	    println ctx "    if v > 0 then return _G.math.floor(v)";
+	    println ctx "    else return _G.math.ceil(v)";
+	    println ctx "    end";
+	    println ctx "  end";
+	    println ctx "  if v > 2251798999999999 then v = v*2 end;";
+	    println ctx "  return _hx_bit_raw.band(v, 2147483647 ) - _hx_bit_raw.band(v, 2147483648)";
+	    println ctx "end";
 	    println ctx "if type(jit) == 'table' then";
-	    println ctx "_hx_bit = setmetatable({},{__index = function(t,k) return function(...) return _hx_bit_clamp(rawget(_hx_bit_raw,k)(...)) end end})";
+	    println ctx "  _hx_bit = setmetatable({},{__index = function(t,k) return function(...) return _hx_bit_clamp(rawget(_hx_bit_raw,k)(...)) end end})";
 	    println ctx "else";
-	    println ctx "_hx_bit = setmetatable({}, { __index = _hx_bit_raw })";
-	    println ctx "_hx_bit.bnot = function(...) return _hx_bit_clamp(_hx_bit_raw.bnot(...)) end";
+	    println ctx "  _hx_bit = setmetatable({}, { __index = _hx_bit_raw })";
+	    println ctx "  _hx_bit.bnot = function(...) return _hx_bit_clamp(_hx_bit_raw.bnot(...)) end";
 	    println ctx "end";
 	end;
 

+ 1 - 3
std/haxe/io/Input.hx

@@ -288,9 +288,7 @@ class Input {
 		else return n;
 #elseif lua
 		var n = bigEndian ? ch4 | (ch3 << 8) | (ch2 << 16) | (ch1 << 24) : ch1 | (ch2 << 8) | (ch3 << 16) | (ch4 << 24);
-		// lua can't do 32 bit ints, the adjustment for 64 bits must be numeric
-		if (n > 2147483647) n -=  untyped 4294967296.;
-		return n ;
+		return lua.Boot.clamp(n);
 #else
 		return bigEndian ? ch4 | (ch3 << 8) | (ch2 << 16) | (ch1 << 24) : ch1 | (ch2 << 8) | (ch3 << 16) | (ch4 << 24);
 #end

+ 2 - 2
std/lua/Boot.hx

@@ -252,9 +252,9 @@ class Boot {
 	}
 
 	/*
-	   A 32 bit clamp function for integers
+	   A 32 bit clamp function for numbers
 	*/
-	public inline static function clamp(x:Int){
+	public inline static function clamp(x:Float){
 		return untyped __define_feature__("lua.Boot.clamp", _hx_bit_clamp(x));
 	}
 

+ 2 - 2
std/lua/_std/Std.hx

@@ -38,8 +38,8 @@ import lua.NativeStringTools;
 		return untyped lua.Boot.__string_rec(s);
 	}
 
-	public static inline function int( x : Float ) : Int {
-		return x > 0 ? Math.floor(x) : Math.ceil(x);
+	public static function int( x : Float ) : Int {
+		return lua.Boot.clamp(x);
 	}
 
 	public static function parseInt( x : String ) : Null<Int> {