Przeglądaj źródła

Add `haxe.Rest` type for representing rest arguments for extern methods (closes #3238)

Dan Korostelev 11 lat temu
rodzic
commit
df91a72708
3 zmienionych plików z 63 dodań i 4 usunięć
  1. 32 0
      std/haxe/Rest.hx
  2. 17 4
      typeload.ml
  3. 14 0
      typer.ml

+ 32 - 0
std/haxe/Rest.hx

@@ -0,0 +1,32 @@
+/*
+ * Copyright (C)2005-2014 Haxe Foundation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+
+package haxe;
+
+/**
+    A special abstract type that represents "rest" function argument.
+
+    Should be used as a type for the last argument of an extern method,
+    representing that arbitrary number of arguments of given type can be
+    passed to that method.
+**/
+abstract Rest<T>(Array<T>) { }

+ 17 - 4
typeload.ml

@@ -1918,10 +1918,23 @@ let init_class ctx c p context_init herits fields =
 					if stat then params else params @ ctx.type_params);
 			let constr = (name = "new") in
 			let ret = if constr then ctx.t.tvoid else type_opt ctx p fd.f_type in
-			let args = List.map (fun (name,opt,t,c) ->
-				let t, c = type_function_param ctx (type_opt ctx p t) c opt p in
-				name, c, t
-			) fd.f_args in
+			let rec loop args = match args with
+				| (name,opt,t,ct) :: args ->
+					let t, ct = type_function_param ctx (type_opt ctx p t) ct opt p in
+					begin match t with
+						| TAbstract({a_path = ["haxe"],"Rest"},_) ->
+							if not c.cl_extern then error "Rest argument are only supported for extern methods" p;
+							if opt then error "Rest argument cannot be optional" p;
+							if ct <> None then error "Rest argument cannot have default value" p;
+							if args <> [] then error "Rest should only be used for the last function argument" p;
+						| _ ->
+							()
+					end;
+					(name, ct, t) :: (loop args)
+				| [] ->
+					[]
+			in
+			let args = loop fd.f_args in
 			let t = TFun (fun_args args,ret) in
 			if c.cl_interface && not stat && fd.f_expr <> None then error (f.cff_name ^ ": An interface method cannot have a body") p;
 			if constr then begin

+ 14 - 0
typer.ml

@@ -737,6 +737,20 @@ let rec unify_call_params ctx ?(overloads=None) cf el args r p inline =
 					List.map fst args, tf
 			else
 				List.map fst args, tf
+		| l , [(name,opt,TAbstract({a_path=(["haxe"],"Rest")},[t]))] ->
+			let rec process acc el =
+				match el with
+				| [] -> acc
+				| ee :: rest ->
+					let e = type_expr ctx ee (WithTypeResume t) in
+					begin try
+						unify_raise ctx e.etype t e.epos
+					with Error (Unify ul,p) ->
+						raise (Error (Stack (Unify ul,Custom ("For rest function argument '" ^ name ^ "'")), p))
+					end;
+					process ((Codegen.Abstract.check_cast ctx t e p,false) :: acc) rest
+			in
+			loop (process acc l) [] [] skip
 		| [] , (_,false,_) :: _ ->
 			error (List.fold_left (fun acc (_,_,t) -> default_value t None :: acc) acc l2) "Not enough"
 		| [] , (name,true,t) :: l ->