What JavaScript engine is Apps Script running on?

Apps Script (intermediate level) posted on 25th October 2018


Writing Apps Script const scoping problems led to me to wonder how to get exactly which  JavaScript features are supported by Apps Script and which are not. By deduction, it seems likely that the current version of Apps Script engine is Rhino v1.7R3. The level of JavaScript capabilities of that version of Rhino are detailed here http://mozilla.github.io/rhino/compat/engines.html

If so that'll give us a documented record of what Apps Script supports. In this article I'll try out a few features that are supported by one version of Rhino but not another.

What is Rhino?

It's a JavaScript engine, written in Java, so can be used to provide a server based JavaScript environment. For more info, see https://developer.mozilla.org/en-US/docs/Mozilla/Projects/Rhino

Is Apps Script running on Rhino?

I don't know for sure (if someone has definitive on that,then please let me know), but I do see some errors in the Apps Script issue tracker that appear to be from rhino eg. 
"com.google.apps.maestro.rhino.UniqueTag@20ba544b: NOT_FOUND"
So let's take that as strong evidence and proceed on that basis.

Versions of Rhino

Those included in the capability list are

Deducing which one it is should be a simple matter of finding capabilities present in one and not in another.   

Shorthand object literal methods

These exist from version 1.7.7 onwards, so if they don't work in Apps Script then we've narrowed it down to between 1.7r3 (or before this table) to 1.7.6
An object literal method example is
const ob = {
    eat: function () {
       return 'burger';
    },
    drink:function() {
      return 'shake';
   }
};
and a shorthand method is
const ob = {
    eat  () { return 'burger';},
    drink () { return 'shake';}
};


That doesn't work, so we know that it's 1.7r3 to 1.7.6


Destructuring Arrays.
This exists in all versions, so we can confirm we are at least at version 1.7r3 (assuming it was introduced then).
An example of array destructuring is, and this works in Apps Script
const [firstName, lastName] = ["Robert","Smith","London"];
Logger.log (firstName + " " + lastName);

Let

As mentioned in Apps Script const scoping problems, let doesn't work in Apps Script. This shows that the highest version of Rhino Apps Script can be is 1.7r3



Getters and Setters

I never noticed that Apps Script supported getters and setters until I did this analysis.

but indeed it does. Here's an example of using set and get to simulate a db result iterator.
function f() {
  
  const dbResult = {
    rowIndex: 0,
    rows:[],
    set data (rows) {
      this.rows = rows;
      this.rowIndex = 0;
    },
    get next() {
      return this.hasNext  ? this.rows[this.rowIndex++] : null;
    },
    get hasNext() {
      return this.rowIndex < this.rows.length;
    }
  };

  dbResult.data= [['a'],['b'],['c']];
  var x;
  while (x=dbResult.next) {
    Logger.log (x);
  }  

}

Conclusion 

It looks like Apps Script is using the Rhino JavaScript engine v1.7r3, so that means that  this list is a pretty good reference for what you can and can't do in Apps Script.


You want to learn Google Apps Script?

Learning Apps Script, (and transitioning from VBA) are covered comprehensively in my my book, Going Gas - from VBA to Apps script, All formats are available from O'ReillyAmazon and all good bookshops. You can also read a preview on O'Reilly

If you prefer Video style learning I also have two courses available. also published by O'Reilly.
Google Apps Script for Developers and Google Apps Script for Beginners.

Comments