ByteListBitwiseOp.js 1.3 KB

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