Преглед изворни кода

[eval] eval.integers.Int64.ofHxInt64

Aleksandr Kuzmenko пре 4 година
родитељ
комит
861a2cb50a

+ 31 - 0
src/macro/eval/evalContext.ml

@@ -531,3 +531,34 @@ let get_instance_field_index_raise proto name =
 let get_instance_field_index proto name p =
 let get_instance_field_index proto name p =
 	try get_instance_field_index_raise proto name
 	try get_instance_field_index_raise proto name
 	with Not_found -> Error.error (Printf.sprintf "Field index for %s not found on prototype %s" (rev_hash name) (rev_hash proto.ppath)) p
 	with Not_found -> Error.error (Printf.sprintf "Field index for %s not found on prototype %s" (rev_hash name) (rev_hash proto.ppath)) p
+
+let is v path =
+	if path = key_Dynamic then
+		v <> vnull
+	else match v with
+	| VInt32 _ -> path = key_Int || path = key_Float
+	| VFloat f -> path = key_Float || (path = key_Int && f = (float_of_int (int_of_float f)) && f <= 2147483647. && f >= -2147483648.)
+	| VTrue | VFalse -> path = key_Bool
+	| VPrototype {pkind = PClass _} -> path = key_Class
+	| VPrototype {pkind = PEnum _} -> path = key_Enum
+	| VEnumValue ve -> path = key_EnumValue || path = ve.epath
+	| VString _ -> path = key_String
+	| VArray _ -> path = key_Array
+	| VVector _ -> path = key_eval_Vector
+	| VInstance vi ->
+		let has_interface path' =
+			try begin match (get_static_prototype_raise (get_ctx()) path').pkind with
+				| PClass interfaces -> List.mem path interfaces
+				| _ -> false
+			end with Not_found ->
+				false
+		in
+		let rec loop proto =
+			if path = proto.ppath || has_interface proto.ppath then true
+			else begin match proto.pparent with
+				| Some proto -> loop proto
+				| None -> false
+			end
+		in
+		loop vi.iproto
+	| _ -> false

+ 0 - 31
src/macro/eval/evalExceptions.ml

@@ -29,37 +29,6 @@ exception Continue
 exception Return of value
 exception Return of value
 exception Sys_exit of int
 exception Sys_exit of int
 
 
-let is v path =
-	if path = key_Dynamic then
-		v <> vnull
-	else match v with
-	| VInt32 _ -> path = key_Int || path = key_Float
-	| VFloat f -> path = key_Float || (path = key_Int && f = (float_of_int (int_of_float f)) && f <= 2147483647. && f >= -2147483648.)
-	| VTrue | VFalse -> path = key_Bool
-	| VPrototype {pkind = PClass _} -> path = key_Class
-	| VPrototype {pkind = PEnum _} -> path = key_Enum
-	| VEnumValue ve -> path = key_EnumValue || path = ve.epath
-	| VString _ -> path = key_String
-	| VArray _ -> path = key_Array
-	| VVector _ -> path = key_eval_Vector
-	| VInstance vi ->
-		let has_interface path' =
-			try begin match (get_static_prototype_raise (get_ctx()) path').pkind with
-				| PClass interfaces -> List.mem path interfaces
-				| _ -> false
-			end with Not_found ->
-				false
-		in
-		let rec loop proto =
-			if path = proto.ppath || has_interface proto.ppath then true
-			else begin match proto.pparent with
-				| Some proto -> loop proto
-				| None -> false
-			end
-		in
-		loop vi.iproto
-	| _ -> false
-
 let s_value_kind = function
 let s_value_kind = function
 	| VNull -> "VNull"
 	| VNull -> "VNull"
 	| VTrue -> "VTrue"
 	| VTrue -> "VTrue"

+ 20 - 9
src/macro/eval/evalIntegers.ml

@@ -20,26 +20,34 @@ let encode_haxe_i64 low high =
 
 
 let encode_haxe_i64_direct i64 =
 let encode_haxe_i64_direct i64 =
 	let low = GInt64.to_int32 i64 in
 	let low = GInt64.to_int32 i64 in
-	let high = GInt64.to_int32 (Int64.shift_right_logical i64 32) in
+	let high = GInt64.to_int32 (GInt64.shift_right_logical i64 32) in
 	encode_haxe_i64 low high
 	encode_haxe_i64 low high
 
 
-let decode_u64 v =
+let decode_haxe_i64 v =
 	match v with
 	match v with
+	| VInstance vi when is v key_haxe__Int64____Int64 ->
+		let high = decode_i32 (vi.ifields.(get_instance_field_index_raise vi.iproto key_high))
+		and low = decode_i32 (vi.ifields.(get_instance_field_index_raise vi.iproto key_low)) in
+		let high64 = GInt64.shift_left (Int32.to_int64 high) 32
+		and low64 = Int32.to_int64 low in
+		GInt64.logor high64 low64
+	| _ ->
+		unexpected_value v "haxe.Int64"
+
+let decode_u64 = function
 	| VUInt64 u -> u
 	| VUInt64 u -> u
-	| _ -> unexpected_value v "eval.integers.UInt64"
+	| v -> unexpected_value v "eval.integers.UInt64"
 
 
-let decode_i64 v =
-	match v with
+let decode_i64 = function
 	| VInt64 i -> i
 	| VInt64 i -> i
-	| _ -> unexpected_value v "eval.integers.Int64"
+	| v -> unexpected_value v "eval.integers.Int64"
 
 
 let encode_size_t t =
 let encode_size_t t =
 	VUInt64 (UInt64.of_int64 (Size_t.to_int64 t))
 	VUInt64 (UInt64.of_int64 (Size_t.to_int64 t))
 
 
-let decode_size_t v =
-	match v with
+let decode_size_t = function
 	| VUInt64 u -> Size_t.of_int64 (UInt64.to_int64 u)
 	| VUInt64 u -> Size_t.of_int64 (UInt64.to_int64 u)
-	| _ -> unexpected_value v "eval.integers.UInt64"
+	| v -> unexpected_value v "eval.integers.UInt64"
 
 
 let uint64_fields = [
 let uint64_fields = [
 	"MAX", VUInt64 UInt64.max_int;
 	"MAX", VUInt64 UInt64.max_int;
@@ -161,6 +169,9 @@ let int64_fields = [
 		try VInt64 (Int64.of_string s)
 		try VInt64 (Int64.of_string s)
 		with Failure _ -> throw_string "The string is not a valid Int64 representation" null_pos
 		with Failure _ -> throw_string "The string is not a valid Int64 representation" null_pos
 	);
 	);
+	"ofHxInt64", vfun1 (fun v ->
+		VInt64 (decode_haxe_i64 v)
+	);
 	"max", vfun2 (fun v1 v2 ->
 	"max", vfun2 (fun v1 v2 ->
 		let a = decode_i64 v1
 		let a = decode_i64 v1
 		and b = decode_i64 v2 in
 		and b = decode_i64 v2 in

+ 5 - 0
std/eval/integers/Int64.hx

@@ -24,6 +24,11 @@ package eval.integers;
 	**/
 	**/
 	static public function ofString(s:String):Int64;
 	static public function ofString(s:String):Int64;
 
 
+	/**
+		Convert `haxe.Int64` to `eval.integers.Int64`
+	**/
+	@:from static public function ofHxInt64(hx:haxe.Int64):Int64;
+
 	/**
 	/**
 		Returns the greater of `a` and `b`.
 		Returns the greater of `a` and `b`.
 	**/
 	**/