DEV Community

brsnumget
brsnumget

Posted on

if javascript only needs the browser to compile why can't other languages do it

because moving away from the native compiler would be a good step for software development. would be a good improvement to switch from physical machines completely to the internet

Top comments (10)

Collapse
 
phlash profile image
Phil Ashby

Excellent question! I'll try to provide some details (apologies if you know some of this already!):

  • Javascript was the first embedded language for client-side programming, so it's in all major browsers, nothing needs to be deployed. WebAssembly is a more recent arrival (see below).
  • Technically, Javascript isn't compiled in the traditional sense: as a programmer, you write a script and the browser executes it, no intermediate steps required (yes, the Javascript engine uses just-in-time compilation to native code, but that's internal to the engine, not your problem).
  • Traditional compiled languages such as C#, Java, C, Pascal, ASM... need translation from their text representation to a binary form, the compilation step. Tools are required to do this, and these do not exist by default in a browser, so compilation is not possible at the client side. Or is it...?
  • WebAssembly (aka WASM) was created to allow languages other than Javascript to be translated to a common binary representation and executed at the browser, opening the browser ecosystem to a wider audience. WASM translators exist for a number of popular languages (see Emscripten). Note this doesn't allow compilation in the browser yet, but..
  • Compilation tools for traditional languages (which are themselves written in a compiled language such as C++) can now be translated to WASM (just once, using a traditional process), then loaded & executed in the browser, allowing compilation in the browser for their target language(s).

Before the creation of WASM, there were hacky (in the best sense of the term) ways to run traditional software (such as compiler tools) in a browser - typically by emulation of an entire computer system, or kernel ABI in Javascript, then loading & executing the target executable (very slowly) in this emulated environment. Some determined people even managed to boot a full operating system (Linux usually) in such an emulator!

Collapse
 
brsnumget profile image
brsnumget

This answer has been very informative. and I learned a lot. what I'm aiming for is codes that don't need to convert to 0 and 1 :). because I think right now the whole software ecosystem is still in the formation phase, I asked this question because I think that there may be a eugenics in the software ecosystem in the future, for example, a language can be very good in more than one field, such as the fact that a language that is not like this will fall into the dusty shelves of history. In this world we have built, similar things happen without us realizing it, and in my opinion, we are going to the eugenics phase. I read an article called the features that a perfect programming language should have. A triangle had 1st side: performance 2nd side: safety
3rd edge: save time (easy to write)
He said that since there is no programming language that has these 3 features at the same time, there is no perfect language, and therefore there cannot be a perfect application and the best developer. Library and framework development by communities is actually like a help for languages ​​to be perfect languages. so: having a language open source and community support means a lot to the future of the language. And this competition between languages ​​(there is a big competition even if we don't see it) takes the software ecosystem to the stage of eugenics. That's why I don't think that current technologies will survive in the future, for example php, c, c#, ruby
but there will definitely be these languages
javascript, python, go, rust
and this phase will definitely be on the ecosystem running entirely on the cloud (totally my opinion)

Collapse
 
sfleroy profile image
Leroy

I think the short answer is that all languages need some kind of runtime. For js this ships with all browsers, for dotnet it ships with windows, for C, C++ this ships with all operating systems.

Collapse
 
darkwiiplayer profile image
𒎏Wii 🏳️‍⚧️

C doesn't really need a runtime.

Collapse
 
saptakbhoumik profile image
SaptakBhoumik • Edited

It does have a runtime called crt which will initialize the argv and argc and call the main function , however it is extremely lightweight

Thread Thread
 
darkwiiplayer profile image
𒎏Wii 🏳️‍⚧️

You don't need a separate runtime environment though. Depending on what system you're compiling for, your compiler could easily just turn argc and argv into two globals, one being 0 and the other an empty array, or add some boilerplate code that reads them from somewhere else, like some external sensor. One could get creative with that.

Thread Thread
 
saptakbhoumik profile image
SaptakBhoumik

That's not how it morks most of the time

Thread Thread
 
darkwiiplayer profile image
𒎏Wii 🏳️‍⚧️

But that's not what we're talking about; it doesn't need a runtime, regardless of whether it usually has one.

Collapse
 
saptakbhoumik profile image
SaptakBhoumik • Edited

Because js is a scripting language and also the official language for writing clint side code. This is why js is supported directly by browser but other languages are not. Also javascript is an interpreted language and not
compiled. It is true that most js engine provide jit but it not necessary+it is not aot compiled like c/c++ so you don't have to compile it before hand

Collapse
 
_genjudev profile image
Larson

Why would it be good to get away from native compiler? The browser is native compiled too. All interpreter are native compiled.

In the end you need one part natively compiled.