Skip to content

Commit d79de32

Browse files
committed
Wasm runtime: implement ocaml_unix_findfirst / ocaml_unix_findnext
1 parent 7da5dd0 commit d79de32

File tree

1 file changed

+55
-6
lines changed

1 file changed

+55
-6
lines changed

runtime/wasm/unix.wat

Lines changed: 55 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,7 @@
480480
(call $caml_unix_error (pop externref) (ref.null eq))))
481481
(ref.i31 (i32.const 0)))
482482

483-
(func (export "unix_opendir") (export "caml_unix_opendir")
483+
(func $unix_opendir (export "unix_opendir") (export "caml_unix_opendir")
484484
(param $name (ref eq)) (result (ref eq))
485485
(try (result (ref eq))
486486
(do
@@ -492,8 +492,18 @@
492492
(call $caml_unix_error (pop externref) (ref.null eq))
493493
(ref.i31 (i32.const 0)))))
494494

495-
(func (export "unix_readdir") (export "caml_unix_readdir")
496-
(param $dir (ref eq)) (result (ref eq))
495+
(func $throw_ebadf (param $cmd (ref eq))
496+
(throw $ocaml_exception
497+
(array.new_fixed $block 5
498+
(ref.i31 (i32.const 0))
499+
(call $get_unix_error_exn)
500+
(ref.i31 (i32.const 3)) ;; EBADF
501+
(local.get $cmd)
502+
(global.get $no_arg))))
503+
504+
(data $readdir "readdir")
505+
506+
(func $readdir_helper (param $dir (ref eq)) (result (ref eq))
497507
(block $end
498508
(return
499509
(try (result (ref eq))
@@ -503,20 +513,59 @@
503513
(br_on_null $end
504514
(call $readdir (call $unwrap (local.get $dir)))))))
505515
(catch $javascript_exception
506-
(call $caml_unix_error (pop externref) (ref.null eq))
516+
(drop (pop externref))
517+
(call $throw_ebadf
518+
(array.new_data $string $readdir
519+
(i32.const 0) (i32.const 7)))
507520
(ref.i31 (i32.const 0))))))
508521
(call $caml_raise_end_of_file)
509522
(ref.i31 (i32.const 0)))
510523

511-
(func (export "unix_closedir") (export "caml_unix_closedir")
524+
(data $closedir "closedir")
525+
526+
(func $unix_closedir (export "unix_closedir") (export "caml_unix_closedir")
527+
(export "win_findclose") (export "caml_unix_findclose")
512528
(param $dir (ref eq)) (result (ref eq))
513529
(try
514530
(do
515531
(call $closedir (call $unwrap (local.get $dir))))
516532
(catch $javascript_exception
517-
(call $caml_unix_error (pop externref) (ref.null eq))))
533+
(drop (pop externref))
534+
(call $throw_ebadf
535+
(array.new_data $string $closedir (i32.const 0) (i32.const 8)))))
518536
(ref.i31 (i32.const 0)))
519537

538+
(func (export "unix_readdir") (export "caml_unix_readdir")
539+
(param $dir (ref eq)) (result (ref eq))
540+
(block $return (result (ref eq))
541+
(br_on_non_null $return (call $readdir_helper (local.get $dir)))
542+
(call $caml_raise_end_of_file)
543+
(ref.i31 (i32.const 0))))
544+
545+
(func $win_find_next (export "win_findnext") (export "caml_unix_findnext")
546+
(param $dir (ref eq)) (result (ref eq))
547+
(block $return (result (ref eq))
548+
(br_on_non_null $return (call $readdir_helper (local.get $dir)))
549+
(drop (call $unix_closedir (local.get $dir)))
550+
(call $caml_raise_end_of_file)
551+
(ref.i31 (i32.const 0))))
552+
553+
(func (export "win_findfirst") (export "caml_unix_findfirst")
554+
(param $vpath (ref eq)) (result (ref eq))
555+
(local $dir (ref eq)) (local $p (ref $string)) (local $p' (ref $string))
556+
(local $len i32)
557+
(local.set $p (ref.cast (ref $string) (local.get $vpath)))
558+
(local.set $len (i32.sub (array.len (local.get $p)) (i32.const 3)))
559+
(local.set $p' (array.new $string (i32.const 0) (local.get $len)))
560+
(array.copy $string $string
561+
(local.get $p') (i32.const 0)
562+
(local.get $p) (i32.const 0)
563+
(local.get $len))
564+
(local.set $dir (call $unix_opendir (local.get $p')))
565+
(array.new_fixed $block 3 (ref.i31 (i32.const 0))
566+
(call $win_find_next (local.get $dir))
567+
(local.get $dir)))
568+
520569
(data $rewinddir_not_implemented "rewinddir not implemented")
521570

522571
(func (export "unix_rewinddir") (export "caml_unix_rewinddir")

0 commit comments

Comments
 (0)