ソースを参照

[lua] add lua_vanilla behavior for integer clamping, rename clamp to clampInt32

Justin Donaldson 6 年 前
コミット
b1c9736436
5 ファイル変更21 行追加7 行削除
  1. 2 2
      std/haxe/Int32.hx
  2. 1 1
      std/haxe/io/Bytes.hx
  3. 1 1
      std/haxe/io/Input.hx
  4. 16 2
      std/lua/Boot.hx
  5. 1 1
      std/lua/_std/Std.hx

+ 2 - 2
std/haxe/Int32.hx

@@ -170,7 +170,7 @@ abstract Int32(Int) from Int to Int {
 
 	#if (lua || python || php)
 	@:op(~A) private static inline function complement(a:Int32):Int32
-		#if lua return lua.Boot.clamp(~a); #else return clamp(~a); #end
+		#if lua return lua.Boot.clampInt32(~a); #else return clamp(~a); #end
 	#else
 	@:op(~A) private function complement():Int32;
 	#end
@@ -273,7 +273,7 @@ abstract Int32(Int) from Int to Int {
 		#elseif python
 		return (python.Syntax.code("{0} % {1}", (x + python.Syntax.opPow(2, 31)), python.Syntax.opPow(2, 32)) : Int) - python.Syntax.opPow(2, 31);
 		#elseif lua
-		return lua.Boot.clamp(x);
+		return lua.Boot.clampInt32(x);
 		#else
 		return (x);
 		#end

+ 1 - 1
std/haxe/io/Bytes.hx

@@ -329,7 +329,7 @@ class Bytes {
 		return if (v & 0x80000000 != 0) v | 0x80000000 else v;
 		#elseif lua
 		var v = get(pos) | (get(pos + 1) << 8) | (get(pos + 2) << 16) | (get(pos + 3) << 24);
-		return lua.Boot.clamp(if (v & 0x80000000 != 0) v | 0x80000000 else v);
+		return lua.Boot.clampInt32(if (v & 0x80000000 != 0) v | 0x80000000 else v);
 		#else
 		return get(pos) | (get(pos + 1) << 8) | (get(pos + 2) << 16) | (get(pos + 3) << 24);
 		#end

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

@@ -292,7 +292,7 @@ class Input {
 			return n;
 		#elseif lua
 		var n = bigEndian ? ch4 | (ch3 << 8) | (ch2 << 16) | (ch1 << 24) : ch1 | (ch2 << 8) | (ch3 << 16) | (ch4 << 24);
-		return lua.Boot.clamp(n);
+		return lua.Boot.clampInt32(n);
 		#else
 		return bigEndian ? ch4 | (ch3 << 8) | (ch2 << 16) | (ch1 << 24) : ch1 | (ch2 << 8) | (ch3 << 16) | (ch4 << 24);
 		#end

+ 16 - 2
std/lua/Boot.hx

@@ -31,6 +31,10 @@ class Boot {
 	static var _:Dynamic;
 	static var _fid = 0;
 
+	static var Max_Int32 = 2147483647;
+	static var Min_Int32 = -2147483648;
+
+
 	// A max stack size to respect for unpack operations
 	public static var MAXSTACKSIZE(default, null) = 1000;
 
@@ -90,7 +94,7 @@ class Boot {
 
 		switch (cl) {
 			case Int:
-				return (Lua.type(o) == "number" && clamp(o) == o);
+				return (Lua.type(o) == "number" && clampInt32(o) == o);
 			case Float:
 				return Lua.type(o) == "number";
 			case Bool:
@@ -297,8 +301,18 @@ class Boot {
 	/**
 		A 32 bit clamp function for numbers
 	**/
-	public inline static function clamp(x:Float) {
+	public inline static function clampInt32(x:Float) {
+#if lua_vanilla
+		if (x < Min_Int32 ) {
+			return Min_Int32;
+		} else if (x > Max_Int32) {
+			return Max_Int32;
+		} else {
+			return Math.floor(x);
+		}
+#else
 		return untyped __define_feature__("lua.Boot.clamp", _hx_bit_clamp(x));
+#end
 	}
 
 	/**

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

@@ -47,7 +47,7 @@ import lua.NativeStringTools;
 		if (!Math.isFinite(x) || Math.isNaN(x))
 			return 0;
 		else
-			return lua.Boot.clamp(x);
+			return lua.Boot.clampInt32(x);
 	}
 
 	public static function parseInt(x:String):Null<Int> {