Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are there other languages than Objective-J that get "compiled" to JavaScript in the browser?

Objective-J is compiled/transformed into JavaScript directly on the browser. (This is contrast to doing this on the server, as GWT does for Java.) Has this approach been implemented for any language, other than Objective-J?

like image 952
avernet Avatar asked Oct 09 '10 01:10

avernet


People also ask

What language is JavaScript compiled in?

More modern browsers use a technology known as Just-In-Time (JIT) compilation, which compiles JavaScript to executable bytecode just as it is about to run. JavaScript is named after Java, and many ideas are borrowed from the Java language.

What programming languages can run in browser?

Every language for which an interpreter exists as a browser plugin: JVML bytecode (Java Applets), CIL bytecode (Silverlight), ActionScript bytecode (Flash), C, many others.

What interpreter does JavaScript use?

As we discussed earlier, JavaScript is interpreted by an interpreter named Ignition as well as compiled by a JIT optimizing compiler named TurboFan.


1 Answers

The CoffeeScript compiler compiles CoffeeScript into ECMAScript. Since the CoffeeScript compiler is itself written in CoffeeScript, it can compile itself to ECMAScript and thus run in the browser. The necessary bits and pieces to support <script type='text/coffeescript'> elements are already included in the standard CoffeeScript compiler.

In general, any language can be compiled to ECMAScript, all you need is a compiler. And, since any language can be compiled to ECMAScript, any compiler can be compiled to ECMAScript, all you need is a compiler for the language that compiler is written in.

This leads to a combinatorial explosion of possibilities for compiling languages within the browser.

For example, there is this guy who writes C compilers which target high-level languages for fun. He has a compiler that compiles C to Java, Perl, Common Lisp, Lua or ECMAScript. So, you can use that compiler to compile any other compiler written in C to ECMAScript. And most languages have some compiler somewhere which is written in C.

Clue is written in C. Clue compiles C to ECMAScript. Ergo, you can use Clue to compile Clue to ECMAScript. Then, you can run Clue in the browser to compile C to ECMAScript on the fly. <script type='text/c'>, anyone? (Fun thought: node.js is written in C. Hmm …)

On a more serious note: there are generally three reasons for compiling to ECMAScript:

  1. reuse
  2. safety
  3. expressivity

If you simply want to reuse existing code written in a different language (or existing knowlwedge in a different language), then compiling/interpreting on the client doesn't make much sense. The code or the coder doesn't expect to be able to use <script> elements anyway. This category includes stuff like GWT or Volta.

If (type-)safety is your goal, then compiling/interpreting on the client simply doesn't work: how can you guarantee safety if you don't control the compiler? That's why Ur/Web, Links, Flapjax, Haxe, Caja and such compile the code on the server. They guarantee safety either by static typing or tight integration or both. (By tight integration I mean that backend, frontend and app are tightly connected, by e.g. specifying data structures once and then generating the corresponding SQL, ECMAScript and HTML forms from that single source to make sure that they all match up. It should be obvious why this requires processing on the server.)

The ones that focus on expressivity, however, expect to be used as a replacement for ECMAScript, i.e. inside <script> elements, and thus they often come with interpreters and/or compilers which run on the client. CoffeeScript, Objective-J and Clamato fall in this category.

like image 97
Jörg W Mittag Avatar answered Oct 13 '22 18:10

Jörg W Mittag