js_of_ocaml icon indicating copy to clipboard operation
js_of_ocaml copied to clipboard

WIP: target JScript

Open ghuysmans opened this issue 3 years ago • 7 comments

I'm trying to run OCaml programs on Windows through JScript (an old implementation of ECMAScript 3). It's an easy and portable way to access COM (actually ActiveX) objects. A generator for wrappers using type libraries is underway.

It mostly works but I had to debug strange issues related to typed arrays (separate compilation seems to break weak symbols resolution) and a curious interpretation of try/catch in JScript, as reported in this document (§2.11). The latter didn't always trigger since it's related to the way scoping works: as the generated variable names change even slightly, the bug might disappear. I wonder if I solved these issues (especially the latter) correctly. Any thoughts?

ghuysmans avatar Aug 25 '22 20:08 ghuysmans

How about "@babel/plugin-transform-jscript" for your use case?

smorimoto avatar Aug 25 '22 20:08 smorimoto

Hi, thanks for your answer!

How about "@babel/plugin-transform-jscript" for your use case?

It doesn't look like they handle anything else than named function expressions (which I don't support yet, thank you for reminding me!), so that won't work. For example, exception variables (after the catch keyword) are accessible to the whole enclosing function. Moreover, the real ES3 doesn't even support redefining array accessors.

ghuysmans avatar Aug 25 '22 20:08 ghuysmans

Oops, I thought it was a little more decent... https://github.com/babel/babel/blob/main/packages/babel-plugin-transform-jscript/src/index.ts

smorimoto avatar Aug 25 '22 21:08 smorimoto

Oops, I thought it was a little more decent... https://github.com/babel/babel/blob/main/packages/babel-plugin-transform-jscript/src/index.ts

Yeah, right... Besides, it would be safer (and more pleasant) to write those transformations using the existing mapper infrastructure in jsoo.

ghuysmans avatar Aug 25 '22 21:08 ghuysmans

I ended up hitting a recursion limit, and there's no way to configure it. So I guess that wouldn't ever run reliably.

ghuysmans avatar Sep 02 '22 15:09 ghuysmans

I ended up hitting a recursion limit, and there's no way to configure it. So I guess that wouldn't ever run reliably.

Can you show the exact error ? Do you have a stack trace ? Do you know what code triggers it ?

hhugo avatar Sep 03 '22 07:09 hhugo

Just to be clear, cscript (the launcher for scripts running in the console) crashes with a stack overflow, not jsoo! I tried compiling code generated by ppx_rapper, which uses a monadic operator; I still have the code in a separate (currently private) repository.

ghuysmans avatar Sep 04 '22 13:09 ghuysmans

@ghuysmans, do you plan to do anything with this PR ? Using --enable effects might solve your stackoverflow issue.

hhugo avatar Jan 13 '23 00:01 hhugo

It mostly works but I had to debug strange issues related to typed arrays (separate compilation seems to break weak symbols resolution) and a curious interpretation of try/catch in JScript, as reported in this document (§2.11). The latter didn't always trigger since it's related to the way scoping works: as the generated variable names change even slightly, the bug might disappear. I wonder if I solved these issues (especially the latter) correctly. Any thoughts?

Maybe https://github.com/ocsigen/js_of_ocaml/commit/731146444ea41ad9a187d0c4be9a72a8789f15d0 fixes the scoping issue...

hhugo avatar Jan 26 '23 09:01 hhugo

@ghuysmans, do you plan to do anything with this PR ? Using --enable effects might solve your stackoverflow issue.

I'll have a try, this looks pretty exciting but I'm too busy right now...

ghuysmans avatar Jan 26 '23 20:01 ghuysmans

js_of_ocaml is currently going the other direction of modernizing its js output. We can certainly make some small changes if it helps your use case but we won't be able maintain compatibility with es3 or JScript.

Maybe looking in the direction of https://babeljs.io/docs/babel-plugin-transform-jscript could help.

I'll close this PR as I don't see what to make of it.

hhugo avatar Dec 02 '23 06:12 hhugo