From b10b9fcb4e064026df306737609e7ba3e2be2e9a Mon Sep 17 00:00:00 2001 From: Fusspawn Date: Wed, 19 Mar 2014 23:12:35 +0000 Subject: [PATCH] Added Function.Prototype.bind shim to world worker to enable running on headless browsers --- .../javascripts/workers/worker_world.js | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/app/assets/javascripts/workers/worker_world.js b/app/assets/javascripts/workers/worker_world.js index 00a2a2d03..45c5e80d7 100644 --- a/app/assets/javascripts/workers/worker_world.js +++ b/app/assets/javascripts/workers/worker_world.js @@ -5,6 +5,32 @@ if(typeof window !== 'undefined' || !self.importScripts) throw "Attempt to load worker_world into main window instead of web worker."; +// Taken from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind +// This is here for running simuations in enviroments lacking function.bind (PhantomJS mostly) +if (!Function.prototype.bind) { + Function.prototype.bind = function (oThis) { + if (typeof this !== "function") { + // closest thing possible to the ECMAScript 5 internal IsCallable function + throw new TypeError("Function.prototype.bind (Shim) - target is not callable"); + } + + var aArgs = Array.prototype.slice.call(arguments, 1), + fToBind = this, + fNOP = function () {}, + fBound = function () { + return fToBind.apply(this instanceof fNOP && oThis + ? this + : oThis, + aArgs.concat(Array.prototype.slice.call(arguments))); + }; + + fNOP.prototype = this.prototype; + fBound.prototype = new fNOP(); + + return fBound; + }; +} + // assign global window so that Brunch's require (in world.js) can go into it self.window = self; self.workerID = "Worker";