NumberBitwiseOp.js 757 B

1234567891011121314151617181920212223242526272829
  1. 'use strict';
  2. var GetIntrinsic = require('get-intrinsic');
  3. var $TypeError = GetIntrinsic('%TypeError%');
  4. var ToInt32 = require('./ToInt32');
  5. var ToUint32 = require('./ToUint32');
  6. var Type = require('./Type');
  7. // https://262.ecma-international.org/11.0/#sec-numberbitwiseop
  8. module.exports = function NumberBitwiseOp(op, x, y) {
  9. if (op !== '&' && op !== '|' && op !== '^') {
  10. throw new $TypeError('Assertion failed: `op` must be `&`, `|`, or `^`');
  11. }
  12. if (Type(x) !== 'Number' || Type(y) !== 'Number') {
  13. throw new $TypeError('Assertion failed: `x` and `y` arguments must be Numbers');
  14. }
  15. var lnum = ToInt32(x);
  16. var rnum = ToUint32(y);
  17. if (op === '&') {
  18. return lnum & rnum;
  19. }
  20. if (op === '|') {
  21. return lnum | rnum;
  22. }
  23. return lnum ^ rnum;
  24. };