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
Related
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.
I'm trying to better understand dart's effect on performance. On the dart website, their benchmarks show that Dart code compiled to Javascript is faster than just Javascript. How is this possible?
I understand how the Dart VM is faster than v8, but what I don't get is how dart2js-generated javascript is faster than plain old javascript, when both are running in the same environment, v8.
dart2js is able to perform optimizations that would not normally be manually added in JavaScript code.
There is nothing particularly special about Dart being the source language in this case: any automated tool that generates JavaScript should be able to do this, for instance the GWT compiler (Java to JavaScript) does this as well. Of course, you can run automated tools on JavaScript source to generate better JavaScript as well, this is what the Closure compiler does.
Technically, you can manually achieve the same speed with handwritten JavaScript if you know all the tricks.
One example is function inlining. If you need a code fragment called repeatedly you would refactor it out in a function/method. Dart2js often does the opposite. Method calls often get replaced with the code fragment contained by the called function/method which is called inlining. If you would do this manually this would lead to unmaintainable code.
I think many of the optimizations go in that direction.
Writing the code that way would just be unreadable and thus unmaintainable.
This doesn't mean it's sloppy.
There is a great presentation by Vyacheslav Egorov from the dart team where he explains in detail some of the optimizations including in lining..
http://www.infoq.com/presentations/dart-compiler
Summary Vyacheslav Egorov details how some of Dart's language features affected the design of a new JIT Dart compiler and how the V8
JavaScript engine influenced the overall design.
There is an interesting video by Seth Ladd and Kasper Lund.
Kasper is involved in creating the Dart2js compiler and gives some code examples on how the compiler optimizes the Javascript code.
Are there any JavaScript (ECMAScript) implementations written in pure Python? It is okay even if its implementation is very slow.
Doesn't seem to be under active development anymore but you could check out pynarcissus, http://code.google.com/p/pynarcissus/source/browse/trunk/jsparser.py
Seems like a binding to V8 (JavaScript interpreter in Google Chromium) is available also, http://www.advogato.org/article/985.html
There is one, of an unknown level of completeness, written in RPython (a subset of Python, that is to say, it runs as normal Python): https://bitbucket.org/pypy/lang-js/overview
You may want to take a look at pydermonkey or python-spidermonkey, both of which, I believe, are python implementations of the Mozilla javascript interpreter.
I would recommend that you just stick to node.js on your local development box, translate your CoffeeScript files over to JavaScript, and deploy the translated scripts with your apps.
I get that you want to avoid having node.js on your servers, that's all fair and good. Jumping through hoops with Python invoking JavaScript to translate CoffeeScript seems more hassle to me than it's worth.
I created Jispy to embed JS in Python.
From the docs:
A JavaScript Interpreter In Python
Jispy is an interpreter for a strict subset of JavaScript, fondly called LittleJ (LJ). It employs recursive descent for parsing and is very easily extendable.
Built for embedding JavaScript
Jispy's original vision was to seamlessly allow embedding JavaScript programs in Python projects. By default, it doesn't expose the host's file system or any other sensitive element. Some checks on infinite looping and infinite recursion are provided to tackle possibly malicious code.
It comes with an interactive console, so you can get up and running in no time.
Hope this helps.
Have you heard of PyV8? It's a Python wrapper of Google's V8 JavaScript engine. It may be what you're looking for.
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.
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.