Comparing version 0.0.6 to 0.0.7
{ | ||
"name": "traceur", | ||
"version": "0.0.6", | ||
"version": "0.0.7", | ||
"description": "Experimental ES6 to ES5 compiler", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -27,7 +27,6 @@ // Copyright 2013 Traceur Authors. | ||
function mkdirRecursive(dir) { | ||
var parts = path.normalize(dir).split('/'); | ||
var parts = path.normalize(dir).split(path.sep); | ||
dir = ''; | ||
for (var i = 0; i < parts.length; i++) { | ||
dir += parts[i] + '/'; | ||
dir += parts[i] + path.sep; | ||
if (!existsSync(dir)) { | ||
@@ -45,4 +44,4 @@ fs.mkdirSync(dir, 0x1FD); // 0775 permissions | ||
function removeCommonPrefix(basedir, filedir) { | ||
var baseparts = basedir.split('/'); | ||
var fileparts = filedir.split('/'); | ||
var baseparts = basedir.split(path.sep); | ||
var fileparts = filedir.split(path.sep); | ||
@@ -53,3 +52,3 @@ var i = 0; | ||
} | ||
return fileparts.slice(i).join('/'); | ||
return fileparts.slice(i).join(path.sep); | ||
} | ||
@@ -56,0 +55,0 @@ |
@@ -53,3 +53,3 @@ // Copyright 2013 Traceur Authors. | ||
if (!(m = opt.match(/^([\w\-]+)(:{0,2})$/))) { | ||
throw new Error('invalid option initializer: ' + opt); | ||
throw new Error('invalid option initialiser: ' + opt); | ||
} | ||
@@ -56,0 +56,0 @@ this.opts_[m[1]] = {name: m[1], arg: m[2], data: data}; |
@@ -147,2 +147,14 @@ // Copyright 2012 Traceur Authors. | ||
// ### Symbols | ||
// | ||
// Symbols are emulated using an object which is an instance of SymbolValue. | ||
// Calling Symbol as a function returns a symbol value object. | ||
// | ||
// If options.symbols is enabled then all property accesses are transformed | ||
// into runtime calls which uses the internal string as the real property | ||
// name. | ||
// | ||
// If options.symbols is disabled symbols just toString as their internal | ||
// representation, making them work but leak as enumerable properties. | ||
var counter = 0; | ||
@@ -158,160 +170,144 @@ | ||
var nameRe = /^__\$(?:\d+)\$(?:\d+)\$__$/; | ||
// The string used for the real property. | ||
var symbolInternalProperty = newUniqueString(); | ||
var symbolDescriptionProperty = newUniqueString(); | ||
var internalStringValueName = newUniqueString(); | ||
// Used for the Symbol wrapper | ||
var symbolDataProperty = newUniqueString(); | ||
// All symbol values are kept in this map. This is so that we can get back to | ||
// the symbol object if all we have is the string key representing the symbol. | ||
var symbolValues = Object.create(null); | ||
function isSymbol(symbol) { | ||
return typeof symbol === 'object' && symbol instanceof SymbolValue; | ||
} | ||
function typeOf(v) { | ||
if (isSymbol(v)) | ||
return 'symbol'; | ||
return typeof v; | ||
} | ||
/** | ||
* Creates a new private name object. | ||
* Creates a new unique symbol object. | ||
* @param {string=} string Optional string used for toString. | ||
* @constructor | ||
*/ | ||
function Name(string) { | ||
if (!string) | ||
string = newUniqueString(); | ||
$defineProperty(this, internalStringValueName, {value: newUniqueString()}); | ||
function Symbol(description) { | ||
var value = new SymbolValue(description); | ||
if (!(this instanceof Symbol)) | ||
return value; | ||
function toString() { | ||
return string; | ||
} | ||
$freeze(toString); | ||
$freeze(toString.prototype); | ||
var toStringDescr = method(toString); | ||
$defineProperty(this, 'toString', toStringDescr); | ||
// new Symbol should throw. | ||
// | ||
// There are two ways to get a wrapper to a symbol. Either by doing | ||
// Object(symbol) or call a non strict function using a symbol value as | ||
// this. To correctly handle these two would require a lot of work for very | ||
// little gain so we are not doing those at the moment. | ||
throw new TypeError('Symbol cannot be new\'ed'); | ||
} | ||
this.public = $freeze($create(null, { | ||
toString: method($freeze(function toString() { | ||
return string; | ||
})) | ||
})); | ||
$freeze(this.public.toString.prototype); | ||
$defineProperty(Symbol.prototype, 'constructor', nonEnum(Symbol)); | ||
$defineProperty(Symbol.prototype, 'toString', method(function() { | ||
var symbolValue = this[symbolDataProperty]; | ||
if (!getOption('symbols')) | ||
return symbolValue[symbolInternalProperty]; | ||
if (!symbolValue) | ||
throw TypeError('Conversion from symbol to string'); | ||
var desc = symbolValue[symbolDescriptionProperty]; | ||
if (desc === undefined) | ||
desc = ''; | ||
return 'Symbol(' + desc + ')'; | ||
})); | ||
$defineProperty(Symbol.prototype, 'valueOf', method(function() { | ||
var symbolValue = this[symbolDataProperty]; | ||
if (!symbolValue) | ||
throw TypeError('Conversion from symbol to string'); | ||
if (!getOption('symbols')) | ||
return symbolValue[symbolInternalProperty]; | ||
return symbolValue; | ||
})); | ||
function SymbolValue(description) { | ||
var key = newUniqueString(); | ||
$defineProperty(this, symbolDataProperty, {value: this}); | ||
$defineProperty(this, symbolInternalProperty, {value: key}); | ||
$defineProperty(this, symbolDescriptionProperty, {value: description}); | ||
$freeze(this); | ||
}; | ||
$freeze(Name); | ||
$freeze(Name.prototype); | ||
function assertName(val) { | ||
if (!NameModule.isName(val)) | ||
throw new TypeError(val + ' is not a Name'); | ||
return val; | ||
symbolValues[key] = this; | ||
} | ||
$defineProperty(SymbolValue.prototype, 'constructor', nonEnum(Symbol)); | ||
$defineProperty(SymbolValue.prototype, 'toString', { | ||
value: Symbol.prototype.toString, | ||
enumerable: false | ||
}); | ||
$defineProperty(SymbolValue.prototype, 'valueOf', { | ||
value: Symbol.prototype.valueOf, | ||
enumerable: false | ||
}); | ||
$freeze(SymbolValue.prototype); | ||
// Private name. | ||
Symbol.iterator = Symbol(); | ||
// Collection getters and setters | ||
var elementDeleteName = new Name(); | ||
var elementGetName = new Name(); | ||
var elementSetName = new Name(); | ||
function toProperty(name) { | ||
if (isSymbol(name)) | ||
return name[symbolInternalProperty]; | ||
return name; | ||
} | ||
// HACK: We should use runtime/modules/std/name.js or something like that. | ||
var NameModule = $freeze({ | ||
Name: function(str) { | ||
return new Name(str); | ||
}, | ||
isName: function(x) { | ||
return x instanceof Name; | ||
}, | ||
elementGet: elementGetName, | ||
elementSet: elementSetName, | ||
elementDelete: elementDeleteName | ||
}); | ||
var filter = Array.prototype.filter.call.bind(Array.prototype.filter); | ||
// Override getOwnPropertyNames to filter out private name keys. | ||
function getOwnPropertyNames(object) { | ||
return filter($getOwnPropertyNames(object), function(str) { | ||
return !nameRe.test(str); | ||
}); | ||
} | ||
// Override Object.prototpe.hasOwnProperty to always return false for | ||
// private names. | ||
function hasOwnProperty(name) { | ||
if (NameModule.isName(name) || nameRe.test(name)) | ||
return false; | ||
return $hasOwnProperty.call(this, name); | ||
} | ||
function elementDelete(object, name) { | ||
if (traceur.options.trapMemberLookup && | ||
hasPrivateNameProperty(object, elementDeleteName)) { | ||
return getProperty(object, elementDeleteName).call(object, name); | ||
var rv = []; | ||
var names = $getOwnPropertyNames(object); | ||
for (var i = 0; i < names.length; i++) { | ||
var name = names[i]; | ||
if (!symbolValues[name]) | ||
rv.push(name); | ||
} | ||
return deleteProperty(object, name); | ||
return rv; | ||
} | ||
function elementGet(object, name) { | ||
if (traceur.options.trapMemberLookup && | ||
hasPrivateNameProperty(object, elementGetName)) { | ||
return getProperty(object, elementGetName).call(object, name); | ||
} | ||
return getProperty(object, name); | ||
function getOwnPropertyDescriptor(object, name) { | ||
return $getOwnPropertyDescriptor(object, toProperty(name)); | ||
} | ||
function elementHas(object, name) { | ||
// Should we allow trapping this too? | ||
return has(object, name); | ||
} | ||
function elementSet(object, name, value) { | ||
if (traceur.options.trapMemberLookup && | ||
hasPrivateNameProperty(object, elementSetName)) { | ||
getProperty(object, elementSetName).call(object, name, value); | ||
} else { | ||
setProperty(object, name, value); | ||
function getOwnPropertySymbols(object) { | ||
var rv = []; | ||
var names = $getOwnPropertyNames(object); | ||
for (var i = 0; i < names.length; i++) { | ||
var symbol = symbolValues[names[i]]; | ||
if (symbol) | ||
rv.push(symbol); | ||
} | ||
return value; | ||
return rv; | ||
} | ||
function assertNotName(s) { | ||
if (nameRe.test(s)) | ||
throw Error('Invalid access to private name'); | ||
// Override Object.prototpe.hasOwnProperty to always return false for | ||
// private names. | ||
function hasOwnProperty(name) { | ||
return $hasOwnProperty.call(this, toProperty(name)); | ||
} | ||
function deleteProperty(object, name) { | ||
if (NameModule.isName(name)) | ||
return delete object[name[internalStringValueName]]; | ||
if (nameRe.test(name)) | ||
return true; | ||
return delete object[name]; | ||
function getOption(name) { | ||
return global.traceur && global.traceur.options[name]; | ||
} | ||
function getProperty(object, name) { | ||
if (NameModule.isName(name)) | ||
return object[name[internalStringValueName]]; | ||
if (nameRe.test(name)) | ||
return undefined; | ||
return object[name]; | ||
} | ||
function hasPrivateNameProperty(object, name) { | ||
return name[internalStringValueName] in Object(object); | ||
} | ||
function has(object, name) { | ||
if (NameModule.isName(name) || nameRe.test(name)) | ||
return false; | ||
return name in Object(object); | ||
} | ||
// This is a bit simplistic. | ||
// http://wiki.ecmascript.org/doku.php?id=strawman:refactoring_put#object._get_set_property_built-ins | ||
function setProperty(object, name, value) { | ||
if (NameModule.isName(name)) { | ||
var descriptor = $getPropertyDescriptor(object, | ||
[name[internalStringValueName]]); | ||
if (descriptor) | ||
object[name[internalStringValueName]] = value; | ||
else | ||
$defineProperty(object, name[internalStringValueName], nonEnum(value)); | ||
} else { | ||
assertNotName(name); | ||
object[name] = value; | ||
var sym, desc; | ||
if (isSymbol(name)) { | ||
sym = name; | ||
name = name[symbolInternalProperty]; | ||
} | ||
object[name] = value; | ||
if (sym && (desc = $getOwnPropertyDescriptor(object, name))) | ||
$defineProperty(object, name, {enumerable: false}); | ||
return value; | ||
} | ||
function defineProperty(object, name, descriptor) { | ||
if (NameModule.isName(name)) { | ||
// Private names should never be enumerable. | ||
if (isSymbol(name)) { | ||
// Symbols should not be enumerable. We need to create a new descriptor | ||
// before calling the original defineProperty because the property might | ||
// be made non configurable. | ||
if (descriptor.enumerable) { | ||
@@ -322,40 +318,20 @@ descriptor = Object.create(descriptor, { | ||
} | ||
$defineProperty(object, name[internalStringValueName], descriptor); | ||
} else { | ||
assertNotName(name); | ||
$defineProperty(object, name, descriptor); | ||
name = name[symbolInternalProperty]; | ||
} | ||
$defineProperty(object, name, descriptor); | ||
return object; | ||
} | ||
function $getPropertyDescriptor(obj, name) { | ||
while (obj !== null) { | ||
var result = $getOwnPropertyDescriptor(obj, name); | ||
if (result) | ||
return result; | ||
obj = $getPrototypeOf(obj); | ||
} | ||
return undefined; | ||
} | ||
function getPropertyDescriptor(obj, name) { | ||
if (NameModule.isName(name)) | ||
return undefined; | ||
assertNotName(name); | ||
return $getPropertyDescriptor(obj, name); | ||
} | ||
function polyfillObject(Object) { | ||
$defineProperty(Object, 'defineProperty', {value: defineProperty}); | ||
$defineProperty(Object, 'deleteProperty', method(deleteProperty)); | ||
$defineProperty(Object, 'getOwnPropertyNames', | ||
{value: getOwnPropertyNames}); | ||
$defineProperty(Object, 'getProperty', method(getProperty)); | ||
$defineProperty(Object, 'getPropertyDescriptor', | ||
method(getPropertyDescriptor)); | ||
$defineProperty(Object, 'has', method(has)); | ||
$defineProperty(Object, 'setProperty', method(setProperty)); | ||
$defineProperty(Object, 'getOwnPropertyDescriptor', | ||
{value: getOwnPropertyDescriptor}); | ||
$defineProperty(Object.prototype, 'hasOwnProperty', | ||
{value: hasOwnProperty}); | ||
Object.getOwnPropertySymbols = getOwnPropertySymbols; | ||
// Object.is | ||
@@ -398,30 +374,8 @@ | ||
// Iterators. | ||
var iteratorName = new Name('iterator'); | ||
var IterModule = { | ||
get iterator() { | ||
return iteratorName; | ||
}, | ||
// TODO: Implement the rest of @iter and move it to a different file that | ||
// gets compiled. | ||
}; | ||
function getIterator(collection) { | ||
return getProperty(collection, iteratorName).call(collection); | ||
} | ||
function returnThis() { | ||
return this; | ||
} | ||
function addIterator(object) { | ||
// Generator instances are iterable. | ||
setProperty(object, iteratorName, returnThis); | ||
return object; | ||
} | ||
function polyfillArray(Array) { | ||
// Make arrays iterable. | ||
defineProperty(Array.prototype, IterModule.iterator, method(function() { | ||
// TODO(arv): This is not very robust to changes in the private names | ||
// option but fortunately this is not something that is expected to change | ||
// at runtime outside of tests. | ||
defineProperty(Array.prototype, Symbol.iterator, method(function() { | ||
var index = 0; | ||
@@ -530,9 +484,13 @@ var array = this; | ||
function PendingModule(func, self) { | ||
function ModuleImpl(url, func, self) { | ||
this.url = url; | ||
this.func = func; | ||
this.self = self; | ||
this.value_ = null; | ||
} | ||
PendingModule.prototype = { | ||
toModule: function() { | ||
return this.func.call(this.self); | ||
ModuleImpl.prototype = { | ||
get value() { | ||
if (this.value_) | ||
return this.value_; | ||
return this.value_ = this.func.call(this.self); | ||
} | ||
@@ -542,12 +500,9 @@ }; | ||
var modules = { | ||
get '@name'() { | ||
return NameModule; | ||
}, | ||
get '@iter'() { | ||
return IterModule; | ||
}, | ||
'@traceur/module': { | ||
PendingModule: PendingModule, | ||
ModuleImpl: ModuleImpl, | ||
registerModule: function(url, func, self) { | ||
modules[url] = new PendingModule(func, self); | ||
modules[url] = new ModuleImpl(url, func, self); | ||
}, | ||
getModuleImpl: function(url) { | ||
return modules[url].value; | ||
} | ||
@@ -560,5 +515,5 @@ } | ||
var module = modules[name]; | ||
if (module instanceof PendingModule) | ||
return modules[name] = module.toModule(); | ||
return module || null; | ||
if (module instanceof ModuleImpl) | ||
return modules[name] = module.value; | ||
return module; | ||
}, | ||
@@ -571,2 +526,7 @@ set: function(name, object) { | ||
function setupGlobals(global) { | ||
if (!global.Symbol) | ||
global.Symbol = Symbol; | ||
if (!global.Symbol.iterator) | ||
global.Symbol.iterator = Symbol(); | ||
polyfillString(global.String); | ||
@@ -582,23 +542,11 @@ polyfillObject(global.Object); | ||
// Return the runtime namespace. | ||
var runtime = { | ||
// This file is sometimes used without traceur.js so make it a new global. | ||
global.$traceurRuntime = { | ||
Deferred: Deferred, | ||
addIterator: addIterator, | ||
assertName: assertName, | ||
createName: NameModule.Name, | ||
deleteProperty: deleteProperty, | ||
elementDelete: elementDelete, | ||
elementGet: elementGet, | ||
elementHas: elementHas, | ||
elementSet: elementSet, | ||
getIterator: getIterator, | ||
getProperty: getProperty, | ||
setProperty: setProperty, | ||
setupGlobals: setupGlobals, | ||
has: has, | ||
toProperty: toProperty, | ||
typeof: typeOf, | ||
}; | ||
// This file is sometimes used without traceur.js. | ||
global.$traceurRuntime = runtime; | ||
})(typeof global !== 'undefined' ? global : this); |
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
17
853433
21289
9