178898bebc2ad7fe9c3505a97fc9c3fea542ba3702b3580664e568e9feb759af315a447840163a88f266b000bc8d20ee79d2e1c6cab8f3f50d512cb7c7455e 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. 'use strict';
  2. var $SyntaxError = require('es-errors/syntax');
  3. var $TypeError = require('es-errors/type');
  4. var IsArray = require('./IsArray');
  5. var IsConstructor = require('./IsConstructor');
  6. var ValidateTypedArray = require('./ValidateTypedArray');
  7. var availableTypedArrays = require('available-typed-arrays')();
  8. var typedArrayLength = require('typed-array-length');
  9. // https://262.ecma-international.org/7.0/#typedarray-create
  10. module.exports = function TypedArrayCreate(constructor, argumentList) {
  11. if (!IsConstructor(constructor)) {
  12. throw new $TypeError('Assertion failed: `constructor` must be a constructor');
  13. }
  14. if (!IsArray(argumentList)) {
  15. throw new $TypeError('Assertion failed: `argumentList` must be a List');
  16. }
  17. if (availableTypedArrays.length === 0) {
  18. throw new $SyntaxError('Assertion failed: Typed Arrays are not supported in this environment');
  19. }
  20. // var newTypedArray = Construct(constructor, argumentList); // step 1
  21. var newTypedArray;
  22. if (argumentList.length === 0) {
  23. newTypedArray = new constructor();
  24. } else if (argumentList.length === 1) {
  25. newTypedArray = new constructor(argumentList[0]);
  26. } else if (argumentList.length === 2) {
  27. newTypedArray = new constructor(argumentList[0], argumentList[1]);
  28. } else {
  29. newTypedArray = new constructor(argumentList[0], argumentList[1], argumentList[2]);
  30. }
  31. ValidateTypedArray(newTypedArray); // step 2
  32. if (argumentList.length === 1 && typeof argumentList[0] === 'number') { // step 3
  33. if (typedArrayLength(newTypedArray) < argumentList[0]) {
  34. throw new $TypeError('Assertion failed: `argumentList[0]` must be <= `newTypedArray.length`'); // step 3.a
  35. }
  36. }
  37. return newTypedArray; // step 4
  38. };