Browse Source

added __init__ handling.

Nicolas Cannasse 19 years ago
parent
commit
47fd050609
2 changed files with 16 additions and 2 deletions
  1. 15 1
      genjs.ml
  2. 1 1
      typer.ml

+ 15 - 1
genjs.ml

@@ -23,6 +23,7 @@ type ctx = {
 	packages : (string list,unit) Hashtbl.t;
 	mutable current : tclass;
 	mutable statics : (tclass * string * texpr) list;
+	mutable inits : texpr list;
 	mutable tabs : string;
 	mutable in_value : bool;
 }
@@ -569,7 +570,15 @@ let generate_static ctx (c,f,e) =
 	newline ctx
 
 let generate_type ctx = function
-	| TClassDecl c -> if not c.cl_extern then generate_class ctx c
+	| TClassDecl c -> 
+		(try
+			let f = PMap.find "__init__" c.cl_statics in
+			match f.cf_expr with
+			| Some { eexpr = TFunction f } -> ctx.inits <- f.tf_expr :: ctx.inits
+			| _ -> ()
+		with
+			Not_found -> ());
+		if not c.cl_extern then generate_class ctx c
 	| TEnumDecl e -> generate_enum ctx e
 
 let generate file types hres =
@@ -577,6 +586,7 @@ let generate file types hres =
 		buf = Buffer.create 16000;
 		packages = Hashtbl.create 0;
 		statics = [];
+		inits = [];
 		current = null_class;
 		tabs = "";
 		in_value = false;
@@ -592,6 +602,10 @@ let generate file types hres =
 	) hres;
 	print ctx "js.Boot.__init()";
 	newline ctx;
+	List.iter (fun e -> 
+		gen_expr ctx e;
+		newline ctx;
+	) (List.rev ctx.inits);
 	List.iter (generate_static ctx) (List.rev ctx.statics);
 	let ch = open_out file in
 	output_string ch (Buffer.contents ctx.buf);

+ 1 - 1
typer.ml

@@ -1334,7 +1334,7 @@ let init_class ctx c p types herits fields =
 				t
 			) in
 			let delay = (
-				if c.cl_extern || c.cl_interface then
+				if (c.cl_extern || c.cl_interface) && cf.cf_name <> "__init__" then
 					(fun() -> ())
 				else begin
 					cf.cf_type <- TLazy r;