Browse Source

added catches implementation

Nicolas Cannasse 19 years ago
parent
commit
d57498ffa7
1 changed files with 33 additions and 1 deletions
  1. 33 1
      genjs.ml

+ 33 - 1
genjs.ml

@@ -239,8 +239,40 @@ and gen_expr ctx e =
 		gen_expr ctx e;
 		newline ctx;
 		spr ctx "catch( $e ) {";
+		let bend = open_block ctx in
 		newline ctx;
-		(* TODO : CATCHES *)
+		let last = ref false in
+		List.iter (fun (v,t,e) ->
+			if !last then () else
+			let t = (match follow t with
+			| TEnum (e,_) -> Some (TEnumDecl e)
+			| TInst (c,_) -> Some (TClassDecl c)
+			| TFun _
+			| TLazy _
+			| TAnon _ ->
+				assert false
+			| TMono _
+			| TDynamic _ ->
+				None
+			) in
+			match t with
+			| None -> 
+				last := true; 
+				gen_expr ctx e;
+				bend();
+				newline ctx;
+			| Some t ->
+				spr ctx "if( js.Boot.__instanceof($e,";
+				gen_value ctx (mk (TType t) (mk_mono()) e.epos);
+				spr ctx ") ) ";
+				gen_expr ctx e;
+				newline ctx;
+		) catchs;
+		if not !last then begin
+			spr ctx "throw($e)";
+			bend();
+			newline ctx;
+		end;		
 		spr ctx "}";
 	| TMatch (e,_,cases,def) ->
 		spr ctx "var $e = ";