Skip to content
This repository was archived by the owner on Jul 19, 2019. It is now read-only.

Generate bindings code using a small syntax #4

Open
ngzhian opened this issue Dec 25, 2017 · 0 comments
Open

Generate bindings code using a small syntax #4

ngzhian opened this issue Dec 25, 2017 · 0 comments

Comments

@ngzhian
Copy link

ngzhian commented Dec 25, 2017

Hi! I had an idea of generating bindings automatically:

  1. Crawl website to generate some sort of simplified AST or a data format like YAML
  2. A script to read in AST/data format to generate code

I wrote some code to do something like 2. pretty easily, you declare the bindings like so.

let auth_module =
                CModule("identity",
                  [CObject("AccountInfo", [("id", "string")])],
                  [CMethod("getAuthToken",
                           [("details", CObject(
                                "getAuthTokenOptions", [
                                  ("interactive", "Js.boolean");
                                  ("account", "accountInfo");
                                  ("scopes", "string list")
                                ]));
                            ("callback", CFn("string", "'a"
                              ))  
                           ])], []) in

Which generates

module Identity = struct
  class type _accountInfo = object
    method id : string
  end [@bs]
  type accountInfo = _accountInfo Js.t
  external mkAccountInfo : ?id:string -> unit -> accountInfo = "" [@@bs.obj]

  class type _getAuthTokenDetails = object
    method interactive : Js.boolean
    method account : accountInfo
    method scopes : string list
  end [@bs]
  type getAuthTokenDetails = _getAuthTokenDetails Js.t
  external mkGetAuthTokenDetails : ?interactive:Js.boolean -> ?account:accountInfo -> ?scopes:string list -> unit -> getAuthTokenDetails = "" [@@bs.obj]

  external getAuthToken : getAuthTokenDetails -> (string -> 'a) -> unit = "getAuthToken" [@@bs.scope "chrome", "identity"] [@@bs.val]

end

So you save some time writing all that boiler place.

Essentially the AST is simple, there are 3 types of chrome level exports, module, type, method; and there are 4 constructors to represent types (object, boolean, string, function/callback).

Right now the codegen is appending strings together (because this is hacked together, and also when I did some research on ocaml code generation projects one example i found used string concatenation). I was thinking it would be nice if we could directly generate OCaml AST, but this was easier to get up and running :)

I uploaded the code generation script on my fork of master, so if you'ld like, you can invoke it like so: ocamlc script.ml && ./a.out. I have an simple example module defined in the script itself just as a test.

Posting this here to see if there is any interest, I think with the script we can generate more 80% of the bindings to Chrome quite easily.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant