LEFT | RIGHT |
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 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
104 | 104 |
105 function createSetter(typeId, offset) | 105 function createSetter(typeId, offset) |
106 { | 106 { |
107 typeId = typeId | 0; | 107 typeId = typeId | 0; |
108 offset = offset | 0; | 108 offset = offset | 0; |
109 | 109 |
110 let views = Array.prototype.slice.call(arguments, 2); | 110 let views = Array.prototype.slice.call(arguments, 2); |
111 let reference = new Reference(types, views); | 111 let reference = new Reference(types, views); |
112 return function(value) | 112 return function(value) |
113 { | 113 { |
114 if (value && !isinstance(typeId, value)) | 114 if (value && !isInstance(typeId, value)) |
115 throw new Error("Incompatible type"); | 115 throw new Error("Incompatible type"); |
116 | 116 |
117 reference.bufferIndex = this.bufferIndex | 0; | 117 reference.bufferIndex = this.bufferIndex | 0; |
118 reference.byteOffset = (this.byteOffset | 0) + offset; | 118 reference.byteOffset = (this.byteOffset | 0) + offset; |
119 if (value) | 119 if (value) |
120 { | 120 { |
121 reference.typeId = value.typeId; | 121 reference.typeId = value.typeId; |
122 reference.targetBufferIndex = value.bufferIndex; | 122 reference.targetBufferIndex = value.bufferIndex; |
123 reference.targetByteOffset = value.byteOffset; | 123 reference.targetByteOffset = value.byteOffset; |
124 } | 124 } |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
202 Object.defineProperties(proto, methods); | 202 Object.defineProperties(proto, methods); |
203 | 203 |
204 // Round up to be a multiple of the maximal property size | 204 // Round up to be a multiple of the maximal property size |
205 byteLength = ((byteLength - 1) | (maxReferenceLength - 1)) + 1; | 205 byteLength = ((byteLength - 1) | (maxReferenceLength - 1)) + 1; |
206 | 206 |
207 // We need to be able to store a typed reference in the object's buffer | 207 // We need to be able to store a typed reference in the object's buffer |
208 byteLength = Math.max(byteLength, TypedReference.byteLength) | 0; | 208 byteLength = Math.max(byteLength, TypedReference.byteLength) | 0; |
209 let typedReferenceViews = getViewsForType(TypedReference, viewTypes, views); | 209 let typedReferenceViews = getViewsForType(TypedReference, viewTypes, views); |
210 | 210 |
211 // Take constructor from meta parameter, allow calling superclass constructor. | 211 // Take constructor from meta parameter, allow calling superclass constructor. |
212 let constructor = null; | 212 let constructor = parentTypeInfo && parentTypeInfo.constructor; |
213 if (meta.hasOwnProperty("constructor") && typeof meta.constructor == "function
") | 213 if (meta.hasOwnProperty("constructor") && typeof meta.constructor == "function
") |
214 { | 214 { |
215 if (parentTypeInfo && parentTypeInfo.constructor) | 215 if (constructor) |
216 constructor = createSubclassMethod(meta.constructor, parentTypeInfo.constr
uctor); | 216 constructor = createSubclassMethod(meta.constructor, constructor); |
217 else | 217 else |
218 constructor = meta.constructor; | 218 constructor = meta.constructor; |
219 } | 219 } |
220 | 220 |
221 let typeId = types.length | 0; | 221 let typeId = types.length | 0; |
222 let typeInfo = { | 222 let typeInfo = { |
223 byteLength: byteLength, | 223 byteLength: byteLength, |
224 bufferSize: "bufferSize" in meta ? Math.max(meta.bufferSize | 0, 2) : 128, | 224 bufferSize: "bufferSize" in meta ? Math.max(meta.bufferSize | 0, 2) : 128, |
225 firstFree: new TypedReference(typeId, typedReferenceViews), | 225 firstFree: new TypedReference(typeId, typedReferenceViews), |
226 proto: proto, | 226 proto: proto, |
227 properties: properties, | 227 properties: properties, |
228 methods: methods, | 228 methods: methods, |
229 buffers: buffers, | 229 buffers: buffers, |
230 viewTypes: viewTypes, | 230 viewTypes: viewTypes, |
231 views: views, | 231 views: views, |
232 typeId: typeId, | 232 typeId: typeId, |
233 parentTypeInfo: parentTypeInfo, | 233 parentTypeInfo: parentTypeInfo, |
234 constructor: constructor | 234 constructor: constructor |
235 }; | 235 }; |
236 | 236 |
237 let result = create.bind(typeInfo); | 237 let result = create.bind(typeInfo); |
238 Object.defineProperties(result, { | 238 Object.defineProperties(result, { |
239 byteLength: fixedPropertyDescriptor(byteLength), | 239 byteLength: fixedPropertyDescriptor(byteLength), |
240 | 240 |
241 referenceLength: fixedPropertyDescriptor(Reference.byteLength), | 241 referenceLength: fixedPropertyDescriptor(Reference.byteLength), |
242 viewTypes: fixedPropertyDescriptor(Reference.viewTypes), | 242 viewTypes: fixedPropertyDescriptor(Reference.viewTypes), |
243 | 243 |
244 typeId: fixedPropertyDescriptor(typeId), | 244 typeId: fixedPropertyDescriptor(typeId), |
245 extend: fixedPropertyDescriptor(extend.bind(null, typeInfo)), | 245 extend: fixedPropertyDescriptor(extend.bind(null, typeInfo)), |
246 isinstance: fixedPropertyDescriptor(isinstance.bind(null, typeId)), | 246 isInstance: fixedPropertyDescriptor(isInstance.bind(null, typeId)), |
247 | 247 |
248 createGetter: fixedPropertyDescriptor(createGetter), | 248 createGetter: fixedPropertyDescriptor(createGetter), |
249 createSetter: fixedPropertyDescriptor(createSetter.bind(null, typeId)) | 249 createSetter: fixedPropertyDescriptor(createSetter.bind(null, typeId)) |
250 }); | 250 }); |
251 types.push(typeInfo); | 251 types.push(typeInfo); |
252 return result; | 252 return result; |
253 } | 253 } |
254 | 254 |
255 function isinstance(typeId, obj) | 255 function isInstance(typeId, obj) |
256 { | 256 { |
257 typeId = typeId | 0; | 257 typeId = typeId | 0; |
258 | 258 |
259 // TODO: This could be optimized by compiling the list of all subclasses for | 259 // TODO: This could be optimized by compiling the list of all subclasses for |
260 // each type up front. Question is whether this is worth it. | 260 // each type up front. Question is whether this is worth it. |
261 let typeInfo = types[obj.typeId | 0]; | 261 let typeInfo = types[obj.typeId | 0]; |
262 while (typeInfo) | 262 while (typeInfo) |
263 { | 263 { |
264 if ((typeInfo.typeId | 0) == typeId) | 264 if ((typeInfo.typeId | 0) == typeId) |
265 return true; | 265 return true; |
266 typeInfo = typeInfo.parentTypeInfo; | 266 typeInfo = typeInfo.parentTypeInfo; |
267 } | 267 } |
268 return false; | 268 return false; |
269 } | 269 } |
270 | 270 |
271 let ObjectBase = exports.ObjectBase = extend(null, { | 271 let ObjectBase = exports.ObjectBase = extend(null, { |
272 equals: function(obj) | 272 equals: function(obj) |
273 { | 273 { |
274 if (!obj) | 274 if (!obj) |
275 return false; | 275 return false; |
276 return this.typeId == obj.typeId && this.bufferIndex == obj.bufferIndex && t
his.byteOffset == obj.byteOffset; | 276 return this.typeId == obj.typeId && this.bufferIndex == obj.bufferIndex && t
his.byteOffset == obj.byteOffset; |
277 } | 277 } |
278 }, null); | 278 }, null); |
279 | 279 |
280 exports.ObjectType = ObjectBase.extend; | 280 exports.ObjectType = ObjectBase.extend; |
LEFT | RIGHT |