-
-
Notifications
You must be signed in to change notification settings - Fork 17
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
Stack overflow in js_of_ocaml #26
Comments
As far as I know, CPS is not optimized into tailcalls by js_of_ocaml. |
@hhugo Is there something that could be done on jsoo's side? Or is the only solution to implement trampolining in markup itself? |
Hello, is there anything new about this problem today ? Do you know any alternative for xml or http parsing that would work with jsoo ? I can confirm the issue still persist with markup 1.0.3 and js_of_ocaml 5.8.1 In my case, the stack overflow occurs even with html string as simple as: let example_html = {|
<!DOCTYPE html>
<html lang="en">
<body>
<script type="text/javascript" src="example.js"></script>
</body>
</html>
|}
let _ = Soup.parse example_html |
Hi, no progress as of now. Could you give your complete workflow, if it's not too much work to simplify, so I can readily observe the problem? Some sample program, its Dune files, commands you are running? Is this in Node or a browser? For parsing XML you could try Xmlm. For HTML there is a very non-standards-compliant HTML parser in ocamlnet, but I doubt ocamlnet itself is compatible with js_of_ocaml, though I recall previously extracting the parser from that library. You might also be able to get by by treating well-enough-formed HTML as if it were XML and using an XML parser. But otherwise, it might be best to adapt Markup.ml to be useful in js_of_ocaml. |
@aantron, just in case it's useful, here is how I adapted jsonm for jsoo : dbuenzli/jsonm#20 |
dune
main.ml
commands:
Adding Note that using the release profile works in that specific example, most probably because jsoo is able to inline more (e.g. cross module inlining)
|
In fact the code is a http parser that fetch the code of a website and extract all links to display them in a simple cli. So basically I use lambdasoup to look for all "" balise, extract their "href" attribute and the rendered text (because it can contains a lot of useless div and I'm only insterested in the rendered visible text). Following is the failing snippet : (* s is the string response from cohttp-lwt-jsoo *)
fun s ->
Soup.parse s $? "div[role=main]" |>> fun div ->
(* Get all links of the current div *)
div $$ "a" |> Soup.to_list |> fun l ->
`Ok
(* Convert all links and keep only non "None" results *)
(List.fold_left
(fun acc x ->
match
do_conversion (Soup.attribute "href" x) (Soup.trimmed_texts x) with
| None -> acc
| Some y -> y :: acc)
[] l)) As I'm fetching website content, the target platform is nodejs (because in web browser it would trigger cors policy). To be honest, it's not a big deal if I cannot use js_of_ocaml as it's was not even originally planned for this project. It's just a little disappointing because I was expecting js_of_ocaml to be an easy way to distribute my program for windows users (for example) without extra work. I've already done some tests so it's really this snippet that cause the all program to fail, if I replace it with a fake returned value, the program compiles and works. |
Not sure if there's interest for upstreaming this functionality, but I made markup work in javascript in this branch: master...v-gb:markup.ml:js. It uses trampolines, which is a fairly lightweight change. I had tried compiling with the jsoo flag that enables cps transformation, but (from memory) it made the code much slower to compile, much slower to run, much bigger, and didn't execute correctly when enabling optimizations. So I much prefer the trampoline version. It's not very fast, but I think that's due to markup's one-character-at-a-time approach, rather than the trampoline overhead. |
Reported by @Armael in #22 (comment):
and confirmed in a subsequent comment.
The text was updated successfully, but these errors were encountered: