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

Side by Side 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.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « lib/typedObjects.js ('k') | test/tests/typedObjects.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * This file is part of Adblock Plus <http://adblockplus.org/>, 2 * This file is part of Adblock Plus <http://adblockplus.org/>,
3 * Copyright (C) 2006-2014 Eyeo GmbH 3 * Copyright (C) 2006-2014 Eyeo GmbH
4 * 4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify 5 * Adblock Plus is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as 6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
8 * 8 *
9 * Adblock Plus is distributed in the hope that it will be useful, 9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after
183 dealloc(reference, origBufferIndex, origByteOffset); 183 dealloc(reference, origBufferIndex, origByteOffset);
184 else 184 else
185 removeBuffer(origBufferIndex, buffers, views); 185 removeBuffer(origBufferIndex, buffers, views);
186 } 186 }
187 } 187 }
188 188
189 return newSize; 189 return newSize;
190 } 190 }
191 } 191 }
192 192
193 function push()
194 {
195 let length = this.length | 0;
196 let newCount = arguments.length | 0;
197 this.length = length + newCount;
198 for (let i = 0; i < newCount; i++)
199 this.set(length + i, arguments[i]);
200 return length + newCount;
201 }
202
203 function pop()
204 {
205 let length = this.length | 0;
206 if (length == 0)
207 throw new Error("No elements in the array");
208
209 let result = this.get(length - 1);
210 this.length = this.length - 1;
211 return result;
212 }
213
214 function splice(index, count)
215 {
216 index = index | 0;
217 count = count | 0;
218 let length = this.length | 0;
219 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
220 index = 0;
221 if (index > length)
222 index = length;
223 if (index + count > length)
224 count = length - index;
225
226 let newCount = (arguments.length | 0) - 2;
227 let diff = newCount - count;
228 let newLength = length + diff;
229 if (diff > 0)
230 {
231 this.length = newLength;
232 for (let i = length - 1; i >= index + count; i--)
233 this.set(i + diff, this.get(i));
234 }
235 else if (diff < 0)
236 {
237 for (let i = index + count; i < length; i++)
238 this.set(i + diff, this.get(i));
239 this.length = newLength;
240 }
241
242 for (let i = 0; i < newCount; i++)
243 this.set(index + i, arguments[i + 2]);
244 }
245
246 function unshift(value)
247 {
248 let args = [0, 0].concat(Array.prototype.slice.apply(arguments));
249 this.splice.apply(this, args);
250 return this.length | 0;
251 }
252
253 function shift()
254 {
255 let length = this.length | 0;
256 if (length == 0)
257 throw new Error("No elements in the array");
258
259 let result = this.get(0);
260 this.splice(0, 1);
261 return result;
262 }
263
193 function createArrayType(elementType, typeDescriptor, meta) 264 function createArrayType(elementType, typeDescriptor, meta)
194 { 265 {
195 if (typeof meta != "object" || meta == null) 266 if (typeof meta != "object" || meta == null)
196 meta = {}; 267 meta = {};
197 268
198 // We need to make sure that all buffer chunks are big enough to hold a 269 // We need to make sure that all buffer chunks are big enough to hold a
199 // reference in order to manage the free chunks as a linked list. Each array 270 // reference in order to manage the free chunks as a linked list. Each array
200 // buffer should be dedicated to arrays of particular size - the number of 271 // buffer should be dedicated to arrays of particular size - the number of
201 // possible sizes is limited as the sizes can only be powers of two. 272 // possible sizes is limited as the sizes can only be powers of two.
202 let {TypedReference} = require("typedObjects/references"); 273 let {TypedReference} = require("typedObjects/references");
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
235 let {int16, uint32} = require("typedObjects/primitiveTypes"); 306 let {int16, uint32} = require("typedObjects/primitiveTypes");
236 typeDescriptor = Object.create(typeDescriptor || {}); 307 typeDescriptor = Object.create(typeDescriptor || {});
237 typeDescriptor.arrayBufferIndex = int16; 308 typeDescriptor.arrayBufferIndex = int16;
238 typeDescriptor.arrayByteOffset = uint32; 309 typeDescriptor.arrayByteOffset = uint32;
239 typeDescriptor.getArrayBuffer = function() 310 typeDescriptor.getArrayBuffer = function()
240 { 311 {
241 return this.arrayBufferIndex >= 0 ? buffers[this.arrayBufferIndex] : null; 312 return this.arrayBufferIndex >= 0 ? buffers[this.arrayBufferIndex] : null;
242 }; 313 };
243 typeDescriptor.length = uint32; 314 typeDescriptor.length = uint32;
244 typeDescriptor.size = uint32; 315 typeDescriptor.size = uint32;
316 typeDescriptor.push = push;
317 typeDescriptor.pop = pop;
318 typeDescriptor.splice = splice;
319 typeDescriptor.unshift = unshift;
320 typeDescriptor.shift = shift;
245 321
246 let elementShift = ilog2(elementType.referenceLength | 0); 322 let elementShift = ilog2(elementType.referenceLength | 0);
247 typeDescriptor.get = createGetter(elementGetter, elementShift); 323 typeDescriptor.get = createGetter(elementGetter, elementShift);
248 typeDescriptor.set = createSetter(elementSetter, elementShift); 324 typeDescriptor.set = createSetter(elementSetter, elementShift);
249 325
250 if (meta.hasOwnProperty("constructor") && typeof meta.constructor == "function ") 326 if (meta.hasOwnProperty("constructor") && typeof meta.constructor == "function ")
251 meta.constructor = createCombinedConstructor(meta.constructor); 327 meta.constructor = createCombinedConstructor(meta.constructor);
252 else 328 else
253 meta.constructor = defaultArrayConstructor; 329 meta.constructor = defaultArrayConstructor;
254 330
255 if (meta.hasOwnProperty("destructor") && typeof meta.destructor == "function") 331 if (meta.hasOwnProperty("destructor") && typeof meta.destructor == "function")
256 meta.destructor = createCombinedDestructor(meta.destructor); 332 meta.destructor = createCombinedDestructor(meta.destructor);
257 else 333 else
258 meta.destructor = defaultArrayDestructor; 334 meta.destructor = defaultArrayDestructor;
259 335
260 if (!meta.watch || typeof meta.watch != "object") 336 if (!meta.watch || typeof meta.watch != "object")
261 meta.watch = {}; 337 meta.watch = {};
262 338
263 meta.watch.length = createLengthWatcher(elementType, elementSetter); 339 meta.watch.length = createLengthWatcher(elementType, elementSetter);
264 meta.watch.size = createSizeWatcher(elementType, minElements, bufferSize, buff ers, viewTypes, views, firstFree); 340 meta.watch.size = createSizeWatcher(elementType, minElements, bufferSize, buff ers, viewTypes, views, firstFree);
265 341
266 let {ObjectBase} = require("typedObjects/objectTypes"); 342 let {ObjectBase} = require("typedObjects/objectTypes");
267 return ObjectBase.extend(typeDescriptor, meta); 343 return ObjectBase.extend(typeDescriptor, meta);
268 } 344 }
269 345
270 exports.createArrayType = createArrayType; 346 exports.createArrayType = createArrayType;
OLDNEW
« 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