Introduction

This is a compiler which takes a program written in ordinary JavaScript (ECMAScript®) and rewrites it so that it always produces the same outputs for given inputs.

Why is this interesting?

It’s useful for writing distributed applications in which the same code executes simultaneously on multiple computers. If the code is deterministic then its state can be synchronized across different computers without requiring network communication. Two examples which make use of this property are smart contract applications and multiplayer games.

Why isn’t JavaScript already deterministic?

There are a few obvious reasons, such as the random number generator and the ability to get the current time. Less obvious are areas of JavaScript that were underspecified in order to allow for improved performance.

How does it work?

The compiler returns your JavaScript code with some parts translated to make them deterministic (e.g., for-in statements) and prepends the result with a header that overrides some of the standard library functions (such as Math.random). You don’t need to do anything different or learn anything new other than to know that there will be minor behavioral differences (such as Math.random always returning the same predictable sequence of values). The compiler also returns warning messages to let you know which parts of your code will behave differently. Because some native code is re-implemented in JavaScript there can be a performance penalty.

Status

First we’re targeting a fully deterministic version of ECMAScript® Edition 5.1. Once we complete that we’ll look at supporting the new features of ECMAScript® 2015.

The compiler is usable today but the following possible sources of non-determinism still need to be ironed out:

Usage

Installation

You need to have Node.js already installed.

npm install deterministic

Example

'use strict';

var
  assert = require('assert'),
  deterministic = require('deterministic');

it("is like magic", function () {
  var
    compiled;

  function magicTrick() {
    // The first random number will be 0.5945264333859086!
    assert.equal(Math.random(), 0.5945264333859086);

    // How about time travel?  The year is 1970 again:
    assert.equal((new Date()).getUTCFullYear(), 1970);
  }

  compiled = deterministic.compile('(' + String(magicTrick) + ')()');
  console.log("Helpful compilation warnings:\n", compiled.warnings);
  eval(compiled.code);
});

Execution produces:

$ mocha test/example.js 

Helpful compilation warnings:
 [ { ruleId: 'math-random',
    severity: 1,
    message: 'Math.random() always returns the same pseudo-random sequence.',
    line: 3,
    column: 18,
    nodeType: 'MemberExpression',
    source: '    assert.equal(Math.random(), 0.5945264333859086);' },
  { ruleId: 'date-new',
    severity: 1,
    message: 'new Date() always returns the epoch.',
    line: 6,
    column: 19,
    nodeType: 'NewExpression',
    source: '    assert.equal((new Date()).getUTCFullYear(), 1970);' } ]
   is like magic (154ms)

  1 passing (158ms)

API

See the full API documentation.

We are now held within un- sub- or super-natural forces.

Copyright

Copyright 2016 David Braun

Licensed under the Apache License, Version 2.0 (the “License”); you may not use these files except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0. Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.