andrewducker (
andrewducker) wrote2010-11-11 12:55 pm
![[personal profile]](https://www.dreamwidth.org/img/silk/identity/user.png)
Coding in web browsers
![[livejournal.com profile]](https://www.dreamwidth.org/img/external/lj-userinfo.gif)
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?
no subject
Compile? What is this 'compile'?
no subject
But for stuff that's linked in there's no reason that the equivalent of:
<script type="byte/code" language="bytecode" src="http://myserver/somecode.byt"></script>
no subject
no subject
no subject
no subject
Apache does serve JS files - that line from earleir was taken from one of our reports here, and was importing a .js file from an apache server before I changed it...
no subject
I'm not doing it, the browser isn't doing it, so it's got to be the server....
no subject
no subject
no subject
<script type="byte/code" language="bytecode" src="http://myserver/somecode.byt"></script>
it will pass it to the bytecode interpreter to run.
When it encounters:
<script type="text/javascript" language="javascript" src="http://myserver/media/js/jquery.js"></script>
it will run it as it currently does through the JS interpreter.
The browser continues to do the work _for JS_, but allows you to write code in other languages if you so desire.
no subject
no subject
Same as currently happens if you're using GWT or anything else which compiles into JS.
no subject
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
no subject
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.
no subject
no subject
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.
no subject
no subject
no subject
It'd need to be part of HTML6, obviously :->
no subject
no subject
no subject
no subject
no subject
no subject
no subject
no subject
no subject
Is native client stuff allowed to interact with the DOM etc, or is it just like a plugin that operates inside an object tag?
no subject
If not then that would be an obvious next step.
no subject
no subject
So we probably want it built in. Which, let's face it, it will be if it takes off...
no subject
(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.)
no subject
Heck, I'd be happy with either Java bytecode or IL if they were suitable.
Compiling Java into JS seems terribly suboptimal to me, although I'm prepared to be told that actually it's a good fit. Having something that can be implemented in a standard way by the big 3 would be good.
no subject
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?
no subject
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?
I'm fine with that if JS is expressive enough and powerful enough for that. And the speed losses are within an order of magnitude. I really should play with something like GWT and see how long it takes to spit out the JS for a page.
Because if it's fast and powerful then, yes, absolutely.
no subject
no subject
Starting over might just be easier than trying to improve JS.
(Or it might lead to ten years in committees arguing over it.)
no subject
Of course, it'd need to get picked up as a standard first...
no subject
no subject
no subject
no subject
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.
no subject
no subject
no subject
no subject
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?
no subject
no subject
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.
Native Client
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.
Re: Native Client
no subject
A bytecode standard is not a bad idea.
no subject
no subject
no subject
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.
no subject
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.
no subject
no subject
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
no subject
My mistake was to go along with the parent poster,
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.
no subject
no subject
no subject
no subject
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.