Pārlūkot izejas kodu

[js] add notion of EcmaScript version, controllable with -D js-es=<value> (default 5), closes #4961

Dan Korostelev 9 gadi atpakaļ
vecāks
revīzija
0800b456f4

+ 5 - 3
src/generators/genjs.ml

@@ -44,6 +44,7 @@ type ctx = {
 	smap : sourcemap;
 	smap : sourcemap;
 	js_modern : bool;
 	js_modern : bool;
 	js_flatten : bool;
 	js_flatten : bool;
+	es_version : int;
 	store_exception_stack : bool;
 	store_exception_stack : bool;
 	mutable current : tclass;
 	mutable current : tclass;
 	mutable statics : (tclass * string * texpr) list;
 	mutable statics : (tclass * string * texpr) list;
@@ -1252,6 +1253,7 @@ let alloc_ctx com =
 		};
 		};
 		js_modern = not (Common.defined com Define.JsClassic);
 		js_modern = not (Common.defined com Define.JsClassic);
 		js_flatten = not (Common.defined com Define.JsUnflatten);
 		js_flatten = not (Common.defined com Define.JsUnflatten);
+		es_version = int_of_string (Common.defined_value com Define.JsEs);
 		store_exception_stack = if Common.has_dce com then (Common.has_feature com "haxe.CallStack.exceptionStack") else List.exists (function TClassDecl { cl_path=["haxe"],"CallStack" } -> true | _ -> false) com.types;
 		store_exception_stack = if Common.has_dce com then (Common.has_feature com "haxe.CallStack.exceptionStack") else List.exists (function TClassDecl { cl_path=["haxe"],"CallStack" } -> true | _ -> false) com.types;
 		statics = [];
 		statics = [];
 		inits = [];
 		inits = [];
@@ -1364,7 +1366,7 @@ let generate com =
 		closureArgs
 		closureArgs
 	in
 	in
 	(* Provide console for environments that may not have it. *)
 	(* Provide console for environments that may not have it. *)
-	let closureArgs = if (not (Common.defined com Define.JsEs5)) then
+	let closureArgs = if ctx.es_version < 5 then
 		var_console :: closureArgs
 		var_console :: closureArgs
 	else
 	else
 		closureArgs
 		closureArgs
@@ -1409,13 +1411,13 @@ let generate com =
 	) include_files;
 	) include_files;
 
 
 	(* If ctx.js_modern, console is defined in closureArgs. *)
 	(* If ctx.js_modern, console is defined in closureArgs. *)
-	if (not ctx.js_modern) && (not (Common.defined com Define.JsEs5)) then
+	if (not ctx.js_modern) && (ctx.es_version < 5) then
 		add_feature ctx "js.Lib.global"; (* console polyfill will check console from $global *)
 		add_feature ctx "js.Lib.global"; (* console polyfill will check console from $global *)
 
 
 	if (not ctx.js_modern) && (has_feature ctx "js.Lib.global") then
 	if (not ctx.js_modern) && (has_feature ctx "js.Lib.global") then
 		print ctx "var %s = %s;\n" (fst var_global) (snd var_global);
 		print ctx "var %s = %s;\n" (fst var_global) (snd var_global);
 
 
-	if (not ctx.js_modern) && (not (Common.defined com Define.JsEs5)) then
+	if (not ctx.js_modern) && (ctx.es_version < 5) then
 		spr ctx "var console = $global.console || {log:function(){}};\n";
 		spr ctx "var console = $global.console || {log:function(){}};\n";
 
 
 	(* TODO: fix $estr *)
 	(* TODO: fix $estr *)

+ 16 - 0
src/main.ml

@@ -1458,6 +1458,22 @@ try
 		| Js ->
 		| Js ->
 			if not (PMap.exists (fst (Define.infos Define.JqueryVer)) com.defines) then
 			if not (PMap.exists (fst (Define.infos Define.JqueryVer)) com.defines) then
 				Common.define_value com Define.JqueryVer "11202";
 				Common.define_value com Define.JqueryVer "11202";
+
+			let es_version =
+				try
+					int_of_string (Common.defined_value com Define.JsEs)
+				with
+				| Not_found ->
+					(Common.define_value com Define.JsEs "5"; 5)
+				| _ ->
+					0
+			in
+
+			if es_version < 3 || es_version = 4 then (* we don't support ancient and there's no 4th *)
+				failwith "Invalid -D js-es value";
+
+			if es_version >= 5 then Common.raw_define com "js-es5"; (* backward-compatibility *)
+
 			add_std "js";
 			add_std "js";
 			"js"
 			"js"
 		| Php ->
 		| Php ->

+ 2 - 2
src/typing/common.ml

@@ -196,7 +196,7 @@ module Define = struct
 		| JavaVer
 		| JavaVer
 		| JqueryVer
 		| JqueryVer
 		| JsClassic
 		| JsClassic
-		| JsEs5
+		| JsEs
 		| JsUnflatten
 		| JsUnflatten
 		| KeepOldOutput
 		| KeepOldOutput
 		| LoopUnrollMaxCost
 		| LoopUnrollMaxCost
@@ -286,7 +286,7 @@ module Define = struct
 		| JavaVer -> ("java_ver", "<version:5-7> Sets the Java version to be targeted")
 		| JavaVer -> ("java_ver", "<version:5-7> Sets the Java version to be targeted")
 		| JqueryVer -> ("jquery_ver", "The jQuery version supported by js.jquery.*. The version is encoded as an interger. e.g. 1.11.3 is encoded as 11103")
 		| JqueryVer -> ("jquery_ver", "The jQuery version supported by js.jquery.*. The version is encoded as an interger. e.g. 1.11.3 is encoded as 11103")
 		| JsClassic -> ("js_classic","Don't use a function wrapper and strict mode in JS output")
 		| JsClassic -> ("js_classic","Don't use a function wrapper and strict mode in JS output")
-		| JsEs5 -> ("js_es5","Generate JS for ES5-compliant runtimes")
+		| JsEs -> ("js_es","Generate JS compilant with given ES standard version (default 5)")
 		| JsUnflatten -> ("js_unflatten","Generate nested objects for packages and types")
 		| JsUnflatten -> ("js_unflatten","Generate nested objects for packages and types")
 		| KeepOldOutput -> ("keep_old_output","Keep old source files in the output directory (for C#/Java)")
 		| KeepOldOutput -> ("keep_old_output","Keep old source files in the output directory (for C#/Java)")
 		| LoopUnrollMaxCost -> ("loop_unroll_max_cost","Maximum cost (number of expressions * iterations) before loop unrolling is canceled (default 250)")
 		| LoopUnrollMaxCost -> ("loop_unroll_max_cost","Maximum cost (number of expressions * iterations) before loop unrolling is canceled (default 250)")

+ 1 - 1
std/js/_std/Array.hx

@@ -45,7 +45,7 @@ extern class Array<T> {
 		return @:privateAccess HxOverrides.remove(this,x);
 		return @:privateAccess HxOverrides.remove(this,x);
 	}
 	}
 
 
-#if js_es5
+#if (js_es >= 5)
 	function indexOf( x : T, ?fromIndex:Int ) : Int;
 	function indexOf( x : T, ?fromIndex:Int ) : Int;
 	function lastIndexOf( x : T, ?fromIndex:Int ) : Int;
 	function lastIndexOf( x : T, ?fromIndex:Int ) : Int;
 
 

+ 2 - 2
std/js/_std/HxOverrides.hx

@@ -76,7 +76,7 @@ class HxOverrides {
 				return "";
 				return "";
 		}
 		}
 
 
-		#if !js_es5
+		#if (js_es < 5)
 		if (pos < 0) {
 		if (pos < 0) {
 			pos = s.length + pos;
 			pos = s.length + pos;
 			if (pos < 0)
 			if (pos < 0)
@@ -138,7 +138,7 @@ class HxOverrides {
 	}
 	}
 
 
 	static function __init__() untyped {
 	static function __init__() untyped {
-#if !js_es5
+#if (js_es < 5)
 		__feature__('HxOverrides.indexOf', if( Array.prototype.indexOf ) __js__("HxOverrides").indexOf = function(a,o,i) return Array.prototype.indexOf.call(a, o, i));
 		__feature__('HxOverrides.indexOf', if( Array.prototype.indexOf ) __js__("HxOverrides").indexOf = function(a,o,i) return Array.prototype.indexOf.call(a, o, i));
 		__feature__('HxOverrides.lastIndexOf', if( Array.prototype.lastIndexOf ) __js__("HxOverrides").lastIndexOf = function(a,o,i) return Array.prototype.lastIndexOf.call(a, o, i));
 		__feature__('HxOverrides.lastIndexOf', if( Array.prototype.lastIndexOf ) __js__("HxOverrides").lastIndexOf = function(a,o,i) return Array.prototype.lastIndexOf.call(a, o, i));
 #end
 #end

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

@@ -91,7 +91,7 @@ import js.Boot;
 			var Void = __feature__("Type.resolveEnum", $hxClasses["Void"] = { __ename__ : ["Void"] }, { __ename__ : ["Void"] });
 			var Void = __feature__("Type.resolveEnum", $hxClasses["Void"] = { __ename__ : ["Void"] }, { __ename__ : ["Void"] });
 		});
 		});
 
 
-#if !js_es5
+#if (js_es < 5)
 		__feature__("Array.map",
 		__feature__("Array.map",
 			if( Array.prototype.map == null )
 			if( Array.prototype.map == null )
 				Array.prototype.map = function(f) {
 				Array.prototype.map = function(f) {

+ 5 - 5
tests/RunCi.hx

@@ -171,7 +171,7 @@ class RunCi {
 				Sys.putEnv("AUDIODEV", "null");
 				Sys.putEnv("AUDIODEV", "null");
 				requireAptPackages([
 				requireAptPackages([
 					"libcurl3:i386", "libglib2.0-0:i386", "libx11-6:i386", "libxext6:i386",
 					"libcurl3:i386", "libglib2.0-0:i386", "libx11-6:i386", "libxext6:i386",
-					"libxt6:i386", "libxcursor1:i386", "libnss3:i386", "libgtk2.0-0:i386"	
+					"libxt6:i386", "libxcursor1:i386", "libnss3:i386", "libgtk2.0-0:i386"
 				]);
 				]);
 				runCommand("wget", ["-nv", "http://fpdownload.macromedia.com/pub/flashplayer/updaters/11/flashplayer_11_sa_debug.i386.tar.gz"], true);
 				runCommand("wget", ["-nv", "http://fpdownload.macromedia.com/pub/flashplayer/updaters/11/flashplayer_11_sa_debug.i386.tar.gz"], true);
 				runCommand("tar", ["-xf", "flashplayer_11_sa_debug.i386.tar.gz", "-C", Sys.getEnv("HOME")]);
 				runCommand("tar", ["-xf", "flashplayer_11_sa_debug.i386.tar.gz", "-C", Sys.getEnv("HOME")]);
@@ -675,8 +675,8 @@ class RunCi {
 	*/
 	*/
 	static function deployPPA():Void {
 	static function deployPPA():Void {
 		if (
 		if (
-			gitInfo.branch == "development" && 
-			Sys.getEnv("DEPLOY") != null && 
+			gitInfo.branch == "development" &&
+			Sys.getEnv("DEPLOY") != null &&
 			Sys.getEnv("haxeci_decrypt") != null
 			Sys.getEnv("haxeci_decrypt") != null
 		) {
 		) {
 			// setup haxeci_ssh
 			// setup haxeci_ssh
@@ -959,11 +959,11 @@ class RunCi {
 					getJSDependencies();
 					getJSDependencies();
 
 
 					var jsOutputs = [
 					var jsOutputs = [
-						for (es5 in       [[], ["-D", "js-es5"]])
+						for (es3 in       [[], ["-D", "js-es=3"]])
 						for (unflatten in [[], ["-D", "js-unflatten"]])
 						for (unflatten in [[], ["-D", "js-unflatten"]])
 						for (classic in   [[], ["-D", "js-classic"]])
 						for (classic in   [[], ["-D", "js-classic"]])
 						{
 						{
-							var extras = args.concat(es5).concat(unflatten).concat(classic);
+							var extras = args.concat(es3).concat(unflatten).concat(classic);
 
 
 							runCommand("haxe", ["compile-js.hxml"].concat(extras));
 							runCommand("haxe", ["compile-js.hxml"].concat(extras));
 
 

+ 1 - 1
tests/unit/src/RunSauceLabs.hx

@@ -226,7 +226,7 @@ class RunSauceLabs {
 
 
 			var browserSuccess = true;
 			var browserSuccess = true;
 			var urls = if (!isEs5(caps)) {
 			var urls = if (!isEs5(caps)) {
-				urls.filter(function(url:String) return url.indexOf("-es5") < 0);
+				urls.filter(function(url:String) return url.indexOf("js-es=3") != -1);
 			} else {
 			} else {
 				urls;
 				urls;
 			}
 			}