Browse Source

[eval] try to get the sys API under control

Simon Krajewski 7 years ago
parent
commit
5e91537e34

+ 1 - 1
src/macro/eval/evalDebugMisc.ml

@@ -167,7 +167,7 @@ let rec expr_to_value ctx env e =
 	let rec loop e = match fst e with
 		| EConst cst ->
 			begin match cst with
-				| String s -> encode_string s
+				| String s -> EvalString.create_unknown s
 				| Int s -> VInt32 (Int32.of_string s)
 				| Float s -> VFloat (float_of_string s)
 				| Ident "true" -> VTrue

+ 1 - 1
src/macro/eval/evalEmitter.ml

@@ -39,7 +39,7 @@ let unexpected_value_p v s p =
 	throw_string str p
 
 let cannot_call v p =
-	throw (encode_string ("Cannot call " ^ (value_string v))) p
+	throw (EvalString.create_unknown ("Cannot call " ^ (value_string v))) p
 
 let decode_int_p v p = match v with
 	| VInt32 i -> Int32.to_int i

+ 1 - 1
src/macro/eval/evalMain.ml

@@ -371,7 +371,7 @@ let compiler_error msg pos =
 	let vi = encode_instance key_haxe_macro_Error in
 	match vi with
 	| VInstance i ->
-		set_instance_field i key_message (encode_string msg);
+		set_instance_field i key_message (EvalString.create_unknown msg);
 		set_instance_field i key_pos (encode_pos pos);
 		exc vi
 	| _ ->

+ 1 - 1
src/macro/eval/evalMisc.ml

@@ -30,7 +30,7 @@ open EvalHash
 open EvalString
 
 let throw_string s p =
-	throw (encode_string s) p
+	throw (create_unknown s) p
 
 let invalid_binop op v1 v2 p =
 	throw_string (Printf.sprintf "Invalid operation: %s %s %s" (value_string v1) (s_binop op) (value_string v2)) p

+ 23 - 17
src/macro/eval/evalStdLib.ml

@@ -548,14 +548,14 @@ module StdCallStack = struct
 		List.iter (fun (pos,kind) ->
 			let file_pos s =
 				let line1,col1,_,_ = Lexer.get_pos_coords pos in
-				encode_enum_value key_haxe_StackItem 2 [|s;encode_string pos.pfile;vint line1;vint col1|] None
+				encode_enum_value key_haxe_StackItem 2 [|s;create_unknown pos.pfile;vint line1;vint col1|] None
 			in
 			match kind with
 			| EKLocalFunction i ->
 				let local_function = encode_enum_value key_haxe_StackItem 4 [|vint i|] None in
 				DynArray.add l (file_pos local_function);
 			| EKMethod(st,sf) ->
-				let local_function = encode_enum_value key_haxe_StackItem 3 [|encode_string (rev_hash_s st); encode_string (rev_hash_s sf)|] None in
+				let local_function = encode_enum_value key_haxe_StackItem 3 [|create_unknown (rev_hash_s st); create_unknown (rev_hash_s sf)|] None in
 				DynArray.add l (file_pos local_function);
 			| EKDelayed ->
 				()
@@ -789,7 +789,7 @@ module StdEReg = struct
 
 	let escape = vfun1 (fun s ->
 		let s = decode_string s in
-		encode_string (Str.quote s)
+		create_unknown (Str.quote s)
 	)
 
 	let map = vifun2 (fun vthis s f ->
@@ -1158,7 +1158,7 @@ module StdFileSystem = struct
 		else remove_trailing_slash s
 
 	let absolutePath = vfun1 (fun relPath ->
-		encode_string (Path.unique_full_path (decode_string relPath))
+		create_unknown (Path.unique_full_path (decode_string relPath))
 	)
 
 	let createDirectory = vfun1 (fun path ->
@@ -1182,7 +1182,7 @@ module StdFileSystem = struct
 	)
 
 	let fullPath = vfun1 (fun relPath ->
-		try encode_string (Extc.get_full_path (decode_string relPath)) with exc -> exc_string (Printexc.to_string exc)
+		try create_unknown (Extc.get_full_path (decode_string relPath)) with exc -> exc_string (Printexc.to_string exc)
 	)
 
 	let isDirectory = vfun1 (fun dir ->
@@ -1192,7 +1192,7 @@ module StdFileSystem = struct
 
 	let readDirectory = vfun1 (fun dir ->
 		let d = try Sys.readdir (decode_string dir) with Sys_error s -> exc_string s in
-		encode_array (Array.to_list (Array.map (fun s -> encode_string s) d))
+		encode_array (Array.to_list (Array.map (fun s -> create_unknown s) d))
 	)
 
 	let rename = vfun2 (fun path newPath ->
@@ -1353,17 +1353,17 @@ module StdHost = struct
 		inet_addr_of_string str
 
 	let localhost = vfun0 (fun () ->
-		encode_string (gethostname())
+		create_unknown (gethostname())
 	)
 
 	let hostReverse = vfun1 (fun ip ->
 		let ip = decode_i32 ip in
-		try encode_string (gethostbyaddr (int32_addr ip)).h_name with Not_found -> exc_string "Could not reverse host"
+		try create_unknown (gethostbyaddr (int32_addr ip)).h_name with Not_found -> exc_string "Could not reverse host"
 	)
 
 	let hostToString = vfun1 (fun ip ->
 		let ip = decode_i32 ip in
-		encode_string (string_of_inet_addr (int32_addr ip))
+		create_unknown (string_of_inet_addr (int32_addr ip))
 	)
 
 	let resolve = vfun1 (fun name ->
@@ -1704,7 +1704,7 @@ module StdResource = struct
 	open Common
 
 	let listNames = vfun0 (fun () ->
-		encode_array (List.map encode_string (hashtbl_keys ((get_ctx()).curapi.MacroApi.get_com()).resources))
+		encode_array (List.map create_unknown (hashtbl_keys ((get_ctx()).curapi.MacroApi.get_com()).resources))
 	)
 
 	let getString = vfun1 (fun name ->
@@ -2291,7 +2291,7 @@ module StdSys = struct
 	open Common
 
 	let args = vfun0 (fun () ->
-		encode_array (List.map encode_string ((get_ctx()).curapi.MacroApi.get_com()).sys_args)
+		encode_array (List.map create_unknown ((get_ctx()).curapi.MacroApi.get_com()).sys_args)
 	)
 
 	let _command = vfun1 (fun cmd ->
@@ -2306,7 +2306,7 @@ module StdSys = struct
 		let h = StringHashtbl.create 0 in
 		Array.iter(fun s ->
 			let k, v = ExtString.String.split s "=" in
-			StringHashtbl.replace h (create_ascii k) (encode_string v)
+			StringHashtbl.replace h (create_ascii k) (create_unknown v)
 		) env;
 		encode_string_map_direct h
 	)
@@ -2325,12 +2325,18 @@ module StdSys = struct
 	let getCwd = vfun0 (fun () ->
 		let dir = Unix.getcwd() in
 		let l = String.length dir in
-		encode_string (if l = 0 then "./" else match dir.[l - 1] with '/' | '\\' -> dir | _ -> dir ^ "/")
+		if l = 0 then
+			encode_string "./"
+		else match dir.[l - 1] with
+		| '/' | '\\' ->
+			create_unknown dir
+		| _ ->
+			create_unknown (dir ^ "/")
 	)
 
 	let getEnv = vfun1 (fun s ->
 		let s = decode_string s in
-		try encode_string (Unix.getenv s) with _ -> vnull
+		try create_unknown (Unix.getenv s) with _ -> vnull
 	)
 
 	let print = vfun1 (fun v ->
@@ -2354,7 +2360,7 @@ module StdSys = struct
 		| None -> assert false
 		| Some p ->
 			match ctx.curapi.get_type (s_type_path p) with
-			| Some(Type.TInst (c, _)) -> encode_string (Extc.get_full_path c.Type.cl_pos.Globals.pfile)
+			| Some(Type.TInst (c, _)) -> create_unknown (Extc.get_full_path c.Type.cl_pos.Globals.pfile)
 			| _ -> assert false
 	)
 
@@ -2700,12 +2706,12 @@ module StdUtf8 = struct
 			incr i
 		) s;
 		let s = Bytes.unsafe_to_string buf in
-		encode_string s
+		create_unknown s
 	)
 
 	let encode = vfun1 (fun s ->
 		let s = decode_string s in
-		encode_string (UTF8.init (String.length s) (fun i -> UChar.of_char s.[i]))
+		create_unknown (UTF8.init (String.length s) (fun i -> UChar.of_char s.[i]))
 	)
 
 	let iter = vfun2 (fun s f ->