BT

New Early adopter or innovator? InfoQ has been working on some new features for you. Learn more

Mozilla Sweet.js: Extending JavaScript with Macros

| by Abel Avram Follow 4 Followers on Oct 19, 2012. Estimated reading time: 1 minute |

Mozilla Sweet.js enables developers to enrich JavaScript by adding new syntax to the language through the use of macros. This helps developers to customize the JavaScript syntax for their style, or to expand it by creating a new JavaScript-based DSL useful for their niche domain.

Sweet.js provides the possibility to define hygienic macros inspired by Scheme and Rust with the macro keyword. A simple example is replacing the keyword function with a shorter one, def:

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

Now, functions can be defined using def:

def add (a, b) {
  return a + b;
}

A more interesting example is introducing the keyword class:

macro class {
  case $className:ident { 
    constructor $constParam $constBody
    $($methodName:ident $methodParam $methodBody) ... } => {

    function $className $constParam $constBody

    $($className.prototype.$methodName 
      = function $methodName $methodParam $methodBody; ) ...
  }
}

An example of using class:

class Person {
  constructor(name) {
    this.name = name;
  }

  say(msg) {
    console.log(this.name + " says: " + msg);
  }
}
var bob = new Person("Bob");
bob.say("Macros are sweet!");

More macro examples can be found on Mozilla/Sweet.js wiki in GitHub where the source code can also be found under a BSD license.

Sweet.js files containing macros are compiled into pure JavaScript files without any foreign syntax with sjs. require-sweet provides an AMD loader and there is a SweetJS gem for compiling Sweet.js files from Ruby.

Sweet.js currently supports declarative macro definitions, but plans are to support imperative definitions in the future, according to Tim Disney from Mozilla Research. That means macros could contain arbitrary JavaScript code that is run at compile time.

Rate this Article

Adoption Stage
Style

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Tell us what you think

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Ugh by Robert Barth

Taking the worst part of C and injecting it into JS. Brilliant!

Re: Ugh by Russell Leggett

Well it more like on of the best parts of Scheme, than the worst parts of C. Very different type of macro. Still... can be polarizing, you might still hate it ;)

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

2 Discuss

Login to InfoQ to interact with what matters most to you.


Recover your password...

Follow

Follow your favorite topics and editors

Quick overview of most important highlights in the industry and on the site.

Like

More signal, less noise

Build your own feed by choosing topics you want to read about and editors you want to hear from.

Notifications

Stay up-to-date

Set up your notifications and don't miss out on content that matters to you

BT