Hip-Hop PHP for Node.js? - javascript

Is there any way to achieve compiling Node.js scripts as native code, like Hip-Hop does for PHP?
I'm not talking about libraries/apis.

Node.js uses Google's V8 javascript engine which does compile all the javascript to native code.

You don't really need to pre-compile on your own. IIRC, in node, V8 compiles your js before it even runs a single line. So, where with PHP you're code is constantly being interpreted (unless you have some opcode caching in place), with node, you're code is compiled once when you start your application, and (if you're running a server) users that connect will invoke that same code that was already compiled when the server started.

No, You cannot compile Node.js into native code. But you can write parts of your application in c++ using v8. But beware, the transition time of changing from js to c++ world is big.
You can also try to get the intermediate compilation from v8. But I haven't seen anybody doing that.

Related

How to create a node.js Native Image with GraalVM

I am trying to create a native image from a simple node.js example.js application.
When running the application with:
node --native -i --native example.js
then the application is starting and working as expected.
Now I would like to create a native image. I tried the following command:
native-image --language:js example.js
however it is not working, because of the error:
Build on Server(pid: 77626, port: 64052)
[example.js:77626] classlist: 3,964.04 ms
error: Main entry point class 'example.js' not found.
Error: Processing image build request failed
As a resolution I created a main entry point in example.js such as:
function main(args) {
console.log("Main app started")
}
however this doesn't work.
Is there a way usually native images are create for js/node.js applications?
No. Currently, as of Dec 2018, creating GraalVM native images for node.js applications is not possible. The native image utility takes Java bytecode and compiles it ahead of time. GraalVM JavaScript engine is a Java program and can be compiled as a native image. This is what you actually run when you execute $GRAALVM_HOME/bin/js. It still loads JavaScript at runtime, interprets it and compiles it just-in-time to machine code.
GraalVM's node implementation is a normal node, a native application, with the JavaScript engine replaced by the GraalVM's one.
The GraalVM team is experimenting with possible ways to save precompiled parts of the JavaScript programs, maybe a standard library, or parts of your application, but when this can become available and in which form is unclear.
AFAICT there are two sides to JS support in GraalVM: "vanilla" JS support and nodejs support.
Oleg's answer here https://stackoverflow.com/a/53749052/202168 explains clearly why building a native image for a nodejs app is not possible.
However it appears that it is possible to build a native image that runs "vanilla" JS:
https://www.graalvm.org/examples/native-image-examples/#polyglot-capabilities
You can also experiment with a more sophisticated ExtListDir example, which takes advantage of GraalVM’s Java and JavaScript polyglot capabilities.
The code for that example is found at:
https://github.com/graalvm/graalvm-demos/blob/master/native-list-dir/ExtListDir.java
In this case there is a Java 'wrapper' for the JS app. I believe this is a necessary part - in other words the GraalVM does not "compile JavaScript to native" but rather it compiles a Java app which includes a Javascript engine implemented in Java.
Maybe this is what Oleg means by "The GraalVM team is experimenting with possible ways to save precompiled parts of the JavaScript programs" i.e. to have some standard Java boilerplate for running JS apps, so you could directly compile a pure JS app to native image. I guess this would effectively be something like Node or Deno but implemented in Java.
The other side of the problem is if you want to use NodeJS features or include code from npm modules in your JS app.
GraalVM states that the Javascript engine implements ES11, basically all of modern JS:
https://www.graalvm.org/reference-manual/js/JavaScriptCompatibility/
It seems like it might be possible to use JS tooling like http://browserify.org/ to convert nodejs JS code into "vanilla" JS that could be run by Graal's JS engine.
Then with appropriate Java wrapper code you could compile all that into a native image.

How to compile Javascript console application locally?

I am a beginner in Javascript, I decided to practice Javascript by problem solving using it, I found an online judge that accepts Javascript V8 4.8.0 code.
So, I searched online to get that version of Javascript V8 on my machine, but I couldn't find any easy way, All the pages were explaining how to build it, and it seems to be a process that I don't need to go through.
Is there an easy way to compile and run command line apps written in Javascript on my machine?
Note: I don't want to use node.js because I tried using it's I/O and
as a beginner I think it is complex in some way.
Update: I found that package manager pbox.me which provides a version of V8 JavaScript Engine and I managed to install it.
Yet another problem appeared: whenever I try to run a js file writing d8 myfile.js in command line nothing happens as if it is an empty program, knowing that I tryied to d8.exe file and it is working, and I made sure the PATH is inserted in the environment variables.
What am I doing wrong?
The easiest way to get started with JavaScript is probably to use it in a browser. You can type simple things directly into the browser's JavaScript console (check the menu); or you can embed your code in a simple HTML document.
If you want, you can even pretty easily implement the readline()/print() functions, so you can pretend to be doing stdin/stdout based I/O: just read from an array of strings, and send output to console.log (or create DOM nodes if you want to be fancy and/or learn how to generate dynamic website content by hand).
Side note: V8 4.8 is severely outdated, don't use it to execute code you haven't written yourself.

How can I get a JavaScript v8 console for offline usage?

I am practicing online programming contest held by codeforces, and I try to play javascript with it. However, I find it very difficult to setup my environment for offline test, they have readline() for input to stdio and print()/write() for output in stdout. Any help? Thanks.
For practising Javascript, you could use any thing from the chrome development console to online editors like JSFiddle. Nodejs allows you to run Javascript (which is used for client side rendering) on a server. What this means is that using node, you could run javascript on your terminal. As for the input , output functions you mentioned, it is easy to require them as node modules or use appropriate Javascript libraries for the same.

Are node.js and V8 builtins written in JS or compiled and run as native code?

In node.js and I assume by extension V8, are built-ins (such as JSON, Math, etc) native code residing in the interpreter or are they javascript code that gets interpreted the same way as the rest of user-written code?
So for example, does a call to JSON.stringify(my_data) jump to compiled machine code or does it just run more JS code?
This appears to be responsible for JSON stringification:
https://github.com/joyent/node/blob/master/deps/v8/src/json-stringifier.h
Quickly browsing the repo will give you an idea of whats written in native code (and what's not):
https://github.com/joyent/node/tree/master/deps/v8/src
Glancing at the V8 source code, I think it's a mix of both. JSON is backed by native code in json-parser.h; (at least some of) math is implemented as JavaScript in math.js.

Running JQuery with CoffeeScript

How can I properly use jQuery and CoffeeScript? All of the examples that I have seen thus far compile the CofeeScript at runtime in the browser; this is not ideal. Normally, I'd simply write in plain old JavaScript, but I think CoffeeScript can allow me do get more done with less code, once I know how to get started. I've worked with JQuery before, but I've not used CoffeeScript. I'm not sure where to get started? Should I place $(document).ready in my external CofeeScript/Javascript?
Just have to put the jquery code after $ ->
Here is a small article about it, and if you are starting The Little Book on CoffeeScript is quite useful, it's very clear and goes from scratch
All of the examples that I have seen thus far compile the CoffeeScript at runtime in the browser; this is not ideal.
Agreed. You should look at projects like The Middleman that let you transparently compile your CoffeeScript to JavaScript on a local server for development, then bundle up the minified JS for deployment. (The Middleman also includes support for Haml and Sass, if you're into those, but you can just use HTML and CSS as well.)
The big advantage of The Middleman (or Rails, or any other web framework with CoffeeScript support) over just running coffee -cw is that the latest version of your compiled CoffeeScript is served every time you refresh the page; you never have to worry about waiting for background compilation to finish.

Categories

Resources