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

Delta Between Two Patch Sets: lib/typedObjects/arrayTypes.js

Issue 29323484: Issue 507 - Implement lightweight array.slice() method (Closed)
Left Patch Set: Created Aug. 12, 2015, 4:41 p.m.
Right Patch Set: Small improvements Created Aug. 19, 2015, 12:14 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « lib/typedObjects.js ('k') | lib/typedObjects/objectTypes.js » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 /* 1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>, 2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-2015 Eyeo GmbH 3 * Copyright (C) 2006-2015 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 19 matching lines...) Expand all
30 { 30 {
31 // Carefully zero length + size if this is a slice of an array 31 // Carefully zero length + size if this is a slice of an array
32 Object.defineProperties(this, { 32 Object.defineProperties(this, {
33 length: fixedPropertyDescriptor(0), 33 length: fixedPropertyDescriptor(0),
34 size: fixedPropertyDescriptor(0) 34 size: fixedPropertyDescriptor(0)
35 }); 35 });
36 } 36 }
37 else 37 else
38 { 38 {
39 // Remove read-only length property for previously sliced arrays 39 // Remove read-only length property for previously sliced arrays
40 if (this.hasOwnProperty("length")) 40 delete this.length;
41 delete this.length;
42 41
43 this.length = 0; 42 this.length = 0;
44 this.size = 0; 43 this.size = 0;
45 } 44 }
46 }; 45 };
47 46
48 function createGetter(elementGetter, elementShift) 47 function createGetter(elementGetter, elementShift)
49 { 48 {
50 return function(index) 49 return function(index)
51 { 50 {
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after
222 if (length == 0) 221 if (length == 0)
223 throw new Error("No elements in the array"); 222 throw new Error("No elements in the array");
224 223
225 let result = this.get(length - 1); 224 let result = this.get(length - 1);
226 this.length = this.length - 1; 225 this.length = this.length - 1;
227 return result; 226 return result;
228 } 227 }
229 228
230 function createSlicer(elementShift) 229 function createSlicer(elementShift)
231 { 230 {
231 let {STATE_UNINITIALIZED} = require("typedObjects/objectTypes");
232
232 return function slice(start, end) 233 return function slice(start, end)
233 { 234 {
234 start = start | 0; 235 start = start | 0;
235 end = end | 0; 236 end = end | 0;
236 237
237 if (start < 0) 238 if (start < 0)
238 start = Math.max(0, this.length + start); 239 start = Math.max(0, this.length + start);
239 else 240 else
240 start = Math.min(start, this.length); 241 start = Math.min(start, this.length);
241 242
242 if (end > 0) 243 if (end > 0)
243 end = Math.min(0, end - this.length); 244 end = Math.min(0, end - this.length);
244 else 245 else
245 end = Math.max(end, -1 * this.length); 246 end = Math.max(end, -1 * this.length);
246 247
247 Object.defineProperties(this, { 248 Object.defineProperties(this, {
248 length: {value: this.length, configurable: true} 249 length: {value: this.length, configurable: true}
249 }); 250 });
250 this.retain(); 251 this.retain();
251 252
252 let result = Object.create(this, { 253 let result = Object.create(this, {
253 arrayByteOffset: fixedPropertyDescriptor( 254 arrayByteOffset: fixedPropertyDescriptor(
254 this.arrayByteOffset + start << elementShift 255 this.arrayByteOffset + start << elementShift
255 ), 256 ),
256 length: {value: this.length - start + end, configurable: true}, 257 length: {value: this.length - start + end, configurable: true},
257 _refCount: {value: 1, writable: true}, 258 _refCount: {value: 1, writable: true},
258 _copy: fixedPropertyDescriptor(true), 259 _copy: fixedPropertyDescriptor(true),
259 _state: {value: 0, writable: true} 260 _state: {value: STATE_UNINITIALIZED, writable: true}
260 }); 261 });
261 262
262 // TODO 263 // TODO
263 // - A sliced array will have a read-only length even if all slices are 264 // - A sliced array will have a read-only length even if all slices are
264 // released. We could fix this by keeping a count of slices, but is it 265 // released. We could fix this by keeping a count of slices, but is it
265 // worth it? 266 // worth it?
266 // - A slice of an array still keeps a reference to the array in it's 267 // - A slice of an array still keeps a reference to the array in it's
267 // __proto__ property after it's released. (We can't just replace it 268 // __proto__ property after it's released. (We can't just replace it
268 // with null as modifying an object's prototype is apparently very slow.) 269 // with null as modifying an object's prototype is apparently very slow.)
269 270
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after
402 meta.watch = {}; 403 meta.watch = {};
403 404
404 meta.watch.length = createLengthWatcher(elementType, elementSetter); 405 meta.watch.length = createLengthWatcher(elementType, elementSetter);
405 meta.watch.size = createSizeWatcher(elementType, minElements, bufferSize, buff ers, viewTypes, views, firstFree); 406 meta.watch.size = createSizeWatcher(elementType, minElements, bufferSize, buff ers, viewTypes, views, firstFree);
406 407
407 let {ObjectBase} = require("typedObjects/objectTypes"); 408 let {ObjectBase} = require("typedObjects/objectTypes");
408 return ObjectBase.extend(typeDescriptor, meta); 409 return ObjectBase.extend(typeDescriptor, meta);
409 } 410 }
410 411
411 exports.createArrayType = createArrayType; 412 exports.createArrayType = createArrayType;
LEFTRIGHT

Powered by Google App Engine
This is Rietveld