2015-10-08 00:08:19 +00:00
|
|
|
'use strict';
|
|
|
|
|
2015-10-08 04:20:14 +00:00
|
|
|
let resizeListKey = exports.resizeListKey = Symbol();
|
2015-10-08 00:08:19 +00:00
|
|
|
|
2015-10-08 04:20:14 +00:00
|
|
|
exports.createList = createList;
|
2015-10-08 00:08:19 +00:00
|
|
|
|
2015-10-08 04:20:14 +00:00
|
|
|
function createList(prototype, getterForLength, getterForIndex, setterForIndex) {
|
|
|
|
var list = prototype ? Object.create(prototype) : {};
|
|
|
|
var size = 0;
|
2015-10-08 00:08:19 +00:00
|
|
|
|
2015-10-08 04:20:14 +00:00
|
|
|
Object.defineProperty(list, 'length', {get: getterForLength});
|
2015-10-08 00:08:19 +00:00
|
|
|
|
2015-10-08 04:20:14 +00:00
|
|
|
list[resizeListKey] = function(length) {
|
|
|
|
if (length == null) {
|
|
|
|
length = this.length;
|
|
|
|
}
|
|
|
|
if (length == size) {
|
2015-10-08 00:08:19 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-10-08 04:20:14 +00:00
|
|
|
if (length > size) {
|
|
|
|
let props = {};
|
|
|
|
|
|
|
|
for (let i = size; i < length; i++) {
|
|
|
|
props[i] = {
|
|
|
|
get: getterForIndex(i),
|
|
|
|
set: setterForIndex && setterForIndex(i),
|
|
|
|
enumerable: true,
|
|
|
|
configurable: true,
|
|
|
|
};
|
|
|
|
}
|
2015-10-08 00:08:19 +00:00
|
|
|
|
2015-10-08 04:20:14 +00:00
|
|
|
Object.defineProperties(this, props);
|
|
|
|
}
|
|
|
|
else if (length < size) {
|
|
|
|
for (let i = size - 1; i >= length; i--) {
|
|
|
|
delete this[i];
|
|
|
|
}
|
|
|
|
}
|
2015-10-08 00:08:19 +00:00
|
|
|
|
2015-10-08 04:20:14 +00:00
|
|
|
size = length;
|
|
|
|
};
|
2015-10-08 00:08:19 +00:00
|
|
|
|
2015-10-08 04:20:14 +00:00
|
|
|
return list;
|
2015-10-08 00:08:19 +00:00
|
|
|
}
|