Explorar o código

added metadata __name__ and Class.toString repr.

Nicolas Cannasse %!s(int64=19) %!d(string=hai) anos
pai
achega
3864493b4e
Modificáronse 3 ficheiros con 49 adicións e 5 borrados
  1. 6 0
      genjs.ml
  2. 20 3
      genneko.ml
  3. 23 2
      genswf8.ml

+ 6 - 0
genjs.ml

@@ -505,6 +505,10 @@ let generate_class ctx c =
 		print ctx "%s.__construct__ = null" p;
 	);
 	newline ctx;
+	print ctx "%s.__name__ = [%s]" p (String.concat "," (List.map (fun s -> Printf.sprintf "\"%s\"" (Ast.s_escape s)) (fst c.cl_path @ [snd c.cl_path])));	
+	newline ctx;
+	print ctx "%s.toString = $class_str" p;
+	newline ctx;
 	(match c.cl_super with
 	| None -> ()
 	| Some (csup,_) ->
@@ -558,6 +562,8 @@ let generate file types hres =
 		tabs = "";
 		in_value = false;
 	} in
+	print ctx "$class_str = function() { return this.__name__.join(\".\"); }";
+	newline ctx;
 	List.iter (generate_type ctx) types;
 	print ctx "js.Boot.__res = {}";
 	newline ctx;

+ 20 - 3
genneko.ml

@@ -413,8 +413,9 @@ let gen_class ctx c =
 	let estat = (EBinop ("=",
 		stpath,
 		(EObject (
-			("__prototype__",clpath) ::
-			("__super__", match c.cl_super with None -> null p | Some _ -> field p esuper "__class__") ::
+			("prototype",clpath) ::
+			("__string", ident p "@class_to_string") ::
+			("__super__", match c.cl_super with None -> null p | Some (c,_) -> gen_type_path p c.cl_path) ::
 			("__interfaces__", interf) ::
 			PMap.fold (gen_method ctx p) c.cl_statics fnew
 		),p)
@@ -512,6 +513,18 @@ let gen_boot hres =
 		EBinop ("=",field null_pos (gen_type_path null_pos (["neko"],"Boot")) "__res",objres),null_pos;
 	],null_pos)
 
+let gen_name acc t =
+	match t with
+	| TEnumDecl _ -> acc
+	| TClassDecl c -> 
+		if c.cl_extern then
+			acc
+		else
+			let p = pos c.cl_pos in
+			let name = fst c.cl_path @ [snd c.cl_path] in
+			let arr = call p (field p (ident p "Array") "new1") [array p (List.map (fun n -> gen_constant p (TString n)) name); int p (List.length name)] in
+			(EBinop ("=",field p (gen_type_path p c.cl_path) "__name__",arr),p) :: acc
+
 let generate file types hres =
 	let ctx = {
 		curblock = [];
@@ -521,11 +534,15 @@ let generate file types hres =
 	let enum_str = (EBinop ("=",ident null_pos "@enum_to_string",(EFunction ([],
 		call null_pos (field null_pos (gen_type_path null_pos (["neko"],"Boot")) "__enum_str") [this null_pos]
 	),null_pos)),null_pos) in
+	let class_str = (EBinop ("=",ident null_pos "@class_to_string",(EFunction ([],
+		field null_pos (call null_pos (field null_pos (field null_pos (this null_pos) "__name__") "join") [gen_constant null_pos (TString ".")]) "__s"
+	),null_pos)),null_pos) in
 	let packs = List.concat (List.map (gen_packages h) types) in
+	let names = List.fold_left gen_name [] types in
 	let methods = List.map (gen_type ctx) types in
 	let boot = gen_boot hres in
 	let vars = List.concat (List.map (gen_static_vars ctx) types) in
-	let e = (EBlock (enum_str :: packs @ methods @ boot :: vars), null_pos) in
+	let e = (EBlock (enum_str :: class_str :: packs @ methods @ boot :: names @ vars), null_pos) in
 	let neko_file = Filename.chop_extension file ^ ".neko" in
 	let ch = IO.output_channel (open_out neko_file) in
 	(if !Plugin.verbose then Nxml.write_fmt else Nxml.write) ch (Nxml.to_xml e);

+ 23 - 2
genswf8.ml

@@ -1100,6 +1100,17 @@ let gen_type_def ctx t =
 			push ctx [VReg 0; VStr "__construct__"; VReg 0];
 			setvar ctx VarObj
 		end;
+		push ctx [VReg 0; VStr "__name__"];
+		let name = fst c.cl_path @ [snd c.cl_path] in
+		let nitems = List.length name in
+		push ctx (List.map (fun s -> VStr s) (List.rev name));
+		push ctx [VInt nitems];
+		write ctx AInitArray;
+		ctx.stack_size <- ctx.stack_size - nitems;
+		setvar ctx VarObj;
+		push ctx [VReg 0; VStr "toString"; VStr "@class_str"];
+		write ctx AEval;
+		setvar ctx VarObj;
 		(match c.cl_super with
 		| None ->
 			push ctx [VReg 0; VStr "__super__"; VNull];
@@ -1283,6 +1294,16 @@ let generate file ver header infile types hres =
 		statics = [];
 	} in
 	write ctx (AStringPool []);
+	push ctx [VStr "@class_str"];
+	let f = func ctx false false [] in
+	push ctx [VStr "."; VInt 1; VThis; VStr "__name__"];
+	getvar ctx VarObj;	
+	push ctx [VStr "join"];
+	call ctx VarObj 1;
+	write ctx AReturn;
+	ctx.reg_max <- ctx.reg_max + 1;
+	f();
+	write ctx ASet;
 	List.iter (fun t -> gen_type_def ctx t) types;
 	gen_type_map ctx;
 	gen_boot ctx hres;
@@ -1291,9 +1312,9 @@ let generate file ver header infile types hres =
 	let end_try = global_try() in
 	(* flash.Boot.__trace(exc) *)
 	let id = gen_type ctx (["flash"],"Boot") false in
-	push ctx [VStr "fileName"; VStr "<static init>"; VStr "lineNumber"; VInt 0; VInt 2];
+	push ctx [VStr "fileName"; VStr "(uncaught exception)"; VInt 1];
 	write ctx AObject;
-	ctx.stack_size <- ctx.stack_size - 4;
+	ctx.stack_size <- ctx.stack_size - 2;
 	push ctx [VReg 0; VInt 2; VStr id];
 	write ctx AEval;
 	push ctx [VStr "__trace"];