-
Notifications
You must be signed in to change notification settings - Fork 470
Future work: Introducing a dsl for FFI instead of abusing attributes #3618
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Fable does something similar with its Some examples of what this could look like: external%bs sum : int -> int -> int = "sum($0, $1)"
let x = sum 1 2 (* Generates: var x = sum(1, 2) *)
(* Instead of [@bs.as {json|...|json}] *)
external%bs cloneNodeDeep : node -> node = "$0.cloneNode(true)"
external%bs attachShadowOpen : node -> node = "$0.attachShadow({ mode: 'open' })"
(* Generalized [@@bs.scope] *)
external%bs locationHref : document -> string = "$0.location.href"
(* Instead of [@@bs.set] *)
external%bs setTitle : document -> string -> unit = "$0.title = $1"
(* Instead of [@@bs.splice] / [@@bs.variadic] *)
external%bs add : collection -> string array -> unit = "$0.add($1...)"
(* Instead of [@@bs.new] *)
external%bs makeRegex : string -> regex = "new RegExp($0)" |
Indeed, this looks more uniform, but it makes code analyzer more difficult. open Fable.Core
[<Emit("$0 - haha($1 + $2)")>]
let add (x: int) (y: string): float = jsNative
let result = add 1 "2" generated code export const result = 1 - haha("2" + null); We could do some analysis, we will see how much effort it needs |
It does check both the JavaScript and template syntax (there's a bit more to it than just placeholders), but does not check the placeholders against the type signature it seems. Fable is built on Babel which as I understand includes a JS parser, so that part is probably trivial for them to implement. I don't think it's necessary to support the entire JS syntax either, even just to cover the existing FFI surface. The above examples are pretty simple syntactically, especially if arguments are restricted to JSON, which of course you already have a parser for. Hygienic macros with full support for the entirety of JavaScript's syntax would be pretty neat though. |
Idris 1 does something similar too, it's really convenient |
@bobzhang is this feature being considered in the current "official" roadmap? Or rather something that contributors could help with? This addition would make BuckleScript way more accessible and expressive for JavaScript developers. ❤️ A more specific question, there seems to be a Flow parser vendored already. (js_of_ocaml also has its own parser as well, based on Menhir). Is the idea to use the vendored Flow parser for the JS snippets in these expressions? |
yeah, it is the road map(after we finish the data representation changes).
The flow parser is more tested (used more).
…On Wed, May 6, 2020 at 5:21 AM Javier Chávarri ***@***.***> wrote:
@bobzhang <https://github.com/bobzhang> is this feature being considered
in the current "official" roadmap? Or rather something that contributors
could help with? This addition would make BuckleScript way more accessible
and expressive for JavaScript developers. ❤️
A more specific question, there seems to be a Flow parser vendored already
<https://github.com/bucklescript/bucklescript/blob/7015ce12d629fd9b6385045e5b486f54a941d956/jscomp/js_parser/flow_ast.ml#L9-L12>.
(js_of_ocaml also has its own parser
<https://github.com/ocsigen/js_of_ocaml/blob/8263a4cb60bcb6944a71e73626b152fb4f3d1622/compiler/lib/js_parser.mly>
as well, based on Menhir).
Is the idea to use the vendored Flow parser for the JS snippets in these
expressions?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#3618 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAFWMK7P6K6SCNTS5API34TRQB7MNANCNFSM4HZRJTHQ>
.
--
Regards
-- Hongbo Zhang
|
I am thinking we can just use the most intuitive way of writing FFIs external add : int -> int -> int = "function(x,y){
return x + y
} Whether inline external or not is up to the compiler, for externals which are hard to inline, we can materialize it: open struct
external add : ..
end
: sig
val add : int -> int -> int
end Compared with external add : int -> int -> int = {|
var {fancyAdd} = require('third-party')
function(x,y){
return fancyAdd(x, y)
}
|} |
My 2c, I kind of like the existing Bucklescript FFI over this: external add : int -> int -> int = {|
var {fancyAdd} = require('third-party')
function(x,y){
return fancyAdd(x, y)
}
|} Purescript does something like this, and the FFI turned out to be a much bigger pain point than Bucklescript because of this kind of thing. When I switched to Bucklescript, writing bindings became much less of a burden. It takes longer to write bindings and it's easier to screw something up in the inlined js code, which ends up eating up time debugging things. I originally thought the proposal here would be more Idris-like, sort of like this: (* Keep bs.module so that bucklescript can output multiple module systems like commonjs
* and es6 without it being hardcoded into the app code *)
external add : int -> int -> int = "fancyAdd($0, $1)" [@@bs.module "third-party"] external add : int -> int -> int = "$0 + $1" And from @glennsl 's comments, something like: (* Generalized [@@bs.scope] *)
external locationHref : document -> string = "$0.location.href"
(* Instead of [@@bs.set] *)
external setTitle : document -> string -> unit = "$0.title = $1" |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
Uh oh!
There was an error while loading. Please reload this page.
basic
similar projects: https://github.com/fdopen/ppx_cstubs
The text was updated successfully, but these errors were encountered: