瀏覽代碼

TestSerialize compiling (not yet passing)

Nicolas Cannasse 9 年之前
父節點
當前提交
52ed1dc49a
共有 5 個文件被更改,包括 105 次插入34 次删除
  1. 73 12
      genhl.ml
  2. 7 7
      std/Lambda.hx
  3. 2 2
      std/haxe/Serializer.hx
  4. 19 10
      std/hl/_std/Type.hx
  5. 4 3
      std/hl/types/ArrayObj.hx

+ 73 - 12
genhl.ml

@@ -1060,6 +1060,9 @@ and cast_to ?(force=false) ctx (r:reg) (t:ttype) p =
 	| HFun (args1,ret1), HFun (args2, ret2) when List.length args1 = List.length args2 ->
 		let fid = gen_method_wrapper ctx rt t p in
 		let fr = alloc_tmp ctx t in
+		op ctx (OJNotNull (r,2));
+		op ctx (ONull fr);
+		op ctx (OJAlways 1);
 		op ctx (OClosure (fr,fid,r));
 		fr
 	| HObj _, HObj _ when is_array_type rt && is_array_type t ->
@@ -1337,12 +1340,6 @@ and eval_expr ctx e =
 				loop l
 		in
 		loop el
-	| TCall ({ eexpr = TField (_,FStatic({ cl_path = [],"Type" },{ cf_name = "enumIndex" })) },[e]) when (match to_type ctx e.etype with HEnum _ -> true | _ -> false) ->
-		let r = alloc_tmp ctx HI32 in
-		let re = eval_expr ctx e in
-		op ctx (ONullCheck re);
-		op ctx (OEnumIndex (r,re));
-		r
 	| TCall ({ eexpr = TConst TSuper } as s, el) ->
 		(match follow s.etype with
 		| TInst (csup,_) ->
@@ -1626,6 +1623,12 @@ and eval_expr ctx e =
 			let v = eval_expr ctx v in
 			op ctx (OSetGlobal (alloc_global ctx "__types__" (rtype ctx v), v));
 			v
+		| "$enumIndex", [v] ->
+			let r = alloc_tmp ctx HI32 in
+			let re = eval_expr ctx v in
+			op ctx (ONullCheck re);
+			op ctx (OEnumIndex (r,re));
+			r
 		| _ ->
 			error ("Unknown native call " ^ v.v_name) e.epos)
 	| TCall (ec,el) ->
@@ -2144,9 +2147,8 @@ and eval_expr ctx e =
 			let rec loop next (cases,e) =
 				let next = List.fold_left (fun next c ->
 					next();
-					let r = eval_expr ctx c in
-					let rv = cast_to ctx rvalue (rtype ctx r) e.epos in
-					let j = jump ctx (fun n -> OJNeq (r,rv,n)) in
+					let r = eval_to ctx c (common_type ctx en c true c.epos) in
+					let j = jump ctx (fun n -> OJNeq (r,rvalue,n)) in
 					j
 				) next cases in
 				let re = eval_to ctx e rt in
@@ -3079,7 +3081,7 @@ let check code =
 				| _ ->
 					is_enum r)
 			| OEnumIndex (r,v) ->
-				is_enum v;
+				if rtype v <> HDyn then is_enum v;
 				reg r HI32;
 			| OEnumField (r,e,f,i) ->
 				(match rtype e with
@@ -3145,6 +3147,7 @@ type value =
 and vabstract =
 	| AHashBytes of (string, value) Hashtbl.t
 	| AHashInt of (int32, value) Hashtbl.t
+	| AHashObject of (value * value) list ref
 	| AReg of regexp
 
 and vfunction =
@@ -4059,7 +4062,7 @@ let interp code =
 				)
 			| OEnumIndex (r,v) ->
 				(match get v with
-				| VEnum (i,_) -> set r (VInt (Int32.of_int i))
+				| VEnum (i,_) | VDyn (VEnum (i,_),_) -> set r (VInt (Int32.of_int i))
 				| _ -> assert false)
 			| OEnumField (r, v, _, i) ->
 				(match get v with
@@ -4320,6 +4323,53 @@ let interp code =
 					if m then Hashtbl.remove h i;
 					VBool m
 				| _ -> assert false)
+			| "hoalloc" ->
+				(function
+				| [] -> VAbstract (AHashObject (ref []))
+				| _ -> assert false)
+			| "hoset" ->
+				(function
+				| [VAbstract (AHashObject l);o;v] ->
+					let rec replace l =
+						match l with
+						| [] -> [o,v]
+						| (o2,_) :: l when o == o2 -> (o,v) :: l
+						| p :: l -> p :: replace l
+					in
+					l := replace !l;
+					VUndef
+				| _ -> assert false)
+			| "hoget" ->
+				(function
+				| [VAbstract (AHashObject l);o] ->
+					(try List.assq o !l with Not_found -> VNull)
+				| _ -> assert false)
+			| "hovalues" ->
+				(function
+				| [VAbstract (AHashObject l)] ->
+					VArray (Array.of_list (List.map snd !l), HDyn)
+				| _ -> assert false)
+			| "hokeys" ->
+				(function
+				| [VAbstract (AHashObject l)] ->
+					VArray (Array.of_list (List.map fst !l), HDyn)
+				| _ -> assert false)
+			| "hoexists" ->
+				(function
+				| [VAbstract (AHashObject l);o] -> VBool (List.mem_assq o !l)
+				| _ -> assert false)
+			| "horemove" ->
+				(function
+				| [VAbstract (AHashObject rl);o] ->
+					let rec loop acc = function
+						| [] -> false
+						| (o2,_) :: l when o == o2 ->
+							rl := (List.rev acc) @ l;
+							true
+						| p :: l -> loop (p :: acc) l
+					in
+					VBool (loop [] !rl)
+				| _ -> assert false)
 			| "sys_print" ->
 				(function
 				| [VBytes str] -> print_string (hl_to_caml str); VUndef
@@ -4370,6 +4420,13 @@ let interp code =
 					VArray (Array.of_list (Hashtbl.fold (fun n _ acc -> VBytes (caml_to_hl n) :: acc) o.dfields []), HBytes)
 				| _ ->
 					VNull)
+			| "enum_parameters" ->
+				(function
+				| [VDyn (VEnum (idx,pl),HEnum e)] ->
+					let _,_, ptypes = e.efields.(idx) in
+					VArray (Array.mapi (fun i v -> make_dyn v ptypes.(i)) pl,HDyn)
+				| _ ->
+					assert false)
 			| "type_instance_fields" ->
 				(function
 				| [VType t] ->
@@ -4599,9 +4656,13 @@ let interp code =
 				(function
 				| [] -> to_date (Unix.localtime (Unix.time()))
 				| _ -> assert false)
+			| "date_get_time" ->
+				(function
+				| [VInt v] -> VFloat (fst (Unix.mktime (date v)))
+				| _ -> assert false)
 			| "date_from_time" ->
 				(function
-				| [] -> assert false
+				| [VFloat f] -> to_date (Unix.localtime f)
 				| _ -> assert false)
 			| "date_get_weekday" ->
 				(function

+ 7 - 7
std/Lambda.hx

@@ -86,10 +86,10 @@ class Lambda {
 			l.add(f(i++,x));
 		return l;
 	}
-	
-	/** 
+
+	/**
 		Concatenate a list of lists.
-		
+
 		The order of elements is preserved.
 	**/
 	public static function flatten<A>( it : Iterable<Iterable<A>> ) : List<A> {
@@ -99,12 +99,12 @@ class Lambda {
 				l.add(x);
 		return l;
 	}
-	
-	/** 
+
+	/**
 		A composition of map and flatten.
-		
+
 		The order of elements is preserved.
-		
+
 		If `f` is null, the result is unspecified.
 	**/
 	public static function flatMap<A,B>( it : Iterable<A>, f: A -> Iterable<B> ) : List<B> {

+ 2 - 2
std/haxe/Serializer.hx

@@ -261,7 +261,7 @@ class Serializer {
 				#if (flash || python)
 				var v : Array<Dynamic> = v;
 				#end
-				var l = #if (neko || flash || php || cs || java || python) v.length #elseif cpp v.__length() #else __getField(v, "length") #end;
+				var l = #if (neko || flash || php || cs || java || python || hl) v.length #elseif cpp v.__length() #else __getField(v, "length") #end;
 				for( i in 0...l ) {
 					if( v[i] == null )
 						ucount++;
@@ -472,7 +472,7 @@ class Serializer {
 				for( i in 0...l )
 					serialize(untyped __field__(v, __php__("params"), i));
 			}
-			#elseif (java || cs || python)
+			#elseif (java || cs || python || hl)
 			if( useEnumIndex ) {
 				buf.add(":");
 				buf.add(Type.enumIndex(v));

+ 19 - 10
std/hl/_std/Type.hx

@@ -117,8 +117,8 @@ class Type {
 	}
 
 	public static function getEnumConstructs( e : Enum<Dynamic> ) : Array<String> {
-		throw "TODO";
-		return null;
+		var e : hl.types.BaseType.Enum = cast e;
+		return e.__constructs__.copy();
 	}
 
 	public static function typeof( v : Dynamic ) : ValueType {
@@ -154,23 +154,32 @@ class Type {
 	}
 
 	public static function enumConstructor( e : EnumValue ) : String {
-		throw "TODO";
+		var en : hl.types.BaseType.Enum = cast getEnum(e);
+		return en.__constructs__[Type.enumIndex(e)];
+	}
+
+	@:hlNative("std","enum_parameters")
+	static function _enumParameters( e : EnumValue ) : hl.types.NativeArray<Dynamic> {
 		return null;
 	}
 
 	public static function enumParameters( e : EnumValue ) : Array<Dynamic> {
-		throw "TODO";
-		return null;
+		var arr = _enumParameters(e);
+		return cast hl.types.ArrayObj.alloc(arr);
 	}
 
-	public static function enumIndex( e : EnumValue ) : Int {
-		throw "TODO";
-		return 0;
+	@:extern public inline static function enumIndex( e : EnumValue ) : Int {
+		return untyped $enumIndex(e);
 	}
 
 	public static function allEnums<T>( e : Enum<T> ) : Array<T> {
-		throw "TODO";
-		return null;
+		var en : hl.types.BaseType.Enum = cast e;
+		var out = [];
+		for( i in 0...en.__evalues__.length ) {
+			var v = en.__evalues__[i];
+			if( v != null ) out.push(v);
+		}
+		return out;
 	}
 
 }

+ 4 - 3
std/hl/types/ArrayObj.hx

@@ -131,8 +131,9 @@ class ArrayObj<T> extends ArrayBase {
 	}
 
 	public function copy() : ArrayObj<T> {
-		throw "TODO";
-		return null;
+		var n = new NativeArray<Dynamic>(array.length);
+		n.blit(0, array, 0, array.length);
+		return alloc(n);
 	}
 
 	public function iterator() : Iterator<T> {
@@ -141,7 +142,7 @@ class ArrayObj<T> extends ArrayBase {
 		return n;
 	}
 
-	public function map<S>( f : T -> S ) : ArrayObj<S> {
+	public function map<S>( f : T -> S ) : ArrayDyn {
 		throw "TODO";
 		return null;
 	}