Browse Source

[eval] allow full range of 32bit integers in `Std.random` (fixes #9974)

Aleksandr Kuzmenko 4 years ago
parent
commit
e4338d6438
3 changed files with 13 additions and 2 deletions
  1. 1 0
      extra/CHANGES.txt
  2. 2 2
      src/macro/eval/evalStdLib.ml
  3. 10 0
      tests/unit/src/unit/issues/Issue9974.hx

+ 1 - 0
extra/CHANGES.txt

@@ -12,6 +12,7 @@
 	php : fixed local vars with certain names (_SERVER, _GET etc) overriding super global values (#9924)
 	macro : added return type hint to haxe.macro.MacroStringTools.formatString (#9928)
 	cs : fixed catching exceptions from static closures (#9957)
+	eval : fixed `Std.random(arg)` for `arg` values of more than 30 bits (#9974)
 
 2020-09-11 4.1.4:
 

+ 2 - 2
src/macro/eval/evalStdLib.ml

@@ -2198,8 +2198,8 @@ module StdStd = struct
 	)
 
 	let random = vfun1 (fun v ->
-		let v = decode_int v in
-		vint (Random.State.int random (if v <= 0 then 1 else v))
+		let v = decode_i32 v in
+		vint32 (Random.State.int32 random (if v <= Int32.zero then Int32.one else v))
 	);
 end
 

+ 10 - 0
tests/unit/src/unit/issues/Issue9974.hx

@@ -0,0 +1,10 @@
+package unit.issues;
+
+class Issue9974 extends unit.Test {
+#if !neko
+	function test() {
+		var r = Std.random(0x7FFFFFFF);
+		t(r >= 0);
+	}
+#end
+}