Nicolas Cannasse 9 лет назад
Родитель
Сommit
0637ad6c96
3 измененных файлов с 26 добавлено и 6 удалено
  1. 11 2
      src/generators/hlinterp.ml
  2. 2 2
      std/hl/_std/Math.hx
  3. 13 2
      std/hl/_std/Std.hx

+ 11 - 2
src/generators/hlinterp.ml

@@ -46,6 +46,7 @@ and vabstract =
 	| AHashInt of (int32, value) Hashtbl.t
 	| AHashObject of (value * value) list ref
 	| AReg of regexp
+	| ARandom
 
 and vfunction =
 	| FFun of fundecl
@@ -1753,10 +1754,18 @@ let interp code =
 					regs.(pos) <- to_int (String.length str);
 					VBytes (caml_to_hl str)
 				| _ -> assert false)
-			| "random" ->
+			| "rnd_init_system" ->
 				(function
-				| [VInt max] -> VInt (if max <= 0l then 0l else Random.int32 max)
+				| [] -> Random.self_init(); VAbstract ARandom
 				| _ -> assert false)
+			| "rnd_int" ->
+				(function
+				| [VAbstract ARandom] -> VInt (Int32.of_int (Random.bits()))
+				| _ -> assert false)
+			| "rnd_float" ->
+				(function
+				| [VAbstract ARandom] -> VFloat (Random.float 1.)
+				| _ -> assert false)				
 			| "regexp_new_options" ->
 				(function
 				| [VBytes str; VBytes opt] ->

+ 2 - 2
std/hl/_std/Math.hx

@@ -45,6 +45,8 @@ class Math {
 	@:hlNative("std","math_pow") public static function pow( v : Float, exp : Float ) : Float 				return 0.;
 	@:hlNative("std","math_atan2") public static function atan2( y : Float, x : Float ) : Float 			return 0.;
 
+	public static function random() : Float return @:privateAccess Std.rnd_float(Std.rnd);
+
 	public static function min( a : Float, b : Float ) : Float 			return a < b || isNaN(a) ? a : b;
 	public static function max( a : Float, b : Float ) : Float 			return a < b || isNaN(b) ? b : a;
 
@@ -53,8 +55,6 @@ class Math {
 	public static var POSITIVE_INFINITY(default,null) : Float;
 	public static var NEGATIVE_INFINITY(default,null) : Float;
 
-	public static function random():Float return 0.15; // I rolled a D20
-
 	static function __init__() : Void {
 		PI = 3.1415926535897932384626433832795;
 		NaN = 0. / 0.;

+ 13 - 2
std/hl/_std/Std.hx

@@ -21,12 +21,23 @@
  */
 import hl.Boot;
 
+private typedef Rand = hl.types.NativeAbstract<"hl_random">;
+
 @:coreApi
 class Std {
 
-	@:hlNative("std","random")
+	static var rnd : Rand;
+
+	static function __init__() : Void {
+		rnd = rnd_sys();
+	}
+	
+	@:hlNative("std","rnd_init_system") static function rnd_sys() : Rand { return null; }
+	@:hlNative("std","rnd_int") static function rnd_int( r : Rand ) : Int { return 0; }
+	@:hlNative("std","rnd_float") static function rnd_float( r : Rand ) : Float { return 0.; }
+
 	public static function random( x : Int ) : Int {
-		return 0;
+		return x <= 0 ? 0 : (rnd_int(rnd) & 0x3FFFFFFF) % x;
 	}
 
 	public static function is( v : Dynamic, t : Dynamic ) : Bool {