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

initial eval code (FileSystem.exists)

Aurel Bílý пре 6 година
родитељ
комит
7929a23181
5 измењених фајлова са 148 додато и 102 уклоњено
  1. 92 92
      libs/uv/uv.ml
  2. 9 9
      libs/uv/uv_stubs.c
  3. 2 0
      src/macro/eval/evalHash.ml
  4. 39 1
      src/macro/eval/evalStdLib.ml
  5. 6 0
      src/macro/eval/evalValue.ml

+ 92 - 92
libs/uv/uv.ml

@@ -2,51 +2,51 @@
 
 (* Handle types *)
 
-type uv_loop_t
-type uv_handle_t
-type uv_dir_t
-type uv_stream_t
-type uv_tcp_t
-type uv_udp_t
-type uv_pipe_t
-type uv_tty_t
-type uv_poll_t
-type uv_timer_t
-type uv_prepare_t
-type uv_check_t
-type uv_idle_t
-type uv_async_t
-type uv_process_t
-type uv_fs_event_t
-type uv_fs_poll_t
-type uv_signal_t
+type t_loop
+type t_handle
+type t_dir
+type t_stream
+type t_tcp
+type t_udp
+type t_pipe
+type t_tty
+type t_poll
+type t_timer
+type t_prepare
+type t_check
+type t_idle
+type t_async
+type t_process
+type t_fs_event
+type t_fs_poll
+type t_signal
 
 (* Request types *)
 
-type uv_req_t
-type uv_getaddrinfo_t
-type uv_getnameinfo_t
-type uv_shutdown_t
-type uv_write_t
-type uv_connect_t
-type uv_udp_send_t
-type uv_fs_t
-type uv_work_t
+type t_req
+type t_getaddrinfo
+type t_getnameinfo
+type t_shutdown
+type t_write
+type t_connect
+type t_udp_send
+type t_fs
+type t_work
 
 (* Other types *)
 
-type uv_cpu_info_t
-type uv_interface_address_t
-type uv_dirent_t
-type uv_passwd_t
-type uv_utsname_t
-type uv_file
-(* type uv_stat_t *)
-type uv_buf_t
+type t_cpu_info
+type t_interface_address
+type t_dirent
+type t_passwd
+type t_utsname
+type t_file
+(* type t_stat *)
+type t_buf
 
 (* Non-abstract type definitions  *)
 
-type uv_stat_t = {
+type t_stat = {
   dev: int;
   kind: int;
   perm: int;
@@ -72,69 +72,69 @@ type uv_stat_t = {
 
 (* ------------- LOOP ----------------------------------------------- *)
 
-external loop_init : unit -> uv_loop_t = "w_loop_init"
-external loop_close : uv_loop_t -> unit = "w_loop_close"
-external run : uv_loop_t -> int -> bool = "w_run"
-external loop_alive : uv_loop_t -> bool = "w_loop_alive"
+external loop_init : unit -> t_loop = "w_loop_init"
+external loop_close : t_loop -> unit = "w_loop_close"
+external run : t_loop -> int -> bool = "w_run"
+external loop_alive : t_loop -> bool = "w_loop_alive"
 
 (* ------------- FILESYSTEM ----------------------------------------- *)
 
 type fs_cb = unit -> unit
 type fs_cb_bytes = string -> unit
 type fs_cb_path = string -> unit
-type fs_cb_file = uv_file -> unit
+type fs_cb_file = t_file -> unit
 type fs_cb_int = int -> unit
-type fs_cb_stat= uv_stat_t -> unit
+type fs_cb_stat= t_stat -> unit
 type fs_cb_scandir = (string * int) list -> unit
 
-external fs_close : uv_loop_t -> uv_file -> fs_cb -> unit = "w_fs_close"
-external fs_open : uv_loop_t -> string -> int -> int -> fs_cb_file -> unit = "w_fs_open"
-external fs_unlink : uv_loop_t -> string -> fs_cb -> unit = "w_fs_unlink"
-external fs_mkdir : uv_loop_t -> string -> int -> fs_cb -> unit = "w_fs_mkdir"
-external fs_mkdtemp : uv_loop_t -> string -> fs_cb_path -> unit = "w_fs_mkdtemp"
-external fs_rmdir : uv_loop_t -> string -> fs_cb -> unit = "w_fs_rmdir"
-external fs_scandir : uv_loop_t -> string -> int -> fs_cb_scandir -> unit = "w_fs_scandir"
-external fs_stat : uv_loop_t -> string -> fs_cb_stat -> unit = "w_fs_stat"
-external fs_fstat : uv_loop_t -> uv_file -> fs_cb_stat -> unit = "w_fs_fstat"
-external fs_lstat : uv_loop_t -> string -> fs_cb_stat -> unit = "w_fs_lstat"
-external fs_rename : uv_loop_t -> string -> string -> fs_cb -> unit = "w_fs_rename"
-external fs_fsync : uv_loop_t -> uv_file -> fs_cb -> unit = "w_fs_fsync"
-external fs_fdatasync : uv_loop_t -> uv_file -> fs_cb -> unit = "w_fs_fdatasync"
-external fs_ftruncate : uv_loop_t -> uv_file -> int64 -> fs_cb -> unit = "w_fs_ftruncate"
-external fs_access : uv_loop_t -> string -> int -> fs_cb -> unit = "w_fs_access"
-external fs_chmod : uv_loop_t -> string -> int -> fs_cb -> unit = "w_fs_chmod"
-external fs_fchmod : uv_loop_t -> uv_file -> int -> fs_cb -> unit = "w_fs_fchmod"
-external fs_utime : uv_loop_t -> string -> float -> float -> fs_cb -> unit = "w_fs_utime"
-external fs_futime : uv_loop_t -> uv_file -> float -> float -> fs_cb -> unit = "w_fs_futime"
-external fs_link : uv_loop_t -> string -> string -> fs_cb -> unit = "w_fs_link"
-external fs_symlink : uv_loop_t -> string -> string -> int -> fs_cb -> unit = "w_fs_symlink"
-external fs_readlink : uv_loop_t -> string -> fs_cb_bytes -> unit = "w_fs_readlink"
-external fs_realpath : uv_loop_t -> string -> fs_cb_bytes -> unit = "w_fs_realpath"
-external fs_chown : uv_loop_t -> string -> int -> int -> fs_cb -> unit = "w_fs_chown"
-external fs_fchown : uv_loop_t -> uv_file -> int -> int -> fs_cb -> unit = "w_fs_fchown"
+external fs_close : t_loop -> t_file -> fs_cb -> unit = "w_fs_close"
+external fs_open : t_loop -> string -> int -> int -> fs_cb_file -> unit = "w_fs_open"
+external fs_unlink : t_loop -> string -> fs_cb -> unit = "w_fs_unlink"
+external fs_mkdir : t_loop -> string -> int -> fs_cb -> unit = "w_fs_mkdir"
+external fs_mkdtemp : t_loop -> string -> fs_cb_path -> unit = "w_fs_mkdtemp"
+external fs_rmdir : t_loop -> string -> fs_cb -> unit = "w_fs_rmdir"
+external fs_scandir : t_loop -> string -> int -> fs_cb_scandir -> unit = "w_fs_scandir"
+external fs_stat : t_loop -> string -> fs_cb_stat -> unit = "w_fs_stat"
+external fs_fstat : t_loop -> t_file -> fs_cb_stat -> unit = "w_fs_fstat"
+external fs_lstat : t_loop -> string -> fs_cb_stat -> unit = "w_fs_lstat"
+external fs_rename : t_loop -> string -> string -> fs_cb -> unit = "w_fs_rename"
+external fs_fsync : t_loop -> t_file -> fs_cb -> unit = "w_fs_fsync"
+external fs_fdatasync : t_loop -> t_file -> fs_cb -> unit = "w_fs_fdatasync"
+external fs_ftruncate : t_loop -> t_file -> int64 -> fs_cb -> unit = "w_fs_ftruncate"
+external fs_access : t_loop -> string -> int -> fs_cb -> unit = "w_fs_access"
+external fs_chmod : t_loop -> string -> int -> fs_cb -> unit = "w_fs_chmod"
+external fs_fchmod : t_loop -> t_file -> int -> fs_cb -> unit = "w_fs_fchmod"
+external fs_utime : t_loop -> string -> float -> float -> fs_cb -> unit = "w_fs_utime"
+external fs_futime : t_loop -> t_file -> float -> float -> fs_cb -> unit = "w_fs_futime"
+external fs_link : t_loop -> string -> string -> fs_cb -> unit = "w_fs_link"
+external fs_symlink : t_loop -> string -> string -> int -> fs_cb -> unit = "w_fs_symlink"
+external fs_readlink : t_loop -> string -> fs_cb_bytes -> unit = "w_fs_readlink"
+external fs_realpath : t_loop -> string -> fs_cb_bytes -> unit = "w_fs_realpath"
+external fs_chown : t_loop -> string -> int -> int -> fs_cb -> unit = "w_fs_chown"
+external fs_fchown : t_loop -> t_file -> int -> int -> fs_cb -> unit = "w_fs_fchown"
 
-external fs_close_sync : uv_loop_t -> uv_file -> unit = "w_fs_close_sync"
-external fs_open_sync : uv_loop_t -> string -> int -> int -> uv_file = "w_fs_open_sync"
-external fs_unlink_sync : uv_loop_t -> string -> unit = "w_fs_unlink_sync"
-external fs_mkdir_sync : uv_loop_t -> string -> int -> unit = "w_fs_mkdir_sync"
-external fs_mkdtemp_sync : uv_loop_t -> string -> string = "w_fs_mkdtemp_sync"
-external fs_rmdir_sync : uv_loop_t -> string -> unit = "w_fs_rmdir_sync"
-external fs_scandir_sync : uv_loop_t -> string -> int -> (string * int) list = "w_fs_scandir_sync"
-external fs_stat_sync : uv_loop_t -> string -> uv_stat_t = "w_fs_stat_sync"
-external fs_fstat_sync : uv_loop_t -> uv_file -> uv_stat_t = "w_fs_fstat_sync"
-external fs_lstat_sync : uv_loop_t -> string -> uv_stat_t = "w_fs_lstat_sync"
-external fs_rename_sync : uv_loop_t -> string -> string -> unit = "w_fs_rename_sync"
-external fs_fsync_sync : uv_loop_t -> uv_file -> unit = "w_fs_fsync_sync"
-external fs_fdatasync_sync : uv_loop_t -> uv_file -> unit = "w_fs_fdatasync_sync"
-external fs_ftruncate_sync : uv_loop_t -> uv_file -> int64 -> unit = "w_fs_ftruncate_sync"
-external fs_access_sync : uv_loop_t -> string -> int -> unit = "w_fs_access_sync"
-external fs_chmod_sync : uv_loop_t -> string -> int -> unit = "w_fs_chmod_sync"
-external fs_fchmod_sync : uv_loop_t -> uv_file -> int -> unit = "w_fs_fchmod_sync"
-external fs_utime_sync : uv_loop_t -> string -> float -> float -> unit = "w_fs_utime_sync"
-external fs_futime_sync : uv_loop_t -> uv_file -> float -> float -> unit = "w_fs_futime_sync"
-external fs_link_sync : uv_loop_t -> string -> string -> unit = "w_fs_link_sync"
-external fs_symlink_sync : uv_loop_t -> string -> string -> int -> unit = "w_fs_symlink_sync"
-external fs_readlink_sync : uv_loop_t -> string -> string = "w_fs_readlink_sync"
-external fs_realpath_sync : uv_loop_t -> string -> string = "w_fs_realpath_sync"
-external fs_chown_sync : uv_loop_t -> string -> int -> int -> unit = "w_fs_chown_sync"
-external fs_fchown_sync : uv_loop_t -> uv_file -> int -> int -> unit = "w_fs_fchown_sync"
+external fs_close_sync : t_loop -> t_file -> unit = "w_fs_close_sync"
+external fs_open_sync : t_loop -> string -> int -> int -> t_file = "w_fs_open_sync"
+external fs_unlink_sync : t_loop -> string -> unit = "w_fs_unlink_sync"
+external fs_mkdir_sync : t_loop -> string -> int -> unit = "w_fs_mkdir_sync"
+external fs_mkdtemp_sync : t_loop -> string -> string = "w_fs_mkdtemp_sync"
+external fs_rmdir_sync : t_loop -> string -> unit = "w_fs_rmdir_sync"
+external fs_scandir_sync : t_loop -> string -> int -> (string * int) list = "w_fs_scandir_sync"
+external fs_stat_sync : t_loop -> string -> t_stat = "w_fs_stat_sync"
+external fs_fstat_sync : t_loop -> t_file -> t_stat = "w_fs_fstat_sync"
+external fs_lstat_sync : t_loop -> string -> t_stat = "w_fs_lstat_sync"
+external fs_rename_sync : t_loop -> string -> string -> unit = "w_fs_rename_sync"
+external fs_fsync_sync : t_loop -> t_file -> unit = "w_fs_fsync_sync"
+external fs_fdatasync_sync : t_loop -> t_file -> unit = "w_fs_fdatasync_sync"
+external fs_ftruncate_sync : t_loop -> t_file -> int64 -> unit = "w_fs_ftruncate_sync"
+external fs_access_sync : t_loop -> string -> int -> unit = "w_fs_access_sync"
+external fs_chmod_sync : t_loop -> string -> int -> unit = "w_fs_chmod_sync"
+external fs_fchmod_sync : t_loop -> t_file -> int -> unit = "w_fs_fchmod_sync"
+external fs_utime_sync : t_loop -> string -> float -> float -> unit = "w_fs_utime_sync"
+external fs_futime_sync : t_loop -> t_file -> float -> float -> unit = "w_fs_futime_sync"
+external fs_link_sync : t_loop -> string -> string -> unit = "w_fs_link_sync"
+external fs_symlink_sync : t_loop -> string -> string -> int -> unit = "w_fs_symlink_sync"
+external fs_readlink_sync : t_loop -> string -> string = "w_fs_readlink_sync"
+external fs_realpath_sync : t_loop -> string -> string = "w_fs_realpath_sync"
+external fs_chown_sync : t_loop -> string -> int -> int -> unit = "w_fs_chown_sync"
+external fs_fchown_sync : t_loop -> t_file -> int -> int -> unit = "w_fs_fchown_sync"

+ 9 - 9
libs/uv/uv_stubs.c

@@ -308,12 +308,12 @@ HL_PRIM int HL_NAME(w_fs_write_sync)(uv_loop_t *loop, uv_file file, const uv_buf
 
 */
 FS_WRAP1(fs_close, (uv_file), handle_fs_cb);
-FS_WRAP3(fs_open, String_val, (int), (int), handle_fs_cb_file);
+FS_WRAP3(fs_open, String_val, Int_val, Int_val, handle_fs_cb_file);
 FS_WRAP1(fs_unlink, String_val, handle_fs_cb);
-FS_WRAP2(fs_mkdir, String_val, (int), handle_fs_cb);
+FS_WRAP2(fs_mkdir, String_val, Int_val, handle_fs_cb);
 FS_WRAP1(fs_mkdtemp, String_val, handle_fs_cb_path);
 FS_WRAP1(fs_rmdir, String_val, handle_fs_cb);
-FS_WRAP2(fs_scandir, String_val, (int), handle_fs_cb_scandir);
+FS_WRAP2(fs_scandir, String_val, Int_val, handle_fs_cb_scandir);
 FS_WRAP1(fs_stat, String_val, handle_fs_cb_stat);
 FS_WRAP1(fs_fstat, (uv_file), handle_fs_cb_stat);
 FS_WRAP1(fs_lstat, String_val, handle_fs_cb_stat);
@@ -322,14 +322,14 @@ FS_WRAP1(fs_fsync, (uv_file), handle_fs_cb);
 FS_WRAP1(fs_fdatasync, (uv_file), handle_fs_cb);
 FS_WRAP2(fs_ftruncate, (uv_file), Int64_val, handle_fs_cb);
 //FS_WRAP4(fs_sendfile, void, uv_file, uv_file, int64_t, size_t, _VOID, _FILE _FILE _I32 _I32, _CB, handle_fs_cb, );
-FS_WRAP2(fs_access, String_val, (int), handle_fs_cb);
-FS_WRAP2(fs_chmod, String_val, (int), handle_fs_cb);
-FS_WRAP2(fs_fchmod, (uv_file), (int), handle_fs_cb);
+FS_WRAP2(fs_access, String_val, Int_val, handle_fs_cb);
+FS_WRAP2(fs_chmod, String_val, Int_val, handle_fs_cb);
+FS_WRAP2(fs_fchmod, (uv_file), Int_val, handle_fs_cb);
 FS_WRAP3(fs_utime, String_val, Double_val, Double_val, handle_fs_cb);
 FS_WRAP3(fs_futime, (uv_file), Double_val, Double_val, handle_fs_cb);
 FS_WRAP2(fs_link, String_val, String_val, handle_fs_cb);
-FS_WRAP3(fs_symlink, String_val, String_val, (int), handle_fs_cb);
+FS_WRAP3(fs_symlink, String_val, String_val, Int_val, handle_fs_cb);
 FS_WRAP1(fs_readlink, String_val, handle_fs_cb_bytes);
 FS_WRAP1(fs_realpath, String_val, handle_fs_cb_bytes);
-FS_WRAP3(fs_chown, String_val, (uv_uid_t), (uv_gid_t), handle_fs_cb);
-FS_WRAP3(fs_fchown, (uv_file), (uv_uid_t), (uv_gid_t), handle_fs_cb);
+FS_WRAP3(fs_chown, String_val, (uv_uid_t)Int_val, (uv_gid_t)Int_val, handle_fs_cb);
+FS_WRAP3(fs_fchown, (uv_file), (uv_uid_t)Int_val, (uv_gid_t)Int_val, handle_fs_cb);

+ 2 - 0
src/macro/eval/evalHash.ml

@@ -130,3 +130,5 @@ let key_sys_net_Mutex = hash "sys.thread.Mutex"
 let key_sys_net_Lock = hash "sys.thread.Lock"
 let key_sys_net_Tls = hash "sys.thread.Tls"
 let key_sys_net_Deque = hash "sys.thread.Deque"
+let key_eval_Uv = hash "eval.Uv"
+let key_eval_uv_Loop = hash "eval.uv.Loop"

+ 39 - 1
src/macro/eval/evalStdLib.ml

@@ -3065,6 +3065,36 @@ module StdUtf8 = struct
 	)
 end
 
+module StdUv = struct
+	open Uv
+
+	let loop_ref = ref None
+	let loop () = Option.get !loop_ref
+
+	module Loop = struct
+		let this vthis = match vthis with
+			| VInstance {ikind = IUv (UvLoop l)} -> l
+			| v -> unexpected_value v "UVLoop"
+	end
+
+	module FileSystem = struct
+		let exists = vfun1 (fun path ->
+			let s = decode_string path in
+			try
+				Uv.fs_access_sync (loop ()) s 0;
+				vtrue
+			with _ ->
+				vfalse
+		)
+	end
+
+	let init = vfun0 (fun () ->
+		loop_ref := Some (Uv.loop_init ());
+		(*encode_instance key_eval_uv_Loop ~kind:(IUv (UvLoop (Uv.loop_init ())))*)
+		vnull
+	)
+end
+
 let init_fields builtins path static_fields instance_fields =
 	let map (name,v) = (hash name,v) in
 	let path = path_hash path in
@@ -3660,4 +3690,12 @@ let init_standard_library builtins =
 	] [
 		"addChar",StdUtf8.addChar;
 		"toString",StdUtf8.toString;
-	]
+	];
+	init_fields builtins (["eval";"uv"],"Loop") [] [];
+	init_fields builtins (["eval";"uv"],"File") [] [];
+	init_fields builtins (["eval"],"Uv") [
+		"init",StdUv.init
+	] [];
+	init_fields builtins (["nusys"],"FileSystem") [
+		"exists",StdUv.FileSystem.exists
+	] [];

+ 6 - 0
src/macro/eval/evalValue.ml

@@ -93,6 +93,11 @@ type vprototype_kind =
 	| PInstance
 	| PObject
 
+type vuv_value =
+	| UvLoop of Uv.t_loop
+	| UvFile of Uv.t_file
+	| UvStat of Uv.t_stat
+
 type value =
 	| VNull
 	| VTrue
@@ -165,6 +170,7 @@ and vinstance_kind =
 	| ITypeDecl of Type.module_type
 	| ILazyType of (Type.tlazy ref) * (unit -> value)
 	| IRef of Obj.t
+	| IUv of vuv_value (* libuv internals *)
 	| INormal
 
 and vinstance = {