Mascara: Next Generation JavaScript compiler

Try it Download Buy About Documentation Blog Contact

Configuring your editor

You should be able to set up Mascara as a custom tool in your favorite editor, so you can translate and then execute an open file with one click.

Mascara is invoked like this:

translator.py inputfile --execute

A command-line JavaScript interpreter should be configured in config.py in the root folder. As default it is configured to cscript.exe, which is the command line interface to Microsofts JScript engine. Just change the config to your favorite engine.

Error handling

When receiving a runtime error from the JavaScript engine, it might not be immediately obvious which line in the source file generated the error, since the engine of course reports the position in the intermediate translated file. This is solved like this: Error messages emitted from the engine are intercepted. If an error messages contain a line numbers, these are annotated with the corresponding line number and file name for the input ES4 source file.

Eg. if the engine writes this to stderr:

 
runtimeerrtest.es4.translated.js(2, 1) 
Microsoft JScript runtime error: 
'a.something' is null or not an object
Then the translator identifies the corresponding position in the source file, and adds:
 [ES4 Source: runtimeerrtest.esx line 3]

Since the output format for error messages may vary from engine to engine, you may have to configure a custom regular expression to capture messages. This is also done in config.py.

Setting it up in Eclipse

In Eclipse:

1) Run -> External tools -> Open External Tools Dialog

2) In the External Tools dialog:

1. Create a new Launch Configuration 2. Give it a name like "Translate ES4" 3. Set Location to the Python interpreter. E.g. C:\Program Files\Python25\pythonw.exe 4. Set Arguments to something like: path-to-mascara-root-folder\translate.py "${resource_loc}" --tempdir --execute 5. Apply and Close

3) Now you should be able to translate and execute the current open file using: Run -> External tools -> Translate ES4

The translator does not care which extension you file has, but I prefer to use .esx.

Other text editors or IDE's can be configured much the same way.

Writing to standard output

Since we are executing the script in a console context, we have a special problem: There is no standard way to write to stdout from inside JavaScript.

For your convenience, we have a standard library package, console, that defines a print function. So you can do:

import console;
print("Hello world");
The print function is defined as native, that is, you have to provide an implementation in the host. A quick (hacky) way to do that, is to prepend the generated file with some additional code that provides an implemention of "print" that writes to stdout. This can be configured with the outfile_prelude setting in config.py.

The default configuration defines en implementation compatible with cscript.exe.

X Try!
Mascara JavaScript Generated JavaScript

Translating...
Show compiler settings