Can you compile JavaScript to ARM assembly? - javascript

Can you compile JavaScript to ARM assembly? I'm learning JavaScript right now and I want to know if there is a way to compile it to ARM assembly (to make a GBA/DS game) without writing something in another programming language.

Ahead-of-time compile, not that I know of. JS is a dynamic language that even supports constructs like eval() of a string, so a hypothetical compiler would still have to embed an interpreter or JIT runtime into the executable. It's also a managed language (sandboxed, and with garbage collection).
node.js will JIT to native machine code on the fly, for architectures the V8 engine supports. But I don't think this will help you make a GBA game, since you'd need to port node.js to the GBA, and have some native libraries to do machine-specific stuff I guess. A quick search finds some GBA emulators written in JS (to run under node.js); I think it's unlikely that you'd find the other thing, a port of node.js to run in a GBA.

Related

Creating a custom LLVM Frontned (VBS >> JS)

I have a project where large data in VBScript needs to be converted to JS, preferably through an automated system.
Have looked at "Script Converter", good but limited usability.
Found LLVM & Emscripten, excellent solution to my issue but the frontend is Clang (C/C++)
My questions are:
1) Is there a way I can go VBS >> C/C++ >> LLVM >>JS (probably not)
2) Or any ideas as to how I could make a custom frontend that uses VBS, (went through some articles, is using lex & yacc the only options? ie. making ur own compiler)
True, Emscripten can be used to translate C/C++ to JS via LLVM. However, I'm not sure how good a fit this will be for you. That's because C/C++ have a different programming model from JS, while the one of VBS is IMHO much closer to JS. So lowering from VBS to C++ and then going to JS sounds kind-of unnecessary.
In other words, I think that compiling VBS to JS is easier that compiling VBS to C/C++ that would be needed to leverage Emscripten/LLVM.
Now, how to compile VBS is a different question. Unless you find an existing solution, you'll need to implement a simple compiler. In your case it may suffice to create a compiler that only supports the subset of VBS your code actually uses as opposed to a fully general VBS frontend.

Is there such a thing as a javascript compiler?

I'm wondering if its possible to write a javascript program and have it compiled and linked into an executable?
If so would it be possible to create a libjs that would be the equivalent of libc for the c/c++ world? wouldn't creating something like this make javascript a full fledged language that could then be compiled and run directly on the target hardware?
If you had a compiler for javascript, couldn't you write a new compiler in javascript?
Yes, you could write a js compiler. Not sure how popular it would be:
js engines are very fast these days, so you're not gaining much speed.
It would be platform specific, or you would have to support multiple platforms. Not pleasant.
What would it be useful for? The great thing about an interpreted language is the very fact that it doesn't need to be compiled. It shortens development cycles and build times (ever sat in front of a C program and had to change a file that the entire project relies on and had to run and rerun makes that take minutes to compile everything?).
Regarding your last point, you're correct. Had you one of these compilers, you could indeed write another one in javascript.
Read this ... and do not miss the comments.
Here are also some options.
Yes you have something called Google Closure Compiler but its not a compiler in the conventional sense,it doesnt convert javascript into machine code but converts javascript into javascript but highly optimized javascript. Its actually an optimizing compiler.Also the compiler runs some tests to detect errors like typos much like the tool JSLint.But Google advises to use this compiler on javascript written in Closure Library. see this for more on Closure Compiler.
But i dont think compiling client-side javascript to machine code is a good idea because machine code is machine dependent so then before you send javascript to the client you have to detects its OS and its processor architecture. So this would become like javascipt for firefox on linux,javascipt for firefox on windows,javascipt for firefox on x86,etc

Practicality of compiling another language to Javascript?

Recently several tools have been released such as pyjamas and Scheme2js that allow one to take code in a language such as Python or Scheme and compile it to Javascript.
But how practical is such a tool?
I could see debugging being quite painful as you would have to debug the compiled javascript code itself, and correlate any bugs in that code with the corresponding lines in the original python/scheme/etc source code. Even with an intelligent stack trace such as the pyjamas -d option provides, this still seems tedious.
Also, libraries such as jQuery make writing Javascript much more fun and productive. But for many developers Javascript is still a new language to learn.
Has anyone worked with compiled Javascript in a production environment? Any recommendations or comments on the practicality of compiling to Javascript instead of writing your code directly in Javascript?
I believe GWT, based on Java, may be the most popular product of this kind, though I wouldn't describing it as "compiling Java to JS" but rather as "generating JS code". While I personally share some of your doubts, and would rather code JS directly, I have to admit that it is indeed an extremely practical as well as popular tool, entirely production-ready: I observe that, internally, many web apps that are rich and complex enough to warrant a front-end / back-end split are more and more often ending up as a Python back-end and a Java front-end -- the latter specifically to allow GWT (of course there are also plenty of Python front-ends, and plenty of Python back-ends, but I think this is a trend).
Google Wave uses GWT and is probably the most talked-about web app using it so far; together with the huge number of GWT-using web apps listed here, I think it establishes beyond any doubt that the approach is practical (as well as popular;-). Whether it's optimal (vs. writing actual javascript with a good framework in support) is a harder question to answer.
One of the more heavily used JavaScript compilers is GWT. This compiles Java to JavaScript, and is definitely used in production. The web interface to Google Wave is written in this system.
Also, Skydeck wrote Ocamljs, in order to make it easy for them to write FireFox extensions. That also worked quite well.
In summary, if you can write a good compiler, there is no showstoppers keeping you from writing a good JavaScript compiler.
Google Web Toolkit does it (Java to Javascript compiling), and GWT is used widely by Google (duh) and many others, so it definitely is practical.
Since the code is autogenerated, you debug problems in Java - assumption that the problem is in your code, and not in the compiler code, is true in 99% of all cases.
List of languages that compile to JS
As an another example Haxe could be mentioned. Haxe is a independent language and compiles to Flash 6-10, JavaScript, NekoVM and also to c++ - source code. Why is this practical?
you could use features the language itself could not offer
recompile code on multiple platforms (e.g.: form check in JavaScript and on server side)
there is a remoting package for communication between the platforms, and its genius.
autocompletion through the compiler
compile time type checks
If you are interested, you could start reading here.

How to turn the V8 compiled javascript into an EXE?

I know that google's v8 compiles javascript into native machine (binary if I understand correctly) code.
Is there a way to take the output and turn it into a exe?
I don't think you can directly turn a piece of JavaScript into an executable using V8, but you can probably make an application that bundles the V8 engine with the JavaScript and runs it as a stand-alone.
You can find all information about V8 on its project page.
Also note that JavaScript can't be completely compiled as it's a dynamic language. With V8, it's JIT-compiled (like .NET, for example.) It's still possible to turn it into a stand-alone executable though (like .NET, for example.)
If you want to develop stand-alone applications that make use of HTML for rendering, you could have a look at Adobe Air as well.
Javascript cannot be compiled just once. The language has eval which is pretty widely used. (for JSON for instance) You need to carry around the JIT, and the whole runtime.
JIT here is only an optimization, not the way to get rid of the compiler/interpreter.
Node.js embeds V8. This might be a good example to learn from.
There have been a few tries at making js into native code, it's not something that can be used in production by any means, more of an academic interest.
The Rhino interpreter for java has an option to make js into (java) bytecode so one approach is to convert to bytecode and then from bytecode to native with GCJ. There is some discussion about Rhino and GCJ but I don't know if anyone ever tried exactly that. https://groups.google.com/forum/#!msg/netscape.public.mozilla.jseng/c3tqyLZ19fw/8V4HeuMtIXUJ
Another approach is using Python, specifically Py-Py which itself is written in a non-standard subset of Python called rPython. rPython is not meant for human consumption but it has the benefit of being something which can be compiled to native. One interesting (albeit wacky) experiment was to compile Javascript to Python and then in some cases that Python happens to be valid as rPython and can be compiled down to native with the rPython compiler.
http://mozakai.blogspot.com/2010/07/experiments-with-static-javascript-as.html
If a .exe file is really important, I would bundle V8 with your app since even if you can compile js to native, you still need a full interpreter if you use any eval() or similar. It would not be hard to write a tool for bundling everything into an .exe file as long as your users don't mind either an 8MB exe or 8MB V8.dll file.
As a last thought, Big G has started allowing "native" apps based on chrome (google: "chrome packaged apps"). They have low level system access and can use the WebKit renderer allowing you to create your GUI in CSS and HTML and they have their own windows and icons so it is not obvious that they are running inside of chrome. This is probably still premature but it's something to keep an eye on in the desktop applications field.

Ruby bindings for a server-side javascript engine?

I'd like my rack application to be able to interface with a server-side javascript engine.
As of now the only way i know this could be possible, is by running JRuby and Rhino on the JVM, but I'm hoping for a leaner solution.
Has anyone heard of another, more rubyish perhaps, option ?
Edit : Reading the comments I'm starting to think I've been mistaken assumig that having both JRuby and Rhino run on the JVM would imply some interoperability between ruby and javascript...?
That's not a desirable solution for me anyhow, but still i'd like to clear that up.
The Ruby Racer is now out of pre-alpha and is hovering somewhere between alpha and beta. It now supports:
calling ruby code from javascript
calling javascript functions from ruby
embedding ruby objects into the javascript scope.
letting ruby objects be your javascript scope
Johnson is a RubyGem that turns the Mozilla SpiderMonkey JavaScript engine into an MRI C extension and allows very deep integration between Ruby and JavaScript,
there is a fork of Johnson which replaces the SpiderMonkey engine with the Mozilla TraceMonkey engine and
Lyndon is like Johnson but with JavaScriptCore instead of SpiderMonkey and MacRuby instead of MRI.
I think I also remember someone working on embedding V8 with MRI, but I can't find the reference right now.
The main problem with Johnson is that MRI is an incredibly crappy language implementation that leaks memory left and right, and the only language implementation in the world that could possibly be even more crappy is SpiderMonkey. Thus, the TODO list in the Johnson Git repository doesn't exactly inspire confidence; it only contains one item, and I quote literally:
Stop freaking segfaulting.
Lyndon is built on a much better foundation, but it obviously requires running OSX on the server. Plus, MacRuby hasn't been released yet.
I think JRuby+Rhino is probably the most stable option, although you will have to build the integration yourself: they are just two independent language implementations that happen to live on the same VM, but there is no integration between them.
A different take on the problem is RKelly which is a JavaScript parser and execution engine written in Ruby.
As an alternative you could try to approach the problem from a different direction: instead of keeping your application logic in JavaScript and running that both on the client and the server, you could keep your application logic in Ruby and run that on the server and the client: there are a couple of compilers out there that can compile (a subset of) Ruby to JavaScript. On of them is RubyJS. (There is also HotRuby, which is a YARV bytecode interpreter written in JavaScript, but that would very likely be tremendous overkill for what you are doing.)
And last but not least you could do what Rails originally did with their JavaScript helpers: you neither define your logic in Ruby nor JavaScript, instead you define it once in an internal Ruby DSL and generate both the Ruby and JavaScript logic from that.
Have a look at The Ruby Rhino. It uses jruby and rhino to embed javascript into your ruby environment. Among other things, it supports safe evaluation, calling ruby functions from javascript and vice-versa(javascript functions from ruby)
There is also "The Ruby Racer" which embeds v8 into MRI. This is still very much pre-alpha phase, but I hope to have a useable version sometime in march of next year
Another engine I know of is Snarl which also uses jruby and rhino to a similar effect.

Categories