JavaScript bytecode compiler? - javascript

For a project I'm tangentially working on, I need a way to compile JavaScript into some intermediate language or bytecode so that I can single-step through it. I know that many of the JavaScript engines in modern browsers do something like this, but they don't make the resulting bytecode accessible. Is there a good off-the-shelf tool for this sort of JavaScript compilation?

Not exactly sure of your needs, however maybe Rhino could work for you.
The JavaScript compiler translates
JavaScript source into Java class
files. The resulting Java class files
can then be loaded and executed at
another time, providing a convenient
method for transfering JavaScript, and
for avoiding translation cost.
More about the compile function is located here.

Related

Is it possible to take a WASM file and go back to a JS file?

Although I suspect the answer is no, I'll ask anyway. Given a Web Assembly file (.wasm), is it possible to de-compile that file into a Javascript file?
No. (Web) assembly is not compiled from JS in the first place, that's the whole point.
No, you cannot.
JavaScript doesn’t compile to WebAssembly, although you can use the experimental AssemblyScript compiler to compile Typescript (which is a JavaScript superset) to WebAssembly.
This point aside, decompilation to the original source is not possible. Much of the information is lost in the process of compiling to WebAssembly.
However, this might become possible in the future. The sourcemaps technology allows you to map between code in different formats. It is typically used for mapping transpiled and minifed JavaScript back to its source. This technology could potentially be used for WebAssembly. This is not decompilation, rather, it is a mapping.

Are node.js and V8 builtins written in JS or compiled and run as native code?

In node.js and I assume by extension V8, are built-ins (such as JSON, Math, etc) native code residing in the interpreter or are they javascript code that gets interpreted the same way as the rest of user-written code?
So for example, does a call to JSON.stringify(my_data) jump to compiled machine code or does it just run more JS code?
This appears to be responsible for JSON stringification:
https://github.com/joyent/node/blob/master/deps/v8/src/json-stringifier.h
Quickly browsing the repo will give you an idea of whats written in native code (and what's not):
https://github.com/joyent/node/tree/master/deps/v8/src
Glancing at the V8 source code, I think it's a mix of both. JSON is backed by native code in json-parser.h; (at least some of) math is implemented as JavaScript in math.js.

Can I use a Dart interface from manually written JavaScript?

I consider using Dart for my next web project and it has made a very neat impression on me so far. Obviously, Dart code can be converted into JavaScript. However, I wonder if this results in code where functions can be used from other JavaScript files.
It is supported to call Dart functions from JavaScript but only functions that are made available to JavaScript explicitely.
See
Calling Dart code from javascript
Expose Dart functions to javascript
When you build Dart to JavaScript minification and tree-shaking make it hard or mostly impossible to call methods because functions might not even be included in the output if the analysis recognized that the function isn't called anywhere and if it actually is included it will have a shortened and cryptic name.
I have seen it mentioned that here are attempts for better support of this use case but there isn't anything available yet.
If your generated JavaScript is accessible then for sure it will regardless of it is generated using dart or anything else

What is a JavaScript pre-compiled library?

I'm learning how to use Magento e-commerce system, and I'm reading its documentation, but I don't understand some term, a "pre-compiled JavaScript library". What do they mean by that? How can JavaScript code be compiled?
The web downloader for upgrading and installing Magento without the
use of SSH (covered in Chapter 2). • js—The core folder where all
JavaScript code included with the installation of Magento is kept. We
will find all pre-compiled libraries of JavaScript here.
Source:
http://www.magentocommerce.com/wiki/2_-_magento_concepts_and_architecture/magento_s_base_structure
I do not know what Magento e-commerce uses, but I know what JavaScript Compilers are.
Pre-Compiled JavaScript is not practical as the various JavaScript interpreters will have their own method of compiling. Therefore, when most people talk about Compiling JavaScript, they are usually referring Minified JavaScript.
However, the latest minifiers go way beyond. A good example is Google Closure Compiler Advanced Mode. It is related to Google Closure Library and Tools, but is well designed even when used by itself.
There is an Online Demo of Closure Compiler.
It is called a Compiler because it is more than a minifier and the name JavaScript Compiler is not used for anything else. For example: This code
function hello(name) {
alert('Hello, ' + name);
}
hello('New user');
compiles to alert("Hello, New user"); in advanced mode. Single-use functions are removed, variable names are shortened, and even re-used.
It is very thorough. Simple mode assumes that there might be other JavaScript involved. in which case it would preserve the hello function. Advanced mode assumes that there is only a single JavaScript file, or that it is properly exported.
The one thing that keeps this from really being compiled is that it is not bytecode like compiled C or Java would be. It still has to be compiled at run-time like Perl.
Magento has an configuration option in the admin at
System -> Configuration -> Developer -> JavaScript Settings
named Merged Javascript Files.
When this setting is on, Magento will take all the javascript files it knows about, "compile" them to a minified version to product smaller files sizes, and then combine all the javascript files into a single javascript file. This way, instead of opening multiple network connections to download multiple files, Magento will open a single network connection to open one file.
So, when the documentation says that folder contains the pre-compiled versions, it means that's where the individual javascript files are stored, and where the files are loaded from when Merged Javascript Files
The term compilation comes from Computer Science. A compiler takes source code, and transforms it from one language into another language.
Traditionally, it's meant taking code from a higher level language (C, .NET, Java, etc.), and transforming it into the machine code (assembly code) that's understood by the computer chip. However, the term is generic and more modern usage includes taking the source code written in one language (Javascript) and transforms it into a different form (minified javascript).

Is GWT compiled JavaScript obfuscated or only minified?

So, as the title says, when GWT compiles Java into JavaScript, is the result just minified, or is it obfuscated also?
According to the GWT FAQ the code is obfuscated, but they state that "This is partly done to protect the intellectual property of the application you develop, but also because obfuscation reduces the size of the generated JavaScript files". I'm not sure if this is true obfuscation or just variable/method renaming and stripping of whitespaces. A "true" obfuscation might actually make the code larger than the original code.
I'd also like to say that the GWT compiler minifies Js because of its optimization. It will, for example, inline method calls where possible and rename variables to shorter names.
Target is to get as small (and fast) code as possible. "Obfuscation" is just a consequence.

Categories

Resources