In association with heise online

15 October 2012, 14:06

Mozilla adds sweetener to JavaScript

  • Twitter
  • Facebook
  • submit to slashdot
  • StumbleUpon
  • submit to reddit

Sweet.js logo 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:

macro def {
case $name:ident $params $body => {
function $name $params $body

which would tell the Sweet.js compiler (sjs) to spot code like

def sweet(a) {
console.log("Macros are sweet!");

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.


Print Version | Send by email | Permalink:

  • July's Community Calendar

The H Open

The H Security

The H Developer

The H Internet Toolkit