Bläddra i källkod

Lua : clean up type metadatas and other raw data attributes

Haxe will stash type-related information on runtime instances in some
cases.  In these situations, it's ok to use simple lua anonymous arrays,
since these native arrays are cheaper to create, and are never exposed
externally.
Justin Donaldson 10 år sedan
förälder
incheckning
ccfbb4ade8
1 ändrade filer med 16 tillägg och 11 borttagningar
  1. 16 11
      genlua.ml

+ 16 - 11
genlua.ml

@@ -326,7 +326,7 @@ let rec gen_call ctx e el in_value =
 			| [] -> ()
 			| [] -> ()
 			| e :: _ -> gen_value ctx e)
 			| e :: _ -> gen_value ctx e)
 	| TLocal { v_name = "__resources__" }, [] ->
 	| TLocal { v_name = "__resources__" }, [] ->
-		spr ctx "[";
+		spr ctx "{";
 		concat ctx "," (fun (name,data) ->
 		concat ctx "," (fun (name,data) ->
 			spr ctx "{ ";
 			spr ctx "{ ";
 			spr ctx "name : ";
 			spr ctx "name : ";
@@ -335,7 +335,7 @@ let rec gen_call ctx e el in_value =
 			gen_constant ctx e.epos (TString (Codegen.bytes_serialize data));
 			gen_constant ctx e.epos (TString (Codegen.bytes_serialize data));
 			spr ctx "}"
 			spr ctx "}"
 		) (Hashtbl.fold (fun name data acc -> (name,data) :: acc) ctx.com.resources []);
 		) (Hashtbl.fold (fun name data acc -> (name,data) :: acc) ctx.com.resources []);
-		spr ctx "]";
+		spr ctx "}";
 	| TLocal { v_name = "`trace" }, [e;infos] ->
 	| TLocal { v_name = "`trace" }, [e;infos] ->
 		if has_feature ctx "haxe.Log.trace" then begin
 		if has_feature ctx "haxe.Log.trace" then begin
 			let t = (try List.find (fun t -> t_path t = (["haxe"],"Log")) ctx.com.types with _ -> assert false) in
 			let t = (try List.find (fun t -> t_path t = (["haxe"],"Log")) ctx.com.types with _ -> assert false) in
@@ -420,7 +420,7 @@ and gen_expr ctx e =
 			print ctx ",$bind($_,$_%s))" (field f.cf_name))
 			print ctx ",$bind($_,$_%s))" (field f.cf_name))
 	| TEnumParameter (x,_,i) ->
 	| TEnumParameter (x,_,i) ->
 		gen_value ctx x;
 		gen_value ctx x;
-		print ctx "[%i]" (i + 2)
+		print ctx "{%i}" (i + 2)
 	| TField ({ eexpr = TConst (TInt _ | TFloat _) } as x,f) ->
 	| TField ({ eexpr = TConst (TInt _ | TFloat _) } as x,f) ->
 		gen_expr ctx { e with eexpr = TField(mk (TParenthesis x) x.etype x.epos,f) }
 		gen_expr ctx { e with eexpr = TField(mk (TParenthesis x) x.etype x.epos,f) }
 	| TField (x, (FInstance(_,_,f) | FStatic(_,f) | FAnon(f))) when Meta.has Meta.SelfCall f.cf_meta ->
 	| TField (x, (FInstance(_,_,f) | FStatic(_,f) | FAnon(f))) when Meta.has Meta.SelfCall f.cf_meta ->
@@ -1153,10 +1153,14 @@ let generate_enum ctx e =
 	let p = s_path ctx e.e_path in
 	let p = s_path ctx e.e_path in
 	let ename = List.map (fun s -> Printf.sprintf "\"%s\"" (Ast.s_escape s)) (fst e.e_path @ [snd e.e_path]) in
 	let ename = List.map (fun s -> Printf.sprintf "\"%s\"" (Ast.s_escape s)) (fst e.e_path @ [snd e.e_path]) in
 	print ctx "%s = " p;
 	print ctx "%s = " p;
-	if has_feature ctx "Type.resolveEnum" then print ctx "_hxClasses[\"%s\"] = " (dot_path e.e_path);
+	if has_feature ctx "Type.resolveEnum" then
+	    spr ctx "_hxClasses[\"%s\"]";
+	    newline ctx;
+	    print ctx "_hxClasses[\"%s\"] = " (dot_path e.e_path);
+
 	print ctx "{";
 	print ctx "{";
-	if has_feature ctx "lua.Boot.isEnum" then print ctx " __ename__ : %s," (if has_feature ctx "Type.getEnumName" then "[" ^ String.concat "," ename ^ "]" else "true");
-	print ctx " __constructs__ : [%s] }" (String.concat "," (List.map (fun s -> Printf.sprintf "\"%s\"" s) e.e_names));
+	if has_feature ctx "lua.Boot.isEnum" then print ctx " __ename__ = %s," (if has_feature ctx "Type.getEnumName" then "{" ^ String.concat "," ename ^ "}" else "true");
+	print ctx " __constructs__ = {%s} }" (String.concat "," (List.map (fun s -> Printf.sprintf "\"%s\"" s) e.e_names));
 	ctx.separator <- true;
 	ctx.separator <- true;
 	newline ctx;
 	newline ctx;
 	List.iter (fun n ->
 	List.iter (fun n ->
@@ -1165,14 +1169,15 @@ let generate_enum ctx e =
 		(match f.ef_type with
 		(match f.ef_type with
 		| TFun (args,_) ->
 		| TFun (args,_) ->
 			let sargs = String.concat "," (List.map (fun (n,_,_) -> ident n) args) in
 			let sargs = String.concat "," (List.map (fun (n,_,_) -> ident n) args) in
-			print ctx "function(%s) { local $x = [\"%s\",%d,%s]; $x.__enum__ = %s;" sargs f.ef_name f.ef_index sargs p;
+			(* TODO: better tmp variable for _x, _estr *)
+			print ctx "function(%s)  local _x = {\"%s\",%d,%s}; _x.__enum__ = %s;" sargs f.ef_name f.ef_index sargs p;
 			if has_feature ctx "may_print_enum" then
 			if has_feature ctx "may_print_enum" then
 				(* TODO: better namespacing for _estr *)
 				(* TODO: better namespacing for _estr *)
-				spr ctx " $x.toString = _estr;";
-			spr ctx " return $x; }";
+				spr ctx " _x.toString = _estr;";
+			spr ctx " return _x; end ";
 			ctx.separator <- true;
 			ctx.separator <- true;
 		| _ ->
 		| _ ->
-			print ctx "[\"%s\",%d]" f.ef_name f.ef_index;
+			print ctx "{\"%s\",%d}" f.ef_name f.ef_index;
 			newline ctx;
 			newline ctx;
 			if has_feature ctx "may_print_enum" then begin
 			if has_feature ctx "may_print_enum" then begin
 				print ctx "%s%s.toString = _estr" p (field f.ef_name);
 				print ctx "%s%s.toString = _estr" p (field f.ef_name);
@@ -1189,7 +1194,7 @@ let generate_enum ctx e =
 				| TFun _ -> false
 				| TFun _ -> false
 				| _ -> true
 				| _ -> true
 		) e.e_names in
 		) e.e_names in
-		print ctx "%s.__empty_constructs__ = [%s]" p (String.concat "," (List.map (fun s -> Printf.sprintf "%s.%s" p s) ctors_without_args));
+		print ctx "%s.__empty_constructs__ = {%s}" p (String.concat "," (List.map (fun s -> Printf.sprintf "%s.%s" p s) ctors_without_args));
 		newline ctx
 		newline ctx
 	end;
 	end;
 	match Codegen.build_metadata ctx.com (TEnumDecl e) with
 	match Codegen.build_metadata ctx.com (TEnumDecl e) with