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 19, 2014, 3:21 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)
220 {
221 index += length;
222 if (index < 0)
223 index = 0;
224 }
225 if (index > length)
226 index = length;
227 if (index + count > length)
228 count = length - index;
229
230 let newCount = (arguments.length | 0) - 2;
231 let diff = newCount - count;
232 let newLength = length + diff;
233 if (diff > 0)
234 {
235 this.length = newLength;
236 for (let i = length - 1; i >= index + count; i--)
237 this.set(i + diff, this.get(i));
238 }
239 else if (diff < 0)
240 {
241 for (let i = index + count; i < length; i++)
242 this.set(i + diff, this.get(i));
243 this.length = newLength;
244 }
245
246 for (let i = 0; i < newCount; i++)
247 this.set(index + i, arguments[i + 2]);
248 }
249
250 function unshift(value)
251 {
252 let args = [0, 0].concat(Array.prototype.slice.apply(arguments));
253 this.splice.apply(this, args);
254 return this.length | 0;
255 }
256
257 function shift()
258 {
259 let length = this.length | 0;
260 if (length == 0)
261 throw new Error("No elements in the array");
262
263 let result = this.get(0);
264 this.splice(0, 1);
265 return result;
266 }
267
193 function createArrayType(elementType, typeDescriptor, meta) 268 function createArrayType(elementType, typeDescriptor, meta)
194 { 269 {
195 if (typeof meta != "object" || meta == null) 270 if (typeof meta != "object" || meta == null)
196 meta = {}; 271 meta = {};
197 272
198 // We need to make sure that all buffer chunks are big enough to hold a 273 // 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 274 // 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 275 // 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. 276 // possible sizes is limited as the sizes can only be powers of two.
202 let {TypedReference} = require("typedObjects/references"); 277 let {TypedReference} = require("typedObjects/references");
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
235 let {int16, uint32} = require("typedObjects/primitiveTypes"); 310 let {int16, uint32} = require("typedObjects/primitiveTypes");
236 typeDescriptor = Object.create(typeDescriptor || {}); 311 typeDescriptor = Object.create(typeDescriptor || {});
237 typeDescriptor.arrayBufferIndex = int16; 312 typeDescriptor.arrayBufferIndex = int16;
238 typeDescriptor.arrayByteOffset = uint32; 313 typeDescriptor.arrayByteOffset = uint32;
239 typeDescriptor.getArrayBuffer = function() 314 typeDescriptor.getArrayBuffer = function()
240 { 315 {
241 return this.arrayBufferIndex >= 0 ? buffers[this.arrayBufferIndex] : null; 316 return this.arrayBufferIndex >= 0 ? buffers[this.arrayBufferIndex] : null;
242 }; 317 };
243 typeDescriptor.length = uint32; 318 typeDescriptor.length = uint32;
244 typeDescriptor.size = uint32; 319 typeDescriptor.size = uint32;
320 typeDescriptor.push = push;
321 typeDescriptor.pop = pop;
322 typeDescriptor.splice = splice;
323 typeDescriptor.unshift = unshift;
324 typeDescriptor.shift = shift;
245 325
246 let elementShift = ilog2(elementType.referenceLength | 0); 326 let elementShift = ilog2(elementType.referenceLength | 0);
247 typeDescriptor.get = createGetter(elementGetter, elementShift); 327 typeDescriptor.get = createGetter(elementGetter, elementShift);
248 typeDescriptor.set = createSetter(elementSetter, elementShift); 328 typeDescriptor.set = createSetter(elementSetter, elementShift);
249 329
250 if (meta.hasOwnProperty("constructor") && typeof meta.constructor == "function ") 330 if (meta.hasOwnProperty("constructor") && typeof meta.constructor == "function ")
251 meta.constructor = createCombinedConstructor(meta.constructor); 331 meta.constructor = createCombinedConstructor(meta.constructor);
252 else 332 else
253 meta.constructor = defaultArrayConstructor; 333 meta.constructor = defaultArrayConstructor;
254 334
255 if (meta.hasOwnProperty("destructor") && typeof meta.destructor == "function") 335 if (meta.hasOwnProperty("destructor") && typeof meta.destructor == "function")
256 meta.destructor = createCombinedDestructor(meta.destructor); 336 meta.destructor = createCombinedDestructor(meta.destructor);
257 else 337 else
258 meta.destructor = defaultArrayDestructor; 338 meta.destructor = defaultArrayDestructor;
259 339
260 if (!meta.watch || typeof meta.watch != "object") 340 if (!meta.watch || typeof meta.watch != "object")
261 meta.watch = {}; 341 meta.watch = {};
262 342
263 meta.watch.length = createLengthWatcher(elementType, elementSetter); 343 meta.watch.length = createLengthWatcher(elementType, elementSetter);
264 meta.watch.size = createSizeWatcher(elementType, minElements, bufferSize, buff ers, viewTypes, views, firstFree); 344 meta.watch.size = createSizeWatcher(elementType, minElements, bufferSize, buff ers, viewTypes, views, firstFree);
265 345
266 let {ObjectBase} = require("typedObjects/objectTypes"); 346 let {ObjectBase} = require("typedObjects/objectTypes");
267 return ObjectBase.extend(typeDescriptor, meta); 347 return ObjectBase.extend(typeDescriptor, meta);
268 } 348 }
269 349
270 exports.createArrayType = createArrayType; 350 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