Explorar o código

allow custom @:require error message as last parameter

Nicolas Cannasse %!s(int64=13) %!d(string=hai) anos
pai
achega
3fd9980179
Modificáronse 5 ficheiros con 13 adicións e 10 borrados
  1. 1 1
      interp.ml
  2. 1 1
      std/haxe/macro/Type.hx
  3. 2 2
      type.ml
  4. 4 3
      typeload.ml
  5. 5 3
      typer.ml

+ 1 - 1
interp.ml

@@ -3960,7 +3960,7 @@ and encode_var_access a =
 		| AccResolve -> 3, []
 		| AccResolve -> 3, []
 		| AccCall s -> 4, [enc_string s]
 		| AccCall s -> 4, [enc_string s]
 		| AccInline	-> 5, []
 		| AccInline	-> 5, []
-		| AccRequire s -> 6, [enc_string s]
+		| AccRequire (s,msg) -> 6, [enc_string s; null enc_string msg]
 	) in
 	) in
 	enc_enum IVarAccess tag pl
 	enc_enum IVarAccess tag pl
 
 

+ 1 - 1
std/haxe/macro/Type.hx

@@ -134,7 +134,7 @@ enum VarAccess {
 	AccResolve;
 	AccResolve;
 	AccCall( m : String );
 	AccCall( m : String );
 	AccInline;
 	AccInline;
-	AccRequire( r : String );
+	AccRequire( r : String, ?msg : String );
 }
 }
 
 
 enum MethodKind {
 enum MethodKind {

+ 2 - 2
type.ml

@@ -36,7 +36,7 @@ and var_access =
 	| AccResolve		(* call resolve("field") when accessed *)
 	| AccResolve		(* call resolve("field") when accessed *)
 	| AccCall of string (* perform a method call when accessed *)
 	| AccCall of string (* perform a method call when accessed *)
 	| AccInline			(* similar to Normal but inline when accessed *)
 	| AccInline			(* similar to Normal but inline when accessed *)
-	| AccRequire of string (* set when @:require(cond) fails *)
+	| AccRequire of string * string option (* set when @:require(cond) fails *)
 
 
 and method_kind =
 and method_kind =
 	| MethNormal
 	| MethNormal
@@ -442,7 +442,7 @@ let s_access = function
 	| AccResolve -> "resolve"
 	| AccResolve -> "resolve"
 	| AccCall m -> m
 	| AccCall m -> m
 	| AccInline	-> "inline"
 	| AccInline	-> "inline"
-	| AccRequire n -> "require " ^ n
+	| AccRequire (n,_) -> "require " ^ n
 
 
 let s_kind = function
 let s_kind = function
 	| Var { v_read = AccNormal; v_write = AccNormal } -> "var"
 	| Var { v_read = AccNormal; v_write = AccNormal } -> "var"

+ 4 - 3
typeload.ml

@@ -1329,11 +1329,12 @@ let init_class ctx c p context_init herits fields =
 		| (":require",conds,_) :: l ->
 		| (":require",conds,_) :: l ->
 			let rec loop = function
 			let rec loop = function
 				| [] -> check_require l
 				| [] -> check_require l
+				| [EConst (String _),_] -> check_require l
 				| (EConst (Ident i),_) :: l ->
 				| (EConst (Ident i),_) :: l ->
 					if not (Common.defined ctx.com i) then
 					if not (Common.defined ctx.com i) then
-						Some i
+						Some (i,(match List.rev l with (EConst (String msg),_) :: _ -> Some msg | _ -> None))
 					else
 					else
-						loop l
+						loop l				
 				| _ -> error "Invalid require identifier" p
 				| _ -> error "Invalid require identifier" p
 			in
 			in
 			loop conds
 			loop conds
@@ -1352,7 +1353,7 @@ let init_class ctx c p context_init herits fields =
 			let req = (match req with None -> if is_static || constr then cl_req else None | _ -> req) in
 			let req = (match req with None -> if is_static || constr then cl_req else None | _ -> req) in
 			(match req with
 			(match req with
 			| None -> ()
 			| None -> ()
-			| Some r -> f.cf_kind <- Var { v_read = AccRequire r; v_write = AccRequire r });
+			| Some r -> f.cf_kind <- Var { v_read = AccRequire (fst r, snd r); v_write = AccRequire (fst r, snd r) });
 			if constr then begin
 			if constr then begin
 				if c.cl_constructor <> None then error "Duplicate constructor" p;
 				if c.cl_constructor <> None then error "Duplicate constructor" p;
 				c.cl_constructor <- Some f;
 				c.cl_constructor <- Some f;

+ 5 - 3
typer.ml

@@ -644,8 +644,10 @@ let field_access ctx mode f t e p =
 			if ctx.untyped then normal() else AKNo f.cf_name
 			if ctx.untyped then normal() else AKNo f.cf_name
 		| AccInline ->
 		| AccInline ->
 			AKInline (e,f,t)
 			AKInline (e,f,t)
-		| AccRequire r ->
-			error_require r p
+		| AccRequire (r,msg) ->
+			match msg with
+			| None -> error_require r p
+			| Some msg -> error msg p
 
 
 let using_field ctx mode e i p =
 let using_field ctx mode e i p =
 	if mode = MSet then raise Not_found;
 	if mode = MSet then raise Not_found;
@@ -2102,7 +2104,7 @@ and type_expr ctx ?(need_val=true) (e,p) =
 			let ct, f = get_constructor ctx c params p in
 			let ct, f = get_constructor ctx c params p in
 			if not (can_access ctx c f true || is_parent c ctx.curclass) && not ctx.untyped then display_error ctx "Cannot access private constructor" p;
 			if not (can_access ctx c f true || is_parent c ctx.curclass) && not ctx.untyped then display_error ctx "Cannot access private constructor" p;
 			(match f.cf_kind with
 			(match f.cf_kind with
-			| Var { v_read = AccRequire r } -> error_require r p
+			| Var { v_read = AccRequire (r,msg) } -> (match msg with Some msg -> error msg p | None -> error_require r p)
 			| _ -> ());
 			| _ -> ());
 			let el = (match follow ct with
 			let el = (match follow ct with
 			| TFun (args,r) ->
 			| TFun (args,r) ->