| 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 16 matching lines...) Expand all Loading... |
| 27 | 27 |
| 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 macro TYPED_ARRAYS(FUNCTION) | |
| 38 // arrayIds below should be synchronized with Runtime_TypedArrayInitialize. | |
| 39 FUNCTION(1, Uint8Array, 1) | |
| 40 FUNCTION(2, Int8Array, 1) | |
| 41 FUNCTION(3, Uint16Array, 2) | |
| 42 FUNCTION(4, Int16Array, 2) | |
| 43 FUNCTION(5, Uint32Array, 4) | |
| 44 FUNCTION(6, Int32Array, 4) | |
| 45 FUNCTION(7, Float32Array, 4) | |
| 46 FUNCTION(8, Float64Array, 8) | |
| 47 FUNCTION(9, Uint8ClampedArray, 1) | |
| 48 endmacro | |
| 49 | |
| 50 macro TYPED_ARRAY_CONSTRUCTOR(ARRAY_ID, NAME, ELEMENT_SIZE) | |
| 51 function NAMEConstructByArrayBuffer(obj, buffer, byteOffset, length) { | |
| 52 var bufferByteLength = buffer.byteLength; | |
| 53 var offset; | |
| 54 if (IS_UNDEFINED(byteOffset)) { | |
| 55 offset = 0; | |
| 56 } else { | |
| 57 offset = ToPositiveInteger(byteOffset, "invalid_typed_array_length"); | |
| 58 | |
| 59 if (offset % ELEMENT_SIZE !== 0) { | |
| 60 throw MakeRangeError("invalid_typed_array_alignment", | |
| 61 "start offset", "NAME", ELEMENT_SIZE); | |
| 62 } | |
| 63 if (offset > bufferByteLength) { | |
| 64 throw MakeRangeError("invalid_typed_array_offset"); | |
| 65 } | |
| 66 } | |
| 67 | |
| 68 var newByteLength; | |
| 69 var newLength; | |
| 70 if (IS_UNDEFINED(length)) { | |
| 71 if (bufferByteLength % ELEMENT_SIZE !== 0) { | |
| 72 throw MakeRangeError("invalid_typed_array_alignment", | |
| 73 "byte length", "NAME", ELEMENT_SIZE); | |
| 74 } | |
| 75 newByteLength = bufferByteLength - offset; | |
| 76 newLength = newByteLength / ELEMENT_SIZE; | |
| 77 } else { | |
| 78 var newLength = ToPositiveInteger(length, "invalid_typed_array_length"); | |
| 79 newByteLength = newLength * ELEMENT_SIZE; | |
| 80 } | |
| 81 if ((offset + newByteLength > bufferByteLength) | |
| 82 || (newLength > %MaxSmi())) { | |
| 83 throw MakeRangeError("invalid_typed_array_length"); | |
| 84 } | |
| 85 %TypedArrayInitialize(obj, ARRAY_ID, buffer, offset, newByteLength); | |
| 86 } | |
| 87 | |
| 88 function NAMEConstructByLength(obj, length) { | |
| 89 var l = IS_UNDEFINED(length) ? | |
| 90 0 : ToPositiveInteger(length, "invalid_typed_array_length"); | |
| 91 if (l > %MaxSmi()) { | |
| 92 throw MakeRangeError("invalid_typed_array_length"); | |
| 93 } | |
| 94 var byteLength = l * ELEMENT_SIZE; | |
| 95 var buffer = new $ArrayBuffer(byteLength); | |
| 96 %TypedArrayInitialize(obj, ARRAY_ID, buffer, 0, byteLength); | |
| 97 } | |
| 98 | |
| 99 function NAMEConstructByArrayLike(obj, arrayLike) { | |
| 100 var length = arrayLike.length; | |
| 101 var l = ToPositiveInteger(length, "invalid_typed_array_length"); | |
| 102 if (l > %MaxSmi()) { | |
| 103 throw MakeRangeError("invalid_typed_array_length"); | |
| 104 } | |
| 105 if(!%TypedArrayInitializeFromArrayLike(obj, ARRAY_ID, arrayLike, l)) { | |
| 106 for (var i = 0; i < l; i++) { | |
| 107 // It is crucial that we let any execptions from arrayLike[i] | |
| 108 // propagate outside the function. | |
| 109 obj[i] = arrayLike[i]; | |
| 110 } | |
| 111 } | |
| 112 } | |
| 113 | |
| 114 function NAMEConstructor(arg1, arg2, arg3) { | |
| 115 | |
| 116 if (%_IsConstructCall()) { | |
| 117 if (IS_ARRAYBUFFER(arg1)) { | |
| 118 NAMEConstructByArrayBuffer(this, arg1, arg2, arg3); | |
| 119 } else if (IS_NUMBER(arg1) || IS_STRING(arg1) || | |
| 120 IS_BOOLEAN(arg1) || IS_UNDEFINED(arg1)) { | |
| 121 NAMEConstructByLength(this, arg1); | |
| 122 } else { | |
| 123 NAMEConstructByArrayLike(this, arg1); | |
| 124 } | |
| 125 } else { | |
| 126 throw MakeTypeError("constructor_not_function", ["NAME"]) | |
| 127 } | |
| 128 } | |
| 129 endmacro | |
| 130 | |
| 131 TYPED_ARRAYS(TYPED_ARRAY_CONSTRUCTOR) | 37 TYPED_ARRAYS(TYPED_ARRAY_CONSTRUCTOR) |
| 132 | 38 |
| 133 function TypedArrayGetBuffer() { | 39 function TypedArrayGetBuffer() { |
| 134 return %TypedArrayGetBuffer(this); | 40 return %TypedArrayGetBuffer(this); |
| 135 } | 41 } |
| 136 | 42 |
| 137 function TypedArrayGetByteLength() { | 43 function TypedArrayGetByteLength() { |
| 138 return %TypedArrayGetByteLength(this); | 44 return %TypedArrayGetByteLength(this); |
| 139 } | 45 } |
| 140 | 46 |
| (...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 295 InstallGetter(constructor.prototype, "byteOffset", TypedArrayGetByteOffset); | 201 InstallGetter(constructor.prototype, "byteOffset", TypedArrayGetByteOffset); |
| 296 InstallGetter(constructor.prototype, "byteLength", TypedArrayGetByteLength); | 202 InstallGetter(constructor.prototype, "byteLength", TypedArrayGetByteLength); |
| 297 InstallGetter(constructor.prototype, "length", TypedArrayGetLength); | 203 InstallGetter(constructor.prototype, "length", TypedArrayGetLength); |
| 298 | 204 |
| 299 InstallFunctions(constructor.prototype, DONT_ENUM, $Array( | 205 InstallFunctions(constructor.prototype, DONT_ENUM, $Array( |
| 300 "subarray", CreateSubArray(elementSize, constructor), | 206 "subarray", CreateSubArray(elementSize, constructor), |
| 301 "set", TypedArraySet | 207 "set", TypedArraySet |
| 302 )); | 208 )); |
| 303 } | 209 } |
| 304 | 210 |
| 305 macro SETUP_TYPED_ARRAY(ARRAY_ID, NAME, ELEMENT_SIZE) | |
| 306 SetupTypedArray (global.NAME, NAMEConstructor, ELEMENT_SIZE); | |
| 307 endmacro | |
| 308 | |
| 309 TYPED_ARRAYS(SETUP_TYPED_ARRAY) | 211 TYPED_ARRAYS(SETUP_TYPED_ARRAY) |
| 310 | 212 |
| 311 // --------------------------- DataView ----------------------------- | 213 // --------------------------- DataView ----------------------------- |
| 312 | 214 |
| 313 var $DataView = global.DataView; | 215 var $DataView = global.DataView; |
| 314 | 216 |
| 315 function DataViewConstructor(buffer, byteOffset, byteLength) { // length = 3 | 217 function DataViewConstructor(buffer, byteOffset, byteLength) { // length = 3 |
| 316 if (%_IsConstructCall()) { | 218 if (%_IsConstructCall()) { |
| 317 if (!IS_ARRAYBUFFER(buffer)) { | 219 if (!IS_ARRAYBUFFER(buffer)) { |
| 318 throw MakeTypeError('data_view_not_array_buffer', []); | 220 throw MakeTypeError('data_view_not_array_buffer', []); |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 351 } | 253 } |
| 352 | 254 |
| 353 function DataViewGetByteLength() { | 255 function DataViewGetByteLength() { |
| 354 if (!IS_DATAVIEW(this)) { | 256 if (!IS_DATAVIEW(this)) { |
| 355 throw MakeTypeError('incompatible_method_receiver', | 257 throw MakeTypeError('incompatible_method_receiver', |
| 356 ['DataView.byteLength', this]); | 258 ['DataView.byteLength', this]); |
| 357 } | 259 } |
| 358 return %DataViewGetByteLength(this); | 260 return %DataViewGetByteLength(this); |
| 359 } | 261 } |
| 360 | 262 |
| 361 macro DATA_VIEW_TYPES(FUNCTION) | |
| 362 FUNCTION(Int8) | |
| 363 FUNCTION(Uint8) | |
| 364 FUNCTION(Int16) | |
| 365 FUNCTION(Uint16) | |
| 366 FUNCTION(Int32) | |
| 367 FUNCTION(Uint32) | |
| 368 FUNCTION(Float32) | |
| 369 FUNCTION(Float64) | |
| 370 endmacro | |
| 371 | |
| 372 function ToPositiveDataViewOffset(offset) { | 263 function ToPositiveDataViewOffset(offset) { |
| 373 return ToPositiveInteger(offset, 'invalid_data_view_accessor_offset'); | 264 return ToPositiveInteger(offset, 'invalid_data_view_accessor_offset'); |
| 374 } | 265 } |
| 375 | 266 |
| 376 | |
| 377 macro DATA_VIEW_GETTER_SETTER(TYPENAME) | |
| 378 function DataViewGetTYPENAME(offset, little_endian) { | |
| 379 if (!IS_DATAVIEW(this)) { | |
| 380 throw MakeTypeError('incompatible_method_receiver', | |
| 381 ['DataView.getTYPENAME', this]); | |
| 382 } | |
| 383 if (%_ArgumentsLength() < 1) { | |
| 384 throw MakeTypeError('invalid_argument'); | |
| 385 } | |
| 386 return %DataViewGetTYPENAME(this, | |
| 387 ToPositiveDataViewOffset(offset), | |
| 388 !!little_endian); | |
| 389 } | |
| 390 | |
| 391 function DataViewSetTYPENAME(offset, value, little_endian) { | |
| 392 if (!IS_DATAVIEW(this)) { | |
| 393 throw MakeTypeError('incompatible_method_receiver', | |
| 394 ['DataView.setTYPENAME', this]); | |
| 395 } | |
| 396 if (%_ArgumentsLength() < 2) { | |
| 397 throw MakeTypeError('invalid_argument'); | |
| 398 } | |
| 399 %DataViewSetTYPENAME(this, | |
| 400 ToPositiveDataViewOffset(offset), | |
| 401 TO_NUMBER_INLINE(value), | |
| 402 !!little_endian); | |
| 403 } | |
| 404 endmacro | |
| 405 | |
| 406 DATA_VIEW_TYPES(DATA_VIEW_GETTER_SETTER) | 267 DATA_VIEW_TYPES(DATA_VIEW_GETTER_SETTER) |
| 407 | 268 |
| 408 function SetupDataView() { | 269 function SetupDataView() { |
| 409 %CheckIsBootstrapping(); | 270 %CheckIsBootstrapping(); |
| 410 | 271 |
| 411 // Setup the DataView constructor. | 272 // Setup the DataView constructor. |
| 412 %SetCode($DataView, DataViewConstructor); | 273 %SetCode($DataView, DataViewConstructor); |
| 413 %FunctionSetPrototype($DataView, new $Object); | 274 %FunctionSetPrototype($DataView, new $Object); |
| 414 | 275 |
| 415 // Set up constructor property on the DataView prototype. | 276 // Set up constructor property on the DataView prototype. |
| (...skipping 24 matching lines...) Expand all Loading... |
| 440 | 301 |
| 441 "getFloat32", DataViewGetFloat32, | 302 "getFloat32", DataViewGetFloat32, |
| 442 "setFloat32", DataViewSetFloat32, | 303 "setFloat32", DataViewSetFloat32, |
| 443 | 304 |
| 444 "getFloat64", DataViewGetFloat64, | 305 "getFloat64", DataViewGetFloat64, |
| 445 "setFloat64", DataViewSetFloat64 | 306 "setFloat64", DataViewSetFloat64 |
| 446 )); | 307 )); |
| 447 } | 308 } |
| 448 | 309 |
| 449 SetupDataView(); | 310 SetupDataView(); |
| OLD | NEW |