andrewducker: (Default)
andrewducker ([personal profile] andrewducker) wrote2010-11-11 12:55 pm

Coding in web browsers

[livejournal.com profile] robhu has a good point over here.

At the moment we write Javascript in web pages, which is then compiled down by the various JIT methods that Firefox/IE/Webkit use to make it super fast.

Seeing as what's run clearly isn't the actual JS itself, but bytecode, why not have a standardised bytecode that all browsers would support, which would then mean you could write your code in any language you liked, providing there was a compiler to convert it to the standardised bytecode?

At the moment Google uses GWT to convert Java into Javascript that then gets converted into the running code, (And MS used to have something similar) wouldn't it be handy if the intermediate step wasn't necessary?

[identity profile] momentsmusicaux.livejournal.com 2010-11-11 01:04 pm (UTC)(link)
> developers can use whatever language they want and just compile it to the opcode language used by the browser.

Compile? What is this 'compile'?

[identity profile] momentsmusicaux.livejournal.com 2010-11-11 02:16 pm (UTC)(link)
Heh if the server can handle compiling it, fine. I'm not going to fart around with an extra level of stuff every time I want to change one line in my JS!

[identity profile] momentsmusicaux.livejournal.com 2010-11-11 02:52 pm (UTC)(link)
That would mean Apache etc having to know how to serve JS files...

[identity profile] momentsmusicaux.livejournal.com 2010-11-11 03:08 pm (UTC)(link)
Yes, but who would do the compiling?

I'm not doing it, the browser isn't doing it, so it's got to be the server....

[identity profile] momentsmusicaux.livejournal.com 2010-11-11 03:26 pm (UTC)(link)
Yeah, you've lost me. How is that different to what happens now? What's the benefit if it is -- the browser is still doing the work.

[identity profile] momentsmusicaux.livejournal.com 2010-11-11 03:52 pm (UTC)(link)
Right, so we are on the same page. So my question was -- who's going to make the bytecode?

[identity profile] figg.livejournal.com 2010-11-12 01:49 am (UTC)(link)
So,

people who write js can just write js

and people who write in something that compiles to js can compile to a different format instead

[identity profile] strawberryfrog.livejournal.com 2010-11-11 02:40 pm (UTC)(link)
What is compile? For languages like Java or C# that use a bytecode, there are two things that you can call "compile".

First is turning the source into platform-independent bytecode, this happens upfront.

Second is "Just in Time compilation" during execution, when the runtime needs to use a method that has not been used before, and so needs to turn the bytecode for it into something that can actually execute on whatever cpu.

I'd say that Andrew is referring to the first of these.

[identity profile] momentsmusicaux.livejournal.com 2010-11-11 02:46 pm (UTC)(link)
Yes I know - I'm being facetious and saying that web developers won't put up with the faffiness of it.

[identity profile] robhu.livejournal.com 2010-11-11 03:53 pm (UTC)(link)
Well, you'll get the choice. You can continue to use JS if you like, but people that want to use other languages (and I suspect there are A LOT of those) can use the byte code system. Think for example how many Ruby or Python web developers there are who are good with that language but not so great with JS.

There's no good reason why they shouldn't be able to write in Ruby/Python for the browser just like they do on the server end.

[identity profile] figg.livejournal.com 2010-11-12 01:57 am (UTC)(link)
and yet they do already without a bytecode but compiling to javascript

[identity profile] ciphergoth.livejournal.com 2010-11-11 01:11 pm (UTC)(link)
How do you get there from here, though?

[identity profile] ciphergoth.livejournal.com 2010-11-11 03:15 pm (UTC)(link)
But what would web developers target?

[identity profile] khoth.livejournal.com 2010-11-11 01:18 pm (UTC)(link)
And once we have standardised bytecode, the next logical step would presumably be to improve performance by creating CPUs that can execute it directly. In 20 years we'll all be back where we started.

[identity profile] robhu.livejournal.com 2010-11-11 02:00 pm (UTC)(link)
Hey we could do this now - just implement the VM in Javascript ;-)

[identity profile] andlosers.livejournal.com 2010-11-11 01:20 pm (UTC)(link)
Google have already proposed this, and I believe it's coming in Chrome. I'll see if I can dig out a link,

[identity profile] robhu.livejournal.com 2010-11-11 03:54 pm (UTC)(link)
Also - native client is not the same sort of thing. It's a way of running native code 'safely'. But we're not talking about running native code.

[identity profile] robhu.livejournal.com 2010-11-11 03:59 pm (UTC)(link)
Interesting. So I can compile some Ruby or whatever to LLVM byte code, then the native client will safely run it in Chrome (and hopefully in the future in other browsers)?

Is native client stuff allowed to interact with the DOM etc, or is it just like a plugin that operates inside an object tag?

[identity profile] robhu.livejournal.com 2010-11-11 04:14 pm (UTC)(link)
DOM interaction is the main thing here :)
drplokta: (Default)

[personal profile] drplokta 2010-11-11 01:21 pm (UTC)(link)
The bytecode that each browser turns the Javascript into is obviously different for each browser, because each browser has different internals. So in your plan, each browser would still have to translate the bytecode into different bytecode in order to execute it. In which case, why not just call the existing Javascript language the bytecode, which also has the advantage of being human-readable, and make it the target for your compilers from other languages?

(The answer is because Javascript is lacking some functionality, not specified with sufficient rigour, in too many versions, with varying degrees of implementation in the browsers, and it takes too long to get changes into the spec, but I don't see how another spec for bytecode is going to be any different in this respect. You can only get round these problems by owning and implementing the spec yourself -- in which case you're re-inventing Flash or Silverlight.)
Edited 2010-11-11 13:22 (UTC)
drplokta: (Default)

[personal profile] drplokta 2010-11-11 01:31 pm (UTC)(link)
It's the big 4, I'm afraid. Safari and Chrome have not dissimilar usage figures (especially when you include all the people using Safari on iPhones, iPod Touches and iPads).

But my point is that it's not possible to have the same bytecode run natively in multiple browsers, or indeed in the same browser on different OS platforms. It will have to be run through a VM that translates the bytecode into actual native code in any case, so why not just call the Javascript interpreter the VM?
drplokta: (Default)

[personal profile] drplokta 2010-11-11 01:39 pm (UTC)(link)
As I said upthread, Javascript probably isn't expressive and powerful enough, but I don't see how your new thing is going to avoid the standard-defining/implementing processes that have bollixed Javascript up. If you do have some new way to avoid the bollixisation of web standards, then simply apply this process to fixing Javascript.

[identity profile] strawberryfrog.livejournal.com 2010-11-11 03:12 pm (UTC)(link)
Javascript can't be fixed without breaking existing javascript code. It was like that from the start.

[identity profile] skington.livejournal.com 2010-11-11 02:32 pm (UTC)(link)
Don't Safari and Chrome have different JS engines? Irrespective of whether they share a rendering engine.

[identity profile] call-waiting.livejournal.com 2010-11-11 01:31 pm (UTC)(link)
In what way is it suboptimal? There's a slight time penalty in the translation from the higher-level language than bytecode, and there's a bit of a semantic gap between java and javascript, but it isn't necessarily terribly inefficient. If the javascript emitted is low-level enough, all the potential optimisation in the original source code can be exposed and there's almost no work for the host JIT to do.

Similarly, it's possible to write C code that's low-level enough that it's virtually impossible to do better by writing in assembly.
drplokta: (Default)

[personal profile] drplokta 2010-11-11 01:34 pm (UTC)(link)
Actually, I believe that C code tends to be faster than assembly these days, because the compiler is better than you are at ordering opcodes to take best advantage of modern CPU architectures and allow simultaneous execution of instructions in nominally single-threaded code while minimising waits.

[identity profile] call-waiting.livejournal.com 2010-11-11 01:37 pm (UTC)(link)
In general, absolutely. Certainly on a modern superscalar CPU. But in the DSP space, assembly programmers have the edge when they have sufficient time to spend on an inner loop (and they're allowed to make assumptions that compilers aren't)

[identity profile] jouni osmala (from livejournal.com) 2010-11-11 02:37 pm (UTC)(link)
In 90's the assembly programming book simply stated, that if you hand translate your C code to assembler you loose. But if you solve the problem directly in assembler you could get ~6X performance improvement.
Another thing to consider is that assembler really requires LOTS of knowledge to actually be better than writing same in C.
Assembly acts as a leverage of your knowledge base, by giving you direct control over constraints that you do not have access to in C.
The amount of knowledge required for assembly to win over C is couple thousand pages worth of knowledge that isn't much use outside assembler programming how many programmers have read it?

[identity profile] strawberryfrog.livejournal.com 2010-11-11 02:45 pm (UTC)(link)
The bytecode that each browser turns the Javascript into is obviously different for each browser

Currently yes. But there's no reason why that should be so. In fact, isn't that the proposal?

Something like how The Java and .Net bytecodes are standardised and designed to be platform-independent, which is kinda the point. They can be executed by different engines. Those engines may have different internals, but executing that standard bytecode is their entire purpose.

.Net bytecode is less obviously "platform-independent" than Java. But there are 32 and 64 bit windows runtimes that work with the same bytecode. And mono/Linux run that same bytecode.
Edited 2010-11-11 14:50 (UTC)

Native Client

[identity profile] http://www.google.com/profiles/albzey (from livejournal.com) 2010-11-11 02:12 pm (UTC)(link)
Exactly this exists already. Check out this:
http://code.google.com/p/nativeclient/

It probably will use LLVM bytecode at some point (I think it already supports that). Atm., it executes x86.

The hard part is the sandboxing. But it seems they have done well on this in the Natice Client.

[identity profile] strawberryfrog.livejournal.com 2010-11-11 02:36 pm (UTC)(link)
Reminds me of this recent rant: "we can write any program we like so long as it's in Javascript".

A bytecode standard is not a bad idea.

[identity profile] lpetrazickis.livejournal.com 2010-11-11 03:08 pm (UTC)(link)
I would imagine that browsers currently have no defences against malicious bytecode, since they generate it themselves and so don't expect it to be malicious. Their defences against forkbombs, buffer overflows, and the like are likely in the Javascript compiler, not the bytecode interpreter. There'd be huge security issues with giving third parties on the internet direct control of the browser internals as they are.

[identity profile] strawberryfrog.livejournal.com 2010-11-11 03:18 pm (UTC)(link)
The defence against forkbombs is that that Javascipt has little-to-no threading ability. Long-running scripts get shut down within a minute.

Security in bytecode systems is a problem that has been solved more than once, so it doesn't appear to be that hard. JS has no pointers, hence no buffer overflows, and I'd expect the bytecode to be likewise. In Java or .Net bytecode, security is mainly about gating access to the system API by untrusted bytecode.
Edited 2010-11-11 15:21 (UTC)

[identity profile] lpetrazickis.livejournal.com 2010-11-11 04:18 pm (UTC)(link)
Historically, Javascript used to be capable of things like opening a new window which recursively opens two copies of itself.

The new Javascript worker threads are indeed incapable of fork-bombs due to the low limit on how many can be spawned.

I agree that adding a bytecode interpreter would be feasible, but I see it as a non-trivial task. Also, at least two browser vendors would have to forge ahead with it for the others to follow.

[identity profile] strawberryfrog.livejournal.com 2010-11-11 04:23 pm (UTC)(link)
I agree that bytecode interpreters aren't trivial, but are they much harder than the things currently being done to JavaScript in V8, tracemonkey, etc?

[identity profile] figg.livejournal.com 2010-11-12 01:56 am (UTC)(link)
heh


bytecode intepreters are trivial, but that is their nature.

meanwhile effective just intime compilation as in v8, tracemonkey etc are a little more than a switch() statement.

lua is a bytecode interpreter and it is pretty simple to see how it works

meanwhile v8 uses jit to assembly + polymorphic inline caches + hidden classes to implement speedy dynamic dispatch

meanwhile nitro (or whatever apple is calling it) uses a technique of context threading, a simplified jit/bytecode hybrid that works by ensuring that the virtual machine doesn't get out of whack with the physical machine (i.e cache stalls, pipelining problems...)


you've got the wrong end of the stick: bytecode interpreters are easy, jit efforts like v8 are mammoth projects

[identity profile] strawberryfrog.livejournal.com 2010-11-12 09:27 am (UTC)(link)
you've got the wrong end of the stick: bytecode interpreters are easy

My mistake was to go along with the parent poster, [livejournal.com profile] lpetrazickis's word usage, which was: I agree that adding a bytecode interpreter would be feasible, but I see it as a non-trivial task

Clearly, in the pedantic sense he is wrong. Bytecode interpreters are pretty simple. Equally clearly, a simple bytecode interpreter would not be sufficient for a theoretical layer under javascript, just as it is not for the JVM or .Net CLR. Jit is needed. I think we can agree that this is not trivial, and probably bears some comparison to V8 or Tracemonkey.
Edited 2010-11-12 09:28 (UTC)

[identity profile] robhu.livejournal.com 2010-11-11 04:01 pm (UTC)(link)
Why give it access to browser internals? Just provide access to the things JS already has access to, like the DOM.

[identity profile] robhu.livejournal.com 2010-11-11 03:51 pm (UTC)(link)
This post is the top item on Hacker News atm btw :)

[identity profile] figg.livejournal.com 2010-11-12 02:20 am (UTC)(link)
the problem here is not compiling to javascript but compiling to javascript *semantics*. if your lists, strings or dictionaries work differently, you have to re-implement them (in javascript)

llvm would be a nice idea but is still relatively young and unsuitable for jit compilation or mobile devices.

anything higher up (like an object system or datatypes like a string or a list or a hash) would present the same problems as compiling to javascript.