caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: "Mauricio Fernández" <mfp@acm.org>
To: Helmut Brandl <helmut.brandl@gmx.net>, caml-list@inria.fr
Subject: Re: [Caml-list] js_of_ocaml with node
Date: Thu, 30 Apr 2015 00:49:02 +0200	[thread overview]
Message-ID: <20150429224902.GA8181@AIKA> (raw)
In-Reply-To: <55415529.2010802@gmx.net>

On Wed, Apr 29, 2015 at 05:03:21PM -0500, Helmut Brandl wrote:
> Thanks to all for the hints.
> 
> I think I have to dig into the documentation of js_of_ocaml to
> resolve the problem.

I've got some bindings for nodejs stuff including FS operations, sockets,
events, processes, etc.; will try to publish it tomorrow. Note that some
nodejs functions require variadic callbacks as supported by the patches
available at https://github.com/ocsigen/js_of_ocaml/issues/273

The hand-written bindings look like this (just a sneak peek):

node_require.eliom:

    {client{
    let require x =
      let require = Js.Unsafe.eval_string "require" in
        require##call(require, Js.string x)
    }}

    (* vim: set ft=ocaml: *)


node_fs.eliom:


    {client{
    open Printf
    open Lwt

    class type error =
    object
      method message : Js.js_string Js.t Js.prop
    end

    type maybe_error = error Js.t Js.opt

    class type stats =
    object
      method dev     : int Js.readonly_prop
      method ino     : int Js.readonly_prop
      method mode    : int Js.readonly_prop
      method nlink   : int Js.readonly_prop
      method uid     : int Js.readonly_prop
      method gid     : int Js.readonly_prop
      method rdev    : int Js.readonly_prop
      method size    : float Js.t Js.readonly_prop
      method blksize : int Js.readonly_prop
      method blocks  : int Js.readonly_prop
      method atime   : Js.date Js.t Js.readonly_prop
      method mtime   : Js.date Js.t Js.readonly_prop
      method ctime   : Js.date Js.t Js.readonly_prop
    end

    and watch_options =
    object
      method persistent : bool Js.t Js.prop
    end

    class type fs =
    object
      method existsSync : Js.js_string Js.t -> bool Js.t Js.meth
      method exists : Js.js_string Js.t -> (bool Js.t -> unit) Js.callback -> unit Js.meth

      method readFile :
        Js.js_string Js.t ->
        (maybe_error -> Node_net.buffer Js.t -> unit) Js.callback -> unit Js.meth

      method readFileSync : Js.js_string Js.t -> Node_net.buffer Js.t Js.meth

      method readdirSync : Js.js_string Js.t -> Js.js_string Js.t Js.js_array Js.t Js.meth

      method statSync : Js.js_string Js.t -> stats Js.t Js.opt Js.meth
      method lstatSync : Js.js_string Js.t -> stats Js.t Js.opt Js.meth

      method mkdir :
        Js.js_string Js.t -> int -> (maybe_error -> unit) Js.callback -> unit Js.meth
      method mkdirSync : Js.js_string Js.t -> int -> unit Js.meth

      method unlink : Js.js_string Js.t -> (maybe_error -> unit) Js.callback -> unit Js.meth
      method unlinkSync : Js.js_string Js.t -> unit Js.meth

      method watch : 'a. Js.js_string Js.t -> (#watch_options as 'a) Js.t ->
                     (Js.js_string -> Js.js_string -> unit) Js.callback -> unit Js.meth
    end

    let fs : fs Js.t = Node_require.require "fs"


    let err_callback1 u err =
      match Js.Opt.to_option err with
          None -> Lwt.wakeup_later u ()
        | Some err -> Lwt.wakeup_later_exn u (Failure (Js.to_string err##message))

    let bool_callback1 u b = Lwt.wakeup_later u (Js.to_bool b)

    let wrap_async1 callback f =
      let t, u = Lwt.wait () in
        f (Js.wrap_callback (callback u));
        t

    let exists s = wrap_async1 bool_callback1 (fun cb -> fs##exists(Js.string s, cb))
    let unlink s = wrap_async1 err_callback1 (fun cb -> fs##unlink(Js.string s, cb))

    let read_file fname =
      let t, u = Lwt.wait () in

      let on_read err data =
        if Js.Opt.test err then
          Lwt.wakeup_later_exn u (Failure (sprintf "could not read %S" fname))
        else Lwt.wakeup_later u (Js.to_string data##toString())
      in
        fs##readFile(Js.string fname, Js.wrap_callback on_read);
        t
    }}
    (* vim: set ft=ocaml: *)

-- 
Mauricio Fernández

      reply	other threads:[~2015-04-29 22:49 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-04-27 20:32 Helmut Brandl
2015-04-27 21:02 ` Sébastien Dailly
2015-04-27 21:16   ` Helmut Brandl
2015-04-28  7:08     ` Sébastien Dailly
2015-04-27 21:11 ` Daniel Bünzli
2015-04-27 22:03 ` Alain Frisch
2015-04-27 22:41   ` Daniel Bünzli
2015-04-28  7:53     ` Alain Frisch
2015-04-28  8:11       ` Alain Frisch
2015-04-28 11:17       ` Drup
2015-04-29 22:03         ` Helmut Brandl
2015-04-29 22:49           ` Mauricio Fernández [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20150429224902.GA8181@AIKA \
    --to=mfp@acm.org \
    --cc=caml-list@inria.fr \
    --cc=helmut.brandl@gmx.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).