Skip to content

Instantly share code, notes, and snippets.

@termi
Created March 3, 2014 13:04
Show Gist options
  • Save termi/9324505 to your computer and use it in GitHub Desktop.
Save termi/9324505 to your computer and use it in GitHub Desktop.
Symbol's polyfill test
applyPolyfills();var $S_polyfill$0 = Symbol["__polyfill__"];var $S_SET$0, $S_GET$0, $isSymbol$0;if($S_polyfill$0){$S_SET$0 = $S_polyfill$0["__setObjectSetter__"];$S_GET$0 = $S_polyfill$0["__setObjectGetter__"];$isSymbol$0 = $S_polyfill$0["__isSymbol__"];$S_polyfill$0 = true;}else {$S_polyfill$0 = false}
var private1 = Symbol();var $S$0 = $S_polyfill$0 && typeof private1 === 'object' && $isSymbol$0(private1);
var private2 = Symbol();var $S$1 = $S_polyfill$0 && typeof private2 === 'object' && $isSymbol$0(private2);
var publicProp1 = 'public_prop1';
var publicProp2 = (function(){return 'public_prop1'})();var $S$2 = $S_polyfill$0 && typeof publicProp2 === 'object' && $isSymbol$0(publicProp2);
function createSymbolOrObject(createSymbol){ return createSymbol ? Symbol() : {} };
{// simple
var obj = {}, obj1 = {};
($S$0 ? $S_SET$0(obj) : obj)[private1] = 'private property 1';
var resultTrue = ($S$0 ? $S_GET$0(obj) : obj)[(private1)] == 'private property 1';
var resultfalse = ($S$0 ? $S_GET$0(obj1) : obj1)[private1] == void 0;
console.log(resultTrue, resultfalse);
$S_polyfill$0 && $S_GET$0(void 0);}
{// compound 1
var obj1$0 = {}, obj2 = {};
var result = (
(obj2[publicProp1] = 0)
, (($S$0 ? $S_SET$0(obj1$0) : obj1$0)[private1] = 0)
, (obj1$0[private1] = 1)
, (($S$0 ? $S_SET$0(obj2) : obj2)[private1] = 9)
, (($S$1 ? $S_SET$0(obj1$0) : obj1$0)[private2] = 2)
, (($S$1 ? $S_SET$0(obj2) : obj2)[private2] = 8)
, (obj1$0[publicProp1] = 0)
, (obj1$0[publicProp1] = 10)
&& (
obj2[publicProp1]
+ ($S$0 ? $S_GET$0(obj1$0) : obj1$0)[private1]
+ ($S$0 ? $S_GET$0(obj2) : obj2)[private1]
+ ($S$1 ? $S_GET$0(obj1$0) : obj1$0)[private2]
+ ($S$1 ? $S_GET$0(obj2) : obj2)[private2]
+ obj1$0[publicProp1]
)
)
console.log(result === 30);
$S_polyfill$0 && $S_GET$0(void 0);}
{// compound 2
var obj1$1 = {}, obj2$0 = {};
obj2$0[publicProp1] = 1;
($S$0 || $S$1 ? $S_SET$0(obj2$0) : obj2$0)[private1] = 2;
obj2$0[private2] = 3;
($S$2 ? $S_SET$0(obj1$1) : obj1$1)[publicProp2] = 1;
($S$0 ? $S_SET$0(obj2$0) : obj2$0)[private1] = 2;
($S$1 ? $S_SET$0(obj1$1) : obj1$1)[private2] = 3;
var result$0 = obj1$1[private2] + ($S$0 ? $S_GET$0(obj2$0) : obj2$0)[private1] + obj2$0[publicProp1] + ($S$0 ? $S_GET$0(obj2$0) : obj2$0)[private1] + ($S$1 ? $S_GET$0(obj2$0) : obj2$0)[private2] + obj1$1[publicProp2];
console.log(result$0 === 12);
$S_polyfill$0 && $S_GET$0(void 0);}
{// compound 3
var obj1$2 = {}, obj2$1 = {}, obj3 = {};
obj1$2[publicProp1] = 'public';
$S_SET$0(obj2$1)[private1] = obj1$2;
$S_SET$0(obj3)[private2] = obj2$1;
console.log($S_GET$0($S_GET$0(obj3[private2])[private1])[publicProp1] === 'public');
}
{// Object#methods
var obj$0 = {};
($S$0 || $S$1 ? $S_SET$0(obj$0) : obj$0)[private1] = 1;
obj$0[private2] = 2;
obj$0[publicProp1] = 7;
console.log( Object.keys(obj$0).join("|") === "public_prop1" );
console.log( Object.getOwnPropertyNames(obj$0).join("|") === "public_prop1" );
console.log( !Object.getPropertyNames || Object.getPropertyNames(obj$0).join("|") === "public_prop1" );
var symbols = Object.getOwnPropertySymbols(obj$0);
console.log(symbols.length === 2, symbols[0] === private1, symbols[1] === private2);
console.log(obj$0[private1] == 1, obj$0[private2] == 2);
}
{var $D$0;// simple typeof
var obj$1 = {}, _boolean = true, num = 123, typeOfPrivate1 = ($S$0 ? 'symbol' : typeof private1);
console.log(($S$0 ? 'symbol' : typeof private1) == 'symbol', typeof publicProp1 != 'symbol', typeOfPrivate1 == 'symbol');
console.log(typeof obj$1 == 'object', ($S$0 ? 'symbol' : typeof private1) != 'object');
console.log(typeof publicProp1 == 'string', typeof private1 != 'string');
console.log(typeof _boolean == 'boolean', typeof private1 != 'boolean');
console.log(typeof num == 'number', typeof private1 != 'number');
var unknown = createSymbolOrObject(true);var $S$2 = $S_polyfill$0 && typeof unknown === 'object' && $isSymbol$0(unknown);
console.log(($S$2 ? 'symbol' : typeof unknown) == 'symbol', (($D$0 = typeof (unknown = createSymbolOrObject())), ($S$2 = $S_polyfill$0 && typeof unknown === 'object' && $isSymbol$0(unknown)) ? 'symbol' : $D$0) == 'object', typeof unknown != 'string', typeof unknown !== 'number', typeof unknown !== 'boolean');
var savePrivate1 = private1;
((private1 = (function(){ return 'not_a_symbol' })()),($S$0 = $S_polyfill$0 && typeof private1 === 'object' && $isSymbol$0(private1)),private1);
console.log(($S$0 ? 'symbol' : typeof private1) == 'string', ($S$0 ? 'symbol' : typeof private1) != 'object', typeof private1 !== 'number', typeof private1 !== 'boolean');
private1 = savePrivate1;
}
function applyPolyfills() {
;(function symbolPolyfill() {
var symbolSupport = false
, obj = {}
, global = (new Function("return this"))()
, globalSymbol
;
try {// native Symbol support test
globalSymbol = global["Symbol"];
if( typeof globalSymbol === 'function' ) {
var symbol = global["Symbol"]();
if( typeof symbol === 'symbol' ) {
var random = Math.random();
obj[symbol] = random;
symbol = Object["getOwnPropertySymbols"](obj)[0];
symbolSupport = obj[symbol] === random;
}
}
}
catch(e){}
if( !symbolSupport ) {
var currentSymbolParentObject = void 0
, currentSymbolAssigmentIsGetter = false
, symbolsUniqueNamePart = "SYMBOL" + (Math.random() * 1e9 | 0)
, globalSymbolCounter = 0
, RE_isSymbolName = new RegExp("__\\$" + symbolsUniqueNamePart + "_\\d+\\$__")
, globalSybmolsMap = {}
, $getOwnPropertyNames = Object.getOwnPropertyNames
;
var SymbolConstructor = function SymbolConstructor(name) {
var symbolName = "__$" + symbolsUniqueNamePart + "_" + ++globalSymbolCounter + "$__";
globalSybmolsMap[symbolName] = this;
Object.freeze(Object.defineProperties(this, {
"name": {"value": name}
, "__$symbol$__": {"value": symbolName}
}));
};
SymbolConstructor.prototype = Object.create(null, {
"toString": {"value": function(){
if( !currentSymbolParentObject ) {
throw new TypeError("Conversion from symbol to string")
}
var stringProp = this["__$symbol$__"];
if( !currentSymbolAssigmentIsGetter && !(stringProp in currentSymbolParentObject) ) {
Object.defineProperty(currentSymbolParentObject, stringProp, {"value": void 0, "configurable": true, "writable": true});
}
return stringProp;
}}
, "constructor": {"value": SymbolConstructor, "configurable": true, "writable": true}
});
globalSymbol = global["Symbol"] = function notAConstructor(name) {
if (this instanceof notAConstructor) throw new Error("Symbol is not a constructor");
return new SymbolConstructor(name);
};
globalSymbol["__polyfill__"] = {
__isSymbol__: function(obj){ return obj && obj instanceof SymbolConstructor }
, __setObjectSetter__: function(parentObj){
currentSymbolParentObject = parentObj;
currentSymbolAssigmentIsGetter = false;
return parentObj;
}
, __setObjectGetter__: function(parentObj){
currentSymbolParentObject = parentObj;
currentSymbolAssigmentIsGetter = true;
return parentObj;
}
};
Object["getOwnPropertySymbols"] = function(obj) {
var result = [];
var names = $getOwnPropertyNames(obj);
for ( var i = 0, len = names.length ; i < len ; i++ ) {
var name = names[i];
if ( RE_isSymbolName.test(name) ) {
result.push(globalSybmolsMap[name]);
}
}
return result;
};
Object.getOwnPropertyNames = function(obj) {
var result = $getOwnPropertyNames(obj);
for ( var i = 0, len = result.length ; i < len ; i++ ) {
var name = result[i];
if ( RE_isSymbolName.test(name) ) {
result.splice(i, 1);
len--;
i--;
}
}
return result;
}
}
if( !globalSymbol["iterator"] )globalSymbol["iterator"] = globalSymbol("@@iterator");
if( !globalSymbol["create"] )globalSymbol["create"] = globalSymbol("@@create");
// TODO:: other pre-created symbol's
})();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment