Prechádzať zdrojové kódy

[php] revert anonymous objects optimization (#7916, fixes #10736)

Aleksandr Kuzmenko 3 rokov pred
rodič
commit
8f210b4dbc

+ 3 - 51
src/generators/genphp7.ml

@@ -2387,57 +2387,9 @@ class code_writer (ctx:php_generator_context) hx_type_path php_name =
 			match fields with
 				| [] -> self#write ("new " ^ (self#use hxanon_type_path) ^ "()")
 				| _ ->
-					let inits,args_exprs,args_names =
-						List.fold_left (fun (inits,args_exprs,args_names) ((name,p,quotes), e) ->
-							let field,arg_name =
-								if quotes = NoQuotes then name,name
-								else "{" ^ (quote_string name) ^ "}", "_hx_" ^ (string_of_int (List.length args_exprs))
-							in
-							(field,mk (TIdent ("$"^arg_name)) e.etype p) :: inits, e :: args_exprs, arg_name :: args_names
-						) ([],[],[]) fields
-					in
-					let anon_name, declare_class =
-						let key = List.map (fun ((name,_,_),_) -> name) fields in
-						try
-							Hashtbl.find ctx.pgc_anons key, false
-						with Not_found ->
-							let name = "_HxAnon_" ^ self#get_name ^ (string_of_int (Hashtbl.length ctx.pgc_anons)) in
-							Hashtbl.add ctx.pgc_anons key name;
-							name, true
-					in
-					self#write ("new " ^ anon_name ^ "(");
-					write_args self#write self#write_expr (List.rev args_exprs);
-					self#write ")";
-					if declare_class then begin
-						(* save writer's state *)
-						let original_buffer = buffer
-						and original_indentation = self#get_indentation in
-						let sm_pointer_before_body = get_sourcemap_pointer sourcemap in
-						(* generate a class for this anon *)
-						buffer <- ctx.pgc_bottom_buffer;
-						self#set_indentation 0;
-						self#write ("\nclass " ^ anon_name ^ " extends " ^ (self#use hxanon_type_path) ^ " {\n");
-						self#indent_more;
-						self#write_with_indentation "function __construct(";
-						write_args self#write (fun name -> self#write ("$" ^ name)) (List.rev args_names);
-						self#write ") {\n";
-						self#indent_more;
-						List.iter (fun (field,e) ->
-							self#write_with_indentation "$this->";
-							self#write field;
-							self#write " = ";
-							self#write_expr e;
-							self#write ";\n";
-						) (List.rev inits);
-						self#indent_less;
-						self#write_line "}";
-						self#indent_less;
-						self#write_with_indentation "}\n";
-						(* restore writer's state *)
-						buffer <- original_buffer;
-						self#set_indentation original_indentation;
-						set_sourcemap_pointer sourcemap sm_pointer_before_body
-					end
+					self#write ("new " ^ (self#use hxanon_type_path)  ^ "(");
+					self#write_assoc_array_decl fields;
+					self#write ")"
 		(**
 			Writes specified type to output buffer depending on type of expression.
 		*)

+ 9 - 4
std/php/Boot.hx

@@ -559,10 +559,8 @@ class Boot {
 	/**
 		Create Haxe-compatible anonymous structure of `data` associative array
 	**/
-	static public function createAnon(data:NativeArray):Dynamic {
-		var o = new HxAnon();
-		Syntax.foreach(data, (field:String, value:Any) -> Syntax.setField(o, field, value));
-		return o;
+	static public inline function createAnon(data:NativeArray):Dynamic {
+		return new HxAnon(data);
 	}
 
 	/**
@@ -948,6 +946,13 @@ private class HxDynamicStr extends HxClosure {
 @:keep
 @:dox(hide)
 private class HxAnon extends StdClass {
+	public function new(fields:NativeArray = null) {
+		super();
+		if (fields != null) {
+			Syntax.foreach(fields, function(name, value) Syntax.setField(this, name, value));
+		}
+	}
+	
 	@:phpMagic
 	function __get(name:String) {
 		return null;

+ 18 - 0
tests/misc/php/projects/Issue10736/compile.hxml

@@ -0,0 +1,18 @@
+-cp src
+
+--each
+
+-D php-prefix=app1
+-main App1
+-php bin/app1
+
+--next
+
+-D php-prefix=app2
+-main App2
+-php bin/app2
+
+--next
+
+--cmd php bin/app1/index.php
+--cmd php bin/app2/index.php

+ 5 - 0
tests/misc/php/projects/Issue10736/src/App1.hx

@@ -0,0 +1,5 @@
+class App1 {
+	static function main() {
+		sys.io.File.saveContent('bin/serialized.data', php.Global.serialize({a:1}));
+	}
+}

+ 5 - 0
tests/misc/php/projects/Issue10736/src/App2.hx

@@ -0,0 +1,5 @@
+class App2 {
+	static function main() {
+		php.Global.unserialize(sys.io.File.getContent('bin/serialized.data'));
+	}
+}