Browse Source

[php7] handle magic __init__ methods on externs

Alexander Kuzmenko 8 years ago
parent
commit
d7d2dfca5d
2 changed files with 38 additions and 3 deletions
  1. 1 1
      extra/haxelib_src
  2. 37 2
      src/generators/genphp7.ml

+ 1 - 1
extra/haxelib_src

@@ -1 +1 @@
-Subproject commit 84e4deb0f677e1255a6ed36ecfa75000e9255be8
+Subproject commit 9d79a55ddb054d79ed8d136f1c0583baa407fd67

+ 37 - 2
src/generators/genphp7.ml

@@ -833,6 +833,10 @@ class virtual type_wrapper (type_path:path) (meta:metadata) (needs_generation:bo
 			Full type path
 		*)
 		method get_type_path = type_path
+		(**
+			If current type requires some additional type to be generated
+		*)
+		method get_service_type : module_type option = None
 	end
 
 (**
@@ -876,6 +880,33 @@ class class_wrapper (cls) =
 			Returns `Type.module_type` instance for this type
 		*)
 		method get_module_type = TClassDecl cls
+		(**
+			If current type requires some additional type to be generated
+		*)
+		method get_service_type : module_type option =
+			if not cls.cl_extern then
+				None
+			else
+				match cls.cl_init with
+					| None -> None
+					| Some body ->
+						let path =
+							match cls.cl_path with
+								| (pack, name) -> (pack @ ["_" ^ name], ("_extern_" ^ name))
+						in
+						let additional_cls = {
+							cls with
+								cl_extern =  false;
+								cl_path = path;
+								cl_fields  = PMap.create (fun a b -> 0);
+								cl_statics  = PMap.create (fun a b -> 0);
+								cl_ordered_fields  = [];
+								cl_ordered_statics  = [];
+								cl_constructor = None;
+								cl_overrides = [];
+								cl_init = Some body
+						} in
+						Some (TClassDecl additional_cls)
 	end
 
 (**
@@ -3454,14 +3485,18 @@ class generator (com:context) =
 let generate (com:context) =
 	let gen = new generator com in
 	gen#initialize;
-	let generate com_type =
+	let rec generate com_type =
 		let wrapper = get_wrapper com_type in
 		if wrapper#needs_generation then
-			match com_type with
+			(match com_type with
 				| TClassDecl cls -> gen#generate (new class_builder com cls);
 				| TEnumDecl enm -> gen#generate (new enum_builder com enm);
 				| TTypeDecl typedef -> ();
 				| TAbstractDecl abstr -> ()
+			);
+		match wrapper#get_service_type with
+			| None -> ()
+			| Some service_type -> generate service_type
 	in
 	List.iter generate com.types;
 	gen#finalize;