Live data from Hacker News

Towards a JavaScript Binary AST

yoric.github.io

91–100 of 211 posts

Re: Towards a JavaScript Binary AST

#91
post #74

Lua has something very similar(bytecode vs AST) via luac for a long while now. We've used to to speed up parse times in the past and it helps a ton in that area.

I'm interested. Do you have a link?

Yup, from the docs(there's also a little bit describing how it's interpreted in the "load()" main docs as well): https://www.lua.org/manual/5.1/luac.html

Re: Towards a JavaScript Binary AST

#92
post #80

Earlier quoted context omitted.

I think everything will eventually settle in the middle where Java and C# currently live. They have an "intermediate code" that they somewhat compile down to. But this is really a more compact version of the source than a binary though. You can see this easily by running a decompiler. Often times the decompiled IL is almost identical to the source. And before the JIT kicks in the IL is typically interpreted. You get…

On .NET's case the IL is never interpreted, it is only a portable executable format. It is either AOT compiled to native code via NGEN, Mono AOT, MDIL or .NET Native. Or JITed on file load before actually executing the code. The only .NET toolchain, from Microsoft, that actually does interpret IL it is .NET Micro Framework. The idea of using bytecodes as portable execution formats goes back to mainframes, of which IB…

Didn't they use to have a IL interpreter, back in the days? Or was it already a compiler-maskerading-as-an-interpreter?

Re: Towards a JavaScript Binary AST

#93
post #62

To clarify how this is not related to WebAssembly, this is for code written in JavaScript , while WASM is for code written in other languages. It's a fairly simple optimization - it's still JavaScript, just compressed and somewhat pre-parsed. WASM doesn't currently have built-in garbage collection, so to use it to compress/speed up/whatever JavaScript, you would have to compile an entire JavaScript Virtual Machine in…

This is important, as there seems to be a lot of misunderstanding in this thread. What's proposed is structural compression of JS with JS-specific bits to speed things up even more. What's proposed is not compiled JS, in that the original JS is not meaningfully transformed at all. There is a very explicit design goal to retain the original syntactic structure. OTOH WebAssembly is like a new low-level ISA accessible f…

This is a very good point. I would also add that there are a lot of languages compiling to JavaScript that would similarly not benefit from wasm. Right now, pretty much all GC-ed languages compiling to JS (such as ClosureScript, Elm, Scala.js, BuckleScript, PureScript, etc.) are in that category. Even if/when wasm supports GC in the future, I expect that dynamically typed languages compiling to JS will still be a long way from benefiting from wasm.

However, all these languages can benefit from the Binary AST right away. Just like any codebase directly written in JavaScript. If the Binary AST has some decent story about position mapping (see [1] which I just filed), they might even get somewhat better tooling/debugging support going through the Binary AST than going through .js source files, out of the box.

[1] https://github.com/syg/ecmascript-binary-ast/issues/17

Re: Towards a JavaScript Binary AST

#94
post #87
post #83

Earlier quoted context omitted.

On our sample, x0.95 for minified code (so that's a 5% improvement) and ~x0.3 for non-minified code. As usual, read these numbers with a pinch of salt, they are bound to change many times before we are done.

Does BinJS perform AST transforms like `!0` to `true` which would be shorter in binary AST encoding? Or does it faithfully model the original code?

It faithfully models the original code. The idea is that if you want to transform `!0` to `true`, or if you want to obfuscate, etc. you can always plug an additional tool.

Re: Towards a JavaScript Binary AST

#95

For those curious about how this would deal with Function.prototype.toSource, via https://github.com/syg/ecmascript-binary-ast#functionprototy... : > This method would return something like "[sourceless code]".

It seems this would break a number of libraries (can't name one, but certain I've seen it), although per the article it would be a simple affair for the browser to reify the encoded AST into a compatible representation, especially if comments are also eventually preserved (per article its on the roadmap)

Re: Towards a JavaScript Binary AST

#96
post #94
post #87

Earlier quoted context omitted.

Does BinJS perform AST transforms like `!0` to `true` which would be shorter in binary AST encoding? Or does it faithfully model the original code?

It faithfully models the original code. The idea is that if you want to transform `!0` to `true`, or if you want to obfuscate, etc. you can always plug an additional tool.

The 5% gains over minified code are impressive given that BinJS performs no AST manipulation.

The BinJS prototype will be written in pure javascript I assume to speed its adoption?

Re: Towards a JavaScript Binary AST

#97
This is what BASIC interpreters on 8-bit systems did from the very beginning. Some BASIC interpreters did not even allow you to type the keywords. Storing a trivially serialized binary form of the source code is a painfully obvious way to reduce RAM usage and improve execution speed. You can also trivially produce the human-readable source back.

It's of course not compilation (though parsing is the first thing a compiler would do, too). It's not generation of machine code, or VM bytecode. it's mere compression.

This is great news because you got to see the source if you want, likely nicely formatted. You can also get rid of the minifiers, and thus likely see reasonable variable names in the debugger.

Re: Towards a JavaScript Binary AST

#98
post #93
post #62

Earlier quoted context omitted.

This is important, as there seems to be a lot of misunderstanding in this thread. What's proposed is structural compression of JS with JS-specific bits to speed things up even more. What's proposed is not compiled JS, in that the original JS is not meaningfully transformed at all. There is a very explicit design goal to retain the original syntactic structure. OTOH WebAssembly is like a new low-level ISA accessible f…

This is a very good point. I would also add that there are a lot of languages compiling to JavaScript that would similarly not benefit from wasm. Right now, pretty much all GC-ed languages compiling to JS (such as ClosureScript, Elm, Scala.js, BuckleScript, PureScript, etc.) are in that category. Even if/when wasm supports GC in the future, I expect that dynamically typed languages compiling to JS will still be a lon…

For context: position mapping is somewhere on our radar, but we haven't reached a stage at which it would make sense to start working on it yet.

If you have ideas and/or spare cycles, of course, they are welcome :)

As a side-note: I believe that we could build upon the (very early) proposed mechanism for comments to also store positions. Size-optimized BinAST files would drop both comments and positions, while debugging-optimized BinAST files would keep both at the end of the file so as to not slow down parsing until they are needed.

Re: Towards a JavaScript Binary AST

#99
post #46

Earlier quoted context omitted.

Firefox had a much higher market share than Chrome back then. Context matters.

Mozilla and others had pretty strong technical objections to Native Client. Context matters (sometimes :))

Oh, I think I misunderstood you. You mean the AST is going to go the way of Native Client because Mozilla doesn't have the muscle it used to? Or do you think Google's going to sandbag it as revenge for Native Client? :)

Re: Towards a JavaScript Binary AST

#100
post #44

Earlier quoted context omitted.

Part of this is fad driven, but part of this is also driven by other human concerns. For example, had the early web been binary (like we're pushing for now) instead of plain text it would have died in its crib. Executing binary code sent blindly from a remote server without a sandbox is a security nightmare. Now that we have robust sandboxes, remote binary execution becomes a viable option again. But, it took a decad…

But there has been Java Applets, Active X, Flash, and Silverlight.

And they all had crippling security problems[1]. I only mentioned the JVM, but you're right, all of those technologies contributed to the work of secure sandboxes.

[1]: Except maybe Silverlight. From what I remember, it didn't see enough success to be a tempting target for hackers.

Post reply on HN