The most important improvement is to the command-line tool. It now supports compilation and execution in one step, which makes it much more fun to use, when integrated with an text editor. Instant gratification!
You should be able to set this up as a custom tool in your favorite editor, so you can translate and then execute an open file with one click.
It works like this:
translator.py inputfile
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.
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.es4 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:
- Create a new Launch Configuration
- Give it a name like "Translate ES4"
- Set Location to the Python interpreter. E.g. C:\Program Files\Python25\pythonw.exe
- Set Arguments to something like:
path-to-mascara-root-folder\translate.py "${resource_loc}"
--tempdir
--execute - Apply and Close
Run -> External tools -> Translate ES4
The translator does not care which extension you file has, but I prefer to use .es4.
Other text editors or IDE's can be configured much the same way.
Writing to stdout
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 file that defines a write function. So you can do:
import write;
write("Hello world");
The write 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 "write" 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.
Have fun!
Ingen kommentarer:
Send en kommentar