Browse Source

detect assignment of TEnum to TDynamic and activate may_print_enum feature (closes #2500)

Simon Krajewski 11 years ago
parent
commit
a660dc949e
3 changed files with 21 additions and 6 deletions
  1. 10 0
      filters.ml
  2. 10 5
      genjs.ml
  3. 1 1
      std/js/Boot.hx

+ 10 - 0
filters.ml

@@ -863,6 +863,15 @@ let check_deprecation com =
 			()
 	) com.types
 
+let check_unification com e t =
+	begin match follow e.etype,follow t with
+		| TEnum _,TDynamic _ ->
+			add_feature com "may_print_enum";
+		| _ ->
+			()
+	end;
+	e
+
 (* PASS 1 end *)
 
 (* Saves a class state so it can be restored later, e.g. after DCE or native path rewrite *)
@@ -1138,6 +1147,7 @@ let run com tctx main =
 
 	(* PASS 1: general expression filters *)
  	let filters = [
+ 		Codegen.UnificationCallback.run (check_unification com);
 		Codegen.Abstract.handle_abstract_casts tctx;
 		blockify_ast;
 		(match com.platform with

+ 10 - 5
genjs.ml

@@ -1067,13 +1067,18 @@ let generate_enum ctx e =
 		(match f.ef_type with
 		| TFun (args,_) ->
 			let sargs = String.concat "," (List.map (fun (n,_,_) -> ident n) args) in
-			print ctx "function(%s) { var $x = [\"%s\",%d,%s]; $x.__enum__ = %s; $x.toString = $estr; return $x; }" sargs f.ef_name f.ef_index sargs p;
+			print ctx "function(%s) { var $x = [\"%s\",%d,%s]; $x.__enum__ = %s;" sargs f.ef_name f.ef_index sargs p;
+			if has_feature ctx "may_print_enum" then
+				spr ctx " $x.toString = $estr;";
+			spr ctx " return $x; }";
 			ctx.separator <- true;
 		| _ ->
 			print ctx "[\"%s\",%d]" f.ef_name f.ef_index;
 			newline ctx;
-			print ctx "%s%s.toString = $estr" p (field f.ef_name);
-			newline ctx;
+			if has_feature ctx "may_print_enum" then begin
+				print ctx "%s%s.toString = $estr" p (field f.ef_name);
+				newline ctx;
+			end;
 			print ctx "%s%s.__enum__ = %s" p (field f.ef_name) p;
 		);
 		newline ctx
@@ -1243,9 +1248,9 @@ let generate com =
 	(* TODO: fix $estr *)
 	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 List.exists (function TEnumDecl { e_extern = false } -> true | _ -> false) com.types
+	let vars = if has_feature ctx "may_print_enum"
 		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
 	(match List.rev vars with
 	| [] -> ()
 	| vl ->

+ 1 - 1
std/js/Boot.hx

@@ -74,7 +74,7 @@ class Boot {
 			return untyped __define_feature__("js.Boot.getClass", o.__class__);
 	}
 
-	@:ifFeature("has_enum")
+	@:ifFeature("may_print_enum")
 	private static function __string_rec(o,s:String) {
 		untyped {
 			if( o == null )