ByteListBitwiseOp.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. 'use strict';
  2. var callBound = require('call-bind/callBound');
  3. var $TypeError = require('es-errors/type');
  4. var $push = callBound('Array.prototype.push');
  5. var IsArray = require('./IsArray');
  6. var isByteValue = require('../helpers/isByteValue');
  7. // https://262.ecma-international.org/12.0/#sec-bytelistbitwiseop
  8. module.exports = function ByteListBitwiseOp(op, xBytes, yBytes) {
  9. if (op !== '&' && op !== '^' && op !== '|') {
  10. throw new $TypeError('Assertion failed: `op` must be `&`, `^`, or `|`');
  11. }
  12. if (!IsArray(xBytes) || !IsArray(yBytes) || xBytes.length !== yBytes.length) {
  13. throw new $TypeError('Assertion failed: `xBytes` and `yBytes` must be same-length sequences of byte values (an integer 0-255, inclusive)');
  14. }
  15. var result = [];
  16. for (var i = 0; i < xBytes.length; i += 1) {
  17. var xByte = xBytes[i];
  18. var yByte = yBytes[i];
  19. if (!isByteValue(xByte) || !isByteValue(yByte)) {
  20. throw new $TypeError('Assertion failed: `xBytes` and `yBytes` must be same-length sequences of byte values (an integer 0-255, inclusive)');
  21. }
  22. var resultByte;
  23. if (op === '&') {
  24. resultByte = xByte & yByte;
  25. } else if (op === '^') {
  26. resultByte = xByte ^ yByte;
  27. } else {
  28. resultByte = xByte | yByte;
  29. }
  30. $push(result, resultByte);
  31. }
  32. return result;
  33. };