Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 the V8 project authors. All rights reserved. | 1 // Copyright 2013 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
| 4 // met: | 4 // met: |
| 5 // | 5 // |
| 6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 28 "use strict"; | 28 "use strict"; |
| 29 | 29 |
| 30 // This file relies on the fact that the following declaration has been made | 30 // This file relies on the fact that the following declaration has been made |
| 31 // in runtime.js: | 31 // in runtime.js: |
| 32 // var $Array = global.Array; | 32 // var $Array = global.Array; |
| 33 var $ArrayBuffer = global.ArrayBuffer; | 33 var $ArrayBuffer = global.ArrayBuffer; |
| 34 | 34 |
| 35 | 35 |
| 36 // --------------- Typed Arrays --------------------- | 36 // --------------- Typed Arrays --------------------- |
| 37 | 37 |
| 38 function CreateTypedArrayConstructor(name, elementSize, arrayId, constructor) { | 38 macro TYPED_ARRAY_CONSTRUCTOR(ARRAY_ID, NAME, ELEMENT_SIZE) |
| 39 function ConstructByArrayBuffer(obj, buffer, byteOffset, length) { | 39 function NAMEConstructor(arg1, arg2, arg3) { |
| 40 var offset = ToPositiveInteger(byteOffset, "invalid_typed_array_length") | 40 function ConstructByArrayBuffer(obj, buffer, byteOffset, length) { |
| 41 var offset = ToPositiveInteger(byteOffset, "invalid_typed_array_length") | |
| 41 | 42 |
| 42 if (offset % elementSize !== 0) { | 43 if (offset % ELEMENT_SIZE !== 0) { |
| 43 throw MakeRangeError("invalid_typed_array_alignment", | 44 throw MakeRangeError("invalid_typed_array_alignment", |
| 44 "start offset", name, elementSize); | 45 "start offset", "NAME", ELEMENT_SIZE); |
| 45 } | 46 } |
| 46 var bufferByteLength = %ArrayBufferGetByteLength(buffer); | 47 var bufferByteLength = %ArrayBufferGetByteLength(buffer); |
| 47 if (offset > bufferByteLength) { | 48 if (offset > bufferByteLength) { |
| 48 throw MakeRangeError("invalid_typed_array_offset"); | 49 throw MakeRangeError("invalid_typed_array_offset"); |
| 50 } | |
| 51 | |
| 52 var newByteLength; | |
| 53 var newLength; | |
| 54 if (IS_UNDEFINED(length)) { | |
| 55 if (bufferByteLength % ELEMENT_SIZE !== 0) { | |
| 56 throw MakeRangeError("invalid_typed_array_alignment", | |
| 57 "byte length", "NAME", ELEMENT_SIZE); | |
| 58 } | |
| 59 newByteLength = bufferByteLength - offset; | |
| 60 newLength = newByteLength / ELEMENT_SIZE; | |
| 61 } else { | |
| 62 var newLength = ToPositiveInteger(length, "invalid_typed_array_length"); | |
| 63 newByteLength = newLength * ELEMENT_SIZE; | |
| 64 } | |
| 65 if (offset + newByteLength > bufferByteLength) { | |
| 66 throw MakeRangeError("invalid_typed_array_length"); | |
| 67 } | |
| 68 %TypedArrayInitialize(obj, ARRAY_ID, buffer, offset, newByteLength); | |
| 49 } | 69 } |
| 50 | 70 |
| 51 var newByteLength; | 71 function ConstructByLength(obj, length) { |
| 52 var newLength; | 72 var l = ToPositiveInteger(length, "invalid_typed_array_length"); |
| 53 if (IS_UNDEFINED(length)) { | 73 var byteLength = l * ELEMENT_SIZE; |
| 54 if (bufferByteLength % elementSize !== 0) { | 74 var buffer = new $ArrayBuffer(byteLength); |
| 55 throw MakeRangeError("invalid_typed_array_alignment", | 75 %TypedArrayInitialize(obj, ARRAY_ID, buffer, 0, byteLength); |
| 56 "byte length", name, elementSize); | |
| 57 } | |
| 58 newByteLength = bufferByteLength - offset; | |
| 59 newLength = newByteLength / elementSize; | |
| 60 } else { | |
| 61 var newLength = ToPositiveInteger(length, "invalid_typed_array_length"); | |
| 62 newByteLength = newLength * elementSize; | |
| 63 } | 76 } |
| 64 if (offset + newByteLength > bufferByteLength) { | |
| 65 throw MakeRangeError("invalid_typed_array_length"); | |
| 66 } | |
| 67 %TypedArrayInitialize(obj, arrayId, buffer, offset, newByteLength); | |
| 68 } | |
| 69 | 77 |
| 70 function ConstructByLength(obj, length) { | 78 function ConstructByArrayLike(obj, arrayLike) { |
| 71 var l = ToPositiveInteger(length, "invalid_typed_array_length"); | 79 var length = arrayLike.length; |
| 72 var byteLength = l * elementSize; | 80 var l = ToPositiveInteger(length, "invalid_typed_array_length"); |
| 73 var buffer = new $ArrayBuffer(byteLength); | 81 if(!%TypedArrayInitializeFromArrayLike(obj, ARRAY_ID, arrayLike, l)) { |
| 74 %TypedArrayInitialize(obj, arrayId, buffer, 0, byteLength); | 82 for (var i = 0; i < l; i++) { |
| 75 } | 83 // It is crucial that we let any execptions from arrayLike[i] |
| 76 | 84 // propagate outside the function. |
| 77 function ConstructByArrayLike(obj, arrayLike) { | 85 obj[i] = arrayLike[i]; |
| 78 var length = arrayLike.length; | 86 } |
| 79 var l = ToPositiveInteger(length, "invalid_typed_array_length"); | |
| 80 if(!%TypedArrayInitializeFromArrayLike(obj, arrayId, arrayLike, l)) { | |
| 81 for (var i = 0; i < l; i++) { | |
| 82 // It is crucial that we let any execptions from arrayLike[i] | |
| 83 // propagate outside the function. | |
| 84 obj[i] = arrayLike[i]; | |
| 85 } | 87 } |
| 86 } | 88 } |
| 87 } | |
| 88 | 89 |
| 89 return function (arg1, arg2, arg3) { | |
| 90 if (%_IsConstructCall()) { | 90 if (%_IsConstructCall()) { |
| 91 if (IS_ARRAYBUFFER(arg1)) { | 91 if (IS_ARRAYBUFFER(arg1)) { |
| 92 ConstructByArrayBuffer(this, arg1, arg2, arg3); | 92 ConstructByArrayBuffer(this, arg1, arg2, arg3); |
| 93 } else if (IS_NUMBER(arg1) || IS_STRING(arg1) || | 93 } else if (IS_NUMBER(arg1) || IS_STRING(arg1) || |
| 94 IS_BOOLEAN(arg1) || IS_UNDEFINED(arg1)) { | 94 IS_BOOLEAN(arg1) || IS_UNDEFINED(arg1)) { |
| 95 ConstructByLength(this, arg1); | 95 ConstructByLength(this, arg1); |
| 96 } else { | 96 } else { |
| 97 ConstructByArrayLike(this, arg1); | 97 ConstructByArrayLike(this, arg1); |
| 98 } | 98 } |
| 99 } else { | 99 } else { |
| 100 throw MakeTypeError("constructor_not_function", [name]) | 100 throw MakeTypeError("constructor_not_function", ["NAME"]) |
| 101 } | 101 } |
| 102 } | 102 } |
| 103 } | 103 endmacro |
| 104 | |
| 105 // arrayIds below should be synchronized with Runtime_TypedArrayInitialize. | |
| 106 TYPED_ARRAY_CONSTRUCTOR(1, Uint8Array, 1) | |
| 107 TYPED_ARRAY_CONSTRUCTOR(2, Int8Array, 1) | |
| 108 TYPED_ARRAY_CONSTRUCTOR(3, Uint16Array, 2) | |
| 109 TYPED_ARRAY_CONSTRUCTOR(4, Int16Array, 2) | |
| 110 TYPED_ARRAY_CONSTRUCTOR(5, Uint32Array, 4) | |
| 111 TYPED_ARRAY_CONSTRUCTOR(6, Int32Array, 4) | |
| 112 TYPED_ARRAY_CONSTRUCTOR(7, Float32Array, 4) | |
| 113 TYPED_ARRAY_CONSTRUCTOR(8, Float64Array, 8) | |
| 114 TYPED_ARRAY_CONSTRUCTOR(9, Uint8ClampedArray, 1) | |
| 104 | 115 |
| 105 function TypedArrayGetBuffer() { | 116 function TypedArrayGetBuffer() { |
| 106 return %TypedArrayGetBuffer(this); | 117 return %TypedArrayGetBuffer(this); |
| 107 } | 118 } |
| 108 | 119 |
| 109 function TypedArrayGetByteLength() { | 120 function TypedArrayGetByteLength() { |
| 110 return %TypedArrayGetByteLength(this); | 121 return %TypedArrayGetByteLength(this); |
| 111 } | 122 } |
| 112 | 123 |
| 113 function TypedArrayGetByteOffset() { | 124 function TypedArrayGetByteOffset() { |
| (...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 240 if (intOffset + l > this.length) { | 251 if (intOffset + l > this.length) { |
| 241 throw MakeRangeError("typed_array_set_source_too_large"); | 252 throw MakeRangeError("typed_array_set_source_too_large"); |
| 242 } | 253 } |
| 243 TypedArraySetFromArrayLike(this, obj, l, intOffset); | 254 TypedArraySetFromArrayLike(this, obj, l, intOffset); |
| 244 return; | 255 return; |
| 245 } | 256 } |
| 246 } | 257 } |
| 247 | 258 |
| 248 // ------------------------------------------------------------------- | 259 // ------------------------------------------------------------------- |
| 249 | 260 |
| 250 function SetupTypedArray(arrayId, name, constructor, elementSize) { | 261 function SetupTypedArray(constructor, fun, elementSize) { |
| 251 %CheckIsBootstrapping(); | 262 %CheckIsBootstrapping(); |
| 252 var fun = CreateTypedArrayConstructor(name, elementSize, | |
| 253 arrayId, constructor); | |
| 254 %SetCode(constructor, fun); | 263 %SetCode(constructor, fun); |
| 255 %FunctionSetPrototype(constructor, new $Object()); | 264 %FunctionSetPrototype(constructor, new $Object()); |
| 256 | 265 |
| 257 %SetProperty(constructor, "BYTES_PER_ELEMENT", elementSize, | 266 %SetProperty(constructor, "BYTES_PER_ELEMENT", elementSize, |
| 258 READ_ONLY | DONT_ENUM | DONT_DELETE); | 267 READ_ONLY | DONT_ENUM | DONT_DELETE); |
| 259 %SetProperty(constructor.prototype, | 268 %SetProperty(constructor.prototype, |
| 260 "constructor", constructor, DONT_ENUM); | 269 "constructor", constructor, DONT_ENUM); |
| 261 %SetProperty(constructor.prototype, | 270 %SetProperty(constructor.prototype, |
| 262 "BYTES_PER_ELEMENT", elementSize, | 271 "BYTES_PER_ELEMENT", elementSize, |
| 263 READ_ONLY | DONT_ENUM | DONT_DELETE); | 272 READ_ONLY | DONT_ENUM | DONT_DELETE); |
| 264 InstallGetter(constructor.prototype, "buffer", TypedArrayGetBuffer); | 273 InstallGetter(constructor.prototype, "buffer", TypedArrayGetBuffer); |
| 265 InstallGetter(constructor.prototype, "byteOffset", TypedArrayGetByteOffset); | 274 InstallGetter(constructor.prototype, "byteOffset", TypedArrayGetByteOffset); |
| 266 InstallGetter(constructor.prototype, "byteLength", TypedArrayGetByteLength); | 275 InstallGetter(constructor.prototype, "byteLength", TypedArrayGetByteLength); |
| 267 InstallGetter(constructor.prototype, "length", TypedArrayGetLength); | 276 InstallGetter(constructor.prototype, "length", TypedArrayGetLength); |
| 268 | 277 |
| 269 InstallFunctions(constructor.prototype, DONT_ENUM, $Array( | 278 InstallFunctions(constructor.prototype, DONT_ENUM, $Array( |
| 270 "subarray", CreateSubArray(elementSize, constructor), | 279 "subarray", CreateSubArray(elementSize, constructor), |
| 271 "set", TypedArraySet | 280 "set", TypedArraySet |
| 272 )); | 281 )); |
| 273 } | 282 } |
| 274 | 283 |
| 275 // arrayIds below should be synchronized with Runtime_TypedArrayInitialize. | 284 |
| 276 SetupTypedArray(1, "Uint8Array", global.Uint8Array, 1); | 285 macro SETUP_TYPED_ARRAY(NAME, ELEMENT_SIZE) |
|
rossberg
2013/10/28 10:40:55
I'd argue that using a macro here is overkill, but
| |
| 277 SetupTypedArray(2, "Int8Array", global.Int8Array, 1); | 286 SetupTypedArray(global.NAME, NAMEConstructor, ELEMENT_SIZE); |
| 278 SetupTypedArray(3, "Uint16Array", global.Uint16Array, 2); | 287 endmacro |
| 279 SetupTypedArray(4, "Int16Array", global.Int16Array, 2); | 288 |
| 280 SetupTypedArray(5, "Uint32Array", global.Uint32Array, 4); | 289 SETUP_TYPED_ARRAY(Uint8Array, 1) |
| 281 SetupTypedArray(6, "Int32Array", global.Int32Array, 4); | 290 SETUP_TYPED_ARRAY(Int8Array, 1) |
| 282 SetupTypedArray(7, "Float32Array", global.Float32Array, 4); | 291 SETUP_TYPED_ARRAY(Uint16Array, 2) |
| 283 SetupTypedArray(8, "Float64Array", global.Float64Array, 8); | 292 SETUP_TYPED_ARRAY(Int16Array, 2) |
| 284 SetupTypedArray(9, "Uint8ClampedArray", global.Uint8ClampedArray, 1); | 293 SETUP_TYPED_ARRAY(Uint32Array, 4) |
| 294 SETUP_TYPED_ARRAY(Int32Array, 4) | |
| 295 SETUP_TYPED_ARRAY(Float32Array, 4) | |
| 296 SETUP_TYPED_ARRAY(Float64Array, 8) | |
| 297 SETUP_TYPED_ARRAY(Uint8ClampedArray, 1) | |
| 285 | 298 |
| 286 | 299 |
| 287 // --------------------------- DataView ----------------------------- | 300 // --------------------------- DataView ----------------------------- |
| 288 | 301 |
| 289 var $DataView = global.DataView; | 302 var $DataView = global.DataView; |
| 290 | 303 |
| 291 function DataViewConstructor(buffer, byteOffset, byteLength) { // length = 3 | 304 function DataViewConstructor(buffer, byteOffset, byteLength) { // length = 3 |
| 292 if (%_IsConstructCall()) { | 305 if (%_IsConstructCall()) { |
| 293 if (!IS_ARRAYBUFFER(buffer)) { | 306 if (!IS_ARRAYBUFFER(buffer)) { |
| 294 throw MakeTypeError('data_view_not_array_buffer', []); | 307 throw MakeTypeError('data_view_not_array_buffer', []); |
| (...skipping 293 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 588 | 601 |
| 589 "getFloat32", DataViewGetFloat32, | 602 "getFloat32", DataViewGetFloat32, |
| 590 "setFloat32", DataViewSetFloat32, | 603 "setFloat32", DataViewSetFloat32, |
| 591 | 604 |
| 592 "getFloat64", DataViewGetFloat64, | 605 "getFloat64", DataViewGetFloat64, |
| 593 "setFloat64", DataViewSetFloat64 | 606 "setFloat64", DataViewSetFloat64 |
| 594 )); | 607 )); |
| 595 } | 608 } |
| 596 | 609 |
| 597 SetupDataView(); | 610 SetupDataView(); |
| OLD | NEW |