Kaynağa Gözat

Only generate Math.__name__ for js if necessary.

Luca Deltodesco 11 yıl önce
ebeveyn
işleme
495b718164
2 değiştirilmiş dosya ile 83 ekleme ve 74 silme
  1. 15 8
      genjs.ml
  2. 68 66
      std/Math.hx

+ 15 - 8
genjs.ml

@@ -917,6 +917,17 @@ let gen_class_field ctx c f =
 		gen_value ctx e;
 		ctx.separator <- false
 
+let generate_class___name__ ctx c =
+	if has_feature ctx "js.Boot.isClass" then begin
+		let p = s_path ctx c.cl_path in
+		print ctx "%s.__name__ = " p;
+		if has_feature ctx "Type.getClassName" then
+			print ctx "[%s]" (String.concat "," (List.map (fun s -> Printf.sprintf "\"%s\"" (Ast.s_escape s)) (fst c.cl_path @ [snd c.cl_path])))
+		else
+			print ctx "true";
+		newline ctx;
+	end
+
 let generate_class ctx c =
 	ctx.current <- c;
 	ctx.id_counter <- 0;
@@ -942,14 +953,7 @@ let generate_class ctx c =
 		newline ctx;
 	end;
 	handle_expose ctx p c.cl_meta;
-	if has_feature ctx "js.Boot.isClass" then begin
-		print ctx "%s.__name__ = " p;
-		if has_feature ctx "Type.getClassName" then
-			print ctx "[%s]" (String.concat "," (List.map (fun s -> Printf.sprintf "\"%s\"" (Ast.s_escape s)) (fst c.cl_path @ [snd c.cl_path])))
-		else
-			print ctx "true";
-		newline ctx;
-	end;
+	generate_class___name__ ctx c;
 	(match c.cl_implements with
 	| [] -> ()
 	| l ->
@@ -1057,6 +1061,9 @@ let generate_type ctx = function
 		| None -> ()
 		| Some e ->
 			ctx.inits <- e :: ctx.inits);
+		(* Special case, want to add Math.__name__ only when required, handle here since Math is extern *)
+		let p = s_path ctx c.cl_path in
+		if p = "Math" then generate_class___name__ ctx c;
 		if not c.cl_extern then
 			generate_class ctx c
 		else if not ctx.js_flatten && Meta.has Meta.InitPackage c.cl_meta then

+ 68 - 66
std/Math.hx

@@ -26,30 +26,30 @@
 extern class Math
 {
 	static var PI(default,null) : Float;
-	
+
 	/**
 		A special Float constant which denotes negative infinity.
-		
+
 		For example, this is the result of -1.0 / 0.0.
-		
+
 		Operations with NEGATIVE_INFINITY as an operand may result in
 		Operations with NEGATIVE_INFINITY as an operand may result in
 		NEGATIVE_INFINITY, POSITIVE_INFINITY or NaN. For detailed information,
 		see ...
-		
+
 		If this constant is converted to an Int, e.g. through Std.int(), the
 		result is unspecified.
 	**/
 	static var NEGATIVE_INFINITY(default, null) : Float;
 	/**
 		A special Float constant which denotes negative infinity.
-		
+
 		For example, this is the result of 1.0 / 0.0.
-		
+
 		Operations with POSITIVE_INFINITY as an operand may result in
 		NEGATIVE_INFINITY, POSITIVE_INFINITY or NaN. For detailed information,
 		see ...
-	
+
 		If this constant is converted to an Int, e.g. through Std.int(), the
 		result is unspecified.
 	**/
@@ -57,18 +57,18 @@ extern class Math
 
 	/**
 		A special Float constant which denotes an invalid number.
-		
+
 		NaN stands for "Not a Number". It occurs when a mathematically incorrect
 		operation is executed, such as taking the square root of a negative
 		number: Math.sqrt(-1).
-		
+
 		All further operations with NaN as an operand will result in NaN.
-		
+
 		If this constant is converted to an Int, e.g. through Std.int(), the
 		result is unspecified.
-		
+
 		In order to test if a value is NaN, you should use Math.isNaN() function.
-		
+
 		(Php) In PHP versions prior to 5.3.1 VC 9 there may be unexpected
 		results when performing arithmetic operations with NaN on Windows, see:
 			https://bugs.php.net/bug.php?id=42143
@@ -77,149 +77,149 @@ extern class Math
 
 	/**
 		Returns the absolute value of `v`.
-		
+
 		If `v` is positive or 0, the result is unchanged. Otherwise the result
 		is -`v`.
-		
+
 		If `v` is NEGATIVE_INFINITY or POSITIVE_INFINITY, the result is
 		POSITIVE_INFINITY.
-		
+
 		If `v` is NaN, the result is NaN.
 	**/
 	static function abs(v:Float):Float;
-	
+
 	/**
 		Returns the smaller of values `a` and `b`.
-		
+
 		If `a` or `b` are NaN, the result is NaN.
-		
+
 		If `a` or `b` are NEGATIVE_INFINITY, the result is NEGATIVE_INFINITY.
-		
+
 		If `a` and `b` are POSITIVE_INFINITY, the result is POSITIVE_INFINITY.
 	**/
 	static function min(a:Float, b:Float):Float;
-	
+
 	/**
 		Returns the greater of values `a` and `b`.
-		
+
 		If `a` or `b` are NaN, the result is NaN.
-		
+
 		If `a` or `b` are POSITIVE_INFINITY, the result is POSITIVE_INFINITY.
-		
+
 		If `a` and `b` are NEGATIVE_INFINITY, the result is NEGATIVE_INFINITY.
 	**/
 	static function max(a:Float, b:Float):Float;
-	
+
 	/**
 		Returns the trigonometric sine of `v`.
-		
+
 		The unit of `v` is radians.
-		
+
 		If `v` is NaN or infinite, the result is NaN.
 	**/
 	static function sin(v:Float):Float;
-	
+
 	/**
 		Returns the trigonometric cosine of `v`.
-		
+
 		The unit of `v` is radians.
-		
+
 		If `v` is NaN or infinite, the result is NaN.
 	**/
 	static function cos(v:Float):Float;
-	
+
 	// TODO
 	static function tan(v:Float):Float;
 	static function asin(v:Float):Float;
 	static function acos(v:Float):Float;
 	static function atan(v:Float):Float;
 	static function atan2(y:Float, x:Float):Float;
-	
+
 	/**
 		Returns Euler's number, raised to the power of `v`.
-		
+
 		exp(1.0) is approximately 2.718281828459.
-		
+
 		If `v` is POSITIVE_INFINITY, the result is POSITIVE_INFINITY.
-		
+
 		If `v` is NEGATIVE_INFINITY, the result is 0.0.
-		
+
 		If `v` is NaN, the result is NaN.
 	**/
 	static function exp(v:Float):Float;
-	
+
 	/**
 		Returns the natural logarithm of `v`.
-		
+
 		If `v` is negative (including NEGATIVE_INFINITY) or NaN, the result is
 		NaN.
-		
+
 		If `v` is POSITIVE_INFINITY, the result is POSITIVE_INFINITY.
-		
+
 		If `v` is 0.0, the result is NEGATIVE_INFINITY.
-		
+
 		This is the inverse operation of exp, i.e. log(exp(v)) == v always
 		holds.
 	**/
 	static function log(v:Float):Float;
-	
+
 	// TODO
 	// http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/Math.html#pow(double, double) <-- wtf?
 	static function pow(v:Float, exp:Float):Float;
-	
+
 	/**
 		Returns the square root of `v`.
-		
+
 		If `v` is negative (including NEGATIVE_INFINITY) or NaN, the result is
 		NaN.
-		
+
 		If `v` is POSITIVE_INFINITY, the result is POSITIVE_INFINITY.
-		
+
 		If `v` is 0.0, the result is 0.0.
 	**/
 	static function sqrt(v:Float):Float;
-	
+
 	/**
 		Rounds `v` to the nearest Int value.
 
 		If v is outside of the signed Int32 range, or is NaN, NEGATIVE_INFINITY or POSITIVE_INFINITY, the result is unspecified.
-		
+
 		TODO: need spec
 	**/
 	static function round(v:Float):Int;
-	
+
 	/**
 		Returns the largest Int value that is not greater than `v`.
-		
-		If v is outside of the signed Int32 range, or is NaN, NEGATIVE_INFINITY or POSITIVE_INFINITY, the result is unspecified.		
-		
+
+		If v is outside of the signed Int32 range, or is NaN, NEGATIVE_INFINITY or POSITIVE_INFINITY, the result is unspecified.
+
 		TODO: need spec
 	**/
 	static function floor(v:Float):Int;
-	
+
 	/**
 		Returns the smallest Int value that is not less than `v`.
 
 		If v is outside of the signed Int32 range, or is NaN, NEGATIVE_INFINITY or POSITIVE_INFINITY, the result is unspecified.
-		
+
 		TODO: need spec
 	**/
 	static function ceil(v:Float):Int;
-	
+
 	/**
 		Returns a pseudo-random number which is greater than or equal to 0.0,
 		and less than 1.0.
 	**/
 	static function random() : Float;
-	
+
 	#if ((flash9 && !as3) || cpp)
-	
+
 	static function ffloor( v : Float ) : Float;
 	static function fceil( v : Float ) : Float;
 	static function fround( v : Float ) : Float;
-	
+
 	#else
-	
+
 	static inline function ffloor( v : Float ) : Float {
 		return floor(v);
 	}
@@ -231,25 +231,25 @@ extern class Math
 	static inline function fround( v : Float ) : Float {
 		return round(v);
 	}
-	
+
 	#end
-	
+
 
 	/**
 		Tells if `f` is a finite number.
-		
+
 		If `f` is POSITIVE_INFINITY, NEGATIVE_INFINITY or NaN, the result is
 		false.
-		
+
 		Otherwise the result is true.
 	**/
 	static function isFinite( f : Float ) : Bool;
-	
+
 	/**
 		Tells if `f` is not a valid number.
-		
+
 		If `f` is NaN, the result is true.
-		
+
 		Otherwise the result is false. In particular, both POSITIVE_INFINITY and
 		NEGATIVE_INFINITY are not considered NaN.
 	**/
@@ -261,7 +261,9 @@ extern class Math
 		NEGATIVE_INFINITY = __global__["Number"].NEGATIVE_INFINITY;
 		POSITIVE_INFINITY = __global__["Number"].POSITIVE_INFINITY;
 	#else
-		Math.__name__ = ["Math"];
+		#if !js // genjs.ml will insert this only if necessary.
+			Math.__name__ = ["Math"];
+		#end
 		Math.NaN = Number["NaN"];
 		Math.NEGATIVE_INFINITY = Number["NEGATIVE_INFINITY"];
 		Math.POSITIVE_INFINITY = Number["POSITIVE_INFINITY"];