OLD | NEW |
1 // Copyright 2013 the V8 project authors. All rights reserved. | 1 // Copyright 2013 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 "use strict"; | 5 "use strict"; |
6 | 6 |
7 // This file relies on the fact that the following declaration has been made | 7 // This file relies on the fact that the following declaration has been made |
8 // in runtime.js: | 8 // in runtime.js: |
9 // var $Array = global.Array; | 9 // var $Array = global.Array; |
10 var $ArrayBuffer = global.ArrayBuffer; | 10 var $ArrayBuffer = global.ArrayBuffer; |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
60 newByteLength = bufferByteLength - offset; | 60 newByteLength = bufferByteLength - offset; |
61 newLength = newByteLength / ELEMENT_SIZE; | 61 newLength = newByteLength / ELEMENT_SIZE; |
62 } else { | 62 } else { |
63 var newLength = length; | 63 var newLength = length; |
64 newByteLength = newLength * ELEMENT_SIZE; | 64 newByteLength = newLength * ELEMENT_SIZE; |
65 } | 65 } |
66 if ((offset + newByteLength > bufferByteLength) | 66 if ((offset + newByteLength > bufferByteLength) |
67 || (newLength > %_MaxSmi())) { | 67 || (newLength > %_MaxSmi())) { |
68 throw MakeRangeError("invalid_typed_array_length"); | 68 throw MakeRangeError("invalid_typed_array_length"); |
69 } | 69 } |
70 %_TypedArrayInitialize(obj, ARRAY_ID, buffer, offset, newByteLength); | 70 %_TypedArrayInitialize(obj, ARRAY_ID, buffer, offset, newByteLength, false); |
71 } | 71 } |
72 | 72 |
73 function NAMEConstructByLength(obj, length) { | 73 function NAMEConstructByLength(obj, length) { |
74 var l = IS_UNDEFINED(length) ? | 74 var l = IS_UNDEFINED(length) ? |
75 0 : ToPositiveInteger(length, "invalid_typed_array_length"); | 75 0 : ToPositiveInteger(length, "invalid_typed_array_length"); |
76 if (l > %_MaxSmi()) { | 76 if (l > %_MaxSmi()) { |
77 throw MakeRangeError("invalid_typed_array_length"); | 77 throw MakeRangeError("invalid_typed_array_length"); |
78 } | 78 } |
79 var byteLength = l * ELEMENT_SIZE; | 79 var byteLength = l * ELEMENT_SIZE; |
80 if (byteLength > %_TypedArrayMaxSizeInHeap()) { | 80 if (byteLength > %_TypedArrayMaxSizeInHeap()) { |
81 var buffer = new $ArrayBuffer(byteLength); | 81 var buffer = new $ArrayBuffer(byteLength); |
82 %_TypedArrayInitialize(obj, ARRAY_ID, buffer, 0, byteLength); | 82 %_TypedArrayInitialize(obj, ARRAY_ID, buffer, 0, byteLength, false); |
83 } else { | 83 } else { |
84 %_TypedArrayInitialize(obj, ARRAY_ID, null, 0, byteLength); | 84 %_TypedArrayInitialize(obj, ARRAY_ID, null, 0, byteLength, false); |
85 } | 85 } |
86 } | 86 } |
87 | 87 |
88 function NAMEConstructByArrayLike(obj, arrayLike) { | 88 function NAMEConstructByArrayLike(obj, arrayLike) { |
89 var length = arrayLike.length; | 89 var length = arrayLike.length; |
90 var l = ToPositiveInteger(length, "invalid_typed_array_length"); | 90 var l = ToPositiveInteger(length, "invalid_typed_array_length"); |
91 | 91 |
92 if (l > %_MaxSmi()) { | 92 if (l > %_MaxSmi()) { |
93 throw MakeRangeError("invalid_typed_array_length"); | 93 throw MakeRangeError("invalid_typed_array_length"); |
94 } | 94 } |
(...skipping 381 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
476 | 476 |
477 "getFloat32", DataViewGetFloat32JS, | 477 "getFloat32", DataViewGetFloat32JS, |
478 "setFloat32", DataViewSetFloat32JS, | 478 "setFloat32", DataViewSetFloat32JS, |
479 | 479 |
480 "getFloat64", DataViewGetFloat64JS, | 480 "getFloat64", DataViewGetFloat64JS, |
481 "setFloat64", DataViewSetFloat64JS | 481 "setFloat64", DataViewSetFloat64JS |
482 )); | 482 )); |
483 } | 483 } |
484 | 484 |
485 SetupDataView(); | 485 SetupDataView(); |
| 486 |
| 487 // --------------- Shared Typed Arrays --------------------- |
| 488 macro SHARED_TYPED_ARRAYS(FUNCTION) |
| 489 // arrayIds below should be synchronized with Runtime_TypedArrayInitialize. |
| 490 FUNCTION(1, SharedUint8Array, 1) |
| 491 FUNCTION(2, SharedInt8Array, 1) |
| 492 FUNCTION(3, SharedUint16Array, 2) |
| 493 FUNCTION(4, SharedInt16Array, 2) |
| 494 FUNCTION(5, SharedUint32Array, 4) |
| 495 FUNCTION(6, SharedInt32Array, 4) |
| 496 FUNCTION(7, SharedFloat32Array, 4) |
| 497 FUNCTION(8, SharedFloat64Array, 8) |
| 498 FUNCTION(9, SharedUint8ClampedArray, 1) |
| 499 endmacro |
| 500 |
| 501 macro SHARED_TYPED_ARRAY_CONSTRUCTOR(ARRAY_ID, NAME, ELEMENT_SIZE) |
| 502 function NAMEConstructByArrayBuffer(obj, buffer, byteOffset, length) { |
| 503 if (!IS_UNDEFINED(byteOffset)) { |
| 504 byteOffset = |
| 505 ToPositiveInteger(byteOffset, "invalid_typed_array_length"); |
| 506 } |
| 507 if (!IS_UNDEFINED(length)) { |
| 508 length = ToPositiveInteger(length, "invalid_typed_array_length"); |
| 509 } |
| 510 |
| 511 var bufferByteLength = %_ArrayBufferGetByteLength(buffer); |
| 512 var offset; |
| 513 if (IS_UNDEFINED(byteOffset)) { |
| 514 offset = 0; |
| 515 } else { |
| 516 offset = byteOffset; |
| 517 |
| 518 if (offset % ELEMENT_SIZE !== 0) { |
| 519 throw MakeRangeError("invalid_typed_array_alignment", |
| 520 ["start offset", "NAME", ELEMENT_SIZE]); |
| 521 } |
| 522 if (offset > bufferByteLength) { |
| 523 throw MakeRangeError("invalid_typed_array_offset"); |
| 524 } |
| 525 } |
| 526 |
| 527 var newByteLength; |
| 528 var newLength; |
| 529 if (IS_UNDEFINED(length)) { |
| 530 if (bufferByteLength % ELEMENT_SIZE !== 0) { |
| 531 throw MakeRangeError("invalid_typed_array_alignment", |
| 532 ["byte length", "NAME", ELEMENT_SIZE]); |
| 533 } |
| 534 newByteLength = bufferByteLength - offset; |
| 535 newLength = newByteLength / ELEMENT_SIZE; |
| 536 } else { |
| 537 var newLength = length; |
| 538 newByteLength = newLength * ELEMENT_SIZE; |
| 539 } |
| 540 if ((offset + newByteLength > bufferByteLength) |
| 541 || (newLength > %_MaxSmi())) { |
| 542 throw MakeRangeError("invalid_typed_array_length"); |
| 543 } |
| 544 %_TypedArrayInitialize(obj, ARRAY_ID, buffer, offset, newByteLength, true); |
| 545 } |
| 546 |
| 547 function NAMEConstructByLength(obj, length) { |
| 548 var l = IS_UNDEFINED(length) ? |
| 549 0 : ToPositiveInteger(length, "invalid_typed_array_length"); |
| 550 if (l > %_MaxSmi()) { |
| 551 throw MakeRangeError("invalid_typed_array_length"); |
| 552 } |
| 553 var byteLength = l * ELEMENT_SIZE; |
| 554 if (byteLength > %_TypedArrayMaxSizeInHeap()) { |
| 555 var buffer = new $SharedArrayBuffer(byteLength); |
| 556 %_TypedArrayInitialize(obj, ARRAY_ID, buffer, 0, byteLength, true); |
| 557 } else { |
| 558 %_TypedArrayInitialize(obj, ARRAY_ID, null, 0, byteLength, true); |
| 559 } |
| 560 } |
| 561 |
| 562 function NAMEConstructor(arg1, arg2, arg3) { |
| 563 if (%_IsConstructCall()) { |
| 564 if (IS_SHAREDARRAYBUFFER(arg1)) { |
| 565 NAMEConstructByArrayBuffer(this, arg1, arg2, arg3); |
| 566 } else if (IS_NUMBER(arg1) || IS_STRING(arg1) || |
| 567 IS_BOOLEAN(arg1) || IS_UNDEFINED(arg1)) { |
| 568 NAMEConstructByLength(this, arg1); |
| 569 } else { |
| 570 throw MakeTypeError("invalid_argument"); |
| 571 } |
| 572 } else { |
| 573 throw MakeTypeError("constructor_not_function", ["NAME"]) |
| 574 } |
| 575 } |
| 576 |
| 577 function NAME_GetBuffer() { |
| 578 if (!(%_ClassOf(this) === 'NAME')) { |
| 579 throw MakeTypeError('incompatible_method_receiver', |
| 580 ["NAME.buffer", this]); |
| 581 } |
| 582 return %TypedArrayGetBuffer(this); |
| 583 } |
| 584 |
| 585 function NAME_GetByteLength() { |
| 586 if (!(%_ClassOf(this) === 'NAME')) { |
| 587 throw MakeTypeError('incompatible_method_receiver', |
| 588 ["NAME.byteLength", this]); |
| 589 } |
| 590 return %_ArrayBufferViewGetByteLength(this); |
| 591 } |
| 592 |
| 593 function NAME_GetByteOffset() { |
| 594 if (!(%_ClassOf(this) === 'NAME')) { |
| 595 throw MakeTypeError('incompatible_method_receiver', |
| 596 ["NAME.byteOffset", this]); |
| 597 } |
| 598 return %_ArrayBufferViewGetByteOffset(this); |
| 599 } |
| 600 |
| 601 function NAME_GetLength() { |
| 602 if (!(%_ClassOf(this) === 'NAME')) { |
| 603 throw MakeTypeError('incompatible_method_receiver', |
| 604 ["NAME.length", this]); |
| 605 } |
| 606 return %_TypedArrayGetLength(this); |
| 607 } |
| 608 |
| 609 var $NAME = global.NAME; |
| 610 |
| 611 function NAMESubArray(begin, end) { |
| 612 if (!(%_ClassOf(this) === 'NAME')) { |
| 613 throw MakeTypeError('incompatible_method_receiver', |
| 614 ["NAME.subarray", this]); |
| 615 } |
| 616 var beginInt = TO_INTEGER(begin); |
| 617 if (!IS_UNDEFINED(end)) { |
| 618 end = TO_INTEGER(end); |
| 619 } |
| 620 |
| 621 var srcLength = %_TypedArrayGetLength(this); |
| 622 if (beginInt < 0) { |
| 623 beginInt = $max(0, srcLength + beginInt); |
| 624 } else { |
| 625 beginInt = $min(srcLength, beginInt); |
| 626 } |
| 627 |
| 628 var endInt = IS_UNDEFINED(end) ? srcLength : end; |
| 629 if (endInt < 0) { |
| 630 endInt = $max(0, srcLength + endInt); |
| 631 } else { |
| 632 endInt = $min(endInt, srcLength); |
| 633 } |
| 634 if (endInt < beginInt) { |
| 635 endInt = beginInt; |
| 636 } |
| 637 var newLength = endInt - beginInt; |
| 638 var beginByteOffset = |
| 639 %_ArrayBufferViewGetByteOffset(this) + beginInt * ELEMENT_SIZE; |
| 640 return new $NAME(%TypedArrayGetBuffer(this), |
| 641 beginByteOffset, newLength); |
| 642 } |
| 643 endmacro |
| 644 |
| 645 SHARED_TYPED_ARRAYS(SHARED_TYPED_ARRAY_CONSTRUCTOR) |
| 646 |
| 647 |
| 648 function SetupSharedTypedArrays() { |
| 649 macro SETUP_SHARED_TYPED_ARRAY(ARRAY_ID, NAME, ELEMENT_SIZE) |
| 650 %CheckIsBootstrapping(); |
| 651 %SetCode(global.NAME, NAMEConstructor); |
| 652 %FunctionSetPrototype(global.NAME, new $Object()); |
| 653 |
| 654 %AddNamedProperty(global.NAME, "BYTES_PER_ELEMENT", ELEMENT_SIZE, |
| 655 READ_ONLY | DONT_ENUM | DONT_DELETE); |
| 656 %AddNamedProperty(global.NAME.prototype, |
| 657 "constructor", global.NAME, DONT_ENUM); |
| 658 %AddNamedProperty(global.NAME.prototype, |
| 659 "BYTES_PER_ELEMENT", ELEMENT_SIZE, |
| 660 READ_ONLY | DONT_ENUM | DONT_DELETE); |
| 661 InstallGetter(global.NAME.prototype, "buffer", NAME_GetBuffer); |
| 662 InstallGetter(global.NAME.prototype, "byteOffset", NAME_GetByteOffset, |
| 663 DONT_ENUM | DONT_DELETE); |
| 664 InstallGetter(global.NAME.prototype, "byteLength", NAME_GetByteLength, |
| 665 DONT_ENUM | DONT_DELETE); |
| 666 InstallGetter(global.NAME.prototype, "length", NAME_GetLength, |
| 667 DONT_ENUM | DONT_DELETE); |
| 668 InstallGetter(global.NAME.prototype, symbolToStringTag, |
| 669 TypedArrayGetToStringTag); |
| 670 InstallFunctions(global.NAME.prototype, DONT_ENUM, $Array( |
| 671 "subarray", NAMESubArray, |
| 672 "set", TypedArraySet |
| 673 )); |
| 674 endmacro |
| 675 |
| 676 SHARED_TYPED_ARRAYS(SETUP_SHARED_TYPED_ARRAY) |
| 677 } |
| 678 |
| 679 SetupSharedTypedArrays(); |
OLD | NEW |