Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: lib/typedObjects/arrayTypes.js

Issue 6273062181797888: Issue 505 - [Typed objects] Implement array helper methods (Closed)
Patch Set: Created May 17, 2014, 9:03 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « lib/typedObjects.js ('k') | test/tests/typedObjects.js » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/typedObjects/arrayTypes.js
===================================================================
--- a/lib/typedObjects/arrayTypes.js
+++ b/lib/typedObjects/arrayTypes.js
@@ -185,16 +185,87 @@ function createSizeWatcher(elementType,
removeBuffer(origBufferIndex, buffers, views);
}
}
return newSize;
}
}
+function push()
+{
+ let length = this.length | 0;
+ let newCount = arguments.length | 0;
+ this.length = length + newCount;
+ for (let i = 0; i < newCount; i++)
+ this.set(length + i, arguments[i]);
+ return length + newCount;
+}
+
+function pop()
+{
+ let length = this.length | 0;
+ if (length == 0)
+ throw new Error("No elements in the array");
+
+ let result = this.get(length - 1);
+ this.length = this.length - 1;
+ return result;
+}
+
+function splice(index, count)
+{
+ index = index | 0;
+ count = count | 0;
+ let length = this.length | 0;
+ if (index < 0)
René Jeschke 2014/05/19 08:35:53 What about the negative indexing one can use on 's
Wladimir Palant 2014/05/19 15:22:02 You are right, I wasn't aware that splice supporte
+ index = 0;
+ if (index > length)
+ index = length;
+ if (index + count > length)
+ count = length - index;
+
+ let newCount = (arguments.length | 0) - 2;
+ let diff = newCount - count;
+ let newLength = length + diff;
+ if (diff > 0)
+ {
+ this.length = newLength;
+ for (let i = length - 1; i >= index + count; i--)
+ this.set(i + diff, this.get(i));
+ }
+ else if (diff < 0)
+ {
+ for (let i = index + count; i < length; i++)
+ this.set(i + diff, this.get(i));
+ this.length = newLength;
+ }
+
+ for (let i = 0; i < newCount; i++)
+ this.set(index + i, arguments[i + 2]);
+}
+
+function unshift(value)
+{
+ let args = [0, 0].concat(Array.prototype.slice.apply(arguments));
+ this.splice.apply(this, args);
+ return this.length | 0;
+}
+
+function shift()
+{
+ let length = this.length | 0;
+ if (length == 0)
+ throw new Error("No elements in the array");
+
+ let result = this.get(0);
+ this.splice(0, 1);
+ return result;
+}
+
function createArrayType(elementType, typeDescriptor, meta)
{
if (typeof meta != "object" || meta == null)
meta = {};
// We need to make sure that all buffer chunks are big enough to hold a
// reference in order to manage the free chunks as a linked list. Each array
// buffer should be dedicated to arrays of particular size - the number of
@@ -237,16 +308,21 @@ function createArrayType(elementType, ty
typeDescriptor.arrayBufferIndex = int16;
typeDescriptor.arrayByteOffset = uint32;
typeDescriptor.getArrayBuffer = function()
{
return this.arrayBufferIndex >= 0 ? buffers[this.arrayBufferIndex] : null;
};
typeDescriptor.length = uint32;
typeDescriptor.size = uint32;
+ typeDescriptor.push = push;
+ typeDescriptor.pop = pop;
+ typeDescriptor.splice = splice;
+ typeDescriptor.unshift = unshift;
+ typeDescriptor.shift = shift;
let elementShift = ilog2(elementType.referenceLength | 0);
typeDescriptor.get = createGetter(elementGetter, elementShift);
typeDescriptor.set = createSetter(elementSetter, elementShift);
if (meta.hasOwnProperty("constructor") && typeof meta.constructor == "function")
meta.constructor = createCombinedConstructor(meta.constructor);
else
« no previous file with comments | « lib/typedObjects.js ('k') | test/tests/typedObjects.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld