/* ==================================================================================================== backport.js v0.1 Makes older versions of the JS API a bit newer: * Implement/upgrade functions * Implement missing constants * Remove deprecated functions/constants Developed by: aubergine http://forums.wz2100.net/memberlist.php?mode=viewprofile&u=4994 Home page: https://warzone.atlassian.net/wiki/display/backportjs Released under the MIT, BSD, and GPL Licenses. v0.1 - Initial release ==================================================================================================== */ if (!backport) { // requires Util.js from https://warzone.atlassian.net/wiki/display/UtilJS if (!Object.prototype.uGetType) throw new Error("Unable to initialise Backport: backport.js requires util.js"); function backport(name,value) { var code; switch (arguments.length) { case 2: { // backport something // make sure it's not already backported if (backport[name] || name == "backport" || name == "native" || name == "global") { throw new Error("Unable backport '"+name+"': Already backported '"+name+"' (or it's a reserved keyword)"); } // work out what sort of backport it is switch (Object.uGetType(value)) { // add/extend function // https://warzone.atlassian.net/wiki/display/backportjs/Adding+missing+functions // https://warzone.atlassian.net/wiki/display/backportjs/Overriding+existing+functions case "function" : { // store funcs where eval can get at them eval("if ("+name+") backport.native = "+name+";"); backport.backport = value; // backport code //code = "function "+name+"(){return "+name+".backport.apply(this,arguments);}"; code = "var "+name+" = backport.backport;"; code += "if (backport.native) {backport['"+name+"']=1;"+name+".native=backport.native;} else {backport['"+name+"']=-1;};" code += name+".backport = backport.backport;"; // execute code in global scope with no scope or prototype chain eval.call(null,code); // clear up any function backport mess delete backport.native; delete backport.backport; break; } // remove deprecated function / constant // https://warzone.atlassian.net/wiki/display/backportjs/Removing+deprecated+API+features case "null" : case "undefined": { // backport code code = "if ("+name+" != undefined) { const "+name+" = undefined; backport['"+name+"']=null;}"; // execute code in global scope with no scope or prototype chain eval.call(null,code); break; } // alter a global - not currently implemented case "array" : case "object" : { throw new Error("Backport: Altering arrays/objects (for '"+name+"') not currently supported."); } // add missing constant // https://warzone.atlassian.net/wiki/display/backportjs/Adding+missing+constants case "boolean" : case "string" : case "number" : { // backport code code = "if ("+name+" == undefined) { const "+name+" = "+value+"; backport['"+name+"']=-1;}"; // execute code in global scope with no scope or prototype chain eval.call(null,code); break; } // something unexpected - throw error default : { throw new Error("Backport: Unrecognised data type for '"+name+"': "+Object.uGetType(value)); } } // return backport status for name return backport(name); } case 1: { // is 'name' backported? // https://warzone.atlassian.net/wiki/display/backportjs/Check+status+of+an+API+feature // -1 = added, null = deprecated, false = native, undefined = not implemented, +1 = extended return (backport.hasOwnProperty(name)) ? backport[name] : undefined; } default: { // return a list of all JS API features // https://warzone.atlassian.net/wiki/display/backportjs/Listing+API+features return Object.keys(backport); } } } // let's keep a reference to the global object for easy access // https://warzone.atlassian.net/wiki/display/backportjs/Accessing+native+API+features backport.global = this; // now grab all the native JS API stuff and mark it as native (false) on backport Object.keys(this).forEach(function(native) { backport[native] = true; // false indicates that native has not yet been touched by backport }); }