1bb4e9fc8d6fae2409a445e9a9b3394dd4c61d922233210111428b133b50d0901ba57c992a1616fb859b3d4906509a66d690d9fbdf4d3f0fa03cec96f8a751 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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 IsTypedArrayOutOfBounds = require('./IsTypedArrayOutOfBounds');
  7. var TypedArrayLength = require('./TypedArrayLength');
  8. var ValidateTypedArray = require('./ValidateTypedArray');
  9. var availableTypedArrays = require('available-typed-arrays')();
  10. // https://262.ecma-international.org/15.0/#typedarraycreatefromconstructor
  11. module.exports = function TypedArrayCreateFromConstructor(constructor, argumentList) {
  12. if (!IsConstructor(constructor)) {
  13. throw new $TypeError('Assertion failed: `constructor` must be a constructor');
  14. }
  15. if (!IsArray(argumentList)) {
  16. throw new $TypeError('Assertion failed: `argumentList` must be a List');
  17. }
  18. if (availableTypedArrays.length === 0) {
  19. throw new $SyntaxError('Assertion failed: Typed Arrays are not supported in this environment');
  20. }
  21. // var newTypedArray = Construct(constructor, argumentList); // step 1
  22. var newTypedArray;
  23. if (argumentList.length === 0) {
  24. newTypedArray = new constructor();
  25. } else if (argumentList.length === 1) {
  26. newTypedArray = new constructor(argumentList[0]);
  27. } else if (argumentList.length === 2) {
  28. newTypedArray = new constructor(argumentList[0], argumentList[1]);
  29. } else {
  30. newTypedArray = new constructor(argumentList[0], argumentList[1], argumentList[2]);
  31. }
  32. var taRecord = ValidateTypedArray(newTypedArray, 'SEQ-CST'); // step 2
  33. if (argumentList.length === 1 && typeof argumentList[0] === 'number') { // step 3
  34. if (IsTypedArrayOutOfBounds(taRecord)) {
  35. throw new $TypeError('new Typed Array is out of bounds'); // step 3.a
  36. }
  37. var length = TypedArrayLength(taRecord); // step 3.b
  38. if (length < argumentList[0]) {
  39. throw new $TypeError('`argumentList[0]` must be <= `newTypedArray.length`'); // step 3.c
  40. }
  41. }
  42. return newTypedArray; // step 4
  43. };