Skip to content

Commit

Permalink
Support @:import meta-data for extern python modules.
Browse files Browse the repository at this point in the history
  • Loading branch information
nadako committed Apr 30, 2014
1 parent 23e0a6b commit 6d0919f
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
1 change: 1 addition & 0 deletions ast.ml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ module Meta = struct
| HxGen
| IfFeature
| Impl
| Import
| Include
| InitPackage
| Internal
Expand Down
1 change: 1 addition & 0 deletions common.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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]])
Expand Down
28 changes: 27 additions & 1 deletion genpy.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 6d0919f

Please sign in to comment.