Browse Source

[js] use $hxEnums for Type.resolveEnum and don't add enums to $hxClasses (see #7165)

also, inline Type.resolve(Enum|Class)
Dan Korostelev 7 years ago
parent
commit
c6bf8dc502
2 changed files with 18 additions and 4 deletions
  1. 8 4
      src/generators/genjs.ml
  2. 10 0
      std/js/_std/Type.hx

+ 8 - 4
src/generators/genjs.ml

@@ -1150,8 +1150,10 @@ let generate_enum ctx e =
 		generate_package_create ctx e.e_path;
 		generate_package_create ctx e.e_path;
 	print ctx "%s = " p;
 	print ctx "%s = " p;
 	let as_objects = not (Common.defined ctx.com Define.JsEnumsAsArrays) in
 	let as_objects = not (Common.defined ctx.com Define.JsEnumsAsArrays) in
-	if has_feature ctx "Type.resolveEnum" then print ctx "$hxClasses[\"%s\"] = " (dot_path e.e_path);
-	if as_objects then print ctx "$hxEnums[\"%s\"] = " dotp;
+	(if as_objects then
+		print ctx "$hxEnums[\"%s\"] = " dotp
+	else if has_feature ctx "Type.resolveEnum" then
+		print ctx "$hxClasses[\"%s\"] = " (dot_path e.e_path));
 	print ctx "{";
 	print ctx "{";
 	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));
@@ -1456,13 +1458,15 @@ let generate com =
 	if (not ctx.js_modern) && (ctx.es_version < 5) 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";
 
 
+	let enums_as_objects = not (Common.defined com Define.JsEnumsAsArrays) in
+
 	(* TODO: fix $estr *)
 	(* TODO: fix $estr *)
 	let vars = [] in
 	let vars = [] in
-	let vars = (if has_feature ctx "Type.resolveClass" || has_feature ctx "Type.resolveEnum" then ("$hxClasses = " ^ (if ctx.js_modern then "{}" else "$hxClasses || {}")) :: vars else vars) in
+	let vars = (if has_feature ctx "Type.resolveClass" || (not enums_as_objects && has_feature ctx "Type.resolveEnum") then ("$hxClasses = " ^ (if ctx.js_modern then "{}" else "$hxClasses || {}")) :: vars else vars) in
 	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 not (Common.defined com Define.JsEnumsAsArrays) then "$hxEnums = {}" :: vars else vars in
+	let vars = if enums_as_objects then "$hxEnums = {}" :: vars else vars in
 	(match List.rev vars with
 	(match List.rev vars with
 	| [] -> ()
 	| [] -> ()
 	| vl ->
 	| vl ->

+ 10 - 0
std/js/_std/Type.hx

@@ -65,6 +65,7 @@ enum ValueType {
 		return a.join(".");
 		return a.join(".");
 	}
 	}
 
 
+	#if js_enums_as_arrays
 	public static function resolveClass( name : String ) : Class<Dynamic> untyped {
 	public static function resolveClass( name : String ) : Class<Dynamic> untyped {
 		var cl : Class<Dynamic> = $hxClasses[name];
 		var cl : Class<Dynamic> = $hxClasses[name];
 		// ensure that this is a class
 		// ensure that this is a class
@@ -80,6 +81,15 @@ enum ValueType {
 			return null;
 			return null;
 		return e;
 		return e;
 	}
 	}
+	#else
+	public static inline function resolveClass( name : String ) : Class<Dynamic> {
+		return untyped __define_feature__("Type.resolveClass", $hxClasses[name]);
+	}
+
+	public static inline function resolveEnum( name : String ) : Enum<Dynamic> {
+		return untyped $hxEnums[name];
+	}
+	#end
 
 
 	#if (js_es < 5)
 	#if (js_es < 5)
 	public static function createInstance<T>( cl : Class<T>, args : Array<Dynamic> ) : T {
 	public static function createInstance<T>( cl : Class<T>, args : Array<Dynamic> ) : T {