Browse Source

apply @:native if a @:generic class name becomes too long (closes #3090)

Simon Krajewski 11 years ago
parent
commit
1908742ba0
3 changed files with 29 additions and 1 deletions
  1. 13 0
      codegen.ml
  2. 4 1
      gencommon.ml
  3. 12 0
      tests/unit/issues/Issue3090.hx

+ 13 - 0
codegen.ml

@@ -278,6 +278,13 @@ let has_ctor_constraint c = match c.cl_kind with
 		) tl;
 	| _ -> false
 
+let get_short_name =
+	let i = ref (-1) in
+	(fun () ->
+		incr i;
+		Printf.sprintf "__hx_type_%i" !i
+	)
+
 let rec build_generic ctx c p tl =
 	let pack = fst c.cl_path in
 	let recurse = ref false in
@@ -420,6 +427,12 @@ let rec build_generic ctx c p tl =
 			f
 		) c.cl_ordered_fields;
 		List.iter (fun f -> f()) !delays;
+		(* In rare cases the class name can become too long, so let's shorten it (issue #3090). *)
+		if String.length (snd cg.cl_path) > 254 then begin
+			let n = get_short_name () in
+			let tp = fst cg.cl_path,n in
+			cg.cl_meta <- (Meta.Native,[EConst(String (s_type_path tp)),p],p) :: cg.cl_meta;
+		end;
 		TInst (cg,[])
 	end
 

+ 4 - 1
gencommon.ml

@@ -1102,7 +1102,10 @@ let generate_modules gen extension source_dir (module_gen : SourceWriter.source_
 		(*let should_write = List.fold_left (fun should md -> module_gen w md or should) false md_def.m_types in*)
 		let should_write = module_gen w md_def in
 		if should_write then begin
-			let path = md_def.m_path in
+			let path = match md_def.m_types with
+				| [TClassDecl c] -> c.cl_path
+				| _ -> md_def.m_path
+			in
 			write_file gen w source_dir path extension;
 		end
 	) gen.gcon.modules

+ 12 - 0
tests/unit/issues/Issue3090.hx

@@ -0,0 +1,12 @@
+package unit.issues;
+import haxe.xml.Printer;
+@:generic
+private class A<T1,T2,T3,T4,T5,T6,T7,T8,T9,T10,T11,T12,T13,T14,T15>{
+    public function new(){}
+}
+
+class Issue3090 extends Test {
+	function test() {
+		var a = new A<Printer,Printer,Printer,Printer,Printer,Printer,Printer,Printer,Printer,Printer,Printer,Printer,Printer,Printer,Printer>();
+	}
+}