Jelajahi Sumber

[lua] introduce @:luaDotMethod for using dot-style invocation on appropriate extern instances

Justin Donaldson 8 tahun lalu
induk
melakukan
1ed7a6315a
2 mengubah file dengan 16 tambahan dan 2 penghapusan
  1. 3 1
      src/context/meta.ml
  2. 13 1
      src/generators/genlua.ml

+ 3 - 1
src/context/meta.ml

@@ -89,6 +89,7 @@ type strict_meta =
 	| LibType
 	| LoopLabel
 	| LuaRequire
+	| LuaDotMethod 
 	| Meta
 	| Macro
 	| MaybeUsed
@@ -281,6 +282,7 @@ let get_info = function
 	| JavaNative -> ":javaNative",("Automatically added by -java-lib on classes generated from JAR/class files",[Platform Java; UsedOnEither[TClass;TEnum]; UsedInternally])
 	| JsRequire -> ":jsRequire",("Generate javascript module require expression for given extern",[Platform Js; UsedOn TClass])
 	| LuaRequire -> ":luaRequire",("Generate lua module require expression for given extern",[Platform Lua; UsedOn TClass])
+	| LuaDotMethod -> ":luaDotMethod",("Indicates that the given extern type instance should have dot-style invocation for methods instead of colon.",[Platform Lua; UsedOnEither[TClass;TClassField]])
 	| Keep -> ":keep",("Causes a field or type to be kept by DCE",[])
 	| KeepInit -> ":keepInit",("Causes a class to be kept by DCE even if all its field are removed",[UsedOn TClass])
 	| KeepSub -> ":keepSub",("Extends @:keep metadata to all implementing and extending classes",[UsedOn TClass])
@@ -429,4 +431,4 @@ let get_documentation_list () =
 			[]
 	in
 	let all = List.sort (fun (s1,_) (s2,_) -> String.compare s1 s2) (loop 0) in
-	all,!m
+	all,!m

+ 13 - 1
src/generators/genlua.ml

@@ -200,6 +200,13 @@ let open_block ctx =
 
 let this ctx = match ctx.in_value with None -> "self" | Some _ -> "self"
 
+let is_dot_access e cf =
+    match follow(e.etype), cf with
+        | TInst (c,_), FInstance(_,_,icf)  when (Meta.has Meta.LuaDotMethod c.cl_meta || Meta.has Meta.LuaDotMethod icf.cf_meta)->
+                true;
+        | _ ->
+                false
+
 let is_dynamic_iterator ctx e =
 	let check x =
 		has_feature ctx "HxOverrides.iter" && (match follow x.etype with
@@ -436,7 +443,10 @@ let rec gen_call ctx e el in_value =
 		    spr ctx ")";
 		end else begin
 		    gen_value ctx e;
-		    print ctx ":%s" (field_name ef);
+                    if is_dot_access e ef then
+                        print ctx ".%s" (field_name ef)
+                    else
+                        print ctx ":%s" (field_name ef);
                     gen_paren ctx el;
 		end;
 	| _ ->
@@ -1690,6 +1700,8 @@ let generate_type ctx = function
 		(* A special case for Std because we do not want to generate it if it's empty. *)
 		if p = "Std" && c.cl_ordered_statics = [] then
 			()
+		else if (not c.cl_extern) && Meta.has Meta.LuaDotMethod c.cl_meta then
+                    abort "LuaDotMethod is valid for externs only" c.cl_pos
 		else if not c.cl_extern then
 			generate_class ctx c
 		else if Meta.has Meta.InitPackage c.cl_meta then