Channels

Services

Mozilla adds sweetener to JavaScript

A Mozilla project, Sweet.js, is setting out to sweeten JavaScript development by giving users hygienic macros to work with. Hygienic macros are macros that will not expand into anything that will interfere with the other code in a program, for example, by not capturing variables. This makes them unlike the macros found in C-based languages which are just text-wise expanded. Hygienic macros are typically a feature of Lisp-like languages such as Scheme and Dylan but also appear in new languages like Mozilla's Rust.

The addition of a macro facility means that developers can redefine the language as needed. On the web site, one example given is redefining the function keyword as def using this macro:

where the first parameter after the def is an identifier, followed by a parameter followed by a body of code. Macros in Sweet.js can also match repeating patterns with or without separators, multiple different cases can exist and macros can even call themselves. Sweet.js has a compiler (sjs) that compiles code into JavaScript for it to be run in unmodified JavaScript runtimes.

The Sweet.js project has been ongoing for at least a few months, but is still in early development. The authors say to expect some bugs and to report issues on the project's tracker. Written in JavaScript and using Node.js's infrastructure, Sweet.js can be installed using the command npm install -g sweet.js. The compiler's source is also available in the project's Github repository. Among recent enhancements during development, it is now possible to require sweet.js within a Node application which will automatically invoke the sjs compiler. The code is available under a two clause BSD licence.