Browse Source

added features to typer context (with typed_cast as demo)

Simon Krajewski 13 years ago
parent
commit
7cc03e11d8
3 changed files with 16 additions and 4 deletions
  1. 1 1
      std/js/Boot.hx
  2. 11 1
      typecore.ml
  3. 4 2
      typer.ml

+ 1 - 1
std/js/Boot.hx

@@ -179,7 +179,7 @@ class Boot {
 		}
 	}
 
-	@:keep private static function __cast(o : Dynamic, t : Dynamic) {
+	@:feature("typed_cast") private static function __cast(o : Dynamic, t : Dynamic) {
 		if (__instanceof(o, t)) return o;
 		else throw "Cannot cast " +Std.string(o) + " to " +Std.string(t);
 	}

+ 11 - 1
typecore.ml

@@ -36,9 +36,13 @@ type macro_mode =
 	| MBuild
 	| MMacroType
 
+type feature =
+	| FtTypedCast
+
 type typer_globals = {
 	types_module : (path, path) Hashtbl.t;
 	modules : (path , module_def) Hashtbl.t;
+	features : (string, feature) Hashtbl.t;
 	mutable delayed : (unit -> unit) list;
 	doinline : bool;
 	mutable core_api : typer option;
@@ -229,4 +233,10 @@ let create_fake_module ctx file =
 		mdep
 	) in
 	Hashtbl.replace ctx.g.modules mdep.m_path mdep;
-	mdep
+	mdep
+
+let feature_name = function
+	| FtTypedCast -> "typed_cast"
+
+let activate_feature ctx ft = Hashtbl.replace ctx.g.features (feature_name ft) ft
+let has_feature ctx s = Hashtbl.mem ctx.g.features s

+ 4 - 2
typer.ml

@@ -1854,6 +1854,7 @@ and type_expr ctx ?(need_val=true) (e,p) =
 		mk (TCast (e,None)) (mk_mono()) p
 	| ECast (e, Some t) ->
 		(* force compilation of class "Std" since we might need it *)
+		activate_feature ctx FtTypedCast;
 		(match ctx.com.platform with
 		| Js | Flash8 | Neko | Flash | Java | Cs ->
 			let std = Typeload.load_type_def ctx p { tpackage = []; tparams = []; tname = "Std"; tsub = None } in
@@ -2115,8 +2116,8 @@ let dce_check_metadata ctx meta =
 		| ":?used",_
 		| ":keep",_ ->
 			true
-(* 		| ":feature",el ->
-			List.exists (fun e -> match e with (EConst(String s),_) when has_feature ctx s -> true | _ -> false) el *)
+ 		| ":feature",el ->
+			List.exists (fun e -> match e with (EConst(String s),_) when has_feature ctx s -> true | _ -> false) el
 		| _ -> false
 	) meta
 
@@ -2824,6 +2825,7 @@ let rec create com =
 			modules = Hashtbl.create 0;
 			types_module = Hashtbl.create 0;
 			type_patches = Hashtbl.create 0;
+			features = Hashtbl.create 0;
 			delayed = [];
 			doinline = not (Common.defined com "no_inline" || com.display);
 			hook_generate = [];