浏览代码

Support @:import meta-data for extern python modules.

Dan Korostelev 11 年之前
父节点
当前提交
6d0919f03c
共有 3 个文件被更改,包括 29 次插入1 次删除
  1. 1 0
      ast.ml
  2. 1 0
      common.ml
  3. 27 1
      genpy.ml

+ 1 - 0
ast.ml

@@ -83,6 +83,7 @@ module Meta = struct
 		| HxGen
 		| IfFeature
 		| Impl
+		| Import
 		| Include
 		| InitPackage
 		| Internal

+ 1 - 0
common.ml

@@ -385,6 +385,7 @@ module MetaInfo = struct
 		| HxGen -> ":hxGen",("Annotates that an extern class was generated by Haxe",[Platforms [Java;Cs]; UsedOnEither [TClass;TEnum]])
 		| IfFeature -> ":ifFeature",("Causes a field to be kept by DCE if the given feature is part of the compilation",[HasParam "Feature name";UsedOn TClassField])
 		| Impl -> ":impl",("Used internally to mark abstract implementation fields",[UsedOn TAbstractField; Internal])
+		| Import -> ":import",("Generates python import statement for extern classes",[Platforms [Python]; UsedOn TClass])
 		| Include -> ":include",("",[Platform Cpp])
 		| InitPackage -> ":initPackage",("?",[])
 		| Meta.Internal -> ":internal",("Generates the annotated field/class with 'internal' access",[Platforms [Java;Cs]; UsedOnEither[TClass;TEnum;TClassField]])

+ 27 - 1
genpy.ml

@@ -1710,7 +1710,33 @@ module Generator = struct
 	let gen_class ctx c =
 		gen_pre_code_meta ctx c.cl_meta;
 		(* print ctx "# print %s.%s\n" (s_type_path c.cl_module.m_path) (snd c.cl_path); *)
-		if not c.cl_extern then begin
+		if c.cl_extern then begin
+			if Meta.has Meta.Import c.cl_meta then begin
+				let _, args, mp = Meta.get Meta.Import c.cl_meta in
+
+				let class_name = match c.cl_path with
+					| [],name -> name
+					| path,name -> (ExtString.String.join "_" path) ^ "_" ^ name
+				in
+
+				let import = match args with 
+					| [(EConst(String(module_name)), _)] ->
+						(* importing whole module *)
+						"import " ^ module_name ^ " as " ^ class_name
+					| [(EConst(String(module_name)), _); (EConst(String(object_name)), _)] ->
+						(* importing a class from a module *)
+						"from " ^ module_name ^ " import " ^ object_name ^ " as " ^ class_name;
+					| _ ->
+						error "Unsupported @:import format" mp
+				in
+
+				let f = fun () ->
+					spr_line ctx import;
+					spr_line ctx ("_hx_c." ^ class_name ^ " = " ^ class_name)
+				in
+				ctx.class_inits <- f :: ctx.class_inits
+			end
+		end else begin
 			let mt = (t_infos (TClassDecl c)) in
 			let p = get_path mt in
 			let p_name = get_full_name mt in