Browse Source

[js] generate enums as objects by default

use `-D js-enums-as-arrays` for previous behavior
Simon Krajewski 7 years ago
parent
commit
eb1d00fc9e
7 changed files with 18 additions and 17 deletions
  1. 1 0
      extra/CHANGES.txt
  2. 2 2
      src/core/define.ml
  3. 4 4
      src/generators/genjs.ml
  4. 1 1
      std/haxe/Serializer.hx
  5. 3 3
      std/js/Boot.hx
  6. 6 6
      std/js/_std/Type.hx
  7. 1 1
      tests/runci/targets/Js.hx

+ 1 - 0
extra/CHANGES.txt

@@ -10,6 +10,7 @@ XXXX-XX-XX: 4.0.0-preview.4
 	all : metadata can now use `.`, e.g. `@:a.b`. This is represented as a string (#3959)
 	all : metadata can now use `.`, e.g. `@:a.b`. This is represented as a string (#3959)
 	js : added externs for js.Date (#6855)
 	js : added externs for js.Date (#6855)
 	js : respect `-D source-map` flag to generate source maps in release builds
 	js : respect `-D source-map` flag to generate source maps in release builds
+	js : enums are now generated as objects instead of arrays (#6350)
 
 
 	Bugfixes:
 	Bugfixes:
 
 

+ 2 - 2
src/core/define.ml

@@ -55,7 +55,7 @@ type strict_defined =
 	| JsEs
 	| JsEs
 	| JsUnflatten
 	| JsUnflatten
 	| JsSourceMap
 	| JsSourceMap
-	| JsEnumsAsObjects
+	| JsEnumsAsArrays
 	| SourceMap
 	| SourceMap
 	| KeepOldOutput
 	| KeepOldOutput
 	| LoopUnrollMaxCost
 	| LoopUnrollMaxCost
@@ -166,7 +166,7 @@ let infos = function
 	| JqueryVer -> "jquery_ver",("The jQuery version supported by js.jquery.*. The version is encoded as an integer. e.g. 1.11.3 is encoded as 11103",[Platform Js])
 	| JqueryVer -> "jquery_ver",("The jQuery version supported by js.jquery.*. The version is encoded as an integer. e.g. 1.11.3 is encoded as 11103",[Platform Js])
 	| JsClassic -> "js_classic",("Don't use a function wrapper and strict mode in JS output",[Platform Js])
 	| JsClassic -> "js_classic",("Don't use a function wrapper and strict mode in JS output",[Platform Js])
 	| JsEs -> "js_es",("Generate JS compliant with given ES standard version (default 5)",[Platform Js; HasParam "version number"])
 	| JsEs -> "js_es",("Generate JS compliant with given ES standard version (default 5)",[Platform Js; HasParam "version number"])
-	| JsEnumsAsObjects -> "js_enums_as_objects",("Generate enum representation as object instead of as array",[Platform Js])
+	| JsEnumsAsArrays -> "js_enums_as_arrays",("Generate enum representation as array instead of as object",[Platform Js])
 	| JsUnflatten -> "js_unflatten",("Generate nested objects for packages and types",[Platform Js])
 	| JsUnflatten -> "js_unflatten",("Generate nested objects for packages and types",[Platform Js])
 	| JsSourceMap -> "js_source_map",("Generate JavaScript source map even in non-debug mode",[Platform Js])
 	| JsSourceMap -> "js_source_map",("Generate JavaScript source map even in non-debug mode",[Platform Js])
 	| SourceMap -> "source_map",("Generate source map for compiled files (Currently supported for php only)",[Platform Php])
 	| SourceMap -> "source_map",("Generate source map for compiled files (Currently supported for php only)",[Platform Php])

+ 4 - 4
src/generators/genjs.ml

@@ -525,13 +525,13 @@ and gen_expr ctx e =
 			print ctx ",$bind($_,$_%s))" (if Meta.has Meta.SelfCall f.cf_meta then "" else (field f.cf_name)))
 			print ctx ",$bind($_,$_%s))" (if Meta.has Meta.SelfCall f.cf_meta then "" else (field f.cf_name)))
 	| TEnumIndex x ->
 	| TEnumIndex x ->
 		gen_value ctx x;
 		gen_value ctx x;
-		if Common.defined ctx.com Define.JsEnumsAsObjects then
+		if not (Common.defined ctx.com Define.JsEnumsAsArrays) then
 		print ctx "._hx_index"
 		print ctx "._hx_index"
 		else
 		else
 		print ctx "[1]"
 		print ctx "[1]"
 	| TEnumParameter (x,f,i) ->
 	| TEnumParameter (x,f,i) ->
 		gen_value ctx x;
 		gen_value ctx x;
-		if Common.defined ctx.com Define.JsEnumsAsObjects then
+		if not (Common.defined ctx.com Define.JsEnumsAsArrays) then
 			let fname = (match f.ef_type with TFun((args,_)) -> let fname,_,_ = List.nth args i in  fname | _ -> assert false ) in
 			let fname = (match f.ef_type with TFun((args,_)) -> let fname,_,_ = List.nth args i in  fname | _ -> assert false ) in
 			print ctx ".%s" (fname)
 			print ctx ".%s" (fname)
 		else
 		else
@@ -1152,7 +1152,7 @@ let generate_enum ctx e =
 	if has_feature ctx "js.Boot.isEnum" then print ctx " __ename__ : %s," (if has_feature ctx "Type.getEnumName" then "[" ^ String.concat "," ename ^ "]" else "true");
 	if has_feature ctx "js.Boot.isEnum" then print ctx " __ename__ : %s," (if has_feature ctx "Type.getEnumName" then "[" ^ String.concat "," ename ^ "]" else "true");
 	print ctx " __constructs__ : [%s] }" (String.concat "," (List.map (fun s -> Printf.sprintf "\"%s\"" s) e.e_names));
 	print ctx " __constructs__ : [%s] }" (String.concat "," (List.map (fun s -> Printf.sprintf "\"%s\"" s) e.e_names));
 	ctx.separator <- true;
 	ctx.separator <- true;
-	let as_objects = Common.defined ctx.com Define.JsEnumsAsObjects in
+	let as_objects = not (Common.defined ctx.com Define.JsEnumsAsArrays) in
 	if as_objects then begin
 	if as_objects then begin
 		newline ctx;
 		newline ctx;
 		print ctx "$hxEnums[\"%s\"] = %s" p p
 		print ctx "$hxEnums[\"%s\"] = %s" p p
@@ -1459,7 +1459,7 @@ let generate com =
 	let vars = if has_feature ctx "has_enum"
 	let vars = if has_feature ctx "has_enum"
 		then ("$estr = function() { return " ^ (ctx.type_accessor (TClassDecl { null_class with cl_path = ["js"],"Boot" })) ^ ".__string_rec(this,''); }") :: vars
 		then ("$estr = function() { return " ^ (ctx.type_accessor (TClassDecl { null_class with cl_path = ["js"],"Boot" })) ^ ".__string_rec(this,''); }") :: vars
 		else vars in
 		else vars in
-	let vars = if Common.defined com Define.JsEnumsAsObjects then "$hxEnums = {}" :: vars else vars in
+	let vars = if not (Common.defined com Define.JsEnumsAsArrays) then "$hxEnums = {}" :: vars else vars in
 	(match List.rev vars with
 	(match List.rev vars with
 	| [] -> ()
 	| [] -> ()
 	| vl ->
 	| vl ->

+ 1 - 1
std/haxe/Serializer.hx

@@ -500,7 +500,7 @@ class Serializer {
 				buf.add("0");
 				buf.add("0");
 			}
 			}
 
 
-			#elseif js_enums_as_objects
+			#elseif (js && !js_enums_as_arrays)
 			if( useEnumIndex ) {
 			if( useEnumIndex ) {
 				buf.add(":");
 				buf.add(":");
 				buf.add(v._hx_index);
 				buf.add(v._hx_index);

+ 3 - 3
std/js/Boot.hx

@@ -79,7 +79,7 @@ class Boot {
 				t = "object";
 				t = "object";
 			switch( t ) {
 			switch( t ) {
 			case "object":
 			case "object":
-				#if js_enums_as_objects
+				#if !js_enums_as_arrays
 				if (o.__enum__) {
 				if (o.__enum__) {
 					var e = $hxEnums[o.__enum__];
 					var e = $hxEnums[o.__enum__];
 					var n = e.__constructs__[o._hx_index];
 					var n = e.__constructs__[o._hx_index];
@@ -94,7 +94,7 @@ class Boot {
 				}
 				}
 				#end
 				#end
 				if( js.Syntax.instanceof(o, Array) ) {
 				if( js.Syntax.instanceof(o, Array) ) {
-					#if !js_enums_as_objects
+					#if js_enums_as_arrays
 					if( o.__enum__ ) {
 					if( o.__enum__ ) {
 						if( o.length == 2 )
 						if( o.length == 2 )
 							return o[0];
 							return o[0];
@@ -206,7 +206,7 @@ class Boot {
 			// do not use isClass/isEnum here
 			// do not use isClass/isEnum here
 			untyped __feature__("Class.*",if( cl == Class && o.__name__ != null ) return true);
 			untyped __feature__("Class.*",if( cl == Class && o.__name__ != null ) return true);
 			untyped __feature__("Enum.*",if( cl == Enum && o.__ename__ != null ) return true);
 			untyped __feature__("Enum.*",if( cl == Enum && o.__ename__ != null ) return true);
-			#if !js_enums_as_objects
+			#if js_enums_as_arrays
 			return o.__enum__ == cl;
 			return o.__enum__ == cl;
 			#else
 			#else
 			return (untyped $hxEnums[o.__enum__]) == cl;
 			return (untyped $hxEnums[o.__enum__]) == cl;

+ 6 - 6
std/js/_std/Type.hx

@@ -41,7 +41,7 @@ enum ValueType {
 	public static function getEnum( o : EnumValue ) : Enum<Dynamic> untyped {
 	public static function getEnum( o : EnumValue ) : Enum<Dynamic> untyped {
 		if( o == null )
 		if( o == null )
 			return null;
 			return null;
-		#if !js_enums_as_objects
+		#if js_enums_as_arrays
 		return o.__enum__;
 		return o.__enum__;
 		#else
 		#else
 		return $hxEnums[o.__enum__];
 		return $hxEnums[o.__enum__];
@@ -191,7 +191,7 @@ enum ValueType {
 				return TNull;
 				return TNull;
 			var e = v.__enum__;
 			var e = v.__enum__;
 			if( e != null ){
 			if( e != null ){
-				#if !js_enums_as_objects
+				#if js_enums_as_arrays
 				return TEnum(e);
 				return TEnum(e);
 				#else
 				#else
 				return TEnum(untyped $hxEnums[e]);
 				return TEnum(untyped $hxEnums[e]);
@@ -216,7 +216,7 @@ enum ValueType {
 		if( a == b )
 		if( a == b )
 			return true;
 			return true;
 		try {
 		try {
-			#if !js_enums_as_objects
+			#if js_enums_as_arrays
 			if( a[0] != b[0] )
 			if( a[0] != b[0] )
 				return false;
 				return false;
 			for( i in 2...a.length )
 			for( i in 2...a.length )
@@ -242,7 +242,7 @@ enum ValueType {
 	}
 	}
 
 
 	public inline static function enumConstructor( e : EnumValue ) : String {
 	public inline static function enumConstructor( e : EnumValue ) : String {
-		#if !js_enums_as_objects
+		#if js_enums_as_arrays
 		return untyped e[0];
 		return untyped e[0];
 		#else
 		#else
 		return untyped $hxEnums[e.__enum__].__constructs__[e._hx_index];
 		return untyped $hxEnums[e.__enum__].__constructs__[e._hx_index];
@@ -250,7 +250,7 @@ enum ValueType {
 	}
 	}
 
 
 	public inline static function enumParameters( e : EnumValue ) : Array<Dynamic> {
 	public inline static function enumParameters( e : EnumValue ) : Array<Dynamic> {
-		#if !js_enums_as_objects
+		#if js_enums_as_arrays
 		return untyped e.slice(2);
 		return untyped e.slice(2);
 		#else
 		#else
 		var n = enumConstructor(e);
 		var n = enumConstructor(e);
@@ -260,7 +260,7 @@ enum ValueType {
 	}
 	}
 
 
 	public inline static function enumIndex( e : EnumValue ) : Int {
 	public inline static function enumIndex( e : EnumValue ) : Int {
-		#if js_enums_as_objects
+		#if !js_enums_as_arrays
 		return untyped e._hx_index;
 		return untyped e._hx_index;
 		#else
 		#else
 		return untyped e[1];
 		return untyped e[1];

+ 1 - 1
tests/runci/targets/Js.hx

@@ -31,7 +31,7 @@ class Js {
 			for (es3 in       [[], ["-D", "js-es=3"]])
 			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"]])
-			for (enums_as_objects in [[], ["-D", "js-enums-as-objects"]])
+			for (enums_as_objects in [[], ["-D", "js-enums-as-arrays"]])
 			{
 			{
 				var extras = args.concat(es3).concat(unflatten).concat(classic).concat(enums_as_objects);
 				var extras = args.concat(es3).concat(unflatten).concat(classic).concat(enums_as_objects);