#2827 How to build Fantom program to JavaScript and run it on a JS VM?

CretinHo Sat 1 May 2021

I use the F4 IDE. I marked my class with @Js. The generated .pod includes a helloWorld.js file and helloWorld.js.map file. I tried to feed helloWorld.js into Mozilla's Spidermonkey JS VM but it complained about sys.js being not found. In the fantom distribution directory there is no such thing as sys.js! I tried to open sys.pod to see if there is any sys.js there but these pod files seem to be not normal zip compressed archive as my helloWorld.pod file. Engrampa archive manager on Linux failed to open sys.pod because of unsupported format. It opened helloWorld.pod fine, though.

p/s: You could get the Spidermonkey JS Shell there:

https://ftp.mozilla.org/pub/firefox/releases/78.1.0esr/jsshell/

brian Sun 2 May 2021

I tried to open sys.pod to see if there is any sys.js there but these pod files seem to be not normal zip compressed archive as my helloWorld.pod file.

Actually sys.pod is a normal zip files (all pods are).

All JS files are always compiled into each pod as a single file. So in the case of the sys JS its inside sys.pod and called "sys.js". Or in concurrent is its in the root of the concurrent.pod zip as "concurrent.js"

Also take a look at the web::FilePack class which has a whole bunch of static utility methods to extract the files you need for JS runtime to script loading into browser, filesystem, etc.

Or maybe take a look at NodeRunner where we use Node.JS to run Fantom tests in JS

CretinHo Thu 13 May 2021

Hello Brian. Today I tried to run the JS generated by Fantom with the Ringo JS shell and it complained that it can't find the sys.js module.

https://ringojs.org/

After I put sys.js I extracted from sys.pod it said there is error in the sys.js module.

Could you try to see if you could get the JS generated by Fantom running well with Ringo JS Shell? And please give me a step by step tutorials of what I needed to do. Thanks.

brian Fri 14 May 2021

If you extracted the sys.js file, then what were you trying to run? And what exactly was your error?

Also, is there a reason you are trying to Fantom JS on a JavaVM when it already using the normal Java support is so much better?

CretinHo Sun 16 May 2021

I just tried with all JS engine I know. I will exclude RingoJS since then.

For example, my program test.fan compiled to test.js. When you run test.js, of course it will complain it can't find module sys.js. So I extracted sys.js from sys.pod and put it in the same dir as test.js. The JS Engine no longer complains about the absent of sys.js but instead says sys.js has errors in it so it refused to load sys.js and run test.js.

Hope my description helps.

brian Sun 16 May 2021

The simplest way to play with JS would be one of the following:

To run tests, use the built-in Node.JS test harness via fant. If you have Node installed, then you run tests in JavaScript like this:

fant <yourpod> -js 

Or to play with JS in the browser, probably the best program to run is the testDomkit which will let you hit "http://localhost:8080/" with your browser and play around with the Fantom domkit UI:

fan testDomkit

SlimerDude Mon 17 May 2021

I've not tried this, so I don't know for certain, but the home page of RingoJS says JS is loaded via CommonJS modules - which may be the cause of your errors.

Fantom JS files are just plain JS with no loader preamble and must be loaded in the correct order to satisfy dependencies.

As Brian said, NodeJS and fant is the preferred means to run Fantom JS on a server.

andy Mon 17 May 2021

You used to be able to run arbitrary code under JS using:

fan compilerJs::Runner <pod>[::<test>[.<method>]]

And we would handle all the dependencies for you. But looks like we didn't carry that over to NodeJs when we moved off Rhino. We can take a look at fixing that — and maybe also adding a nice bin/ script)

But in the meantime you can poke around the *Runner code to see how it works:

https://github.com/fantom-lang/fantom/tree/master/src/compilerJs/fan/runner

09q5jfqc Wed 19 May 2021

I interpret the idea of the OP a bit different. He seems wanted to use existing JS Engines (SpiderMonkey, CharkraCore) as a foundation to build up to a standalone Fantom without the JVM, by utilizing the existing ability of Fantom to compile to JS. It's somewhat like asm.js. Fantom compiles to optimized JS and run on top of the JS Engine, no longer depends on the JVM like now. Curently despite being able to compile to various targets, Fantom itself still relying on the JVM to run.

I have a better idea I presented with the Fantom developers. Using the Eclipse OMR framework to develop a standalone Fantom interpreter. You do not need to go the complicated route of building on top of a JS Engine like this.

CretinHo Mon 14 Jun 2021

No. I just want to run Fantom code on another VM that not JVM, this case is a JS Engine.

CretinHo Wed 16 Jun 2021

@brian: On Thunar File Manager Fantom's .pod file being considered as Perl source code (Perl script). There is no option to open it as an archive from the file manager. If I open from Engrampa Archive Manager it will explicitly say Archive type is not supported!

Surprisingly enough, it seems only sys.pod is troubled as I could open build.pod from Engrampa without any problems. It's being displayed as normal zip archive!

CretinHo Wed 16 Jun 2021

It seems there is something wrong with sys.pod as Engrampa can't open it but running unzip sys.pod from the terminal has no problems extracting it. So it must be a valid zip archive. So the pod files generated by Fantom has something standard non-compliant in them!

CretinHo Wed 16 Jun 2021

Here is why Thunar said pod files being Perl script:

https://perldoc.perl.org/perlpod

Login or Signup to reply.