Quellcode durchsuchen

[php7] php.NativeStructArray for associatie arrays used as a set of options

Alexander Kuzmenko vor 8 Jahren
Ursprung
Commit
395b8707cb
2 geänderte Dateien mit 92 neuen und 1 gelöschten Zeilen
  1. 52 1
      src/generators/genphp7.ml
  2. 40 0
      std/php7/NativeStructArray.hx

+ 52 - 1
src/generators/genphp7.ml

@@ -791,6 +791,26 @@ let is_std_is expr =
 		| TField (_, FStatic ({ cl_path = path }, { cf_name = "is" })) -> path = boot_type_path || path = std_type_path
 		| _ -> false
 
+(**
+	Check if provided expression is actually a casting to NativeStructArray
+*)
+let is_native_struct_array_cast expr =
+	match expr.eexpr with
+		| TCall ({ eexpr = TField (_, field) }, _) ->
+			(match field with
+				| FStatic ({ cl_path = (["php"; "_NativeStructArray"], "NativeStructArray_Impl_") }, { cf_name = "__fromObject" }) -> true
+				| _ -> false
+			)
+		| _ -> false
+
+(**
+	Check if `expr` is an anonymous object declaration
+*)
+let is_object_declaration expr =
+	match (reveal_expr expr).eexpr with
+		| TObjectDecl _ -> true
+		| _ -> false
+
 (**
 	Check if `subject_arg` and `type_arg` can be generated as `$subject instanceof Type` expression.
 *)
@@ -1527,6 +1547,11 @@ class virtual type_builder ctx wrapper =
 		*)
 		method private write str =
 			Buffer.add_string buffer str
+		(**
+			Writes constant double-quoted string to output buffer
+		*)
+		method private write_const_string str =
+			self#write ("\"" ^ (escape_bin str) ^ "\"")
 		(**
 			Writes fixed amount of empty lines (E.g. between methods)
 		*)
@@ -1684,6 +1709,7 @@ class virtual type_builder ctx wrapper =
 				| TObjectDecl fields -> self#write_expr_object_declaration fields
 				| TArrayDecl exprs -> self#write_expr_array_decl exprs
 				| TCall (target, [arg1; arg2]) when is_std_is target && instanceof_compatible arg1 arg2 -> self#write_expr_lang_instanceof [arg1; arg2]
+				| TCall (_, [arg]) when is_native_struct_array_cast expr && is_object_declaration arg -> self#write_assoc_array_decl arg
 				| TCall ({ eexpr = TLocal { v_name = name }}, args) when is_magic expr -> self#write_expr_magic name args
 				| TCall ({ eexpr = TField (expr, access) }, args) when is_string expr -> self#write_expr_call_string expr access args
 				| TCall (expr, args) when is_lang_extern expr -> self#write_expr_call_lang_extern expr args
@@ -1761,7 +1787,7 @@ class virtual type_builder ctx wrapper =
 		method private write_expr_const const =
 			match const with
 				| TFloat str -> self#write str
-				| TString str -> self#write ("\"" ^ (escape_bin str) ^ "\"")
+				| TString str -> self#write_const_string str
 				| TBool value -> self#write (if value then "true" else "false")
 				| TNull -> self#write "null"
 				| TThis -> self#write "$this"
@@ -1789,6 +1815,31 @@ class virtual type_builder ctx wrapper =
 					self#indent_less;
 					self#write_indentation;
 					self#write "])"
+		(**
+			Write associative array declaration (used for NativeStructArray)
+		*)
+		method private write_assoc_array_decl object_decl =
+			match (reveal_expr object_decl).eexpr with
+				| TObjectDecl fields ->
+					if List.length fields = 0 then
+						self#write "[]"
+					else begin
+						self#write "[\n";
+						self#indent_more;
+						List.iter
+							(fun (name, field) ->
+								self#write_indentation;
+								self#write_const_string name;
+								self#write " => ";
+								self#write_expr field;
+								self#write ",\n"
+							)
+							fields;
+						self#indent_less;
+						self#write_indentation;
+						self#write "]";
+					end
+				| _ -> fail object_decl.epos (try assert false with Assert_failure mlpos -> mlpos)
 		(**
 			Writes TArray to output buffer
 		*)

+ 40 - 0
std/php7/NativeStructArray.hx

@@ -0,0 +1,40 @@
+/*
+ * Copyright (C)2005-2017 Haxe Foundation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+package php;
+
+/**
+	This type can be used in places where externs use associative arrays as a list of configuration options.
+	This type is a special case for Haxe->Php generator: object declaration is generated as php array declaration whenever
+	that object declaration is passed directly to a variable or a function argument of this type.
+**/
+@:forward
+abstract NativeStructArray<T:{}>(NativeArray) to NativeArray {
+	@:from @:pure(false)
+	static function __fromObject<T:{}>(obj:T):NativeStructArray<T> {
+		return cast Syntax.array(obj);
+	}
+
+	@:to
+	inline function __toObject():T {
+		return Boot.createAnon(this);
+	}
+}